ROS 2 Lyrical Luth · Lesson 11 of 86
Parameters
Declaring, reading and changing parameters, YAML files and validation callbacks.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain parameters 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
Parameters are named settings of a node, such as a speed limit or frame name. In C++ every parameter must be declared with declare_parameter(name, default) before use; the declared type is enforced. Read it with get_parameter(name).
Parameters can change while the node runs (ros2 param set) or be loaded from a YAML file at start-up. Register a parameter callback to validate changes and reject bad values. Parameters are for configuration, not for streaming data.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the parameters example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 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
declare_parameter<double>("max_speed", 1.2);
// Declare a double with a default; without this the parameter does not exist.
declare_parameter<std::string>("frame", "base_link");
// Declare a string parameter.
max_speed_ = get_parameter("max_speed").as_double();
// Read the current value.
handle_ = add_on_set_parameters_callback([](const std::vector<rclcpp::Parameter>& ps) {
// Validate changes made at run time.
rcl_interfaces::msg::SetParametersResult r; r.successful = true;
// Accept by default.
for (const auto& p : ps) if (p.get_name() == "max_speed" && p.as_double() <= 0.0) r.successful = false;
// Reject a non-positive speed.
return r;
// Return the verdict.
});
// End of the callback.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 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.
Demo for Parameters. Expected result: ros2 param get /arm_node max_speed -> Double value is: 0.8
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.
# config/arm.yaml
arm_node:
ros__parameters:
# TODO: set max_speed to 0.8 and frame to "base_link"
# Run: ros2 run robot_nodes arm_node --ros-args --params-file config/arm.yamlExpected output
ros2 param get /arm_node max_speed -> Double value is: 0.8Stuck? Show the full solution (try it yourself first)
# config/arm.yaml
arm_node:
ros__parameters:
max_speed: 0.8
frame: "base_link"
# Run: ros2 run robot_nodes arm_node --ros-args --params-file config/arm.yamlExpected 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
Add max_speed and frame parameters to a node, load them from a YAML file with --ros-args --params-file, change one with ros2 param set, and confirm a negative speed is rejected.
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
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.