rclpy: Time
To get the equivalent of rospy.Time.now(), you now need a ROS 2 node:
import rclpy
from rclpy.node import Node
class MyNode(Node):
def some_func(self):
t = self.get_clock().now()
msg.header.stamp = t.to_msg()
Converting from Duration to messages is common:
Timers
Timers are created from the Node:
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__("my_node")
# Create a timer that fires every quarter second
timer_period = 0.25
self.timer = self.create_timer(timer_period, self.callback)
def callback(self):
self.get_logger().info("timer has fired")
Rates
Using Rate objects in ROS 2 is a bit more complex than in ROS 1. Due to implementation details, we need to spin() or the sleep() function will block. This is most easily accomplished using a thread: