0
$\begingroup$

I want to delay a ROS2 Node, This answer is given respect to ROS1:

Whats the equivalent in ROS2. A quick on the fly time.sleep() but for ROS because I know time.sleep() in ros is a nono.

$\endgroup$

1 Answer 1

1
$\begingroup$

For ROS 2 node functionality that should run at fixed time intervals, I use a callback that runs on a timer.

The simple C++ publisher tutorial is an example:

https://docs.ros.org/en/rolling/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html#write-the-publisher-node

Here's an example in Python from:

https://github.com/mikeferguson/ros2_cookbook/blob/main/rclpy/time.md#timers


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")

I know time.sleep() in ros is a nono.

You can use rate.sleep() from the cookbook above if you need a single or variable delay and you're worried about a sleep call blocking other node functionality. Note the need to inform the thread about the rclpy.spin function if you want rate.sleep() to be nonblocking.

$\endgroup$
1
  • $\begingroup$ If I use Node A to make a request to Node B, will rate.sleep() in Node B pause the flow in main thread such that Node A waits until Node B completes finishes its rate.sleep(). (using a future or whatever the condition is)? $\endgroup$
    – nohacks
    Commented Mar 11 at 14:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.