4

I am writing a program to read data from rosbag directly without playing it in ros2. Sample code snippet is below. The intention of the code is that it checks for a ros2 topic and fetches only message in that topic. I am not able to fetch the data from the bag. When printed the console is printing hexadecimal values.

auto read_only_storage = factory.open_read_only(bag_file_path, storage_id);
while(read_only_storage->has_next())
{
    auto msg = read_only_storage->read_next();
    if(msg->topic_name == topic)
    {
        cout << msg->serialized_data<<endl;
    }
}

Any help in this regard would be appreciable.

confidential
  • 61
  • 1
  • 3

1 Answers1

1

You have to deserialize "msg->serialized_data" data. If you are using data serialized "cdr" format, please look below code.

    // deserialization and conversion to ros message
    my_pkg::msg::Msg msg;
    auto ros_message = std::make_shared<rosbag2_introspection_message_t>();
    ros_message->time_stamp = 0;
    ros_message->message = nullptr;
    ros_message->allocator = rcutils_get_default_allocator();
    ros_message->message = &msg;
    auto type_support = rosbag2::get_typesupport("my_pkg/msg/Msg", "rosidl_typesupport_cpp");

    rosbag2::SerializationFormatConverterFactory factory;
    std::unique_ptr<rosbag2::converter_interfaces::SerializationFormatDeserializer> cdr_deserializer_;
    cdr_deserializer_ = factory.load_deserializer("cdr");

    cdr_deserializer_->deserialize(msg, type_support, ros_message);

Full code: https://github.com/Kyungpyo-Kim/ROS2BagFileParsing

  • Do you know of a way to make the type of msg (ie my_pkg::msg::Msg) generic? What I have right now is a string which has the message type ("my_pkg/msg/Msg") and a SerializedMessage, and I somehow need to get it back to the original message so I can introspect the MemberFields. – nyxaria Aug 25 '20 at 08:52