Atlas Robotics
Lesson 8/11 · about 8 min

Watch on YouTube ↗Watch on Facebook ↗

Parameters

Parameter is a configuration mechanism associated with each node: a value (number, string, bool, list...) declared as declare_parameter() in the code, has a default name and value, but can be read/written from the outside while the node is running — via CLI (ros2 param), via launch file, or from another node calling service.

Unlike hard-coded constants in code, parameters allow changing node behavior (alarm threshold, publishing frequency, topic name...) without having to rebuild or edit the source code.

Most confusing thing: read it once or read it again?

When ros2 param set changes value, the parameter server updates immediately. But whether the node uses it depends on how the code reads that value.

Two ways to write in code

# Method A: read once at initialization. After that param set has no effect.
self.speed = self.get_parameter('speed').value

# Method B: reread each time you use it. Param set takes effect immediately.
def timer_callback(self):
    speed = self.get_parameter('speed').value
    ...

Method A is fast and simple. Method B keeps up with the change but requires an additional read each time. Choose according to need: use A for configuration that rarely changes; parameters that need to be adjusted midway (alarm threshold, speed), then B.

Try it

Need 2 terminals (already source install/setup.bash, see Module 0):

# Terminal 1 — keep running; do not close; See the base_temperature log output
ros2 launch ros2_basics demo.launch.py
# Terminal 2
ros2 param list /sensor_publisher
ros2 param get /sensor_publisher base_temperature
ros2 param set /sensor_publisher base_temperature 30.0

Run ros2 param set and then look at Terminal 1: log sensor_publisher changes to the new value immediately (reread each time — Method B above).

Parameters · Module 1: ROS 2 Core Concepts — Atlas Robotics