time synchronize RosBag Data (Odometry and sensor values)

30 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!
Cam Salzberger
Cam Salzberger on 11 Apr 2022
Hello Jefilia,
The answer may depend on what you mean by "synchronize":
First of all, are you talking about the "rosbag timestamps" (the time at which the message was received and recorded in the rosbag) or the "message timestamps" (generally the time at which the message was created or the sensor data was recorded)?
Next, what do you need. If you have messages in sorted order based on whichever timestamp, are you processing the sensor data and just need the most recent odometry message based on the sensor message's time? Or do you need to group one (or more) odometry message with their "closest" sensor messages, even if the odometry message time is later than the sensor message time?
If you have more than one sensor (besides odometry), are you "synchronizing" them all together, or processing each sensor message/topic independently?
How do you want to handle dropped messages? If you have sensor data that is expected to be paired with other sensor data or odometry data, but those messages were lost, what do you do? Drop that data point entirely? Reuse the last-received sensor data? Interpolate data based on whatever was received previously and next?
A lot of considerations kind of go into this. But if you can at least answer which timestamps you are focusing on, I can provide a general recommendation.
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 Network Connection and Exploration 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!