Actions are one of the communication types in ROS 2 and are intended for long running tasks. They consist of three parts: a goal, feedback, and a 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 which return a single response.
Actions use a client-server model, similar to the publisher-subscriber model (described in the topics tutorial). 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.
Start up the two turtlesim nodes, /turtlesim
and /teleop_turtle
.
Open a new terminal and run:
ros2 run turtlesim turtlesim_node
Open another terminal and run:
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.
Let’s focus on the second line, which corresponds to an action. (The first instruction corresponds to the “cmd_vel” topic, discussed previously in the topics tutorial.)
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:
[INFO] [turtlesim]: Rotation goal canceled