# Get bag file time through callback function c++

I have a large bag file that publishes sensor data in many different topics. One of them is for camera footage. I have a custom node which subscribes to some of those topics and process the data. Let's say that I want to know exactly when a specific image frame from the bag file arrives in my custom node (assuming I know the exact Bag Time of this frame in the back file). Is there a way to get this information from my callback function?

I tried to number the incoming images inside my code (starting with 1, the first image that arrives). Then I found the image number I was interested, with printing the incoming image. But this is not consistent (I guess because of some timing issues with the callbacks) and every time I run the code a different image is saved on the same image number.

I have the queue size to 1, cause this is a demo for a real time application.

Is it possible to do what I want? and if yes, how? Any feedback is appreciated.

I use Ubuntu 18.04, ROS melodic, c++11

edit retag close merge delete

Quick comment: do you absolutely need to use a separate node and subscribe? If not: use the rosbag API and iterate over the bag that way.

Otherwise, this doesn't seem different from the general case when you'd like to know the arrival time of a message (ie: the rosbag aspect doesn't change anything I believe). Use a MessageEvent subscription.

It's rare that knowing the receipt time is important though. Most of the times the header.stamp would be better for synchronisation and related tasks.

( 2021-06-03 01:50:20 -0600 )edit

@gvdhoorn in my case it's necessary to have the node. I'm trying to do it this way because I want to test my node. Is the arrival time of the message consistent though? i.e. Is a specific image frame from the bag file going to arrive always at the same time in my callback function, every time I run my code? I have a feeling that this will be affected by how fast or slow the callback is called. If that is the case, and the one image frame does not arrive always at the same time in the callback, then knowing the arrival time will have the same results as the image numbering I'm using already.

( 2021-06-03 10:58:30 -0600 )edit

The arrival time is when the middleware received the message. Not when the callback gets called.

And re: consistent: no, that's why I mentioned relying on receipt time is rare, as it's dependent on quite a few factors. Counting messages is also not really robust.

For time-synchronisation of messages, header.stamp should really be used, preferably in combination with something like the message_filters package.

Async pub-sub systems are not really well suited for deterministic message flows. Or at least, not unless the middleware specifically supports that. And plain TCPROS doesn't.

One of the reasons ROS 2 uses DDS.

( 2021-06-03 11:20:14 -0600 )edit

ok. Since this is just a demo to try the node, i'll find a workaround to get the frames i'm interested in from the bag file. But thanks for the notes. I'll have this in mind for future work.

( 2021-06-03 11:27:39 -0600 )edit