Base Adapter
mosaicolabs.ros_bridge.adapter_base ¶
RosSchemaMetadata ¶
Encapsulates Mosaico's reserved _ros_ topic-metadata namespace in a single place.
Every topic ingested by the ROS bridge carries ROS-specific bookkeeping (original
msgtype, raw msgdef, extracted enums) plus bridge-internal fields (e.g. the
source bag file) under one reserved key, so that:
- The literal string
"_ros_"exists in exactly one place (:attr:KEY), instead of being duplicated across adapters, loaders, and the injector. - Callers build up this namespace incrementally via :meth:
updatewithout ever touching the wrapping dict shape by hand, which is what previously caused bugs (e.g. accidentally overwriting the whole_ros_block instead of merging into it).
Example
KEY
class-attribute
¶
The reserved metadata key. Adapters/loaders/the injector should reference this constant rather than the literal string, so the namespace can be renamed in one place.
update ¶
Merges additional fields into this block, in place. Returns self for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**fields
|
Any
|
Additional fields to merge. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
RosSchemaMetadata |
'RosSchemaMetadata'
|
The updated metadata instance. |
to_dict ¶
Wraps the current fields under the reserved key, e.g. {"_ros_": {...}}.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary containing the |
merge_into ¶
Merges this block into an existing metadata dict's _ros_ namespace, creating it
if absent. Mutates and returns metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict
|
The existing metadata dict to merge into. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The updated metadata dict with the |
extract
classmethod
¶
Reads the _ros_ block out of a metadata dict (e.g. a topic's user_metadata),
or {} if absent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Optional[dict]
|
A metadata dict, typically |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The extracted |
from_dict
classmethod
¶
Creates a RosSchemaMetadata from a plain metadata dict, e.g. the return value of
ROSAdapterBase.schema_metadata(). Any keys outside the _ros_ namespace are ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Optional[dict]
|
A metadata dict, typically |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RosSchemaMetadata |
'RosSchemaMetadata'
|
A new instance seeded with the extracted |
ROSAdapterBase ¶
Bases: ABC, Generic[T]
Abstract Base Class for converting ROS messages to Mosaico Ontology types.
The Adaptation Layer is the semantic core of the ROS Bridge. Rather than performing simple parsing, adapters actively translate raw ROS data into standardized, strongly-typed Mosaico Ontology objects.
Attributes:
| Name | Type | Description |
|---|---|---|
ros_msgtype |
str | Tuple[str, ...]
|
The ROS message type string (e.g., 'sensor_msgs/msg/Imu') or a tuple of supported types. |
is_default_adapter |
str | Tuple[str, ...]
|
whether this adapter is the default one for mosaico_ontology_type |
__mosaico_ontology_type__ |
Type[T]
|
The target Mosaico class (e.g., IMU). |
_REQUIRED_KEYS |
Tuple[str, ...]
|
Internal validation list for mandatory ROS message fields. |
ros_msg_type
abstractmethod
classmethod
¶
Returns the specific ROS message type handled by this adapter.
translate
classmethod
¶
Translates a ROS message instance into a Mosaico Message.
Implementation should handle recursive unwrapping, unit conversion, and validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ros_msg
|
ROSMessage
|
The source container yielded by the ROSLoader. |
required |
**kwargs
|
Any
|
Contextual data such as calibration parameters or frame overrides. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Message |
Message
|
A Mosaico Message object containing the instantiated ontology data. |
Raises:
| Type | Description |
|---|---|
Exception
|
If translation fails due to missing fields, type mismatches, or other errors. |
from_dict
abstractmethod
classmethod
¶
Maps the raw ROS dictionary to the Pydantic model.
This method performs field validation and reconstruction.
is_rosmsg_type_valid
classmethod
¶
Checks whether a given ROS message type string is handled by this adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_to_validate
|
str
|
The full ROS message type string to check
(e.g., |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
unpack_mosaico_msg
classmethod
¶
Extracts the typed Mosaico payload and its Header (if present) from a wrapped or bare message.
Handles two input cases:
Messagewrapper: the typed data is extracted viaget_data().- Raw ontology instance: returned as-is with
the Header is extracted from the ontology (if supported), otherwise an default Header (empty frame_id and zero Time) is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mosaico_msg
|
Union[Message, T]
|
Either a |
required |
Returns:
| Type | Description |
|---|---|
T
|
tuple[T, Header]: A |
Header
|
header is the corresponding |
tuple[T, Header]
|
zero |
Raises:
| Type | Description |
|---|---|
TypeError
|
If mosaico_msg is neither a |
to_ros
abstractmethod
classmethod
¶
Converts a Mosaico message or ontology object back into a native ROS message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mosaico_data
|
Union[Message, T]
|
A |
required |
typestore
|
Typestore
|
The rosbags typestore used to resolve and construct target ROS types. |
required |
ros_msg_type
|
Optional[str]
|
Override for the output ROS type string. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MsgType |
MsgType
|
The constructed ROS message instance, or raises an error if:
|
schema_metadata
classmethod
¶
Extract the ROS message specific schema metadata, if any.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
typestore
|
Typestore
|
The rosbags typestore for target type resolution. |
required |
ros_msg_type
|
str
|
The ROS message type to extract metadata for. |
required |
ros_version
|
int
|
The ROS version (1 or 2) to consider for metadata extraction. |
required |
Returns:
| Type | Description |
|---|---|
Optional[dict]
|
Optional[dict]: A dictionary containing the schema metadata, or None if not applicable. |
For the BatteryStateAdapter the expected output is { "ros": { "enums": { "POWER_SUPPLY_STATUS_UNKNOWN": 0, "POWER_SUPPLY_STATUS_CHARGING": 1, "POWER_SUPPLY_STATUS_DISCHARGING": 2, "POWER_SUPPLY_STATUS_NOT_CHARGING": 3, "POWER_SUPPLY_STATUS_FULL": 4, "POWER_SUPPLY_HEALTH_UNKNOWN": 0, "POWER_SUPPLY_HEALTH_GOOD": 1, "POWER_SUPPLY_HEALTH_OVERHEAT": 2, "POWER_SUPPLY_HEALTH_DEAD": 3, "POWER_SUPPLY_HEALTH_OVERVOLTAGE": 4, "POWER_SUPPLY_HEALTH_UNSPEC_FAILURE": 5, "POWER_SUPPLY_HEALTH_COLD": 6, "POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE": 7, "POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE": 8, "POWER_SUPPLY_TECHNOLOGY_UNKNOWN": 0, "POWER_SUPPLY_TECHNOLOGY_NIMH": 1, "POWER_SUPPLY_TECHNOLOGY_LION": 2, "POWER_SUPPLY_TECHNOLOGY_LIPO": 3, "POWER_SUPPLY_TECHNOLOGY_LIFE": 4, "POWER_SUPPLY_TECHNOLOGY_NICD": 5, "POWER_SUPPLY_TECHNOLOGY_LIMN": 6, }, "msgtype": "sensor_msgs/msg/BatteryState" "msgdef": "..." } }
ontology_data_type
classmethod
¶
Returns the Ontology class type associated with this adapter.