ROS 2 for Robot Arms, step 6
Configuring nodes (parameters)
Make the mover's speed and home pose parameters, take them from a launch configuration, and reject bad values at runtime.
Settings, not constants
A parameter is a named setting of a node that can change without editing its code. Declare it with a default, then read it:
self.declare_parameter("speed", 0.5, ParameterDescriptor(description="fastest joint speed (rad/s)"))
speed = self.get_parameter("speed").value
The default fixes the type. 0.5 makes a double, and a list of floats makes a double array. Write 0.0, not 0, or you get an integer, and a later set of the other type is refused.
Overrides
On a robot the values come from a YAML file or a launch file when the node starts:
mover:
ros__parameters:
speed: 0.8
ros2 run my_pkg mover --ros-args -p speed:=0.8 does the same for one value. An override replaces the default your code declares. Here the step plays the launch file: /mover starts with speed: 0.8.
Changes at runtime
ros2 param set /mover speed 1.4 calls the node's ~/set_parameters service, so the node has to be spinning to answer. Before anything changes, rclpy calls the callbacks you register with add_on_set_parameters_callback. Each gets the new parameters and returns SetParametersResult(successful=..., reason=...). Refuse a bad value and it never takes effect: the caller gets your reason. Read a parameter where you use it, so accepted changes show up straight away.
Your task
Mover tours between P1 and P2, and keeps /go_home from step 5.
- Declare
speed(default0.5) andhome(defaultHOME), each with a description. - Use the
speedparameter instep()and thehomeparameter inon_go_home. - In
on_params, refuse a speed of 0 or less, and a home that isn't 4 values insideLIMITS, each with a reason.
During the run the grader sets speed to 1.4, then −1.0, then sets two homes (one out of range), and calls /go_home at 11.5 s.