ROS2 Basics in 5 Days (Python), Part 4 Services, quiz, timer or delay


In quiz of part 4 one of the inputs is int32 for time.
meaning that the operation should be performed for N seconds.
e.g. if the user give the input of:
ros2 run services_quiz services_quiz_server left 2.0 10
the robot will twist left around Z axis at 2.0 rad/sec for 10 seconds and then stops.

Is there a way to create this delay in a straight forward way, one-line command without using timer of spin?
I read about sleep_for or sleep_until but I couldn’t understand the syntax or how to import, (couldn’t find a proper example or guide for me to understand).

And Ideas how to solve it. It will be vey helpful to finally finish part 4.

Thank you

Hello @TAURD ,

I’d say the easiest way to do that is uning the sleep() method. You can use like this:

import time


This will make the program “sleep” for 10 seconds before continuing.

That is something I could not find much information about. So it is very unclear to me what is the proper way of doing this. I researched into rclpys’ documentation about the timer object and I ended up doing the following:

    def turn_callback(self, request, response):
            f"I've been called for {request.time}s at {request.angular_velocity}rad/sec towards the {request.direction}")

        self.cmd.angular.z = request.angular_velocity if request.direction == 'left' else - \


        timer = self.create_timer(
            request.time, lambda: self.publisher.publish(Twist()))

        while not timer.is_ready():
                f'The robot will turn for {timer.time_until_next_call()} more nseconds')

        self.get_logger().info('robot stopped')

        response.success = True
        return response

Is this bad, is it good? I really would appreciate some feedback on my code.


This solved the problem
(ROS2 Basics in 5 Days (Python), Part 4 Services, quiz, timer or delay - #2 by albertoezquerro)

self.req.time = int(sys.argv[3])

import time


1 Like

This topic was automatically closed after 18 hours. New replies are no longer allowed.