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

How to associate Rosbag time with data

asked 2013-09-22 06:44:54 -0500

updated 2013-09-23 04:19:24 -0500

Hello everyone!

I have saved some flight data using rosbag record, but the header of my topic /odom doesn't contain the time. When I extract the data using rostopic echo -b foo.bag -p /odom > odom.csv, the column %time is always 0.

I was wondering how can the rosbag and rxbag can synchronize the data... I would like to know the mechanic and how I can get the rosbag time to associate it with the data.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2013-09-23 04:17:35 -0500

updated 2013-09-23 04:18:50 -0500

You can use the Rosbag API to do what you want. If you look at the bottom of the page, the Python API returns the time that the message was recorded (t in the example). Probably there's something equivalent in the C++ API, too.

However, if you have the chance to repeat your experiment, it would be much better to fix the publisher of the odom topic so that it includes the actual time stamp in the header, and then record a fresh rosbag. This will be necessary in the long run anyways, since many nodes working with odom data expect the time stamp to be valid. Also, it will lead to more precise time stamps: you typically want to know when your sensor produced the measurement, not when rosbag recorded it.

edit flag offensive delete link more


Yes of course, the bug have been fixed! Thank you for the link for the Rosbag API. To fix my problem I have reruned my bag, subscribe to each topic, fix the header and republished to message under a other name.

ETS-Dronolab gravatar image ETS-Dronolab  ( 2013-09-23 05:28:52 -0500 )edit

Thx, that is exactly what i am looking for~ .. : ) Let's see if there is corresponding API in C++

lunke gravatar image lunke  ( 2019-10-28 02:24:01 -0500 )edit

answered 2018-03-07 17:27:15 -0500

chutsu gravatar image

updated 2018-03-07 22:03:32 -0500

jayess gravatar image

To add on @Martin Günther's answer, here is an example script that adds the missing "timestamp" info to the sensor_msgs/Imu Message.

import os
import sys

import rosbag

def print_usage():
    print("Usage: <ros bag> <target topic>")
    print("Usage: recorded.bag /robot/imu")

if __name__ == "__main__":
    # Check CLI args
    if len(sys.argv) != 3:

    bag_path = sys.argv[1]  # Path to ROS bag you want to repair
    target_topic = sys.argv[2]  # Target topic you want to repair
    repair_path = bag_path.replace(".bag", "-repaired.bag")  # Output bag path

    # Open bag
    bag = rosbag.Bag(bag_path, 'r')
    fix_bag = rosbag.Bag(repair_path, "w")  # Create a repaired bag

    # Iterate through bag
    for topic, msg, t in bag.read_messages():
        # Add time back to the target message header
        if topic == target_topic:
            msg.header.stamp.secs = t.secs
            msg.header.stamp.nsecs = t.nsecs

        # Write message to bag
        fix_bag.write(topic, msg, t)

    # Close bag - Very important else you'll have to reindex it
edit flag offensive delete link more


One can also use the script given in the rosbag/Cookbook to replace message timestamps in a bag with header timestamps

Subodh Malgonde gravatar image Subodh Malgonde  ( 2018-09-06 03:14:43 -0500 )edit

Question Tools

1 follower


Asked: 2013-09-22 06:44:54 -0500

Seen: 8,905 times

Last updated: Mar 07 '18