Skip to content

Change it live

Goal: turn the BONK distance into a parameter you can change while the node is running.

The idea

In Challenge 3 the stopping distance is a constant in the code, BONK_THRESHOLD = 0.25. To try another distance you edit the file, stop the node and start it again. That is slow.

A parameter is a named setting that belongs to a node. You can read it and change it from the terminal while the node runs, with no restart.

A node declares a parameter. The terminal changes it, and the node uses the new value at once.

Look at it live

  • Run ros2 param list. It shows each running node, followed by its parameters.
  • Read one: ros2 param get /robot_state_publisher use_sim_time. It prints Boolean value is: True. The node follows the simulator's clock.

Read the code

A node must declare a parameter before anyone can read or change it. Here is a parameter called speed with the default value 0.15.

# in __init__: declare it, with a name and a default value
self.declare_parameter('speed', 0.15)

# in a callback: read the current value
speed = self.get_parameter('speed').value

While the node runs, you change it from a second terminal:

ros2 param set /my_node speed 0.3
ros2 param get /my_node speed

The important point: read the parameter inside the callback, every time. If you read it once in __init__, your node never sees the new value.

Your mission

Open starter_kit/change_it_live.py. It is your Challenge 3 node, with the threshold as a parameter.

One driver at a time

This node drives the robot itself, so stop your Challenge 3 node first with Ctrl+C.

  • TODO 1: declare bonk_threshold as a parameter with a default value.
  • TODO 2: read the current value inside scan_callback, not just once at startup.
  • TODO 3: reuse your Challenge 3 function front_min_distance.

Run your node with ros2 run starter_kit change_it_live. Then, in a second terminal while the robot is driving:

ros2 param set /change_it_live bonk_threshold 0.5
ros2 param get /change_it_live bonk_threshold

What you should see

Running ros2 param set while the node is live changes the stopping distance immediately, with no restart. Set the threshold higher, for example 0.5, and the robot stops farther from obstacles.

Stuck?

Hint 1

A parameter must be declared before it can be read or set from the terminal.

Hint 2

ros2 node list shows the exact node name to use in ros2 param set.

Hint 3

If changing the value has no effect, check that you read it inside the callback every time.

Submit

When it works, open the Submit tab, choose 4 · Change It Live, and submit.