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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

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

Seen: 6,447 times

Last updated: Mar 18 '16