CodeTheRobot

ROS 2 Lyrical Luth · Lesson 10 of 86

Actions: goal, feedback and result

Long-running tasks with progress and cancellation, .action files, servers and clients.

Estimated time · 90–120 min

Learning goals

What you will understand

  • Explain actions: goal, feedback and result in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

An action is for work that takes time. A client sends a goal, the server accepts it, sends feedback while it works, and finally returns a result. The client may cancel the goal at any moment. The .action file has three parts separated by dashes: goal, result, feedback.

An rclcpp_action server needs three callbacks: handle_goal (accept or reject), handle_cancel, and handle_accepted (start the work, usually on another thread so the executor stays free). Test with ros2 action send_goal --feedback.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the actions: goal, feedback and result example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

ros2 interface show example_interfaces/action/Fibonacci
# Print the goal, result and feedback of a demo action.
ros2 run action_tutorials_cpp fibonacci_action_server
# Start a ready-made action server (from the tutorials package).
ros2 action list -t
# List running actions and their types.
ros2 action info /fibonacci
# Show which nodes act as clients and servers.
ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci '{order: 5}' --feedback
# Send a goal and print feedback until the result arrives.
# Press Ctrl+C in the goal terminal to cancel it
# Cancellation is a normal part of the protocol.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Messages between nodes

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

requestresponseclientservercalls answered: 0MESSAGES BETWEEN NODES

Demo for Actions: goal, feedback and result. Expected result: Goal accepted, feedback lines 0 1 1 2 3 5, then Result: sequence

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# Terminal A
ros2 run action_tutorials_cpp fibonacci_action_server
# Terminal B
# TODO: send a goal of order 5 to /fibonacci and print feedback

Expected output

Goal accepted, feedback lines 0 1 1 2 3 5, then Result: sequence
Stuck? Show the full solution (try it yourself first)
# Terminal A
ros2 run action_tutorials_cpp fibonacci_action_server
# Terminal B
ros2 action send_goal /fibonacci action_tutorials_interfaces/action/Fibonacci "{order: 5}" --feedback

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Write a CountDown action (goal: seconds, feedback: remaining, result: finished). Implement the server with a cancel check inside its loop, send a goal from the CLI, then cancel it and confirm the server stops early.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.