Skip to content

📝 Advanced tutorial for recording a bag from your own node is incorrect on Humble #6277

@JustASimpleCoder

Description

@JustASimpleCoder

Issue Type

  • 🐛 Bug / Problem
  • ✏️ Typo / Grammar
  • 📖 Outdated Content
  • 🚀 Enhancement

Generated by Generative AI

Not Generated by AI

Distribution

Humble

Description

I could not build the first example for Recording a Bag From Your Own Node CPP, where I kept getting error message:

$ colcon build --packages-select

Starting >>> bag_recorder_nodes
--- stderr: bag_recorder_nodes                             
/home/j/ros2_ws/src/bag_recorder_nodes/src/simple_bag_recorder.cpp: In member function ‘void SimpleBagRecorder::topic_callback(std::shared_ptr<const rclcpp::SerializedMessage>) const’:
/home/j/ros2_ws/src/bag_recorder_nodes/src/simple_bag_recorder.cpp:27:19: error: no matching function for call to ‘rosbag2_cpp::Writer::write(std::shared_ptr<const rclcpp::SerializedMessage>&, const char [8], const char [20], rclcpp::Time&)’
   27 |     writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp);
      |     ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Further investigation shows that the humble branch indeed does not include an overloaded method in the rosbag2_cpp::Writer class (located in writer.cpp); although later branches do include it:

void Writer::write(
  std::shared_ptr<const rclcpp::SerializedMessage>
  const std::string & topic_name,
  const std::string & type_name,
  const rclcpp::Time & time)

It just has the non-const case on the humble branch:

void Writer::write(
  std::shared_ptr<rclcpp::SerializedMessage> message,
  const std::string & topic_name,
  const std::string & type_name,
  const rclcpp::Time & time)
{
  auto serialized_bag_message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
  serialized_bag_message->topic_name = topic_name;
  serialized_bag_message->time_stamp = time.nanoseconds();

  serialized_bag_message->serialized_data = std::shared_ptr<rcutils_uint8_array_t>(
    new rcutils_uint8_array_t,
    [](rcutils_uint8_array_t * msg) {
      auto fini_return = rcutils_uint8_array_fini(msg);
      delete msg;
      if (fini_return != RCUTILS_RET_OK) {
        RCLCPP_ERROR_STREAM(
          rclcpp::get_logger("rosbag2_cpp"),
          "Failed to destroy serialized message: " << rcutils_get_error_string().str);
      }
    });

  *serialized_bag_message->serialized_data = message->release_rcl_serialized_message();

  return write(serialized_bag_message, topic_name, type_name, rmw_get_serialization_format());
}

The package will build sucessfully if we change the topic_callback to the non-const version:

  void topic_callback(std::shared_ptr<rclcpp::SerializedMessage> msg) const
  {
    rclcpp::Time time_stamp = this->now();

    writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp);
  }

It looks like this was an oversight from when issue #6113 (or backport from #6005) was merged.

Affected Pages/Sections

https://docs.ros.org/en/humble/Tutorials/Advanced/Recording-A-Bag-From-Your-Own-Node-CPP.html

Screenshots or Examples (if applicable)

Image

Suggested Fix

Either remove the const from the rclcpp::SerializedMessage in the documentation for topic_callback:

  void topic_callback(std::shared_ptr<rclcpp::SerializedMessage> msg) const
  {
    rclcpp::Time time_stamp = this->now();

    writer_->write(msg, "chatter", "std_msgs/msg/String", time_stamp);
  }

Or we can update the humble branch for rosbag2's rosbag2_cpp package to include the write method that has the std::shared_ptr<const rclcpp::SerializedMessage> and leave the documentation as is.

I already did a test on a local ros2 humble build where I copied the the write method for const rclcpp::SerializedMessage from older branches (iron, jazzy, etc) and added it to the rosbag2_cpp package. I was able to sucessfully build and run ros2 simple_bag _recorder node using that write method. Here is my fork for it: const SerializedMessage add to humble branch.

I will create a pull request for the rosbag2 repo on the humble branch after this post in case other users want to utilize std::shared_ptr<const rclcpp::SerializedMessage> when writing to rosbag outside of this tutorial.

Additional Context

This was tested on Ubuntu Jammy (22.04.5 LTS) on an AMD64 machine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions