time synchronize RosBag Data (Odometry and sensor values)

57 views (last 30 days)
I have a problem. I have a created a RosBag that contains the Odometry of a Robot and some values from a sensor that got measured while the robot was driving.
Now I want to get the data in Matlab and plot the values and the odometry using the scatteredInterpolant function. The problem is, that I need to synchronize the sensors data and the odometry. My idea would simply be to read out the timestamps and then match the data. Unfortunately I cannot find a way to get data at a specific timestamp or the timestamp of one value in the ROSbag.
Is there any way to get those timestamps? Does it make sense to save the rosbag data as a timeseries?
Hopefully somebody has an idea. Thank you!
Jefilia on 11 Apr 2022
Hey Cam, thank you for your answer!
I think I need the message timestamp.
I just want to match the sensor's data with the robot's odometry at the time when they where recorded so I can put them in a plot together.
Have a nice evening!

Sign in to comment.

Answers (1)

Cam Salzberger
Cam Salzberger on 12 Apr 2022
Hello Jefilia,
The easiest thing to do is probably to "select" the rosbag down to the Odometry topic, then extract all of the messages with DataFormat 'struct' from there. Since these messages are relatively small in size, it shouldn't take too much time or memory to do this. Once you have a cell array of structs, you can extract the timestamps into an array with something like:
timeArray = cellfun(@(msg) msg.Header.Sec+msg.Header.Nsec*1e-9, odomMsgs);
Alternatively, you could use timeseries. Either way works.
Then, as you process your other sensor messages (whether you extract them all at once, batch-process them, or extract one at a time), you can simply do something like:
sensorTimestamp = msg.Header.Sec+msg.Header.Nsec*1e-9;
odomIdx = find(timeArray > sensorTimestamp, "first"); % Gets the "next" odom message
if isempty(odomIdx) % Not found -> use last odom message
odomIdx = numel(timeArray);
elseif odomIdx > 1 % If sensor timestamp before odometry, use first odom message
odomIdx = odomIdx-1; % Use "most recent" odom message at that timestamp
odomMsg = odomMsgs{odomIdx};
to get the most recent odometry message at the time of the sensor data.
There are ways to make this more efficient, but this is the basic workflow you can follow and improve on as needed.


Find more on ROS Log Files and Transformations in Help Center and File Exchange



Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by