Skip to content

Commit b646cc7

Browse files
Add ParameterDescriptor to 'Using parameters in a class (Python)' (ros2#772)
* Add ParameterDescriptor to example Signed-off-by: Chris Lalancette <[email protected]> Co-authored-by: Marya Belanger <[email protected]>
1 parent ee469bd commit b646cc7

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

source/Tutorials/Using-Parameters-In-A-Class-Python.rst

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ Inside the ``dev_ws/src/python_parameters/python_parameters`` directory, create
6767
.. code-block:: Python
6868
6969
import rclpy
70-
from rclpy.node import Node
70+
import rclpy.node
7171
from rclpy.exceptions import ParameterNotDeclaredException
72+
from rcl_interfaces.msg import ParameterType
7273
73-
class MinimalParam(Node):
74+
class MinimalParam(rclpy.node.Node):
7475
def __init__(self):
7576
super().__init__('minimal_param_node')
7677
timer_period = 2 # seconds
7778
self.timer = self.create_timer(timer_period, self.timer_callback)
79+
7880
self.declare_parameter("my_parameter")
7981
8082
def timer_callback(self):
@@ -107,6 +109,30 @@ Inside the ``dev_ws/src/python_parameters/python_parameters`` directory, create
107109
~~~~~~~~~~~~~~~~~~~~
108110
Declaring a parameter before getting or setting it is compulsory, or you will raise a ``ParameterNotDeclaredException`` exception.
109111

112+
2.1.1 (Optional) Add ParameterDescriptor
113+
""""""""""""""""""""""""""""""""""""""""
114+
Optionally, you can set a descriptor for the parameter.
115+
Descriptors allow you to specify the type of the parameter and some description text.
116+
For that to work, the ``__init__`` code has to be changed to:
117+
118+
.. code-block:: Python
119+
120+
# ...
121+
122+
class MinimalParam(rclpy.node.Node):
123+
def __init__(self):
124+
super().__init__('minimal_param_node')
125+
timer_period = 2 # seconds
126+
self.timer = self.create_timer(timer_period, self.timer_callback)
127+
128+
my_parameter_descriptor = rclpy.node.Node.ParameterDescriptor(type=ParameterType.PARAMETER_STRING,
129+
description='This parameter is mine!')
130+
self.declare_parameter("my_parameter",
131+
"default value for my_parameter",
132+
my_parameter_descriptor)
133+
134+
The rest of the code remains the same.
135+
Once you run the node, you can then run ``ros2 param describe /minimal_param_node my_parameter`` to see the type and description.
110136

111137
2.2 Add an entry point
112138
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)