How to sync data in a ros bag

asked 2021-02-16 16:57:02 -0500

HumbleBee gravatar image

updated 2021-02-16 17:05:29 -0500

I'm trying to integrate a LiDAR unit with an INS (IMU+GPS) and as it seems there's no way to sync them together unless we're willing to let go of IMU data (only GPS data is output in NMEA format if they are connected). So we connected them separately to a laptop and recorded some sample data and here's the issue: They are not in sync and when I try to do LiDAR mapping in any application ( here's one as an example) I get misaligned point clouds and jerky movement!

I was wondering if there's a way read the bag file, synchronize the data and save it in a new bag?

I have looked into TimeSynchronizer here but I'm not sure how to apply it, or if it's what I need for this purpose at all.

Here's also a hacky script I came up with in Python, but it doesn't produce the desired result I guess the timings are too close or it's not the proper method at all:

def sync__to_current_timestamp(bag, output_path):
topic_list = ["/os_cloud_node/points", "/os_cloud_node/imu", "/imu/data", "/gnss"]
with rosbag.Bag(output_path + 'data_synced.bag', 'w') as outbag:
    bar = ChargingBar('Syncing', max=bag.get_message_count())
    for topic, msg, t in bag.read_messages():
        bar.next()
        if topic == "/tf" and msg.transforms:
            outbag.write(topic, msg, rospy.Time.now())
        elif topic in topic_list:
            msg.header.stamp = rospy.Time.now()
            outbag.write(topic, msg, rospy.Time.now())
    bar.finish()
    print("\n Done!")

Would appreciate any pointers.

edit retag flag offensive close merge delete

Comments

Have you seen the rosbag cookbook? It gives a good example of how to use the TimeSynchronizer to sync bag data.

tryan gravatar image tryan  ( 2021-02-16 19:59:22 -0500 )edit

@tryan Thank you for the url, I had a look although it's in C++ and that alone is a challenge for me (I was looking for a Python solution) but couldn't find the "saving" logic in that code?

HumbleBee gravatar image HumbleBee  ( 2021-02-18 13:37:58 -0500 )edit

I may have misinterpreted your question, so please clarify if I missed the point. If you have data that were recorded concurrently (is that your situation?), you could play the bag file, and run a custom synchronizer node. ApproximateTimeSynchronizer will collect the messages in a buffer and trigger a group callback when it has collected an set that are near in time. You've probably already looked at this, but here's a Python example. You could save the data to a new bag with a new timestamp in the callback function.

How exactly are the topics "not in sync"? Are they offset by a small delay, different frequencies, or something else?

tryan gravatar image tryan  ( 2021-02-18 15:48:51 -0500 )edit