ROS 2 Lyrical lab 05 ยท 2 hours
Services and motion actions
Use services for short requests and actions for cancellable, feedback-producing work.
Practice this lab in your browser: Action feedback and resultUnderstand before building
How this part of ROS 2 works
Services fit short request-response work. Actions add goal acceptance, feedback, results, and cancellation for work that takes time.
Motion belongs in an action because an operator must be able to observe progress and request cancellation.
Before you start
- Lab 04 complete
- Callbacks and smart pointers
Files you will touch
src/robot_interfaces/action/MoveJoint.actionsrc/robot_motion/src/move_server.cpp
Step 1
Set up
# MoveJoint.action
float64 target
---
bool reached
string message
---
float64 remainingStep 2
Create the file
using MoveJoint = robot_interfaces::action::MoveJoint;
auto server = rclcpp_action::create_server<MoveJoint>(
this, "move_joint",
[](const auto &, const auto &) { return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE; },
[](const auto) { return rclcpp_action::CancelResponse::ACCEPT; },
[this](const auto goal_handle) { execute(goal_handle); });What each part does
- 1.The action type defines goal, result, and feedback sections separated by `---`.
- 2.The server has callbacks for accepting goals, accepting cancellation, and starting controlled execution.
Step 3
Build, run and inspect
colcon build --packages-select robot_interfaces robot_motion
source install/setup.bash
ros2 action list -t
ros2 action send_goal /move_joint robot_interfaces/action/MoveJoint "{target: 0.5}" --feedbackProof that it works
- The action appears with its custom type
- Feedback decreases toward zero
- A cancel request stops simulated motion and returns a canceled result
Recovery notes
- Generate interfaces in a dedicated ament_cmake package with rosidl_default_generators.
- Never block the executor callback; run accepted goals on controlled worker logic.