rosros 0.2.5
Simple unified interface to ROS1 / ROS2 Python API
All Classes Namespaces Files Functions Variables Properties Pages
parameter.py
Go to the documentation of this file.
1"""
2Partial stand-in of ROS2 `rclpy.parameter`.
3
4And some parameter-related classes from `rcl_interfaces.msg` for ROS1.
5
6Heavily modified copy from ROS2 `rclpy.parameter`,
7at https://github.com/ros2/rclpy (`rclpy/rclpy/parameter.py`),
8released under the Apache 2.0 License.
9
10------------------------------------------------------------------------------
11This file is part of rosros - simple unified interface to ROS1 / ROS2.
12Released under the BSD License.
13
14@author Erki Suurjaak
15@created 17.02.2022
16@modified 24.06.2022
17------------------------------------------------------------------------------
18"""
19## @namespace rosros.rclify.parameter
20
21# Original file copyright notice:
22#
23# Copyright 2016 Open Source Robotics Foundation, Inc.
24#
25# Licensed under the Apache License, Version 2.0 (the "License");
26# you may not use this file except in compliance with the License.
27# You may obtain a copy of the License at
28#
29# http://www.apache.org/licenses/LICENSE-2.0
30#
31# Unless required by applicable law or agreed to in writing, software
32# distributed under the License is distributed on an "AS IS" BASIS,
33# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34# See the License for the specific language governing permissions and
35# limitations under the License.
36import array
37from enum import Enum
38
39PARAMETER_SEPARATOR_STRING = "."
40
41
43 """ROS node parameter, a stand-in for ROS2 `rclpy.parameter.Parameter` in ROS1."""
44
45 class Type(Enum):
46 """ROS parameter type, a stand-in for ROS2 `rclpy.parameter.Parameter.Type` in ROS1."""
47
48 NOT_SET = 0
49 BOOL = 1
50 INTEGER = 2
51 DOUBLE = 3
52 STRING = 4
53 BYTE_ARRAY = 5
54 BOOL_ARRAY = 6
55 INTEGER_ARRAY = 7
56 DOUBLE_ARRAY = 8
57 STRING_ARRAY = 9
58
59 @classmethod
60 def from_parameter_value(cls, parameter_value):
61 """
62 Returns `Parameter.Type` for given value.
63
64 @return a `Parameter.Type` corresponding to the instance type of the given value
65 @throws `TypeError` if the conversion to a type was not possible
66 """
67 if isinstance(parameter_value, (list, tuple, array.array)):
68 for enumtype, valtype in Parameter.ARRAYS.items():
69 if all(isinstance(v, valtype) for v in parameter_value):
70 return enumtype
71 raise TypeError("The given value is not a list of one of the allowed types.")
72 for enumtype, valtype in Parameter.SCALARS.items():
73 if isinstance(parameter_value, valtype):
74 return enumtype
75 raise TypeError("The given value is not one of the allowed types.")
76
77 def check(self, parameter_value):
78 """Returns whether given value matches this type."""
79 if self in Parameter.SCALARS:
80 return isinstance(parameter_value, Parameter.SCALARS[self])
81 if self not in Parameter.ARRAYS \
82 or not isinstance(parameter_value, (list, tuple, array.array)):
83 return False
84 if self is self.BYTE_ARRAY:
85 return all(isinstance(v, bytes) and len(v) == 1 for v in parameter_value)
86 return all(isinstance(v, Parameter.ARRAYS[self]) for v in parameter_value)
87
88
89
90 SCALARS = {Type.NOT_SET: type(None), Type.BOOL: bool, Type.INTEGER: int,
91 Type.DOUBLE: float, Type.STRING: str}
92
93
94 ARRAYS = {Type.BYTE_ARRAY: bytes, Type.BOOL_ARRAY: bool, Type.INTEGER_ARRAY: int,
95 Type.DOUBLE_ARRAY: float, Type.STRING_ARRAY: str}
96
97
98 def __init__(self, name, type_=None, value=None):
99 """Raises error if unknown type or given value not of given type."""
100 if type_ is None:
101 type_ = Parameter.Type.from_parameter_value(value) # Raises if unknown type
102 if not isinstance(type_, Parameter.Type):
103 raise TypeError("Type must be an instance of %r" % Parameter.Type)
104 if not type_.check(value):
105 raise ValueError("Type '%s' and value '%s' do not agree" % (type_, value))
106
107 self._type_ = type_
108 self._name = name
109 self._value = value
110
111 @property
112 def name(self):
113 return self._name
114
115 @property
116 def type_(self):
117 return self._type_
119 @property
120 def value(self):
121 return self._value
122
123 def get_parameter_value(self):
124 """Returns `ParameterValue`."""
125 parameter_value = ParameterValue(type=self._type_.value)
126 if Parameter.Type.BOOL == self._type_:
127 parameter_value.bool_value = self.valuevalue
128 elif Parameter.Type.INTEGER == self._type_:
129 parameter_value.integer_value = self.valuevalue
130 elif Parameter.Type.DOUBLE == self._type_:
131 parameter_value.double_value = self.valuevalue
132 elif Parameter.Type.STRING == self._type_:
133 parameter_value.string_value = self.valuevalue
134 elif Parameter.Type.BYTE_ARRAY == self._type_:
135 parameter_value.byte_array_value = self.valuevalue
136 elif Parameter.Type.BOOL_ARRAY == self._type_:
137 parameter_value.bool_array_value = self.valuevalue
138 elif Parameter.Type.INTEGER_ARRAY == self._type_:
139 parameter_value.integer_array_value = self.valuevalue
140 elif Parameter.Type.DOUBLE_ARRAY == self._type_:
141 parameter_value.double_array_value = self.valuevalue
142 elif Parameter.Type.STRING_ARRAY == self._type_:
143 parameter_value.string_array_value = self.valuevalue
144 return parameter_value
145
146
147
148
149class ParameterValue:
150 """
151 Used to determine which of the next *_value fields are set.
152 `Parameter.Type.NOT_SET` indicates that the parameter was not set
153 (if gotten) or is uninitialized.
154
155 Stand-in for ROS2 `rcl_interfaces.msg.ParameterValue` in ROS1.
156 """
157 def __init__(self, type=0, bool_value=False, integer_value=0, double_value=0.0, string_value="",
158 byte_array_value=None, bool_array_value=None, integer_array_value=None,
159 double_array_value=None, string_array_value=None):
160
161 self.type = type
163 self.bool_value = bool_value
164
165 self.integer_value = integer_value
166
167 self.double_value = double_value
168
169 self.string_value = string_value
170
171 self.byte_array_value = byte_array_value or []
172
173 self.bool_array_value = bool_array_value or []
174
175 self.integer_array_value = integer_array_value or []
176
177 self.double_array_value = double_array_value or []
178
179 self.string_array_value = string_array_value or []
180
181 def get_value(self):
182 """Returns raw value according to type."""
183 value = None
184 type_ = Parameter.Type(self.type)
185 if Parameter.Type.BOOL == type_:
186 value = self.bool_value
187 elif Parameter.Type.INTEGER == type_:
188 value = self.integer_value
189 elif Parameter.Type.DOUBLE == type_:
190 value = self.double_value
191 elif Parameter.Type.STRING == type_:
192 value = self.string_value
193 elif Parameter.Type.BYTE_ARRAY == type_:
194 value = self.byte_array_value
195 elif Parameter.Type.BOOL_ARRAY == type_:
196 value = self.bool_array_value
197 elif Parameter.Type.INTEGER_ARRAY == type_:
198 value = self.integer_array_value
199 elif Parameter.Type.DOUBLE_ARRAY == type_:
200 value = self.double_array_value
201 elif Parameter.Type.STRING_ARRAY == type_:
202 value = self.string_array_value
203 return value
204
205
207 """
208 This is the message to communicate a parameter's descriptor.
209
210 Stand-in for ROS2 `rcl_interfaces.msg.ParameterDescriptor` in ROS1.
211 """
212
213 def __init__(self, name="", type=0, description="", additional_constraints="", read_only=False,
214 dynamic_typing=False, floating_point_range=None, integer_range=None):
215
216 self.name = name or ""
218 self.type = type or 0
220 self.description = description or ""
221
225 self.additional_constraints = additional_constraints or ""
227 self.read_only = read_only or False
228
229 self.dynamic_typing = dynamic_typing or False
230
232 self.floating_point_range = floating_point_range or []
235 self.integer_range = integer_range or []
236
237
239 """
240 This is the message to communicate the result of setting parameters.
242 Stand-in for ROS2 `rcl_interfaces.msg.SetParametersResult` in ROS1.
243 """
244
245 def __init__(self, successful=False, reason=""):
246
248 self.successful = successful or False
251 self.reason = reason or ""
252
253
255 """
256 Represents bounds and a step value for a floating point typed parameter.
258 Stand-in for ROS2 `rcl_interfaces.msg.FloatingPointRange` in ROS1.
259 """
260 def __init__(self, from_value=0.0, to_value=0.0, step=0.0):
261
262 self.from_value = from_value or 0.0
263
264 self.to_value = to_value or 0.0
283 self.step = step or 0.0
284
285
286class IntegerRange:
287 """
288 Represents bounds and a step value for an integer typed parameter.
289
290 Stand-in for ROS2 `rcl_interfaces.msg.IntegerRange` in ROS1.
291 """
292 def __init__(self, from_value=0, to_value=0, step=0):
293
294 self.from_value = from_value or 0
295
296 self.to_value = to_value or 0
312 self.step = step or 0
313
314
315__all__ = [
316 "PARAMETER_SEPARATOR_STRING", "Parameter", "ParameterValue", "ParameterDescriptor",
317 "SetParametersResult", "FloatingPointRange", "IntegerRange",
318]
Represents bounds and a step value for a floating point typed parameter.
Definition parameter.py:265
__init__(self, from_value=0.0, to_value=0.0, step=0.0)
Ideally, the step would be less than or equal to the distance between the bounds, as well as an even ...
Definition parameter.py:266
to_value
End value for valid values, inclusive.
Definition parameter.py:270
step
Size of valid steps between the from and to bound.
Definition parameter.py:275
from_value
Start value for valid values, inclusive.
Definition parameter.py:268
Represents bounds and a step value for an integer typed parameter.
Definition parameter.py:297
step
Size of valid steps between the from and to bound.
Definition parameter.py:304
ROS parameter type, a stand-in for ROS2 `rclpy.parameter.Parameter.Type` in ROS1.
Definition parameter.py:46
from_parameter_value(cls, parameter_value)
Returns `Parameter.Type` for given value.
Definition parameter.py:66
check(self, parameter_value)
Returns whether given value matches this type.
Definition parameter.py:77
This is the message to communicate a parameter's descriptor.
Definition parameter.py:217
dynamic_typing
If true, the parameter is allowed to change type.
Definition parameter.py:235
read_only
If 'true' then the value cannot change after it has been initialized.
Definition parameter.py:233
description
Description of the parameter, visible from introspection tools.
Definition parameter.py:226
floating_point_range
FloatingPointRange consists of a from_value, a to_value, and a step.
Definition parameter.py:238
__init__(self, name="", type=0, description="", additional_constraints="", read_only=False, dynamic_typing=False, floating_point_range=None, integer_range=None)
Definition parameter.py:220
additional_constraints
Plain English description of additional constraints which cannot be expressed with the available cons...
Definition parameter.py:231
integer_range
IntegerRange consists of a from_value, a to_value, and a step.
Definition parameter.py:241
type
Enum values are defined in `Parameter.Type`.
Definition parameter.py:224
ROS node parameter, a stand-in for ROS2 `rclpy.parameter.Parameter` in ROS1.
Definition parameter.py:42
__init__(self, name, type_=None, value=None)
Raises error if unknown type or given value not of given type.
Definition parameter.py:98
get_parameter_value(self)
Returns `ParameterValue`.
Definition parameter.py:129
string_array_value
An array of string values.
Definition parameter.py:185
bool_value
Boolean value, can be either true or false.
Definition parameter.py:169
string_value
A textual value with no practical length limit.
Definition parameter.py:175
byte_array_value
An array of bytes, used for non-textual information.
Definition parameter.py:177
type
The type of this parameter, which corresponds to the appropriate field below.
Definition parameter.py:167
bool_array_value
An array of boolean values.
Definition parameter.py:179
integer_array_value
An array of 64-bit integer values.
Definition parameter.py:181
__init__(self, type=0, bool_value=False, integer_value=0, double_value=0.0, string_value="", byte_array_value=None, bool_array_value=None, integer_array_value=None, double_array_value=None, string_array_value=None)
Definition parameter.py:165
double_array_value
An array of 64-bit floating point values.
Definition parameter.py:183
get_value(self)
Returns raw value according to type.
Definition parameter.py:187
double_value
A double precision floating point value.
Definition parameter.py:173
This is the message to communicate the result of setting parameters.
Definition parameter.py:249
reason
Reason why the setting was either successful or a failure.
Definition parameter.py:257
__init__(self, successful=False, reason="")
Definition parameter.py:251