time from rostime::now doesn't change
Hi, when I use ros::Time::now().toSec() function to get time, the time from rostime::now doesn't change . But time from date command and gettimeofday function is changing.
I think there is a bug in rostime. Could you please fix the bug? Br, xiaofyao
Edit: This is an occasional problem, the first time we've had this problem with ros in a long time.
In this function, the ros time is printed every second, but the ros time doesn't change:
int main(int argc, char *argv[])
{
ros::init(argc, argv);
while(1)
{
printf("ros time is : %u\n", ros::Time::now().toSec());
sleep(1);
}
}
The code run results on the problem device :
ros time is : 2917579440
ros time is : 2917579440
ros time is : 2917579440
ros time is : 2917579440
ros time is : 2917579440
ros time is : 2917579440
Asked by xiaofyao on 2020-02-07 03:12:51 UTC
Answers
I believe there is a mistake here:
printf("ros time is : %u\n", ros::Time::now().toSec());
From the API documentation for double ros::TimeBase<..>::toSec() const, we see it returns a double
.
You are trying to print it as an Unsigned decimal integer (%u
, see here), which is not the same type. So the bytes that constitute the double
returned by toSec()
are being interpreted as an integer
by printf(..)
.
The compiler helps here:
warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'double'
This would seem to be the cause of the strange values (2917579440
interpreted as a Unix epoch is Thursday, June 15, 2062 6:44:00 AM
).
If you change the format specifier to %f
instead, it should start printing values you are expecting.
Edit:
According to your opinion, I have modified the code but the ros time has not changed
Please show the full code and what it outputs. And also tell us how you start things.
As-is, your code sample doesn't compile (ros::init(..)
takes at least one additional argument), and is also missing a ros::NodeHandle
.
Without a ros::NodeHandle
, I get:
terminate called after throwing an instance of 'ros::TimeNotInitializedException' what(): Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called. If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()
Asked by gvdhoorn on 2020-02-07 04:10:28 UTC
Comments
According to your opinion, I have modified the code but the ros time has not changed
Asked by xiaofyao on 2020-02-07 04:21:48 UTC
Thanks for help me. I changed the code according to your suggestion
int main()
{
ros::Time::init();
uint8_t cnt = 0;
while(1)
{
cnt++;
if(cnt >= 10)
break;
printf("ros time : %f\n", ros::Time::now().toSec());
sleep(1);
}
}
Result:
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
ros time : 1581068933.704649
Asked by xiaofyao on 2020-02-07 04:52:24 UTC
You still haven't told us how you start everything.
And show us the full code. Including headers that you include
.
Asked by gvdhoorn on 2020-02-07 04:56:13 UTC
I was facing the same issue and realized that it happened because my node runs before gazebo simulation is ready. So, for me the solution was to simply run the node that contains ros::Time::now only after the gazebo simulation is ready and running.
I guess this is because ros::Time::now().toSec() gets the time of simulation that you see in gazebo, that's why it has to be run after running gazebo.
I hope this helps
Asked by AA A on 2022-08-10 09:56:20 UTC
Comments
That part of the ROS 1 infrastructure has been around for at least 12 years.
Age does not guarantee code is bug free, but it would be surprising if no one has run into this before.
Could you please show a sample of code that exhibits this problem? And please also tell us a little bit about how you start things (ie: launch files,
rosrun
, with or without Gazebo, etc).Asked by gvdhoorn on 2020-02-07 03:34:22 UTC