Ask Your Question
0

time from rostime::now doesn't change

asked 2020-02-07 02:12:51 -0500

xiaofyao gravatar image

updated 2020-02-07 03:47:56 -0500

gvdhoorn gravatar image

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
edit retag flag offensive close merge delete

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).

gvdhoorn gravatar image gvdhoorn  ( 2020-02-07 02:34:22 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-02-07 03:10:28 -0500

gvdhoorn gravatar image

updated 2020-02-07 03:43:38 -0500

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()
edit flag offensive delete link more

Comments

According to your opinion, I have modified the code but the ros time has not changed

xiaofyao gravatar image xiaofyao  ( 2020-02-07 03:21:48 -0500 )edit

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
xiaofyao gravatar image xiaofyao  ( 2020-02-07 03:52:24 -0500 )edit

You still haven't told us how you start everything.

And show us the full code. Including headers that you include.

gvdhoorn gravatar image gvdhoorn  ( 2020-02-07 03:56:13 -0500 )edit

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: 2020-02-07 02:12:51 -0500

Seen: 259 times

Last updated: Feb 07 '20