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

how to play bag files from c++ or python class

asked 2017-09-04 19:49:03 -0600

Auton0mous gravatar image

updated 2017-09-07 05:41:24 -0600

At the moment I'm reading bag files and publishing back the results:

bag = rosbag.Bag('2017-09-04-16-11-56.bag')
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(0.5) # Not sure what this should be set to  
pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)
for topic, msg, t in bag.read_messages(topics=['cmd_vel']): 

But this is not the same as playing back the data since it does not take into account the time difference between each message.

In this answer ( ) the poster included their solution which as far as I can tell, just publishes all the messages to a topic.

So now the only thing I can think of is to go look through the source code of the player.cpp and see how they are publishing rosbag messages at the same rate at which they were recorded ( )

Is there a better way?


I'm trying to build a model of a real life differential drive robot in gazebo. I created a URDF which is just some simple shapes but think it should be accurate enough.

However it's difficult to figure out the right parameters that the URDF should have. For example, I don't know what the weight of the caster wheels are or how much torque is being applied to the front wheels. I can't take the robot apart and measure each component.

So my plan was to take the real robot outside for a test run and use rosbag to record all the commands being sent to it. Then use rosbag to playback all those commands on various different URDF models until I find one that accurately matches the behavior of the real robot (was hoping to use a genetic algorithm to find the best parameters).

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-09-06 11:09:38 -0600

huanxiner gravatar image

I would try calling rosbag play directly.

import subprocess
player_proc = subprocess.Popen(['rosbag', 'play', bag_filename, ... (other args)], cwd=bagfile_folder_path)
edit flag offensive delete link more


Thanks, that saves me a lot of trouble. However I've noticed that the playbag is fairly inconsistent. I'm playing back a recording of a robot that drives around in a circle and returns to a spot but every time it returns to a different location. It's subscribed to the cmd_vel topic. Is this normal?

Auton0mous gravatar image Auton0mous  ( 2017-09-07 05:12:30 -0600 )edit

Just to make sure we can avoid an xy-problem: can you update your original question with some more info on what it is that you are actually trying to do? It might be that you are expecting something to work which is not supposed to be used in that way.

gvdhoorn gravatar image gvdhoorn  ( 2017-09-07 05:16:00 -0600 )edit

Updated with explanation of what I'm trying to do. Again not sure if it's the best way to go about it but don't know of any other way. I've already tried to manually tweak the parameters but it's a pretty tedious task.

Auton0mous gravatar image Auton0mous  ( 2017-09-07 05:40:44 -0600 )edit

Your approach is ok - I don't know how successful it will be though. I'm guessing you're going to use the other topics to compare performance?

Note that a simulation will never be like the real thing, as simulators are always limited.

gvdhoorn gravatar image gvdhoorn  ( 2017-09-07 06:46:04 -0600 )edit

answered 2017-09-07 06:46:59 -0600

gvdhoorn gravatar image

updated 2017-09-07 06:49:49 -0600

Instead of running rosbag as a subprocess as @huanxiner suggests, perhaps you'd want to take a look at osrf/nodelet_rosbag.

That exposes an Action interface which lets you control a rosbag-like node with an action client.

Doing it that way would be a bit more 'the ROS way'.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-09-04 19:49:03 -0600

Seen: 3,588 times

Last updated: Sep 07 '17