Atlas Robotics
ROS2 PackageIntermediate

Parameters in ROS2 — configuring nodes without changing code

Atlasbot · 17/8/2026 · 11 min read

Parameters in ROS2 — configuring nodes without changing code
Three sources can set the value for a parameter, in order of priority

If a motor control node has a hard written max_speed = 0.5 in the code, every time you change the robot (or simply want to try a different speed), you have to edit the code and rebuild. Parameter solves this problem correctly: declare the configuration value outside the code, read by node at runtime — changing the value just requires editing the YAML file or typing a command line flag, no need to rebuild.

Key concepts

import rclpy
from rclpy.node import Node

class MotorNode(Node):
    def __init__(self):
        super().__init__("motor_node")
        self.declare_parameter("max_speed", 0.5)
        max_speed = self.get_parameter("max_speed").value
        self.get_logger().info(f"max_speed = {max_speed}")

declare_parameter(name, default_value) must be called before when reading — ROS2 proactively requires explicit declaration (unlike ROS1, which allows reading any parameter without declaring it first) to avoid the error of mistyping the parameter name without knowing it.

How it works

Three ways to load values, in order of preference:

# config/motor_params.yaml
motor_node:
  ros__parameters:
    max_speed: 0.8
    frame_id: "base_link"
# Trong launch file
Node(
    package="my_pkg",
    executable="motor_node",
    parameters=["config/motor_params.yaml"],
)
# Quick overwrite from command line, no need to edit YAML file
ros2 run my_pkg motor_node --ros-args -p max_speed:=1.2

The final value the node receives is the value loaded last — if both the launch file and command line -p are set to max_speed, the -p flag on the command line wins because it runs later.

In short: The default value in declare_parameter() is a last-ditch precaution if no YAML/launch file will overwrite it — so always default to the safest value (e.g. low speed), not the "most commonly used" value.

Callback when parameters are changed while running:

from rcl_interfaces.msg import SetParametersResult

def parameter_callback(self, params):
    for p in params:
        if p.name == "max_speed":
            self.get_logger().info(f"max_speed đổi thành {p.value}")
    return SetParametersResult(successful=True)

# Trong __init__:
self.add_on_set_parameters_callback(self.parameter_callback)

This callback allows the node to react immediately when someone calls ros2 param set without restarting the node.

Technical details

Parameters

Supported parameter data types, and some constraints can be declared via ParameterDescriptor:

Parameter typeExample
boolenable_camera: true
int64max_retries: 3
doublemax_speed: 0.8
stringframe_id: "base_link"
Array (bool[], int64[], double[], string[])wheel_names: ["left", "right"]

ParameterDescriptor allows additional constraints when declaring, eg: read_only=True (does not allow ros2 param set to change value at runtime — used for parameters that should only be set once at startup, like the I2C address of a sensor).

Protocol

Each ROS2 node automatically "exposes" a set of standard Services for parameter manipulation — get_parameters, set_parameters, list_parameters, describe_parameters — this is exactly the mechanism by which both ros2 param (command line) and rqt_reconfigure (transfer) graphical interface) used below to read/write parameters of any running node, nothing more special than a normal Service call learned in the ROS2 Communication section.

Practical applications

For a robot product line with many different models sharing the same codebase (eg: small and large AMR robots that only differ in wheel radius, maximum speed, sensor mounting position), separating these values into separate YAML files for each model (config/robot_small.yaml, config/robot_large.yaml) allows the same set of code to run correctly on all models — just change the YAML file loaded at launch time, without having to maintain many other branches of code. different for each robot size.

Practical example

$ ros2 param get /motor_node max_speed
Output:
Double value is: 0.8
$ ros2 param set /motor_node max_speed 1.2
Output:
Set parameter successful
Log of node (if there is parameter_callback as in the example above):
[INFO] [motor_node]: max_speed changed to 1.2

This entire operation takes place without restarting node — direct proof of the value of add_on_set_parameters_callback, useful when you need to fine-tune parameters right while the robot is running to observe the immediate effect.

Common mistakes

  • Have you read the parameter declare_parameter() — causes an exception immediately, different from ROS1's silent behavior — always declare before reading, even with the value "will definitely be set from YAML".
  • Wrong data type between default value and value in YAML — For example: declare declare_parameter("max_speed", 0.5) (type double) but YAML records max_speed: 1 (no decimal point, interpreted as int) — ROS2 may report a type mismatch error (ParameterTypeException) depending on the configuration.
  • Node name in YAML does not match actual node name — file config/motor_params.yaml declares motor_node: but actual node runs with different name (due to remap or namespace) — parameter will not be applied, no explicit warning, node still runs with default value.
  • Attempt ros2 param set a parameter declared read_only=True — request rejected with successful=False in response — check ros2 param describe to know which parameter is locked before debug mistakenly thinks it is a callback error.

Conclusion

Parameter is a standard mechanism to separate configuration from code, supports 3 levels of value loading (default → YAML/launch → command line) and can be changed at runtime via the learned Service mechanism. Always set safe default values ​​and declare them explicitly before reading. This is also the last article of the ROS2 Package category - enough foundation to completely package a ROS2 package from code, build system, to runtime configuration. The next section, ROS2 Tools, delves into each of the command line tools mentioned throughout the previous articles.

Get new articles and lessons by email

When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.

Unsubscribe any time with the link in each email. See our Privacy policy.

Parameters in ROS2 — configuring nodes without changing code — Atlas Robotics