C++ Advanced · Lesson 15 of 23
Parameters
declare_parameter, get_parameter, defaults, and reacting to changes.
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 rate or a limit. In C++ you must declare each one with declare_parameter(name, default) and then read it with get_parameter(name). The declared type is enforced.
Parameters can be changed while the node runs with ros2 param set. Register a parameter callback to validate and apply changes, and return a failure for an invalid value. Collect them in the configuration struct from Phase 6.
Guided lesson
Work through these steps
- 1.
Read the parameters example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
class ArmNode : public rclcpp::Node {
// A node with settings.
public:
// Public interface.
ArmNode() : Node("arm_node") {
// Name the node.
declare_parameter<double>("max_speed", 1.2);
// Declare a double parameter with a default of 1.2.
declare_parameter<std::string>("frame", "base_link");
// Declare a string parameter.
cfg_.max_speed = get_parameter("max_speed").as_double();
// Read the current value into the config struct.
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;
// Start by accepting the change.
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 to ROS.
});
// End of the callback.
}
// End of the constructor.
private:
// Hidden state.
struct { double max_speed{1.2}; } cfg_;
// A tiny config struct.
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr handle_;
// Keeps the callback registered.
};
// End of the class.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
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: 1.2
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.
// src/arm_node.cpp
#include "rclcpp/rclcpp.hpp"
class ArmNode : public rclcpp::Node {
public:
ArmNode() : Node("arm_node") {
// TODO: declare max_speed (double, default 1.2) and frame (string, default "base_link"), then read max_speed
RCLCPP_INFO(get_logger(), "max_speed = %.2f", max_speed_);
}
private:
double max_speed_{0.0};
};
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<ArmNode>());
rclcpp::shutdown();
return 0;
}Expected output
ros2 param get /arm_node max_speed -> Double value is: 1.2Stuck? Show the full solution (try it yourself first)
// src/arm_node.cpp
#include "rclcpp/rclcpp.hpp"
class ArmNode : public rclcpp::Node {
public:
ArmNode() : Node("arm_node") {
declare_parameter<double>("max_speed", 1.2);
declare_parameter<std::string>("frame", "base_link");
max_speed_ = get_parameter("max_speed").as_double();
RCLCPP_INFO(get_logger(), "max_speed = %.2f", max_speed_);
}
private:
double max_speed_{0.0};
};
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<ArmNode>());
rclcpp::shutdown();
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Add rate_hz and frame parameters with defaults. Change them with ros2 param set, try to set a negative rate and confirm it is rejected, and print the values with ros2 param list and get.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.