# ROS2 run command for fixed time

**URL:** <https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447>\
**Category:** General Support\
**Tags:** howto\
**Created:** [September 23, 2022, 11:18am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447 "2022-09-23T11:18:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.malmvig](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/r.malmvig/32/5286_2.png) [@r.malmvig](https://get-help.theconstruct.ai/u/r.malmvig)\
**Post date:** [September 23, 2022, 11:18am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/1 "2022-09-23T11:18:35Z")

</div>

Hi developers

I am learning about services in ROS2 and need to run a robot for 10 seconds and then stop then robot. How can I do this?

/Rasmus

---

<div class="post-metadata">

**Author:** ![GasPatxo](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/gaspatxo/32/5363_2.png) [@GasPatxo](https://get-help.theconstruct.ai/u/GasPatxo)\
**Post date:** [September 23, 2022, 11:23am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/2 "2022-09-23T11:23:49Z")

</div>

The simplest way is to use the `time` library:

```python
import time

# Send your command to the robot

time.sleep(10)

```

---

<div class="post-metadata">

**Author:** ![r.malmvig](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/r.malmvig/32/5286_2.png) [@r.malmvig](https://get-help.theconstruct.ai/u/r.malmvig)\
**Post date:** [September 23, 2022, 11:27am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/3 "2022-09-23T11:27:44Z")

</div>

Thank you very much. I had such a hard time finding the information on the net. I know see it is because this is python related and not ROS2

---

<div class="post-metadata">

**Author:** ![GasPatxo](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/gaspatxo/32/5363_2.png) [@GasPatxo](https://get-help.theconstruct.ai/u/GasPatxo)\
**Post date:** [September 23, 2022, 11:47am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/4 "2022-09-23T11:47:27Z")

</div>

Glad I could be of help.

There are also ways using for example [`rclpy.timer.Timer`](https://docs.ros2.org/latest/api/rclpy/api/timers.html).

_PD: Remember to mark the solution to your posts_

---

<div class="post-metadata">

**Author:** ![r.malmvig](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/r.malmvig/32/5286_2.png) [@r.malmvig](https://get-help.theconstruct.ai/u/r.malmvig)\
**Post date:** [September 23, 2022, 11:50am UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/5 "2022-09-23T11:50:17Z")

</div>

I tried using that but failed… Could you give me an example of how that works?

---

<div class="post-metadata">

**Author:** ![GasPatxo](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/gaspatxo/32/5363_2.png) [@GasPatxo](https://get-help.theconstruct.ai/u/GasPatxo)\
**Post date:** [September 23, 2022, 12:05pm UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/6 "2022-09-23T12:05:58Z")

</div>

```python
import rclpy
from rclpy.node import Node
from services_quiz_srv.srv import Turn
from geometry_msgs.msg import Twist

class TurningServer(Node):
    def __init__ (self):
        super(). __init__ ('turning_service_server_node')
        self.cmd = Twist()
        self.publisher = self.create_publisher(Twist, 'cmd_vel', 10)
        self.server = self.create_service(
            Turn, 'turn', self.turn_callback)

    def turn_callback(self, request, response):
        self.get_logger().info(
            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 - \
            request.angular_velocity

        self.publisher.publish(self.cmd) # Send the movement command

        timer = self.create_timer(
            request.time, lambda: self.publisher.publish(Twist())) # Send a stop message after the requested time

        while not timer.is_ready(): # Wait for the timer action to be triggered, but you could do other processes in the meantime
            print(
                f'The robot will turn for {timer.time_until_next_call()} more nseconds')

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

        response.success = True
        return response

def main(args=None):
    rclpy.init(args=args)
    server = TurningServer()
    rclpy.spin(server)
    rclpy.shutdown()

```

---

<div class="post-metadata">

**Author:** ![system](https://get-help.theconstruct.ai/uploads/default/original/2X/9/9d3ce8305cc0d0d9b6e07d15767a6f245c526f1b.jpeg) [@system](https://get-help.theconstruct.ai/u/system)\
**Post date:** [October 3, 2022, 12:06pm UTC](https://get-help.theconstruct.ai/t/ros2-run-command-for-fixed-time/19447/7 "2022-10-03T12:06:47Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
