module documentation

The Projection engine: how one ROS2 type is rendered in OPC UA.

A Binding connects one ROS2 path to one OPC UA object. A Projection is about the type of what flows through it, is reused by every Binding whose messages carry that type, and applies wherever the type appears, at the root of a message or nested inside another. It has three pure functions:

  • shape, run once at init, turns a message dataclass into a node plan of PlanObject and PlanVariable. The generic walk emits only variables, which nest as complex Variables; a repeated message becomes one variable holding an array of a structure derived from its type, and a plan that asks for an Object is refused where its nodes are made.
  • decode, run per message, turns a payload into a map from plan name to value.
  • encode, run per write on the publishing side, turns that map back into a payload. An entry that cannot run backwards has no Projection.inverse. That is derived from the recipe and asked as Projection.inverts, of a type, because for some recipes it depends on the message too.

The generic field-walk is an ordinary registry entry that matches last, so turning curation off for one type means naming the generic entry for it. Authors write recipes (opensmi.ros2.server.recipes), not these functions. No step sees an rclpy object.

Class EnumDataType An enumeration DataType to mint into the server, and the values it lists.
Class NodeLevel One inner level of a node path: a structure, or a group, that holds the nodes below it.
Class PlanObject One OPC UA Object to create, with its own entries below it.
Class PlanVariable One OPC UA Variable to create, and the ROS2 field it reads.
Class ProjectDataType A DataType this project's own nodeset defines, named by its generated NodeId definition.
Class Projection How one ROS2 type is rendered, as the steps and the type they answer for.
Class ProjectionContext What a Projection needs besides the message in front of it.
Class ProjectionRegistry The ordered entries a Component renders its types through.
Class StandardDataType A DataType OPC UA itself defines, named by its numeric identifier in namespace 0.
Function browse_name_of Return the browse name of one field of the message the context is on.
Function curated_naming Return the naming of curated levels: the qualifier and the node path, joined with dots.
Function element_structure Return the structure one element of a repeated message renders as.
Function flat_name Return node path parts as the one name a plan entry has in the decode and encode maps.
Function generic_decode Read one payload into the names generic_shape created for its type.
Function generic_encode Build one payload out of the names generic_shape created, and nothing else.
Function generic_shape Render a message type field by field, one flat variable per leaf.
Function inner_levels Return every structure a plan's variables sit in, by node path.
Function join_path Return one field path below another, spelled the way the selection spells it.
Function level_datatypes Return the structure DataType of every level of a plan and of its root, by node path.
Function level_value Return the instance of the structure of one level, from what a message decoded to.
Function message_class Return the structure of a ROS2 message type, named by it, with the fields of its definition.
Function narrowed Return a Component's context narrowed to what one surface's selection kept.
Function require_invertible Refuse a declaration that writes a type into ROS2 through a Projection with no inverse.
Function ros2_value Return a value a client wrote in the shape a payload holds.
Function shaped Return the nodes one message type becomes, through the context's registry.
Function ua_shape Return the value one modelled ROS2 field boots with, and the OPC UA type it carries.
Function ua_value Return a decoded field value in the shape an OPC UA write wants.
Constant ANY_TYPE The type name of an entry that answers for every type; the generic walk carries it.
Type Alias DecodeFunction Run per message: a payload becomes a map from plan name to the value to write.
Type Alias EncodeFunction Run per publish: that same map becomes a payload again, where the recipe has an inverse.
Type Alias NodeLevelNaming How a level whose subtree is curated is named: from its node path and its ROS2 type.
Type Alias NodePath The browse names from the top of a plan down to one node, such as ("Pose", "Position", "X").
Type Alias PlanDataType What a plan may say about a variable's DataType, resolved to a NodeId where the nodes are made.
Type Alias PlanEntry The two verbs, and there is no third.
Type Alias ShapeFunction Run once at init: a message dataclass becomes a node plan.
Function _annotated Return the annotation of a field and the length of its fixed-size array, from its boot value.
Function _below Carry one entry up under the field that produced it, which becomes a level above it.
Function _below_values Return the values belonging to one nested field, with its own name taken off the front.
Function _decoded_elements Return one repeated message field as the array of elements its variable carries.
Function _display_name Return what a client is told the field is called: the new name, else the raw ROS2 name.
Function _element_context Return the context the elements of a repeated message field are rendered in.
Function _element_structure Work out the structure of one element, which element_structure remembers.
Function _element_variant_type Return the OPC UA type of a scalar, or of what a container holds.
Function _encoded_elements Return the ROS2 sequence that the array of elements of one repeated message field describes.
Function _instance Undocumented
Function _leaf_annotation Return the field annotation of a leaf, and the length of its fixed-size array.
Function _narrowed_scalar Return one scalar as exactly the Python type its ROS2 element type is, where it can be.
Function _plan_structures Return the variable a repeated message field becomes: one array of the element's structure.
Function _plan_variable Return the variable one modelled ROS2 field becomes under the generic walk.
Function _refuse_a_partial_payload Refuse to build a message with a hole in it, naming the field and the reason.
Function _repeats Return whether a field holds many of its type rather than one.
Function _require_no_silent_shadowing Refuse a second entry for one type unless it says it is replacing the first.
Function _selected_fields Yield each field the selection kept, with its context and the message type it nests.
Function _siblings Return the browse name of each of these sibling fields, refusing two that share one.
Function _signature Return the fields of the structure of a ROS2 message type, as its definition has them.
Constant _INTEGERS The primitive names whose Python type is int, taken from the guard's table.
Constant _PACKABLE The OPC UA types a field of a structure can carry.
Constant _VARIANT_TYPES Undocumented
Constant _ZEROS Undocumented
def browse_name_of(context: ProjectionContext, field_name: str) -> str:

Return the browse name of one field of the message the context is on.

The key a field has in the decode and encode maps, for a recipe that works one field out itself.

def curated_naming(qualifier: str) -> NodeLevelNaming:

Return the naming of curated levels: the qualifier and the node path, joined with dots.

Platform.Odom names the root and Platform.Odom.Pose the level below it. The qualifier is the Component and the Binding, so a name does not depend on what else is declared.

def element_structure(element_type: type[Message], context: ProjectionContext) -> type:

Return the structure one element of a repeated message renders as.

Public because a recipe that repeats a message of its own needs it too. Its fields are the element's plan, nested like the message. Memoised on the registry (ProjectionRegistry.element_structures).

Raises
UnprojectableFieldErrorWhen the element's Projection emits an Object, or a field of it is of a type that has no place in a structure.
def flat_name(*parts: str) -> str:

Return node path parts as the one name a plan entry has in the decode and encode maps.

Empty parts are skipped: an entry named "" takes the name of whatever contains it, so a message rendered as one variable browses as the field it sits in. The decode and encode maps are keyed the same way, so this is the one place that spells that name. It is a key and not a BrowseName: the nodes nest.

def generic_decode(message: Message, context: ProjectionContext, overrides: Mapping[str, object] | None = None) -> Mapping[str, object]:

Read one payload into the names generic_shape created for its type.

Parameters
message:MessageUndocumented
context:ProjectionContextUndocumented
overrides:Mapping[str, object] | NoneWhat a field's variable reads instead of the field itself, by ROS2 field name. It pairs with the shape override of the same name.
Returns
Mapping[str, object]Undocumented
def generic_encode(message_type: type[Message], values: Mapping[str, object], context: ProjectionContext, overrides: Mapping[str, object] | None = None) -> Message:

Build one payload out of the names generic_shape created, and nothing else.

The inverse of generic_decode. A field the selection removed is not filled from a default: a covariance of zeros means perfectly certain in ROS2, not unknown.

Parameters
message_type:type[Message]Undocumented
values:Mapping[str, object]Undocumented
context:ProjectionContextUndocumented
overrides:Mapping[str, object] | NoneThe ROS2 value of a field the caller has already worked out, by field name.
Returns
MessageUndocumented
Raises
NotInvertibleErrorWhen a field of the message has no value, whether because the selection removed it or because the caller supplied none.
def generic_shape(message_type: type[Message], context: ProjectionContext, overrides: Mapping[str, PlanEntry] | None = None) -> tuple[PlanEntry, ...]:

Render a message type field by field, one flat variable per leaf.

Recursion runs through the registry, so a nested type with a curated Projection is rendered by that entry and its names are carried up under this field's name. A field that repeats a message is one variable holding an array of a structure.

Parameters
message_type:type[Message]Undocumented
context:ProjectionContextUndocumented
overrides:Mapping[str, PlanEntry] | NoneWhat to emit for a field instead of walking it, by ROS2 field name. The blob and enum recipes are overrides and nothing else. An override of a field the selection removed is never reached.
Returns
tuple[PlanEntry, ...]Undocumented
Raises
UnprojectableFieldErrorWhen a repeated message's element cannot render as a structure: its Projection emits an Object, or a field of it has a type no structure field can carry.
def inner_levels(plan: Iterable[PlanEntry]) -> dict[NodePath, NodeLevel]:

Return every structure a plan's variables sit in, by node path.

These are the nodes that become complex Variables.

def join_path(prefix: str, name: str) -> str:

Return one field path below another, spelled the way the selection spells it.

def level_datatypes(plan: Iterable[PlanVariable], root: type[Message], naming: NodeLevelNaming) -> dict[NodePath, type]:

Return the structure DataType of every level of a plan and of its root, by node path.

A level whose rendered subtree is the one its ROS2 type has (no field excluded or renamed, no leaf of another type) gets the class of that type, message_class. Any other gets a class of the rendered shape, named by naming. A level is curated when anything below it is, since its fields differ then. The root is ().

Parameters
plan:Iterable[PlanVariable]The variables of one message, as shaped returns them.
root:type[Message]The ROS2 type of the message.
naming:NodeLevelNamingUndocumented
Returns
dict[NodePath, type]Undocumented
Raises
UnprojectableFieldErrorWhen a leaf has a type no structure field can carry.
def level_value(plan: Iterable[PlanVariable], classes: Mapping[NodePath, type], values: Mapping[str, object], path: NodePath = ()) -> Any:

Return the instance of the structure of one level, from what a message decoded to.

This is the value a level's complex Variable holds. values is what the Projection's decode returns for the message, keyed by flat name.

Parameters
plan:Iterable[PlanVariable]Undocumented
classes:Mapping[NodePath, type]The result of level_datatypes for the plan.
values:Mapping[str, object]Undocumented
path:NodePathThe level to build. The root by default.
Returns
AnyUndocumented
Raises
KeyErrorWhen a leaf below the level has no value.
def message_class(message_type: type[Message]) -> type:

Return the structure of a ROS2 message type, named by it, with the fields of its definition.

It is the DataType of a level whose subtree is not curated. Equal for equal types.

def narrowed(context: ProjectionContext, message_type: type[Message], selection: FieldSelection) -> ProjectionContext:

Return a Component's context narrowed to what one surface's selection kept.

A Binding with several surfaces (an action's goal, feedback and result; a service's request and response) narrows its context once per surface.

A type with no fields at all (a bare trigger goal, a ping request) skips the selector, since nothing was removed. A pattern written against such a type still reaches the selector and is refused by name.

Raises
FieldSelectionErrorWhen a pattern matches nothing, or the two leave no field.
def require_invertible(message_type: type[Message], context: ProjectionContext):

Refuse a declaration that writes a type into ROS2 through a Projection with no inverse.

A Binding that publishes is declarable only if every Projection reachable in its message type inverts. The error names the ROS2 type, the field path and the entry. An action's goal type is checked like a published topic's. Reachable means through the selection, so an excluded field is never asked.

Raises
NotInvertibleErrorWhen any reachable entry cannot run backwards.
def ros2_value(value: object, field_type: FieldType) -> object:

Return a value a client wrote in the shape a payload holds.

The mirror image of ua_value. It reads the modelled field, since [] alone does not say whether a tuple or a list is wanted.

It also narrows a number to the exact Python type of its ROS2 field, because the guard that runs next (opensmi.ros2.runtime.guard) tests exact types, and OPC UA's scalar types subclass int, float and str (a Double node written with 1 hands back an integer). The field decides the type. A value the field cannot hold is left as it came, for the guard to refuse by name.

def shaped(message_type: type[Message], context: ProjectionContext) -> tuple[PlanEntry, ...]:

Return the nodes one message type becomes, through the context's registry.

def ua_shape(field_type: FieldType) -> tuple[object, ua.VariantType]:

Return the value one modelled ROS2 field boots with, and the OPC UA type it carries.

Public because a declared ROS2 parameter type is shaped here too (opensmi.ros2.runtime.parameters.ParameterType.field_type, opensmi.ros2.server.parameters.ua_parameter_shape), so one table decides the mapping.

def ua_value(value: object) -> object:

Return a decoded field value in the shape an OPC UA write wants.

A payload holds a tuple and the library builds an array from a list, so that is the one conversion.

ANY_TYPE: str =

The type name of an entry that answers for every type; the generic walk carries it.

Value
'*'
DecodeFunction: TypeAlias =

Run per message: a payload becomes a map from plan name to the value to write.

Value
Callable[[Message, ProjectionContext], Mapping[str, object]]
EncodeFunction: TypeAlias =

Run per publish: that same map becomes a payload again, where the recipe has an inverse.

Value
Callable[[type[Message], Mapping[str, object], ProjectionContext], Message]
NodeLevelNaming: TypeAlias =

How a level whose subtree is curated is named: from its node path and its ROS2 type.

Value
Callable[[NodePath, type[Message]], str]
NodePath: TypeAlias =

The browse names from the top of a plan down to one node, such as ("Pose", "Position", "X").

Value
tuple[str, ...]
PlanDataType: TypeAlias =

What a plan may say about a variable's DataType, resolved to a NodeId where the nodes are made.

A plan is a pure value, and the NodeId of one of our types depends on the server, while an enumeration or a structure has no NodeId until it is minted (opensmi.ros2.server.mutation). A type is the structure class that is the element type of an array of structures, which the generic walk emits for a repeated message.

Value
StandardDataType | ProjectDataType | EnumDataType | type
PlanEntry: TypeAlias =

The two verbs, and there is no third.

Value
PlanVariable | PlanObject
ShapeFunction: TypeAlias =

Run once at init: a message dataclass becomes a node plan.

Value
Callable[[type[Message], ProjectionContext], tuple[PlanEntry, ...]]
def _annotated(element: Any, initial: object) -> tuple[Any, int]:

Return the annotation of a field and the length of its fixed-size array, from its boot value.

def _below(level: NodeLevel, entry: PlanEntry) -> PlanEntry:

Carry one entry up under the field that produced it, which becomes a level above it.

def _below_values(values: Mapping[str, object], name: str) -> Mapping[str, object]:

Return the values belonging to one nested field, with its own name taken off the front.

def _decoded_elements(value: object, element_type: type[Message], context: ProjectionContext) -> list[Mapping[str, object]]:

Return one repeated message field as the array of elements its variable carries.

Each element is decoded by the Projection covering its type into a map from its structure's field names to values. An empty sequence decodes to an empty list, which is a value.

The elements are plain maps, not ExtensionObjects: encoding one needs the server's namespace and belongs to the variable being written (opensmi.ros2.server.mutation).

def _display_name(context: ProjectionContext, field_name: str) -> str:

Return what a client is told the field is called: the new name, else the raw ROS2 name.

def _element_context(element_type: type[Message], context: ProjectionContext) -> ProjectionContext:

Return the context the elements of a repeated message field are rendered in.

The path goes back to empty, because an element's leaves are named from its own root. The selection becomes the element type's whole field set: the selector treats a repeated message as one leaf and never walks inside it (opensmi.ros2.runtime.selection), so an element is never partial and can be rebuilt exactly. Fields keep their ROS2 names and no rename applies, because they are the field names of a DataType. The registry is kept, so curated Projections still apply to elements.

def _element_structure(element_type: type[Message], context: ProjectionContext) -> type:

Work out the structure of one element, which element_structure remembers.

def _element_variant_type(element: ElementType) -> ua.VariantType:

Return the OPC UA type of a scalar, or of what a container holds.

def _encoded_elements(value: object, element_type: type[Message], context: ProjectionContext) -> tuple[Message, ...]:

Return the ROS2 sequence that the array of elements of one repeated message field describes.

Each element is a map from the names its Projection knows to values, rebuilt by that Projection's own inverse. An element that lacks a field is refused by name, and an empty list builds an empty sequence.

Raises
NotInvertibleErrorWhen an element has no value for a field of its type.
ValueErrorWhen the value is not a list, or an element of it is not a map. This is a client's write, not a declaration fault.
def _instance(leaves: list[PlanVariable], classes: Mapping[NodePath, type], values: Mapping[str, object], path: NodePath) -> Any:

Undocumented

def _leaf_annotation(leaf: PlanVariable) -> tuple[Any, int]:

Return the field annotation of a leaf, and the length of its fixed-size array.

Only a structure class and a plain value can be a field. Any other DataType (an enumeration, a type from the nodeset or one OPC UA defines) is left to the built-in type, so Components that curate such a field differently still share one structure.

Raises
UnprojectableFieldErrorWhen the leaf is of a type no structure field can carry.
def _narrowed_scalar(value: object, element: ElementType) -> object:

Return one scalar as exactly the Python type its ROS2 element type is, where it can be.

def _plan_structures(described: FieldDescription, context: ProjectionContext, element_type: type[Message], name: str, display: str) -> PlanVariable:

Return the variable a repeated message field becomes: one array of the element's structure.

It boots as an empty array, a legal ROS2 value, so "nobody has written this yet" is told by the StatusCode, BadWaitingForInitialData (opensmi.ros2.server.status).

Raises
UnprojectableFieldErrorWhen the element cannot render as a structure.
def _plan_variable(described: FieldDescription, context: ProjectionContext, name: str, display: str) -> PlanVariable:

Return the variable one modelled ROS2 field becomes under the generic walk.

def _refuse_a_partial_payload(message_type: type[Message], path: str, why: str):

Refuse to build a message with a hole in it, naming the field and the reason.

def _repeats(field_type: FieldType) -> bool:

Return whether a field holds many of its type rather than one.

def _require_no_silent_shadowing(entry: Projection, covered: Iterable[Projection]):

Refuse a second entry for one type unless it says it is replacing the first.

def _selected_fields(message_type: type[Message], context: ProjectionContext) -> Iterator[tuple[FieldDescription, ProjectionContext, type[Message] | None]]:

Yield each field the selection kept, with its context and the message type it nests.

The generic walk's steps share this route and differ only in what they do at each stop.

def _siblings(context: ProjectionContext, field_names: list[str]) -> dict[str, str]:

Return the browse name of each of these sibling fields, refusing two that share one.

Raises
DuplicateVariableNameErrorWhen two give one browse name, whether by conversion or by a rename. The error names both ROS2 names.
def _signature(message_type: type[Message]) -> tuple[StructureField, ...]:

Return the fields of the structure of a ROS2 message type, as its definition has them.

_INTEGERS: frozenset[str] =

The primitive names whose Python type is int, taken from the guard's table.

Value
frozenset(INTEGER_RANGES)
_PACKABLE =

The OPC UA types a field of a structure can carry.

Value
frozenset((getattr(ua.VariantType, name) for name in ('Boolean', 'SByte', 'Byte'↵
, 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64', 'Float', 'Double', 'S↵
tring', 'ByteString')))
_VARIANT_TYPES: Mapping[PrimitiveName, ua.VariantType] =

Undocumented

Value
{'bool': ua.VariantType.Boolean,
 'byte': ua.VariantType.Byte,
 'char': ua.VariantType.String,
 'wchar': ua.VariantType.String,
 'float32': ua.VariantType.Float,
 'float64': ua.VariantType.Double,
 'int8': ua.VariantType.SByte,
...
_ZEROS: Mapping[ua.VariantType, object] =

Undocumented

Value
{ua.VariantType.Boolean: False,
 ua.VariantType.String: '',
 ua.VariantType.Float: 0.0,
 ua.VariantType.Double: 0.0}