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

Extract timestamp from bag for some topics without the time

asked 2023-05-23 04:00:49 -0500

Marcus Barnet gravatar image

updated 2023-05-23 04:40:00 -0500

Hi to all,

unfortunately, I recorded a very long bag file containing some topics that have a timestamp and two topics that do not include it since I forgot to add it before to launch the ROS node. Unfortunately, all the topics have different sampling time, so I cannot find a valid method to reference the value recorded in the topics without the timestamp against the timestamp from the other topics.

Is there a way to add the timestamp also in the topics that do not include it? I was hoping that when you launch "rosbag record -a" the utility somehow keeps trace of the time acquisition. :)

This is the topic list and the topics without the timestamp are loadcell and steering. Hope you can help me, thank you!

topics:      /diagnostics                 692 msgs    : diagnostic_msgs/DiagnosticArray
             /joy                       18326 msgs    : sensor_msgs/Joy                
             /robo_explorer/cmd_vel     18326 msgs    : geometry_msgs/Twist            
             /robo_explorer/enc          4537 msgs    : std_msgs/String                
             /robo_explorer/io_status   18326 msgs    : robo_explorer/robo_io          
             /robo_explorer/loadcell     7049 msgs    : std_msgs/String                
             /robo_explorer/steering     9887 msgs    : std_msgs/String                
             /robo_explorer/sys          4538 msgs    : std_msgs/String                
             /robo_explorer/velocity     4537 msgs    : std_msgs/String                
             /rosout                    56497 msgs    : rosgraph_msgs/Log               (4 connections)
             /rosout_agg                56484 msgs    : rosgraph_msgs/Log

This is the test.bag file as reference.

edit retag flag offensive close merge delete

Comments

1

quick comment: I'm not sure exactly what you're asking, but rosbag does store the reception time with each message it records. It will be wildly inaccurate though (well .. depends on your requirements) compared to stamps stored in header.stamp. See #q199941 and #q318536 for earlier questions, and wiki/rosbag/Cookbook for a collection of example Python rosbag processing scripts.

Is there a way to add the timestamp also in the topics that do not include it?

no, not without changing your .msg definition and completely rewriting/processing the .bag file.

You can figure out reception time for all messages recorded, but you wouldn't then "add it" like you seem to be asking.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-23 04:27:12 -0500 )edit

Thank you a lot for your support. I already tried that links before opening the topic since I wasn't able to retrieve the time in any way by using that examples. The loadcell topic is just a string with values separated by a comma. I uploaded the bag file in the main topic just for reference.

Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-23 04:35:46 -0500 )edit
1

The first example in the Cookbook () basically does this:

for topic, msg, t in rosbag.Bag('input.bag').read_messages():
    ...

The t there is the time the message was received/recorded.

Just to clarify: you will not somehow get a "timestamp" as part of the messages that were recorded. Rosbag does not change what it records (actually, it doesn't even deserialise the msgs it receives, so it only records a binary blob). The only thing you can do is ask Rosbag to give you the time at which it recorded the message.

That is what the t is in the snippet above.

The loadcell topic is just a string with values separated by a comma

pedantic, but that's of course never a good idea in a strongly typed system such as ROS.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-23 05:45:06 -0500 )edit

I already tried that specific example but it only gives me the original topic as output without any timestamp.

Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-23 05:56:31 -0500 )edit

I'm confused.

The only time information you would have access to in case of a message stream in a .bag where the .msg themselves don't contain a std_msgs/Header would be the time at which each individual message got recorded.

That time is what you have access to in the t variable in the line:

for topic, msg, t in rosbag.Bag('input.bag').read_messages():

Notice the t in the topic, msg, t (unpacked) tuple.

That t is not part of your .msg definition. It's what Rosbag itself records, in addition to the message contents.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-23 06:57:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-23 07:02:52 -0500

gvdhoorn gravatar image

updated 2023-05-23 08:36:31 -0500

To make it really concrete: the following should print the time t at which message msg was recorded followed by the contents for each message received on the topic /robo_explorer/loadcell:

for topic, msg, t in rosbag.Bag('input.bag').read_messages():
    if topic == "/robo_explorer/loadcell":
        print (f"{t}: '{msg}'")

(haven't checked the syntax, so there may be errors)

That would be the best you could do in the absence of a std_msgs/Header field.

And note again: this would get you time at which the message was recorded, not when it was published.

edit flag offensive delete link more

Comments

I'm not a python expert, so probably I had to change the code you provide, however, I tried your code as it is but unfortunately it just prints a series of: {t}: '{msg}'

{t}: '{msg}'
{t}: '{msg}'
{t}: '{msg}'
{t}: '{msg}'
Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-23 08:34:23 -0500 )edit

Ok, my fault, I needed to check the syntax, now it works and prints the timestamp! Thank you for your support!

print (t, msg)
Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-23 08:37:15 -0500 )edit
1

As I wrote:

haven't checked the syntax, so there may be errors

I've added the f prefix, to make it an f-string. Note that if you're not using Python 3, it still won't work.

Finally: this is all just an example. Please experiment and use it to arrive at a satisfactory solution.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-23 08:37:56 -0500 )edit

Thank you for your help and support, I was able to correctly recover the timestamp.

Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-23 15:36:13 -0500 )edit

Just to make it extra clear: you did not recover a / the timestamp.

You're now using the time Rosbag recorded your message.

Those are two very different things.

gvdhoorn gravatar image gvdhoorn  ( 2023-05-24 01:52:06 -0500 )edit

Yes, you have reason, I should edit my previous comment to avoid misunderstandings. I just recovered the time related to when rosbag utility received the message from the original topic. It is still OK for me since it was just a test. EDIT: I cannot change my previous post, unfortunately.

Marcus Barnet gravatar image Marcus Barnet  ( 2023-05-24 01:57:46 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2023-05-23 04:00:49 -0500

Seen: 219 times

Last updated: May 23 '23