ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Rosbag migration: merging messages

asked 2020-11-10 14:18:38 -0600

JohnStechschulte gravatar image

I have two message types being published on two different topics (pretty much synchronously), and I'm merging them into a single message--basically, adding one as a subtype of the other. I'd like to migrate old bags to this new arrangement, but I'm pretty sure the tools in rosbag are not capable of this. Is there a lower-level approach to migrating bags that might work? How can I access both the old and new message definitions in a Python script?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-11-10 14:42:00 -0600

updated 2020-11-10 14:43:07 -0600

The python bag API is actually really easy to use. In your case something like this should work (not tested). I suppose the main question is how you want to synchronize the messages. Here I assume that the new topic should be publishes every time a new topic2 message has arrived and there was a topic1 message (anytime) before. This may not be exactly what you want, but I hope this will help you enough to get going.

import rosbag
from std_msgs.msg import Int32, String

inbag = rosbag.Bag('in.bag')
outbag = rosbag.Bag('out.bag', 'w')

    parent = None;
    for topic, msg, t in inbag.read_messages(topics=['topic1', 'topic2']):
        if topic == 'topic1':
            parent = msg
            if parent != None:
                parent.sub = msg;          
                outbag.write('newtopic', parent)
edit flag offensive delete link more



Thanks--I've mostly just used ROS in C++, and so I basically didn't expect an out-of-date bagfile to open at all.

The only issue with your suggested solution is that you can't just assign to parent.sub, since the old message doesn't have the new sub attribute. So instead I imported the new message and created a new one, copying over all the other fields from the old message, and then inserting the sub-type.

JohnStechschulte gravatar image JohnStechschulte  ( 2020-11-10 16:06:40 -0600 )edit

Question Tools



Asked: 2020-11-10 14:18:38 -0600

Seen: 122 times

Last updated: Nov 10 '20