Robotics StackExchange | Archived questions

How to write ROS message object into BAG file?

I am working on a software stack in C++ for which I need to write custom ROS message object into a BAG file. I am missing the link to convert the ROS message object to rosbag::MessageInstance.

my_msgs::SensorAPI::Ptr sensor_api = GetMessageObject(); 
rosbag::MessageInstance mi = [HOW TO GET mi FROM sensor_api?] 
output_bag.write(mi.getTopic(), mi.getTime(), mi, mi.getConnectionHeader());

How the rosbag::MessageInstance can be retrieved from a ROS message object? Or how ROS message object can be written into a BAG file?

Asked by sosoup on 2020-06-04 17:13:28 UTC

Comments

Did you check the C++ API wiki page? You don't need to use a MessageInstance, the message itself is enough.

Or does this not suite you needs?

Asked by mgruhler on 2020-06-05 03:54:51 UTC

Thanks. Yes I have seen the wiki, but is there a way to write into a BAG without knowing topic name, thereby creating a generic method?

Asked by sosoup on 2020-06-05 04:06:03 UTC

Seeing as a .bag is a collection of messages published on topics, I don't understand what "write into a bag without knowing topic name" would do.

Could you clarify?

Asked by gvdhoorn on 2020-06-05 04:09:02 UTC

Right. By "not knowing the topic", I mean to have a generic routine and possibility to retrieve topic name from the message object itself, which I think is trivial. Anyway, thank you for the pointers.

Asked by sosoup on 2020-06-05 04:25:30 UTC

If you read the class documentation of MessageInstance, you'll see this:

A class pointing into a bag file.

The MessageInstance class itself is fairly light weight. It simply contains a pointer to a bag-file and the index_entry necessary to get access to the corresponding data.

In other words: you get a MessageInstance from a .bag, you don't write those directly yourself.

I'm sure there would be (convoluted) ways to construct one, but whether that would result in anything meaningful is unclear.

Asked by gvdhoorn on 2020-06-05 04:32:17 UTC

Answers