ROS2 (Robot Operating System 2) forms a crucial foundation for myriad robotic applications, with parameters being pivotal in configuring nodes at runtime. This tutorial elucidates the process of managing ROS2 parameters using rclpy in a Python node, ensuring your node is aptly configured for its environment.
Declaring ROS2 Parameters with rclpy
The initial step is to declare the parameters your node will utilize. This declaration, typically performed in the node’s constructor, acts as a placeholder for the values these parameters will hold.
import rclpy
from rclpy.node import Node
class TestParams(Node):
def __init__(self):
super().__init__('test_params_rclpy')
self.declare_parameter('my_char_array', rclpy.Parameter.Type.BYTE_ARRAY)
self.declare_parameter('my_int_array', rclpy.Parameter.Type.INTEGER_ARRAY)
self.declare_parameter('my_uint_array', rclpy.Parameter.Type.INTEGER_ARRAY)
def main(args=None):
rclpy.init(args=args)
node = TestParams()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == "__main__":
main()
In this code snippet:
- Node Initialization: A class
TestParams
is defined, inheriting fromrclpy.node.Node
. - Parameter Declaration: Three parameters are declared using the
declare_parameter
method:my_char_array
,my_int_array
, andmy_uint_array
.
Running Your Node with and without Params
Now, observe the behavior when your node is run without setting any parameter values.
$ ros2 run ros2_tutorials test_params_rclpy
You can list the parameters of your node using another terminal:
$ ros2 param list
The parameters are just declared, not yet set. To set parameters, execute the command:
bashCopy code$ ros2 run ros2_tutorials test_params_rclpy --ros-args -p my_char_array:="[a, b, c]" -p my_int_array:="[1, 2, 3]" -p my_uint_array:="[4, 5, 6]"
The values for the parameters are now set as per your command.
Fetching Params with rclpy
Once your parameters are declared and (optionally) set, you can access them within your node.
...
def __init__(self):
...
param_char_array = self.get_parameter('my_char_array')
param_int_array = self.get_parameter('my_int_array')
param_uint_array = self.get_parameter('my_uint_array')
...
Here, the get_parameter
method fetches the parameter values, which can then be used in your application.
Setting Default Values
You can provide default values for your parameters during declaration.
pythonCopy code ...
def __init__(self):
...
self.declare_parameter('my_char_array', ['x', 'y', 'z'])
self.declare_parameter('my_int_array', [7, 8, 9])
self.declare_parameter('my_uint_array', [10, 11, 12])
...
This way, your parameters have a value even if none are provided during node launch.
Setting Parameters within Your Node
Sometimes, you might want to override parameter values from within your code.
from rclpy.parameter import Parameter
...
def __init__(self):
...
param_char_array = Parameter('my_char_array', Parameter.Type.BYTE_ARRAY, ['g', 'h', 'i'])
...
self.set_parameters([param_char_array, param_int_array, param_uint_array])
...
The set_parameters
method allows you to modify parameter values.
Allowing Undeclared Parameters
Although declaring parameters is the norm, ROS2 allows for undeclared parameters under certain conditions.
...
def __init__(self):
super().__init__('test_params_rclpy',
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True)
...
By setting allow_undeclared_parameters
and automatically_declare_parameters_from_overrides
to True, you can work with undeclared parameters.
Parameter Callbacks
ROS2 has a practical feature to notify your node when a parameter value changes.
...
def parameter_callback(self, params):
for param in params:
if param.name == 'my_char_array' and param.type_ == Parameter.Type.BYTE_ARRAY:
self.my_char_array = param.value
return SetParametersResult(successful=True)
def __init__(self):
...
self.add_on_set_parameters_callback(self.parameter_callback)
...
The add_on_set_parameters_callback
method specifies a callback to be invoked whenever a parameter is modified.
Conclusion
This tutorial has led you through the essentials of managing ROS2 parameters in a Python node. The capabilities of rclpy
provide a robust way to handle node configurations, paving the way for more intricate, dynamic, and interactive robotic systems.
For a deeper exploration into ROS2 parameters, especially with C++, refer to the complementary tutorial on rclcpp params. Additionally, consider delving into utilizing YAML config files for managing your parameters, making your ROS2 applications more scalable and well-organized.