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

Publish ROSBAG in synch with /clock by roscpp

asked 2020-09-14 06:15:45 -0500

MatRad gravatar image

updated 2022-11-09 07:08:05 -0500

lucasw gravatar image

Hey, i wannn sync the publishing of an rosbag with the /clock topic offered in the running system out of a roscpp node.

What is your suggestion to solve this issue? Maybe i have to subscribe /clock within my node and make message_filters/ApproximateTime work? But i have not any clue about this message filter. My understanding of c++ is not properly advanced...

So my suggestion: Subscribe /clock, read msg from rosbag, searching for nearest msg with function above (or other recommendated methods?) and publish the synchronized msg from rosbag.

Is this workaround correct? Do you prefere any other methods? What is your way to solve this issue? Pls help an beginner in c++ and ros.. :) Thx for your time!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2020-09-16 04:42:26 -0500

Weasfas gravatar image

Welcome @MatRad.

The main problem I see here is with what you want to synchronize your rosbag clock. The rosbag already contains the clock topic if you saved it during the record. If you want to synchronize somehow the rosbag clock with the system (roscore) clock you will need the paramater use_sim_time and set it to true. This is used to have common time when using data from a rosbag with a node running in your system, in order the processing to not have time problems.

There is a nice explanation of what this variable is used for here.

On the other hand, if you want to synchronize your rosbag topics (that I assume contain a header with the time), you should use message_filters that will filter every message with its corresponding time. However bear in mind this; message_filters only work if you topics contain a header since they need the timestamp on those headers. This internally have nothing to do with the /clock topic since the message filter is based on header time not system time.

Hope that solve your problem.


edit flag offensive delete link more



Thanks for your explanation! I tried the /use_sim_time option. But either I do not understand it completely or it does not work in my use case. I use a simulator that publishes the /clock topic. The problem is, I would like to play a Rosbag. But - for reasons I don't know - it is not in the same rhythm (faster) as my simulated time. Therefore my approach was to read the current time (subscribe) and compare it with the header time of the Rosbag msg.
For message_filters I only find the possibility to subscribe topics but not to read them from a rosbag. So my more concrete question would be: How can I convert the following lines so that I subscribe a topic and read a topic from my Rosbag, and synchronize these two?

message_filters::Subscriber<Image> image_sub(nh, "image", 1);
message_filters::Subscriber<CameraInfo> info_sub(nh, "camera_info", 1);
TimeSynchronizer<Image, CameraInfo> sync(image_sub ...
MatRad gravatar image MatRad  ( 2020-09-16 07:17:21 -0500 )edit

Ok @MatRad, I really need you to put me in context. Let us imagine that you have a set of simulated (from Gazebo for instance) topics, a node that is processing some of that set of topics and a rosbag that is publishing another set of topics. The thing is, to synchronize properly your simulated topics with the topics produced by the bag you need some steps:

  1. Launch the simulation to produce the simulated topics.
  2. Set the use_sim_time to true: rosparam set use_sim_time "true"
  3. Launch the bag: rosbag play --clock my_bag.bag
  4. Now, if you execute rostopic list command you will see both, simulated topic and rosbag topics.

From that point, you can treat any of those topic like standard ros topics and perform a subscription on each topic with the normal Subscriber or with the message_filter Subscriber. You do not really need to read the bag, since it is ...(more)

Weasfas gravatar image Weasfas  ( 2020-09-16 15:25:00 -0500 )edit

Thx @Weasfas As I see it, in the case you describe, the simulation synchronizes with my Rosbag time stamp, is that correct?

However, I would like it the other way round. I have to add the information, that the simulation is controlled by a Matlab program through a certain Plugin. Now I would like the stored data from Rosbag to be played synchronously with the simulation.
The simulation, especially Matlab, is much slower than the natural playback speed of Rosbag. So the "synchronization" of my Rosbag would always have to wait for the next timestamp before revealing its contents. That is my problem and i couldn't solved it with your first approach yet. I hope this makes it a lot clearer. And I would be happy if you have a solution/suggestion for my dilemma

MatRad gravatar image MatRad  ( 2020-09-17 00:42:58 -0500 )edit

Mmmm, so lets say your simulated plugin runs with 10Hz, why not just playing the rosbag with the the -r or --rate parameter so it plays at the same frequency. Would be something like rosbag play -r 10 my_bag.bag.

Weasfas gravatar image Weasfas  ( 2020-09-17 05:05:21 -0500 )edit

@Weasfas did you get the answer? I have a similar issue where Im synchronizing 5 different topics using a rosbag. It works perfectly with live camera feed. But using rosbag and sim time don't work.

TK27 gravatar image TK27  ( 2021-05-08 13:21:56 -0500 )edit

@TK27, I am not the OP, that is @MatRad, thus, I am not able to answer you if the answer I posted solve the problem he had. Hovewer I can say that you may want to post your problem as a new question in this Forum, explaining the issue with more depth. Also you may take into account that the bag have to contain the /clock topic and, for you specific case, you may need to use the Approximate time Policy instead of the Exact Policy for the message_filter package.

Weasfas gravatar image Weasfas  ( 2021-05-08 14:14:29 -0500 )edit

answered 2022-11-09 07:55:48 -0500

lucasw gravatar image

It would be nice if rosbag play could sync to an external /clock topic, but it doesn't- maybe there is another tool out there that provides this feature? Or adding that to rosbag play may not be too difficult.

You could make something work quickly with the rosbag python api - you could read every message of interest in a bag and put them in a dict with timestamp keys (which will be memory intensive for large bags), set up publishers for each topic, then wait for clock messages. Making it generic and highly configurable would be more work than hard coding a small number of topics and message types (but very useful if it was made open source).

There could be some logic that plays back all the messages since the last clock callback if the clock appears to be moving forward continuously, but if it jumps forward or backward in time don't publish all the intervening messages (it would be fun if bags could be played in reverse, but not really necessary), just skip to the new time and play back only messages from say 0.01 seconds before the new time up until it, then wait for next clock.

edit flag offensive delete link more

Question Tools



Asked: 2020-09-14 06:15:45 -0500

Seen: 1,461 times

Last updated: Nov 09 '22