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

Replacement for in ROS2?

asked 2019-04-21 14:13:11 -0600

OwnageManFromLOL gravatar image

In ROS1 if I wanted to publish an Odometry msg I would use to get the current time and publish the odom msg using that time. However, I haven't been able to find anything similar in ROS2? Does anyone know what functionality I can use to get the time for a msg I am publishing in ROS2?

now =
odom = Odometry()
odom.header.frame_id = self._odom_frame
odom.header.stamp = now
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-04-21 19:40:17 -0600

tfoote gravatar image

There's no longer a global instance of the communication library that you can rely on in rclpy.

You need to get a clock instance from your node instance.


More info on the design can be found here:

edit flag offensive delete link more


Hey tfoote thanks for your help :)

OwnageManFromLOL gravatar image OwnageManFromLOL  ( 2019-04-22 19:33:20 -0600 )edit

dont forget to add .to_msg() at the end.

another helpful answer

flynneva gravatar image flynneva  ( 2020-06-09 19:59:00 -0600 )edit

The .to_msg() is critical!

mschickler gravatar image mschickler  ( 2020-08-26 08:42:23 -0600 )edit

hello, is it the same for c++ ?

Wissem gravatar image Wissem  ( 2023-03-12 16:46:31 -0600 )edit

Yes, there's an equivalent API in rclcpp

tfoote gravatar image tfoote  ( 2023-03-12 21:57:28 -0600 )edit

answered 2022-07-20 10:47:04 -0600

lorepieri gravatar image
from rclpy.clock import Clock
time_stamp = Clock().now()

to use it:

p = PoseStamped()
p.header.stamp = time_stamp.to_msg()
edit flag offensive delete link more


This is incorrect. The default constructed clock will not have a timesource attached and obey simulated time.

tfoote gravatar image tfoote  ( 2022-07-27 15:39:10 -0600 )edit

Question Tools

1 follower


Asked: 2019-04-21 14:13:11 -0600

Seen: 16,718 times

Last updated: Jul 20 '22