Ask Your Question
1

[ROS2]How to write data to rosbag2 in ros2 foxy

asked 2020-06-25 01:36:35 -0500

sasaki gravatar image

I am writing a program to write data directly to the rosbag2 in ros2 foxy.
The sample code snippet is as follows.

        #include <rosbag2_cpp/writers/sequential_writer.hpp>
        #include <osbag2_cpp/typesupport_helpers.hpp>

        nav_msgs::msg::Path path;
        path = ...

        rosbag2_cpp::writers::SequentialWriter writer;
        rosbag2_cpp::StorageOptions storage_options{};

        storage_options.uri = "aaaa";
        storage_options.storage_id = "sqlite3";

        rosbag2_cpp::ConverterOptions converter_options{};
        converter_options.input_serialization_format = "cdr";
        converter_options.output_serialization_format = "cdr";
        writer.open(storage_options, converter_options);

        auto ros_message = std::make_shared<rosbag2_cpp::rosbag2_introspection_message_t>();
        ros_message->message = &path;

        rosbag2_cpp::SerializationFormatConverterFactory factory;
        std::unique_ptr<rosbag2_cpp::converter_interfaces::SerializationFormatSerializer> cdr_serializer;
        cdr_serializer = factory.load_serializer("cdr");
        auto library = rosbag2_cpp::get_typesupport_library("nav_msgs/Odometry", "rosidl_typesupport_cpp");
        auto type_support = rosbag2_cpp::get_typesupport_handle("nav_msgs/Odometry", "rosidl_typesupport_cpp", library);

        auto message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
        cdr_serializer->serialize(ros_message, type_support, message);

        writer.create_topic({"path_gt", "nav_msgs/Odometry", "", ""});
        writer.write(message);

It was compiled, but an error occurs while writing data.

terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid

I want to know how to write properly.
Thanks.

Reference
・rosbag2
https://github.com/ros2/rosbag2
・How to read data from rosbag2 in ros2
https://stackoverflow.com/questions/6...

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2020-07-23 12:59:19 -0500

Karsten gravatar image

Please apologize the late response. This post really just now came to my attention. We've recently added a test which should illustrate the use of the C++ API to write data to a rosbag. https://github.com/ros2/rosbag2/blob/...

We're working on making this more user-friendly in the near future and to enhance the functionality further to allow writing non-serialized messages directly.

Looking at the above code snippet, there might be a confusion on when to use the converter API. The converters should really only be used when dealing with different middlewares, aka rmw implementations, which each have different serialization formats. A bit of context: ROS2 is designed to support multiple middlewares and we can't guarantee that the format in which data was recorded is the same as the format used when replaying this data. Therefore, we introduced plugins to convert data on-the-fly to different serialization formats. This shouldn't be necessary though when dealing with only one middleware.

edit flag offensive delete link more

Comments

That minimal writer example is a lifesaver; it's pretty clear from that code how to set up a simple helper class to get back to one line open and write statements (at least until the improved API is available :-).

Jeffrey Kane Johnson gravatar image Jeffrey Kane Johnson  ( 2020-07-23 13:51:03 -0500 )edit

Thanks for the reply!

sasaki gravatar image sasaki  ( 2020-07-24 05:40:48 -0500 )edit
0

answered 2020-07-10 15:27:50 -0500

There's some discussion about this here: https://github.com/ros2/rosbag2/issue...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-06-25 01:15:34 -0500

Seen: 643 times

Last updated: Jul 24 '20