Ask Your Question
0

Rosbag migration: merging messages

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

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
0

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

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

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')

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

Comments

1

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 -0500 )edit

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-11-10 14:18:38 -0500

Seen: 63 times

Last updated: Nov 10 '20