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

How can I get time difference of two messages published by same publisher on same topic?

asked 2016-03-18 03:09:20 -0500

cybodroid gravatar image

updated 2016-03-18 03:17:48 -0500

I need to calculate time gap between two messages from same publisher on same topic and print it in printable format like string. Please somebody help me.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-03-18 04:10:39 -0500

al-dev gravatar image

Use ros::Time (or any system library for time..) :

ros::Time lasttime=ros::Time::now();
ros::Duration(3).sleep();
ros::Time currtime=ros::Time::now();
ros::Duration diff=currtime-lasttime;

cout<<"diff: "<<diff<<endl;

Note that depending on your application you may want to measure either 1) the time difference between the two calls to publish() by your publisher, or 2) the time difference between the two executions of the callback of your subscriber.

The two can be quite different depending on the amount of delay on your network and whether your subscribing node is busy doing something else when the message is published on the topic...

A robust approach is to include timestamps in the messages that you publish, so that your subscribing node can check if the messages are outdated, or even if they are not arriving in chronological order.

edit flag offensive delete link more

Comments

Hi, I tried this code but printed it with ROS_INFO

 ROS_INFO("%2f secs", diff);

and it prints out 0.0000000 secs. any idea what the problem might be?

naihart gravatar image naihart  ( 2022-04-12 12:34:59 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-18 03:09:20 -0500

Seen: 14,486 times

Last updated: Mar 18 '16