OpenSMI-ROS2 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opensmi/ros2/__init__.py +89 -0
- opensmi/ros2/__main__.py +11 -0
- opensmi/ros2/cli.py +143 -0
- opensmi/ros2/py.typed +0 -0
- opensmi/ros2/runtime/__init__.py +67 -0
- opensmi/ros2/runtime/_decode.py +101 -0
- opensmi/ros2/runtime/_encode.py +102 -0
- opensmi/ros2/runtime/_parser.py +147 -0
- opensmi/ros2/runtime/errors.py +141 -0
- opensmi/ros2/runtime/goal.py +232 -0
- opensmi/ros2/runtime/graph.py +288 -0
- opensmi/ros2/runtime/guard.py +213 -0
- opensmi/ros2/runtime/interface.py +140 -0
- opensmi/ros2/runtime/messages/__init__.py +503 -0
- opensmi/ros2/runtime/messages/industrial_msgs.py +62 -0
- opensmi/ros2/runtime/messages/industrial_msgs.pyi +159 -0
- opensmi/ros2/runtime/messages/motoros2_interfaces.py +79 -0
- opensmi/ros2/runtime/messages/motoros2_interfaces.pyi +307 -0
- opensmi/ros2/runtime/messages/neo_msgs2.py +55 -0
- opensmi/ros2/runtime/messages/neo_msgs2.pyi +138 -0
- opensmi/ros2/runtime/messages/neo_srvs2.py +72 -0
- opensmi/ros2/runtime/messages/neo_srvs2.pyi +224 -0
- opensmi/ros2/runtime/outcomes.py +103 -0
- opensmi/ros2/runtime/parameters.py +723 -0
- opensmi/ros2/runtime/publisher.py +125 -0
- opensmi/ros2/runtime/qos.py +394 -0
- opensmi/ros2/runtime/runtime.py +1171 -0
- opensmi/ros2/runtime/selection.py +150 -0
- opensmi/ros2/runtime/stamp.py +100 -0
- opensmi/ros2/runtime/subscription.py +354 -0
- opensmi/ros2/runtime/tf.py +714 -0
- opensmi/ros2/server/__init__.py +148 -0
- opensmi/ros2/server/access.py +109 -0
- opensmi/ros2/server/bindings/__init__.py +92 -0
- opensmi/ros2/server/bindings/_base.py +154 -0
- opensmi/ros2/server/bindings/action.py +133 -0
- opensmi/ros2/server/bindings/keyed.py +127 -0
- opensmi/ros2/server/bindings/lifecycle.py +131 -0
- opensmi/ros2/server/bindings/method.py +130 -0
- opensmi/ros2/server/bindings/monitoring.py +109 -0
- opensmi/ros2/server/bindings/parameter.py +133 -0
- opensmi/ros2/server/bindings/publisher.py +192 -0
- opensmi/ros2/server/bindings/registry.py +95 -0
- opensmi/ros2/server/bindings/tf.py +256 -0
- opensmi/ros2/server/browse_names.py +94 -0
- opensmi/ros2/server/component.py +334 -0
- opensmi/ros2/server/config.py +493 -0
- opensmi/ros2/server/containers.py +191 -0
- opensmi/ros2/server/drivers.py +421 -0
- opensmi/ros2/server/errors.py +159 -0
- opensmi/ros2/server/events.py +406 -0
- opensmi/ros2/server/introspection.py +850 -0
- opensmi/ros2/server/keyed.py +749 -0
- opensmi/ros2/server/lifecycle.py +238 -0
- opensmi/ros2/server/limits.py +66 -0
- opensmi/ros2/server/loader.py +977 -0
- opensmi/ros2/server/machine.py +225 -0
- opensmi/ros2/server/method.py +307 -0
- opensmi/ros2/server/mirror.py +582 -0
- opensmi/ros2/server/mutation.py +992 -0
- opensmi/ros2/server/nodesets/Opc.Ua.SmartFactory.ROS2.NodeSet2.xml +85 -0
- opensmi/ros2/server/nodesets/__init__.py +18 -0
- opensmi/ros2/server/nodesets/smart_factory_ros2.py +26 -0
- opensmi/ros2/server/parameters.py +704 -0
- opensmi/ros2/server/projection.py +1176 -0
- opensmi/ros2/server/projections/__init__.py +41 -0
- opensmi/ros2/server/projections/scope.py +222 -0
- opensmi/ros2/server/provenance.py +121 -0
- opensmi/ros2/server/publisher.py +781 -0
- opensmi/ros2/server/recipes.py +514 -0
- opensmi/ros2/server/required.py +633 -0
- opensmi/ros2/server/skill.py +363 -0
- opensmi/ros2/server/spatial.py +283 -0
- opensmi/ros2/server/startup.py +626 -0
- opensmi/ros2/server/status.py +107 -0
- opensmi/ros2/server/structures.py +135 -0
- opensmi/ros2/server/transforms/__init__.py +39 -0
- opensmi/ros2/server/transforms/conversions.py +58 -0
- opensmi/ros2/server/transforms/filing.py +340 -0
- opensmi/ros2/server/transforms/mirror.py +462 -0
- opensmi/ros2/server/transforms/outward.py +180 -0
- opensmi/ros2/server/transforms/records.py +195 -0
- opensmi/ros2/server/transforms/spatial.py +154 -0
- opensmi_ros2-0.1.0.dist-info/METADATA +183 -0
- opensmi_ros2-0.1.0.dist-info/RECORD +89 -0
- opensmi_ros2-0.1.0.dist-info/WHEEL +5 -0
- opensmi_ros2-0.1.0.dist-info/entry_points.txt +2 -0
- opensmi_ros2-0.1.0.dist-info/licenses/LICENSES/MIT.txt +18 -0
- opensmi_ros2-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Technologie-Initiative SmartFactory KL e.V.
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""Message dataclasses: the payloads that cross the seam, and the ROS2 facts they carry.
|
|
6
|
+
|
|
7
|
+
A ROS2 message reaches the rest of this package as an instance of a frozen dataclass built here,
|
|
8
|
+
never as an ``rclpy`` object. There is one dataclass per ROS2 message type, built once and cached.
|
|
9
|
+
It carries its :class:`MessageDescription` on the ``ros2`` class variable, so Projections, field
|
|
10
|
+
selection and the publish-time guard read ROS2 facts from a plain value.
|
|
11
|
+
|
|
12
|
+
The structure is taken by introspection over the imported interface class, not by parsing
|
|
13
|
+
definition text, which gets four things wrong: fixed-size arrays (``double[9]`` is nine values),
|
|
14
|
+
fields named like a constant (``points`` beside ``POINTS``), string bounds (``string<256>``) and
|
|
15
|
+
float widths (``float32`` against ``float64``).
|
|
16
|
+
|
|
17
|
+
Dataclasses built at run time have no static field set, so the types come from stubs beside this
|
|
18
|
+
module. ``<package>.py`` and ``<package>.pyi`` are generated from the definition catalog for
|
|
19
|
+
interface packages without a binary release. ``from opensmi.ros2.runtime.messages.neo_msgs2
|
|
20
|
+
import USBoardV2`` type-checks against the stub and resolves through :func:`message_dataclass`
|
|
21
|
+
at run time, to the dataclass a Binding of that type holds.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import re
|
|
27
|
+
from dataclasses import dataclass, make_dataclass
|
|
28
|
+
from functools import cache
|
|
29
|
+
from typing import TYPE_CHECKING, ClassVar, Final, Literal, TypeAlias
|
|
30
|
+
|
|
31
|
+
from opensmi.ros2.runtime.errors import InvalidMessageTypeNameError, MessageTypeError
|
|
32
|
+
|
|
33
|
+
if TYPE_CHECKING:
|
|
34
|
+
from collections.abc import Callable, Mapping
|
|
35
|
+
|
|
36
|
+
__all__ = [
|
|
37
|
+
"ACTION_MEMBERS",
|
|
38
|
+
"SERVICE_MEMBERS",
|
|
39
|
+
"ActionTypes",
|
|
40
|
+
"ArrayType",
|
|
41
|
+
"ElementType",
|
|
42
|
+
"FieldDescription",
|
|
43
|
+
"FieldType",
|
|
44
|
+
"Message",
|
|
45
|
+
"MessageDescription",
|
|
46
|
+
"NestedType",
|
|
47
|
+
"PrimitiveName",
|
|
48
|
+
"PrimitiveType",
|
|
49
|
+
"SequenceType",
|
|
50
|
+
"ServiceTypes",
|
|
51
|
+
"StringType",
|
|
52
|
+
"action_types",
|
|
53
|
+
"build_message_dataclass",
|
|
54
|
+
"canonical_type_name",
|
|
55
|
+
"is_byte_container",
|
|
56
|
+
"is_single_value",
|
|
57
|
+
"message_dataclass",
|
|
58
|
+
"parse_field_type",
|
|
59
|
+
"repeated_message",
|
|
60
|
+
"service_types",
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
_KINDS = ("msg", "srv", "action")
|
|
64
|
+
_TWO_PART = 2
|
|
65
|
+
_THREE_PART = 3
|
|
66
|
+
|
|
67
|
+
ACTION_MEMBERS: Final = ("Goal", "Feedback", "Result")
|
|
68
|
+
"""The three message types a ROS2 action is made of, as the generated class spells them.
|
|
69
|
+
|
|
70
|
+
They are ordinary message types (``example_interfaces/action/Fibonacci_Goal``).
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
SERVICE_MEMBERS: Final = ("Request", "Response")
|
|
74
|
+
"""The two message types a ROS2 service is made of, as the generated class spells them.
|
|
75
|
+
|
|
76
|
+
A service package exports its members (``std_srvs.srv.SetBool_Request``), while an action's goal
|
|
77
|
+
is only reachable as ``Fibonacci.Goal``. The ``_Event`` member Jazzy adds is not listed.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
PrimitiveName: TypeAlias = Literal[
|
|
81
|
+
"bool",
|
|
82
|
+
"byte",
|
|
83
|
+
"char",
|
|
84
|
+
"wchar",
|
|
85
|
+
"float32",
|
|
86
|
+
"float64",
|
|
87
|
+
"int8",
|
|
88
|
+
"int16",
|
|
89
|
+
"int32",
|
|
90
|
+
"int64",
|
|
91
|
+
"uint8",
|
|
92
|
+
"uint16",
|
|
93
|
+
"uint32",
|
|
94
|
+
"uint64",
|
|
95
|
+
]
|
|
96
|
+
"""The primitive ROS2 field types this Runtime models, spelled as a ``.msg`` file spells them."""
|
|
97
|
+
|
|
98
|
+
# The interface class reports IDL names (`octet`, `boolean`, `float`, `double`, ...), which are
|
|
99
|
+
# translated to the `.msg` vocabulary. IDL `long double` is absent on purpose: mapping it onto
|
|
100
|
+
# `float64` would narrow a value, so it raises `MessageTypeError`.
|
|
101
|
+
_IDL_PRIMITIVES: Final[Mapping[str, PrimitiveName]] = {
|
|
102
|
+
"boolean": "bool",
|
|
103
|
+
"octet": "byte",
|
|
104
|
+
"char": "char",
|
|
105
|
+
"wchar": "wchar",
|
|
106
|
+
"float": "float32",
|
|
107
|
+
"double": "float64",
|
|
108
|
+
"int8": "int8",
|
|
109
|
+
"int16": "int16",
|
|
110
|
+
"int32": "int32",
|
|
111
|
+
"int64": "int64",
|
|
112
|
+
"uint8": "uint8",
|
|
113
|
+
"uint16": "uint16",
|
|
114
|
+
"uint32": "uint32",
|
|
115
|
+
"uint64": "uint64",
|
|
116
|
+
"short": "int16",
|
|
117
|
+
"unsigned short": "uint16",
|
|
118
|
+
"long": "int32",
|
|
119
|
+
"unsigned long": "uint32",
|
|
120
|
+
"long long": "int64",
|
|
121
|
+
"unsigned long long": "uint64",
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_PYTHON_TYPES: Final[Mapping[PrimitiveName, type]] = {
|
|
125
|
+
"bool": bool,
|
|
126
|
+
"byte": int,
|
|
127
|
+
"char": str,
|
|
128
|
+
"wchar": str,
|
|
129
|
+
"float32": float,
|
|
130
|
+
"float64": float,
|
|
131
|
+
"int8": int,
|
|
132
|
+
"int16": int,
|
|
133
|
+
"int32": int,
|
|
134
|
+
"int64": int,
|
|
135
|
+
"uint8": int,
|
|
136
|
+
"uint16": int,
|
|
137
|
+
"uint32": int,
|
|
138
|
+
"uint64": int,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
_BYTE_LIKE: Final[frozenset[PrimitiveName]] = frozenset({"byte", "uint8"})
|
|
142
|
+
|
|
143
|
+
_SEQUENCE = re.compile(r"^sequence<(?P<element>.+?)(?:, (?P<bound>\d+))?>$")
|
|
144
|
+
_ARRAY = re.compile(r"^(?P<element>.+)\[(?P<length>\d+)\]$")
|
|
145
|
+
_STRING = re.compile(r"^(?P<wide>w?)string(?:<(?P<bound>\d+)>)?$")
|
|
146
|
+
_NESTED = re.compile(r"^(?P<package>[A-Za-z_]\w*)/(?P<name>[A-Za-z_]\w*)$")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
150
|
+
class PrimitiveType:
|
|
151
|
+
"""A single number, boolean or character.
|
|
152
|
+
|
|
153
|
+
:ivar name: The ``.msg`` spelling, so that ``float32`` and ``float64`` stay distinguishable
|
|
154
|
+
after both have become Python ``float``.
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
name: PrimitiveName
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
161
|
+
class StringType:
|
|
162
|
+
"""A string field, with the bound the interface declared for it.
|
|
163
|
+
|
|
164
|
+
:ivar wide: Whether this is a ``wstring``.
|
|
165
|
+
:ivar maximum_length: The declared bound, or ``None`` when the string is unbounded.
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
wide: bool = False
|
|
169
|
+
maximum_length: int | None = None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
173
|
+
class NestedType:
|
|
174
|
+
"""A field whose type is another ROS2 message.
|
|
175
|
+
|
|
176
|
+
:ivar type_name: The fully qualified ``package/kind/Name``, resolved from the interface
|
|
177
|
+
class rather than from the truncated ``package/Name`` the type mapping reports.
|
|
178
|
+
"""
|
|
179
|
+
|
|
180
|
+
type_name: str
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
ElementType: TypeAlias = "PrimitiveType | StringType | NestedType"
|
|
184
|
+
"""What may appear inside an array or a sequence, and on its own as a scalar field."""
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
188
|
+
class ArrayType:
|
|
189
|
+
"""A fixed-size array: exactly :attr:`length` elements, always.
|
|
190
|
+
|
|
191
|
+
Distinct from :class:`SequenceType`; collapsing the two would turn ``double[9]`` into a scalar.
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
element: ElementType
|
|
195
|
+
length: int
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
199
|
+
class SequenceType:
|
|
200
|
+
"""A variable-length sequence, bounded or not.
|
|
201
|
+
|
|
202
|
+
:ivar maximum_length: The declared bound, or ``None`` when unbounded.
|
|
203
|
+
"""
|
|
204
|
+
|
|
205
|
+
element: ElementType
|
|
206
|
+
maximum_length: int | None = None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
FieldType: TypeAlias = "ElementType | ArrayType | SequenceType"
|
|
210
|
+
"""Every shape a ROS2 message field can have."""
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
214
|
+
class FieldDescription:
|
|
215
|
+
"""One field of a ROS2 message.
|
|
216
|
+
|
|
217
|
+
:ivar name: The ROS2 field name, which is also the dataclass attribute name.
|
|
218
|
+
:ivar field_type: The modelled shape of the field.
|
|
219
|
+
:ivar ros2_text: The interface class's own type string, kept verbatim.
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
name: str
|
|
223
|
+
field_type: FieldType
|
|
224
|
+
ros2_text: str
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
228
|
+
class MessageDescription:
|
|
229
|
+
"""Everything the rest of the package needs to know about one ROS2 message type.
|
|
230
|
+
|
|
231
|
+
:ivar type_name: The fully qualified ``package/kind/Name``.
|
|
232
|
+
:ivar fields: The fields, in declaration order. Constants are not fields and are not here.
|
|
233
|
+
"""
|
|
234
|
+
|
|
235
|
+
type_name: str
|
|
236
|
+
fields: tuple[FieldDescription, ...]
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class Message:
|
|
240
|
+
"""Base of every message dataclass built by :func:`message_dataclass`.
|
|
241
|
+
|
|
242
|
+
Carries no data. It makes "a payload that crossed the seam" a type that can be annotated and
|
|
243
|
+
checked with ``isinstance`` without touching ``rclpy``.
|
|
244
|
+
|
|
245
|
+
:cvar ros2: The ROS2 facts the subclass was built from.
|
|
246
|
+
:cvar ros2_nested: For each field whose type is another message, the dataclass that message
|
|
247
|
+
was built as, resolved once at build time.
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
__slots__ = ()
|
|
251
|
+
|
|
252
|
+
ros2: ClassVar[MessageDescription]
|
|
253
|
+
ros2_nested: ClassVar[Mapping[str, type[Message]]]
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def parse_field_type(text: str, *, nested_type_name: str | None = None) -> FieldType:
|
|
257
|
+
"""Model one entry of an interface class's field-type mapping.
|
|
258
|
+
|
|
259
|
+
A pure function, so the type model needs no ROS2 installation to test.
|
|
260
|
+
|
|
261
|
+
:param text: The type string, as the interface class reports it (``double``, ``double[9]``,
|
|
262
|
+
``string<256>``, ``sequence<uint8>``, ``sequence<double, 3>``, ``geometry_msgs/Point``).
|
|
263
|
+
:param nested_type_name: The fully qualified name to give a nested type. Without it the
|
|
264
|
+
``package/Name`` form is read as a message type, which is wrong only for the generated
|
|
265
|
+
wrapper types of a service or an action.
|
|
266
|
+
:raises MessageTypeError: When the string is a form this Runtime does not model.
|
|
267
|
+
"""
|
|
268
|
+
sequence = _SEQUENCE.match(text)
|
|
269
|
+
if sequence:
|
|
270
|
+
bound = sequence["bound"]
|
|
271
|
+
return SequenceType(
|
|
272
|
+
element=_parse_element(sequence["element"], nested_type_name),
|
|
273
|
+
maximum_length=int(bound) if bound else None,
|
|
274
|
+
)
|
|
275
|
+
array = _ARRAY.match(text)
|
|
276
|
+
if array:
|
|
277
|
+
return ArrayType(
|
|
278
|
+
element=_parse_element(array["element"], nested_type_name),
|
|
279
|
+
length=int(array["length"]),
|
|
280
|
+
)
|
|
281
|
+
return _parse_element(text, nested_type_name)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _parse_element(text: str, nested_type_name: str | None) -> ElementType:
|
|
285
|
+
"""Model the scalar type inside an array, inside a sequence, or standing on its own."""
|
|
286
|
+
primitive = _IDL_PRIMITIVES.get(text)
|
|
287
|
+
if primitive is not None:
|
|
288
|
+
return PrimitiveType(name=primitive)
|
|
289
|
+
string = _STRING.match(text)
|
|
290
|
+
if string:
|
|
291
|
+
bound = string["bound"]
|
|
292
|
+
return StringType(wide=bool(string["wide"]), maximum_length=int(bound) if bound else None)
|
|
293
|
+
nested = _NESTED.match(text)
|
|
294
|
+
if nested:
|
|
295
|
+
return NestedType(type_name=nested_type_name or f"{nested['package']}/msg/{nested['name']}")
|
|
296
|
+
message = f"{text!r} is not a ROS2 field type this Runtime models"
|
|
297
|
+
raise MessageTypeError(message)
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def canonical_type_name(type_name: str) -> str:
|
|
301
|
+
"""Return ``package/kind/Name``, filling in ``msg`` for the two-part ROS2 shorthand.
|
|
302
|
+
|
|
303
|
+
:raises InvalidMessageTypeNameError: When the name has neither shape, or names a kind that is
|
|
304
|
+
not one of ``msg``, ``srv`` or ``action``.
|
|
305
|
+
"""
|
|
306
|
+
parts = type_name.split("/")
|
|
307
|
+
if len(parts) == _TWO_PART:
|
|
308
|
+
return f"{parts[0]}/msg/{parts[1]}"
|
|
309
|
+
if len(parts) == _THREE_PART and parts[1] in _KINDS:
|
|
310
|
+
return type_name
|
|
311
|
+
message = f"{type_name!r} is not a ROS2 type name; expected 'package/Name' or 'package/kind/Name'"
|
|
312
|
+
raise InvalidMessageTypeNameError(message)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
316
|
+
class ActionTypes:
|
|
317
|
+
"""The three message types one ROS2 action carries, resolved together.
|
|
318
|
+
|
|
319
|
+
A declaration names one action and gets the goal, feedback and result surfaces from it.
|
|
320
|
+
|
|
321
|
+
:ivar type_name: The fully qualified ``package/action/Name``.
|
|
322
|
+
:ivar goal: What a client's Parameters are built from, and what is sent.
|
|
323
|
+
:ivar feedback: What arrives while the goal runs.
|
|
324
|
+
:ivar result: What the server answers with when the goal ends.
|
|
325
|
+
"""
|
|
326
|
+
|
|
327
|
+
type_name: str
|
|
328
|
+
goal: type[Message]
|
|
329
|
+
feedback: type[Message]
|
|
330
|
+
result: type[Message]
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def action_types(type_name: str) -> ActionTypes:
|
|
334
|
+
"""Return the goal, feedback and result message dataclasses of a ROS2 action type.
|
|
335
|
+
|
|
336
|
+
Synchronous and free of the Runtime, like :func:`message_dataclass`.
|
|
337
|
+
|
|
338
|
+
:param type_name: ``package/action/Name``. The ``msg`` shorthand is not accepted.
|
|
339
|
+
:raises InvalidMessageTypeNameError: When the name is not a three-part action name.
|
|
340
|
+
:raises MissingInterfacePackageError: When the interface package is not installed.
|
|
341
|
+
:raises UnusableMessageTypeError: When the package has no such action.
|
|
342
|
+
"""
|
|
343
|
+
canonical = canonical_type_name(type_name)
|
|
344
|
+
if canonical.split("/")[1] != "action":
|
|
345
|
+
message = f"{type_name!r} is not a ROS2 action name; expected 'package/action/Name'"
|
|
346
|
+
raise InvalidMessageTypeNameError(message)
|
|
347
|
+
goal, feedback, result = (message_dataclass(f"{canonical}_{member}") for member in ACTION_MEMBERS)
|
|
348
|
+
return ActionTypes(type_name=canonical, goal=goal, feedback=feedback, result=result)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
352
|
+
class ServiceTypes:
|
|
353
|
+
"""The two message types one ROS2 service carries, resolved together.
|
|
354
|
+
|
|
355
|
+
The counterpart of :class:`ActionTypes`; a service has no feedback. A custom lifecycle Driver
|
|
356
|
+
gets one from :meth:`RuntimeInterface.describe_service` and passes it to
|
|
357
|
+
:meth:`RuntimeInterface.call_service`.
|
|
358
|
+
|
|
359
|
+
:ivar type_name: The fully qualified ``package/srv/Name``.
|
|
360
|
+
:ivar request: What a Method's Parameters are built from, and what is sent.
|
|
361
|
+
:ivar response: What the server answers with.
|
|
362
|
+
"""
|
|
363
|
+
|
|
364
|
+
type_name: str
|
|
365
|
+
request: type[Message]
|
|
366
|
+
response: type[Message]
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def service_types(type_name: str) -> ServiceTypes:
|
|
370
|
+
"""Return the request and response message dataclasses of a ROS2 service type.
|
|
371
|
+
|
|
372
|
+
Synchronous and free of the Runtime, like :func:`action_types`.
|
|
373
|
+
|
|
374
|
+
:param type_name: ``package/srv/Name``. The ``msg`` shorthand is not accepted.
|
|
375
|
+
:raises InvalidMessageTypeNameError: When the name is not a three-part service name.
|
|
376
|
+
:raises MissingInterfacePackageError: When the interface package is not installed.
|
|
377
|
+
:raises UnusableMessageTypeError: When the package has no such service.
|
|
378
|
+
"""
|
|
379
|
+
canonical = canonical_type_name(type_name)
|
|
380
|
+
if canonical.split("/")[1] != "srv":
|
|
381
|
+
message = f"{type_name!r} is not a ROS2 service name; expected 'package/srv/Name'"
|
|
382
|
+
raise InvalidMessageTypeNameError(message)
|
|
383
|
+
request, response = (message_dataclass(f"{canonical}_{member}") for member in SERVICE_MEMBERS)
|
|
384
|
+
return ServiceTypes(type_name=canonical, request=request, response=response)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def message_dataclass(type_name: str) -> type[Message]:
|
|
388
|
+
"""Return the message dataclass for a ROS2 message type, importing the interface package.
|
|
389
|
+
|
|
390
|
+
Synchronous and free of the Runtime: a Binding declares its type while a Component is being
|
|
391
|
+
constructed, before any event loop exists, and a missing interface package must fail there.
|
|
392
|
+
|
|
393
|
+
The result is cached, and two Bindings declaring the same type get the same dataclass, so
|
|
394
|
+
``isinstance`` checks between them hold.
|
|
395
|
+
|
|
396
|
+
:param type_name: ``package/kind/Name``, or ``package/Name`` for a message.
|
|
397
|
+
:raises InvalidMessageTypeNameError: When the name has the wrong shape.
|
|
398
|
+
:raises MissingInterfacePackageError: When the interface package is not installed.
|
|
399
|
+
:raises UnusableMessageTypeError: When the package has no such message.
|
|
400
|
+
:raises MessageTypeError: When a field has a type this Runtime does not model.
|
|
401
|
+
"""
|
|
402
|
+
# Canonicalised before the cache so `std_msgs/String` and `std_msgs/msg/String` share one dataclass.
|
|
403
|
+
return _build_from_interface(canonical_type_name(type_name))
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
@cache
|
|
407
|
+
def _build_from_interface(canonical: str) -> type[Message]:
|
|
408
|
+
"""Build and memoise the dataclass for one canonical ROS2 type name."""
|
|
409
|
+
# Imported here so this module stays importable without ROS2.
|
|
410
|
+
from opensmi.ros2.runtime import _parser
|
|
411
|
+
|
|
412
|
+
return build_message_dataclass(_parser.describe(canonical))
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def build_message_dataclass(
|
|
416
|
+
description: MessageDescription,
|
|
417
|
+
*,
|
|
418
|
+
resolve: Callable[[str], type[Message]] | None = None,
|
|
419
|
+
) -> type[Message]:
|
|
420
|
+
"""Build the frozen dataclass a :class:`MessageDescription` describes.
|
|
421
|
+
|
|
422
|
+
A pure function of a value, so a test can hand it a description of a type nobody has installed.
|
|
423
|
+
|
|
424
|
+
:param resolve: How to turn a nested field's type name into its dataclass. Defaults to
|
|
425
|
+
:func:`message_dataclass`.
|
|
426
|
+
"""
|
|
427
|
+
resolver = resolve or message_dataclass
|
|
428
|
+
nested = {
|
|
429
|
+
field.name: resolver(element.type_name)
|
|
430
|
+
for field in description.fields
|
|
431
|
+
if isinstance(element := element_of(field.field_type), NestedType)
|
|
432
|
+
}
|
|
433
|
+
fields = [(field.name, _annotation(field.field_type, nested.get(field.name))) for field in description.fields]
|
|
434
|
+
name = description.type_name.rpartition("/")[2]
|
|
435
|
+
built = make_dataclass(
|
|
436
|
+
name,
|
|
437
|
+
fields,
|
|
438
|
+
bases=(Message,),
|
|
439
|
+
frozen=True,
|
|
440
|
+
slots=True,
|
|
441
|
+
# Keyword-only: a definition that gains a field would silently reorder positional payloads.
|
|
442
|
+
kw_only=True,
|
|
443
|
+
namespace={
|
|
444
|
+
"ros2": description,
|
|
445
|
+
"ros2_nested": nested,
|
|
446
|
+
"__doc__": f"The ROS2 message {description.type_name}, as it crosses the seam.",
|
|
447
|
+
},
|
|
448
|
+
)
|
|
449
|
+
built.__module__ = __name__
|
|
450
|
+
built.__qualname__ = name
|
|
451
|
+
return built
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def element_of(field_type: FieldType) -> ElementType:
|
|
455
|
+
"""Return the scalar type a field holds, unwrapping an array or a sequence."""
|
|
456
|
+
if isinstance(field_type, (ArrayType, SequenceType)):
|
|
457
|
+
return field_type.element
|
|
458
|
+
return field_type
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def repeated_message(message_type: type[Message], field_name: str) -> type[Message] | None:
|
|
462
|
+
"""Return the message type one field repeats, or ``None`` when it repeats no message.
|
|
463
|
+
|
|
464
|
+
The field must exist, be an array or a sequence, and hold a message. The Keyed Binding and
|
|
465
|
+
the recipe that turns such a field into a standard value both ask this.
|
|
466
|
+
"""
|
|
467
|
+
described = next((each for each in message_type.ros2.fields if each.name == field_name), None)
|
|
468
|
+
if described is None or not isinstance(described.field_type, (ArrayType, SequenceType)):
|
|
469
|
+
return None
|
|
470
|
+
return message_type.ros2_nested.get(field_name)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
def is_single_value(field_type: FieldType) -> bool:
|
|
474
|
+
"""Return whether a field is one value: a primitive or a string, neither nested nor repeated.
|
|
475
|
+
|
|
476
|
+
What a key may be read from, and what a pair's two halves have to be.
|
|
477
|
+
"""
|
|
478
|
+
return isinstance(field_type, (PrimitiveType, StringType))
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
def is_byte_container(field_type: FieldType) -> bool:
|
|
482
|
+
"""Return whether a field is an array or sequence of bytes, and so renders as ``bytes``.
|
|
483
|
+
|
|
484
|
+
Shared by the annotation and the Decoder, which must agree on which fields these are.
|
|
485
|
+
"""
|
|
486
|
+
return isinstance(field_type, (ArrayType, SequenceType)) and (
|
|
487
|
+
isinstance(field_type.element, PrimitiveType) and field_type.element.name in _BYTE_LIKE
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
def _annotation(field_type: FieldType, nested: type[Message] | None) -> object:
|
|
492
|
+
"""Return the Python type a modelled field is annotated and decoded as."""
|
|
493
|
+
if is_byte_container(field_type):
|
|
494
|
+
return bytes
|
|
495
|
+
match field_type:
|
|
496
|
+
case PrimitiveType(name=name):
|
|
497
|
+
return _PYTHON_TYPES[name]
|
|
498
|
+
case StringType():
|
|
499
|
+
return str
|
|
500
|
+
case NestedType():
|
|
501
|
+
return nested
|
|
502
|
+
case ArrayType(element=element) | SequenceType(element=element):
|
|
503
|
+
return tuple[_annotation(element, nested), ...]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Technologie-Initiative SmartFactory KL e.V.
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""The ROS2 interface package ``industrial_msgs``, as message dataclasses.
|
|
6
|
+
|
|
7
|
+
Generated by `python -m tools.catalog_sync --stubs` from the definition catalog.
|
|
8
|
+
Edit the catalog and regenerate; do not edit this file.
|
|
9
|
+
|
|
10
|
+
``from opensmi.ros2.runtime.messages.industrial_msgs import CmdJointTrajectory_Request`` resolves through
|
|
11
|
+
:func:`~opensmi.ros2.runtime.messages.message_dataclass` at run time and type-checks against the
|
|
12
|
+
generated ``industrial_msgs.pyi`` beside this file. The two halves answer different questions: the stub
|
|
13
|
+
says what the fields *are*, and this module makes sure what you get is the very dataclass every
|
|
14
|
+
Binding carrying that type already holds.
|
|
15
|
+
|
|
16
|
+
Importing a name from here needs the interface package installed, exactly as a Binding on it does
|
|
17
|
+
-- the catalog makes a type describable, never receivable.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from typing import TYPE_CHECKING
|
|
23
|
+
|
|
24
|
+
from opensmi.ros2.runtime.messages import message_dataclass
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from opensmi.ros2.runtime.messages import Message
|
|
28
|
+
|
|
29
|
+
_TYPES = {
|
|
30
|
+
"CmdJointTrajectory_Request": "industrial_msgs/srv/CmdJointTrajectory_Request",
|
|
31
|
+
"CmdJointTrajectory_Response": "industrial_msgs/srv/CmdJointTrajectory_Response",
|
|
32
|
+
"DebugLevel": "industrial_msgs/msg/DebugLevel",
|
|
33
|
+
"DeviceInfo": "industrial_msgs/msg/DeviceInfo",
|
|
34
|
+
"GetRobotInfo_Request": "industrial_msgs/srv/GetRobotInfo_Request",
|
|
35
|
+
"GetRobotInfo_Response": "industrial_msgs/srv/GetRobotInfo_Response",
|
|
36
|
+
"RobotMode": "industrial_msgs/msg/RobotMode",
|
|
37
|
+
"RobotStatus": "industrial_msgs/msg/RobotStatus",
|
|
38
|
+
"ServiceReturnCode": "industrial_msgs/msg/ServiceReturnCode",
|
|
39
|
+
"SetDrivePower_Request": "industrial_msgs/srv/SetDrivePower_Request",
|
|
40
|
+
"SetDrivePower_Response": "industrial_msgs/srv/SetDrivePower_Response",
|
|
41
|
+
"SetRemoteLoggerLevel_Request": "industrial_msgs/srv/SetRemoteLoggerLevel_Request",
|
|
42
|
+
"SetRemoteLoggerLevel_Response": "industrial_msgs/srv/SetRemoteLoggerLevel_Response",
|
|
43
|
+
"StartMotion_Request": "industrial_msgs/srv/StartMotion_Request",
|
|
44
|
+
"StartMotion_Response": "industrial_msgs/srv/StartMotion_Response",
|
|
45
|
+
"StopMotion_Request": "industrial_msgs/srv/StopMotion_Request",
|
|
46
|
+
"StopMotion_Response": "industrial_msgs/srv/StopMotion_Response",
|
|
47
|
+
"TriState": "industrial_msgs/msg/TriState",
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def __getattr__(name: str) -> type[Message]:
|
|
52
|
+
"""Return the message dataclass one of this package's types is built as."""
|
|
53
|
+
type_name = _TYPES.get(name)
|
|
54
|
+
if type_name is None:
|
|
55
|
+
message = f"module {__name__!r} has no attribute {name!r}"
|
|
56
|
+
raise AttributeError(message)
|
|
57
|
+
return message_dataclass(type_name)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def __dir__() -> list[str]:
|
|
61
|
+
"""List what this module answers for, since it has no module-level names of its own."""
|
|
62
|
+
return sorted(_TYPES)
|