0
$\begingroup$

Rosanswers logo

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.


Originally posted by Marcus Barnet on ROS Answers with karma: 287 on 2023-05-23

Post score: 0


Original comments

Comment by gvdhoorn on 2023-05-23:
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.

Comment by Marcus Barnet on 2023-05-23:
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.

Comment by gvdhoorn on 2023-05-23:
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.

Comment by Marcus Barnet on 2023-05-23:
I already tried that specific example but it only gives me the original topic as output without any timestamp.

Comment by gvdhoorn on 2023-05-23:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2023-05-23

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Marcus Barnet on 2023-05-23:
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}'

Comment by Marcus Barnet on 2023-05-23:
Ok, my fault, I needed to check the syntax, now it works and prints the timestamp! Thank you for your support!

print (t, msg)

Comment by gvdhoorn on 2023-05-23:
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.

Comment by Marcus Barnet on 2023-05-23:
Thank you for your help and support, I was able to correctly recover the timestamp.

Comment by gvdhoorn on 2023-05-24:
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.

Comment by Marcus Barnet on 2023-05-24:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.