ROS2 Actions is a powerful component of the Robot Operating System 2 (ROS 2) framework that enables users to execute long-running tasks on remote robots and receive feedback on the task's progress. It provides a standardized way for nodes to communicate with each other asynchronously, allowing for more efficient and robust communication between nodes.

ROS2 Actions uses a server-client model, where a server node receives requests to execute an action and provides feedback on the progress of the action, while a client node sends requests to the server and receives feedback on the status of the action. This enables users to easily monitor the progress of a long-running task and take corrective action if necessary.

1. What are Actions?

Like the other communication types and their respective interfaces (topics/msg services/srv), you can also custom-define actions in your packages. Actions are one of the communication types intended for long-running tasks. They consist of three parts: a goal, feedback, and result.

Actions are built on topics and services. Their functionality is similar to services, except actions can be canceled. They also provide steady feedback, as opposed to services that return a single response.

Actions use a client-server model, similar to the publisher-subscriber model. An “action client” node sends a goal to an “action server” node that acknowledges the goal and returns a stream of feedback and a result.

Action-SingleActionClient.gif

a. Turtlesim example

For an example of how to use actions, we will use the turtlesim node.

Open a new terminal and run:

humble
ros2 run turtlesim turtlesim_node

Open another terminal and run:

humble
ros2 run turtlesim turtle_teleop_key

When you launch the /teleop_turtle node, you will see the following message in your terminal

Use arrow keys to move the turtle.
Use G|B|V|C|D|E|R|T keys to rotate to absolute orientations. 'F' to cancel a rotation.

Action1.mp4

Let's focus on the second line, which corresponds to an action. Notice that the letter keys G|B|V|C|D|E|R|T form a “box” around the F key on a US QWERTY keyboard (if you are not using a QWERTY keyboard, see this link to follow along). Each key’s position around F corresponds to that orientation in turtlesim. For example, the E will rotate the turtle’s orientation to the upper left corner.

Pay attention to the terminal where the /turtlesim node is running. Each time you press one of these keys, you are sending a goal to an action server that is part of the /turtlesim node. The goal is to rotate the turtle to face a particular direction. A message relaying the result of the goal should display once the turtle completes its rotation:

[INFO] [turtlesim]: Rotation goal completed successfully

The F key will cancel a goal mid-execution.

Try pressing the C key, and then pressing the F key before the turtle can complete its rotation. In the terminal where the /turtlesim node is running, you will see the message: