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

Is there a more elegant way to get the time in seconds in a ROS 2 node?

asked 2023-05-29 18:27:09 -0600

Hi,

Is there a more elegant way to get the time in seconds in a ROS 2 Python node?

Currently, what I'm doing is the following:

current_time = self.get_clock().now().nanoseconds

dt = (current_time - self.last_time) * 1e-9
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-30 06:48:31 -0600

danzimmerman gravatar image

It does not seem like there is. There's a .seconds() method that was added to rclcpp durations:

https://github.com/ros2/rclcpp/issues...

but it looks like that is not supported in rclpy:

>>> from rclpy.node import Node
>>> t1 = Node("blah").get_clock().now()
>>> t2 = Node("blah").get_clock().now()
>>> d = t2-t1
>>> type(d)
<class 'rclpy.duration.Duration'>
>>> dir(d)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_duration_handle', 
'from_msg', 'get_c_duration', 'nanoseconds', 'to_msg']

The example above is from ROS 2 Humble, but it's the same in the Rolling docs.

https://docs.ros.org/en/rolling/p/rcl...

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2023-05-29 18:27:09 -0600

Seen: 2,943 times

Last updated: May 30 '23