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

[ROS2] Why rclcpp node now() function returns 0 in node construtor?

asked 2020-07-09 05:11:29 -0600

avzmpy gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-07-10 12:27:04 -0600

sloretz gravatar image

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.

edit flag offensive delete link more

Comments

Thank you!

avzmpy gravatar image avzmpy  ( 2020-07-14 05:23:02 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-09 05:11:29 -0600

Seen: 2,430 times

Last updated: Jul 10 '20