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

Replacement for rospy.time.now() in ROS2?

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

OwnageManFromLOL gravatar image

In ROS1 if I wanted to publish an Odometry msg I would use rospy.Time.now() 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 = rospy.Time.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
17

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

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.

my_node.get_clock().now()

More info on the design can be found here: https://design.ros2.org/articles/cloc...

edit flag offensive delete link more

Comments

Hey tfoote thanks for your help :)

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

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

another helpful answer

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

The .to_msg() is critical!

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

hello, is it the same for c++ ?

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

Yes, there's an equivalent API in rclcpp

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

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

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

Comments

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 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 19,472 times

Last updated: Jul 20 '22