Handle ROS parameters

Node piece of programs accepts the argument to be parametrized, speed, name of the topic, frequencies or whatever. This tutorial visits how ROS2 handle parameters... But first, let see how to read Python3 script arguments.

Python3 arguments

It is very common to add arguments in the command line while starting a program or a script. Most of the Linux commands accept arguments.

In python, the arguments can be read with the sys python package, on a argv list (argument values).

lets initialize a test-script.py

import sys

print( sys.argv )

and try:

python3 test-script.py -r salut nounou 42

You notice that the executed script itself is the first argument (at indice \(0\)).

Use ROS2 Parameters

ROS2 provides parameter management on top of command arguments. Typically, it is possible to list the parameters with ros2 param list command. Start a turtlesim in a terminal an search for trutlesim parameters in another.

To activate parameters (and topic remapping) ros2 run command needs --ros-args attributes,
then the syntax is -p parameter_name:=value.

For instance, to set a beautiful orange on the turtlesim background:

ros2 run turtlesim turtlesim_node --ros-args \
    -p background_r:=100 \
    -p background_g:=60 \
    -p background_b:=0

In YAML launch file parameters should be defined on a param list.

launch:
- node: 
    pkg: turtlesim
    exec: turtlesim_node
    name: orange_simu
    param:
    - { name: "background_r", value: 100 }
    - { name: "background_g", value: 60 }
    - { name: "background_b", value: 0 }

Python3 Integration

The ROS2 client API (Python3 as C++) provides tools to declare and read ROS2 parameters value.

  • declare_parameter method attached to a Node instance, declare a ROS variable. On a simple usage, it takes 2 parameters:
    • The variable name to use.
    • A default value (ATTENTION, ROS parameter has type, so \(10\) is different of \(10.0\))
  • get_parameter( 'variable_name' ) returning a parameter instance, with aParameter.value the value.

(To notice that a aParameter.get_parameter_value() exists and returns an object containing the parameter value on diferent possible types: [bool_value, integer_value, double_value, string_value, byte_array_value, bool_array_value, integer_array_value, double_array_value, string_array_value])

For instance, a node initialization with an integer parameters lookalike:

# Initialize ROS node with ROS client
rclpy.init()
rosNode= Node( "myOneParameterNode" )

# Node Parameters : 
rosNode.declare_parameter( 'my_parameter', 10 )
aProgramVariable= rosNode.get_parameter( 'my_parameter' ).value

print( f"> Parameter value: {aProgramVariable}" )

# Start infinite loop
rclpy.spin(rosNode)

# Clean everything and switch the light off
rosNode.destroy_node()
rclpy.shutdown()

Add this node to your tutorial_pkg and test it.

# Shell-1
ros2 run tutorial_pkg oneParamNode

# Shell-2
ros2 param list

# Shell-1
ros2 run tutorial_pkg oneParamNode --ros-args -p my_parameter:=42

# Shell-1
ros2 run tutorial_pkg oneParamNode --ros-args -p my_parameter:="42"

# Shell-1
ros2 run tutorial_pkg oneParamNode --ros-args -p my_parameter:="quarante-deux"

Implements ROS parameters to set-up the message to be sent by your talker node (cf. Node and Topic tutorial). Ideally, the node parameter is read each times, before to publish the message to guaranty to sent the last message in case of parameter modification.

# With Node: 'myTalker' and parameter 'message'
ros2 param set myTalker message "New important information to broadcast..."