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

ros::Time doesn't update in variable

asked 2013-09-08 09:40:14 -0500

benjaminabruzzo gravatar image

I have trimmed most of the irrelevant code from this post.

int main(int argc, char **argv)
{
 float f_grabtime;
 do
 {
  ROS_INFO("\n###############################################");
  f_grabtime = ros::Time::now().toSec();
  ROS_INFO("f_grabtime = : %6.4f", f_grabtime);
  DummyFunction();
  ROS_INFO("ros::Time::now().toSec(); %6.4f", ros::Time::now().toSec());
  ROS_INFO("delta time; %6.4f", ros::Time::now().toSec() - f_grabtime);
 } 
 while (ros::ok());
return 0;
} //end main

When run, I get the output:

###############################################
[ INFO] [1378668866.709873391]: f_grabtime = : 1378668928.0000
[ INFO] [1378668866.709900160]: ros::Time::now().toSec(); 1378668866.7099
[ INFO] [1378668866.709920681]: delta time; -61.2901
[ INFO] [1378668866.881583005]: 
###############################################
[ INFO] [1378668866.881641471]: f_grabtime = : 1378668928.0000
[ INFO] [1378668866.881672380]: ros::Time::now().toSec(); 1378668866.8817
[ INFO] [1378668866.881696694]: delta time; -61.1183
[ INFO] [1378668867.037738183]: 
###############################################
[ INFO] [1378668867.037806140]: f_grabtime = : 1378668928.0000
[ INFO] [1378668867.037836070]: ros::Time::now().toSec(); 1378668867.0378
[ INFO] [1378668867.037860362]: delta time; -60.9622
[ INFO] [1378668867.201626074]: 
###############################################
[ INFO] [1378668867.201681767]: f_grabtime = : 1378668928.0000
[ INFO] [1378668867.201705114]: ros::Time::now().toSec(); 1378668867.2017
[ INFO] [1378668867.201727003]: delta time; -60.7983

A) why does f_grabtime have a value in the future?

B) why doesn't it update at every loop (as the raw ros::Time callout does)?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
5

answered 2013-09-08 21:10:29 -0500

tfoote gravatar image

You are just observing floating point precision errors. That is why we use 64 bit precision to store ROS time values.

I added this line to your program:

   ROS_INFO("delta between float and double %f %f float cast of time %f", f_grabtime, d_grabtime, (float) d_grabtime);

Where d_grabtime is a double instead of a float. And you get this output:

[ INFO] [1378710468.378363642]: delta between float and double 1378710528.000000 1378710468.378310 float cast of time 1378710528.000000
edit flag offensive delete link more

Comments

Thanks. I'll make that change tomorrow and ensure that fixes the problem (I assume it will). I'll check this as the correct answer when I'm done.

benjaminabruzzo gravatar image benjaminabruzzo  ( 2013-09-10 10:41:55 -0500 )edit

That fixed it, thanks!

benjaminabruzzo gravatar image benjaminabruzzo  ( 2013-09-11 03:22:20 -0500 )edit
0

answered 2013-09-08 19:32:20 -0500

If you are using ros::Time functions outside of a ROS node, as it seems to be the case, first you need to call:

ros:Time::init();
edit flag offensive delete link more

Comments

Sorry, trimming the code seems to have made that less obvious. The code IS in a ROS node, though its good to know that it doesn't need to be, thanks.

benjaminabruzzo gravatar image benjaminabruzzo  ( 2013-09-10 10:40:03 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-08 09:40:14 -0500

Seen: 2,923 times

Last updated: Sep 08 '13