Message types and operators
The message types Rosetta decodes and encodes, the operators an apply list
can name, and the API for registering more. The decoders and encoders ship
with the ros2 robot interface in rosetta_ros. The operators and the
registration API are part of core rosetta. The contract keys that use them
are on Contract.
Decoders
A decoder turns a message into an array or a string. Observations, record-only
sections and teleop.input load only for a type with a built-in decoder or a
custom decoder. Porting decodes every source, actions
included.
Type |
dtype |
|
Entries |
|---|---|---|---|
|
|
not used |
Whole image. Encodings |
|
|
not used |
Whole image, decoded with OpenCV ( |
|
|
not used |
Whole tensor, at the source’s |
|
|
required |
|
|
|
required |
First point only. |
|
|
required |
|
|
|
required |
|
|
|
required |
Dotted path from the message root, such as |
|
|
required |
Dotted path, such as |
|
|
required |
Dotted path, such as |
|
|
required |
Dotted path from the root, so |
|
|
optional |
The whole |
|
|
optional |
Same as |
|
|
optional |
Same as |
|
|
optional |
One value from |
|
|
not used |
|
A message that fails to decode is dropped. The failure is logged once per stream, and the recovery is logged when a message decodes again. These fail per message:
A depth image:
Imageencodingsmono16,16UC1,32FC1,32FC, and aCompressedImagewhoseformatnames a depth encoding orcompressedDepth.An
Imagewith an empty or unlisted encoding.A joint or DOF name that is not in the message.
A
JointTrajectorywith no points.A
Joyindex that is negative or past the end of the array.A value whose width differs from the source’s width.
Encoders
An encoder turns an action vector into a message. actions and
teleop.feedback load only for a type with a built-in encoder or a custom
encoder.
Type |
|
Header stamp |
Notes |
|---|---|---|---|
|
required |
set |
Each selected field must cover every named joint. Unselected fields stay empty. |
|
required |
set |
One point, |
|
required |
none |
Full coverage, as for |
|
required |
set |
Each array is sized to its highest selected index. Gaps are zero. Buttons are rounded to the nearest integer. |
|
required |
none |
Dotted path, as for decoding. |
|
required |
set |
Dotted path from the root, such as |
|
optional |
none |
The source must be one value wide. |
|
optional |
none |
|
|
optional |
none |
|
The stamp is the node clock at publish time. Coverage, width and the one-value rule fail the encode, not the load.
Encoding runs the inverse apply pipeline, then a width check, then a
finiteness check. A live action frame with NaN or Inf in any value is
dropped whole and logged, and nothing from it is published. If frames keep
failing, the watchdog publishes each channel’s safety action.
Operators
Operator |
Form |
Inverse |
On actions |
Behavior |
|---|---|---|---|---|
|
|
degrees to radians |
yes |
Takes no arguments. Round-trip checked at load. |
|
|
the same clip |
yes |
Element-wise clip to |
|
|
none |
no |
Nearest-neighbour resize. Image keys only. |
Decoding runs apply front to back. Encoding runs it back to front through
the inverses. Each operator has one of three invertibility kinds:
Kind |
Inverse |
Allowed on |
|---|---|---|
|
none |
no |
|
yes, need not undo |
yes |
|
yes, must undo |
yes |
Custom codecs
Register a decoder for a type Rosetta does not ship, and an encoder too if the type is used as an action or feedback.
import numpy as np
from geometry_msgs.msg import Point
from rosetta.codecs import register_decoder, register_encoder
@register_decoder("geometry_msgs/msg/Point", dtype="float64")
def decode_point(msg, spec):
return np.array([msg.x, msg.y, msg.z], dtype=np.float64)
@register_encoder("geometry_msgs/msg/Point")
def encode_point(values, spec, stamp_ns=None):
return Point(x=float(values[0]), y=float(values[1]), z=float(values[2]))
A source of that type then declares select: [x, y, z] to make it three wide.
register_decoder(type_str, dtype, *, override=False, requires_select=False)
Argument |
Meaning |
|---|---|
|
Message type as the contract writes it. |
|
One of |
|
Replace the codec already registered for |
|
A source of this type without |
register_encoder(type_str, *, override=False, requires_select=False) takes
the same arguments without dtype.
Function |
Signature |
Returns |
|---|---|---|
Decoder |
|
A numpy array as wide as the source, or a |
Encoder |
|
A message. |
spec.names is the select list as a tuple, empty without select.
spec.dim is the source’s width. spec.source.channel is the parsed
channel block.
There are two ways to make Rosetta use a codec:
Advertise the module under the entry-point group
rosetta.codecs, with the module path as the value. Rosetta imports every module in the group on the first contract load, and the contract names only the type. A module that fails to import fails every contract load in that process.Name the function on the channel with
decoder: my_pkg.codecs:decode_pointorencoder: my_pkg.codecs:encode_point. The module must be importable. The path is checked at load, and it wins over the registry for that channel. A channel with a custom decoder resolves tofloat64unless it setsdtype.
Custom operators
from rosetta.errors import ContractValidationError
from rosetta.operators import Invertibility, Operator, register_operator
@register_operator("scale", kind=Invertibility.BIJECTIVE)
class ScaleOperator(Operator):
def __init__(self, args, ctx):
if not isinstance(args, (int, float)) or isinstance(args, bool) or args == 0:
raise ContractValidationError(f"scale expects a non-zero number, got {args!r}")
self.k = float(args)
def forward(self, arr):
return arr * self.k
def inverse(self, arr):
return arr / self.k
A contract then writes apply: [scale: 1000].
register_operator(name, *, kind, override=False) registers the class under
name. A name already registered is a ValueError unless override=True. A
class without forward, or without inverse for a kind other than
FORWARD_ONLY, is a TypeError.
Member |
Meaning |
|---|---|
|
|
|
The decode direction. |
|
The encode direction. |
|
|
|
The array the |
Advertise the module under the entry-point group rosetta.operators, with
the module path as the value. Rosetta imports the group the first time a
contract names an operator. A module that fails to import fails every
contract with an apply list in that process.