title | author | date |
---|---|---|
ROS2 Parameter Collections Package |
a.whit ([email](mailto:[email protected])) |
July 2022 |
A ROS2 package that provides an interface for treating ROS2 parameters
(and parameter groups) like standard Python container data types (e.g., dict
,
list
, set
, and tuple
).
For more information about Python containers, see the documentation for
emulating container types, as well as the documentation for the
collections package. The table of collection types in the collections.abc
documentation can be especially informative.
For more information about working with ROS2 parameters, see the Understanding parameters tutorial.
At present, only the following classes from collections.abc are implemented:
Container
Iterable
Sized
Collection
Mapping
MutableMapping
In addition, the Dict
class is defined as a child of MutableMapping
that implements a subset of [dict] functionality. In particular, the
setdefault method is implemented.
This package can be added to any ROS2 workspace. ROS2 workspaces are built using [colcon]. See the installation documentation for further information.
See the testing documentation for further information.
Perhaps the best way to get started is via a simple example.
From within a configured ROS2 environment, initialize a ROS2 interface.
>>> import rclpy
>>> rclpy.init()
Use the ROS2 interface to create a ROS2 node.
>>> import rclpy.node
>>> node = rclpy.node.Node('test')
Add a parameter to the node.
>>> parameter = node.declare_parameter('parameter_a', 1.0)
>>> node.get_parameter('parameter_a').value
1.0
Create a mapping interface to the node parameters.
>>> from ros_parameter_collections import MutableMapping
>>> mapping = MutableMapping()
>>> mapping.node = node
This mapping can now be treated as a dict-like data structure, in order to access the parameters of the node.1
>>> mapping['parameter_b'] = 2
>>> mapping['parameter_a'] = 'one'
>>> list(mapping)
['parameter_b', 'parameter_a']
>>> {k: v for (k, v) in mapping.items()}
{'parameter_b': 2, 'parameter_a': 'one'}
Any modifications to the mapping data structure are reflected in the node parameters.
>>> node.get_parameter('parameter_a').value
'one'
>>> node.get_parameter('parameter_b').value
2
Clean up by destroying the node and shutting down the ROS2 interface.
>>> del mapping
>>> node.destroy_node()
>>> rclpy.shutdown()
Further examples are available in the docstrings of each Python module. Additional example documentation is forthcoming.
Copyright 2022 Neuromechatronics Lab, Carnegie Mellon University
Contributors:
- a. whit. ([email protected])
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
Footnotes
-
Note that the order of mapping keys is not guaranteed, which is consistent with legacy
dict
functionality, but not currentdict
functionality. In this example, the type of theparameter_a
is changed, causing the keys to re-order (when the parameter is undeclared). โฉ