Skip to content

tf2_msgs Adapters

mosaicolabs.ros_bridge.adapters.tf2_msgs

FrameTransformAdapter

Bases: ROSAdapterBase[FrameTransform]

Adapter for translating ROS TF2 messages to Mosaico FrameTransform.

Supported ROS Types:

Example
ros_msg = ROSMessage(
    timestamp=17000,
    topic="/tf",
    msg_type="tf2_msgs/msg/TFMessage",
    data={
        "transforms": [
            {
                "header": {
                    "stamp": {
                        "sec": 17000,
                        "nanosec": 0,
                    },
                    "frame_id": "map",
                },
                "child_frame_id": "base_link",
                "transform": {
                    "translation": {
                        "x": 0.0,
                        "y": 0.0,
                        "z": 0.0,
                    },
                    "rotation": {
                        "x": 0.0,
                        "y": 0.0,
                        "z": 0.0,
                        "w": 1.0,
                    },
                },
            }
        ]
    },
)
# Automatically resolves to a flat Mosaico FrameTransform with attached metadata
mosaico_frame_transform = FrameTransformAdapter.translate(ros_msg)

translate classmethod

translate(ros_msg, **kwargs)

Translates a ROS message into a Mosaico Message.

Returns:

Name Type Description
Message Message

The translated message containing a FrameTransform object.

Raises:

Type Description
Exception

Wraps any translation error with context (topic name, timestamp).

from_dict classmethod

from_dict(ros_data)

Converts the raw dictionary data into the specific Mosaico type.

Example
ros_data={
    "transforms": [
        {
            "header": {
                "stamp": {
                    "sec": 17000,
                    "nanosec": 0,
                },
                "frame_id": "map",
            },
            "child_frame_id": "base_link",
            "transform": {
                "translation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": 0.0,
                },
                "rotation": {
                    "x": 0.0,
                    "y": 0.0,
                    "z": 0.0,
                    "w": 1.0,
                },
            },
        }
    ]
}
# Automatically resolves to a flat Mosaico FrameTransform with attached metadata
mosaico_frame_transform = FrameTransformAdapter.from_dict(ros_data)

to_ros classmethod

to_ros(mosaico_data, typestore, ros_msg_type=None)

Converts a Mosaico FrameTransform (or a Message wrapping one) into a tf2_msgs/msg/TFMessage.

Parameters:

Name Type Description Default
mosaico_data Union[Message, FrameTransform]

A Message wrapping a FrameTransform, or a raw FrameTransform.

required
typestore Typestore

The rosbags typestore for target type resolution.

required
ros_msg_type Optional[str]

Override for the output ROS type. Only tf2_msgs/msg/TFMessage is supported.

None

Returns:

Name Type Description
MsgType MsgType

The constructed tf2_msgs/msg/TFMessage, or raises an error if:

  • the ros_msg_type is unsupported by adapter (TypeError)
  • the ros_msg_type or default type are unsupported by typestore (TypeError)
  • the ros_msg_type or default type are supported but translation is not implemented (NotImplementedError)

schema_metadata classmethod

schema_metadata(typestore, ros_msg_type, ros_version)

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.

ros_msg_type abstractmethod classmethod

ros_msg_type()

Returns the specific ROS message type handled by this adapter.

is_rosmsg_type_valid classmethod

is_rosmsg_type_valid(type_to_validate)

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., "sensor_msgs/msg/Imu").

required

Returns:

Name Type Description
bool bool

True if the adapter supports this type, False otherwise.

unpack_mosaico_msg classmethod

unpack_mosaico_msg(mosaico_msg)

Extracts the typed Mosaico payload and its Header (if present) from a wrapped or bare message.

Handles two input cases:

  • Message wrapper: the typed data is extracted via get_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 Message envelope or a raw instance of cls.__mosaico_ontology_type__.

required

Returns:

Type Description
T

tuple[T, Header]: A (data, header) tuple where data is the typed ontology object and

Header

header is the corresponding Header, or a default Header (empty frame_id and

tuple[T, Header]

zero Time) if not present.

Raises:

Type Description
TypeError

If mosaico_msg is neither a Message nor an instance of the expected ontology type.

ontology_data_type classmethod

ontology_data_type()

Returns the Ontology class type associated with this adapter.