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

Renaming a topic inside a bag file

asked 2011-03-11 23:59:03 -0500

dlmypr gravatar image

When we record a bag file for example X.bag and it contains the topic data /a_topic

Is there any way to rename the topics name inside the X.bag /a_topic to /b_topic?

Best regards..

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
18

answered 2015-04-16 14:34:18 -0500

Enrique gravatar image

updated 2016-01-23 20:36:50 -0500

I know this is quite old, but there's also this option:

rosrun rosbag topic_renamer.py <in topic> <in bag> <out topic> <out bag>

Another option is to simply remap the topics when you use rosbag play. For example, if you have the topic /foo inside the bag, you can to this to rename it (when published) into /bar:

rosbag play file.bag /foo:=/bar

The leading '/' might not be needed.

edit flag offensive delete link more

Comments

yes. very good! it's very convenient!

asimay_y gravatar image asimay_y  ( 2016-06-07 04:34:22 -0500 )edit

This info is very good!

harderthan gravatar image harderthan  ( 2018-08-15 20:28:09 -0500 )edit
9

answered 2011-03-15 19:06:26 -0500

Tim Field gravatar image

I'd use the rosbag Python API:

from rosbag import Bag
with Bag('Y.bag', 'w') as Y:
    for topic, msg, t in Bag('X.bag'):
        Y.write('/b_topic' if topic == '/a_topic' else topic, msg, t)
edit flag offensive delete link more

Comments

Awesome...saved me a bunch of time finding this. Though I do have one oddity as a result - the bag is now 2x the size. Anyone know why?

Daniel Stonier gravatar image Daniel Stonier  ( 2014-07-19 09:02:10 -0500 )edit

Every time you use rosbag Python API, and I think also rosbag filter, the output bag is uncompressed. You have to rosbag compress after that. I'd say that's what happened to you.

Enrique gravatar image Enrique  ( 2016-01-23 20:38:40 -0500 )edit

how can you actually replace a topic name by another? how could one change the code for that?

rubot gravatar image rubot  ( 2018-07-25 14:40:04 -0500 )edit
1

Here is my code:

from rosbag import Bag
with Bag('Y.bag', 'w') as Y:
for topic, msg, t in Bag('X.bag'):
    if topic == '/a_topic':
        Y.write('/b_topic', msg, t)
    else:
        Y.write(topic, msg, t)
kentam gravatar image kentam  ( 2018-10-31 18:00:37 -0500 )edit
2

answered 2011-03-13 11:22:12 -0500

William gravatar image

You might consider using the tf_remap node that comes with the tf package: http://www.ros.org/wiki/tf#Nodes

From the wiki:

tf_remap

tf_remap listens to the /tf_old topic and republishes transforms on the /tf topic. This is mainly used with out-of-date bag files that need their coordinate frame IDs updated. The typical operation for this node is to play the bag file with /tf:=/old_tf. The tf_remap node is run with a ~mappings parameter that describes the mapping of frame IDs from old to new.

edit flag offensive delete link more

Comments

This will only work if @dlmpyr is trying to do stuff with /tf. If it's some other topic, tf_remap probably won't work.
Eric Perko gravatar image Eric Perko  ( 2011-03-13 15:37:06 -0500 )edit
2

answered 2011-03-12 00:16:00 -0500

Eric Perko gravatar image

Not sure if rosbag filter can do it, but you should be able to rename the topics using the rosbag API. For an example that reads in data on the tf topic and then writes it back out, see rosbag cookbook. That should be a good example to get you started.

edit flag offensive delete link more

Comments

Thank you for the response. I also couldn't find anything about the filter. I changed the topic names of openni_camera directly as another approach.
dlmypr gravatar image dlmypr  ( 2011-03-12 03:32:31 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2011-03-11 23:59:03 -0500

Seen: 20,752 times

Last updated: Jan 23 '16