0
$\begingroup$

Rosanswers logo

I am trying to create a controller plugin for the nav2 stack. As per the tutorials, I have rclcpp_lifecycle::LifecycleNode shared pointer which I use to create a clock (node->get_clock()) and use clock-> now() to get the latest time. I have created libraries which are used with this plugin. I want to get the latest time in these libraries without having to pass down the node handles.

In ROS1 we could create node handles in the library itself and get the latest time using ros::Time::now() but the library itself wouldn't be a node. However, I don't think we can have a ros2 node handle spawned out of the blue, without it being tied to an actual node. I don't want to have to pass a node handle 3 layers into this library for two timers that can be paused with simulation time. I have seen some examples where to get the latest time without node handles they use rclccpp::Time() but I am not sure what it means and what it does. Also, is there an alternative to ros::Time::now() or ros::Time(0) in ROS2 without passing down the node handles as mentioned earlier and what exactly does rclcpp::Time() mean without any node initialisation?

I am using ROS2 Foxy in Ubuntu 20.04


Originally posted by Divyanshu on ROS Answers with karma: 15 on 2020-11-19

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The short answer is that you cannot get time now without a Node instance. In ROS 1 you could create a NodeHandle from nothing as long as you'd initialized ROS because there was a global node instance actually under the hood. And the bare functions could rely on that. In ROS 2 we've removed that global under the hood so you cannot invoke the bare functions, nor construct a Node without appropriate context.

You can create Time objects themselves, but you have to get now() from a clock object. And that clock object must have a TimeSource attached, which is provided by the Node instance. The use of rclcpp::Time is forced to wall time and is not recommended for general use. There's more to extend/implement in the Time API see: https://github.com/ros2/rclcpp/issues/1248 and https://github.com/ros2/rclcpp/issues/465


Originally posted by tfoote with karma: 58457 on 2020-11-20

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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