0
$\begingroup$

Rosanswers logo

I'm calling client now function while constructing the node, but this value is still zero:

now().seconds(); // 0 always (only in constructor) for some reason

Is it intended behaviour? I'm using simulation time. Later, when calling same exact function in a method, it returns correct time value.


Originally posted by avzmpy on ROS Answers with karma: 83 on 2020-07-09

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Time is zero in the node constructor when using simulated time because the clock has not received the current time yet.

The clock gets the simulated time through a subscription to a /clock topic. Subscription callbacks are called by the executor, but the node must be constructed before it can be added to an executor. That's why simulated time will always be zero in the constructor.

I'm guessing you'd like record the initial time when a node starts. I would recommend doing that by checking if the time is zero when the node starts, and if so register a time jump callback. When the new simulated time is received the time jump callback will be called and the initial time can be recorded.

You might also be interested in the Clock and Time ROS 2 design doc which describes how simulated time works.

In particular:

If time has not been set it will return zero if nothing has been received. A time value of zero should be considered an error meaning that time is uninitialized.


Originally posted by sloretz with karma: 3061 on 2020-07-10

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by avzmpy on 2020-07-14:
Thank you!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.