Handle ROS parameters
Node piece of programme accepts argument to be parrametrized, speed, name of the topic, frequencies or what ever.
This tutorial visits how ROS2 handle parameters...
But first let see how to read Python3
script argements.
Python3 arguments
Its is very commun add arguments in the command line while starting a programme or a script. Most of the linux commands accepts 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 managment on top of command arguments.
Typically, it is possiple 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 remaping) ros2 run
command needs --ros-args
attributs,
then the syntaxe is -p parameter:=value
.
For instance to set a beautifull orange at 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 }
Simple Python3 Integration
Thes ROS2 client API (Python3 or C++) provides tools to déclare and read ROS2 parameters value.
However, one of the simple way to use parameters
is to allow the ROS node to administrate them automatically.
It requires to set the node attributs allow_undeclared_parameters
and automatically_declare_parameters_from_overrides
at True.
myNode= Node(
'my_node_name',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True
)
This way, the node will create (if necessary) and handle ROS parameters each time developers try to get value from them.
The methode Node::get_parameter_or
permits to read a parameters of a given type or return a default value if the parameter is not provided.
get_parameter_or
takes \(2\) attributes: parameter_name
used in the ros2 run
command or in the launch files, defaultParameter
a Parameter instance defined with a name
, a type
and a default value
.
To notice tha the parameter_name
and the Parameter instance.name
can be the same or diferent.
The Parameter type can be : NOT_SET, BOOL, INTEGER, DOUBLE, STRING, BYTE_ARRAY, BOOL_ARRAY, INTEGER_ARRAY, DOUBLE_ARRAY or STRING_ARRAY
(cf. Parameter Class)
for instance:
def initializeAttributsFromROSParameters( self, aROSNode ):
self._aTopicName = aROSNode.get_parameter_or( 'topic_to_use',
rclpy.Parameter('aTopic', rclpy.Parameter.Type.STRING, 'topic')
)
self._aId = self.get_parameter_or( 'id',
Parameter('id', Parameter.Type.INTEGER, 8)
)
self._aVector = self.get_parameter_or( 'vector',
Parameter('floatList', Parameter.Type.DOUBLE_ARRAY, [1.1, 2.2])
)
Implements ROS parameters to set-up different messages to your talker node (cf. Node and Topic tutorial).