Skip to main content

ROS 2 interface definitions, serialization, hashes, and ROS Python conversions

Project description

ROS 2 PyTerfaces IDL

ROS 2 message and service definitions, metadata and serialization in Python.

Create new message types, (de)serialize them, compute the RIHS01 hash, and convert to and from ROS 2 Python messages. All ROS 2 common_interfaces are reimplemented, and every message tested to interoperate with ROS.

Based on Cyclone DDS IDL, but specialized for ROS 2.

[!NOTE] This is a low level tool to send/receive raw payload with ROS 2, and to set up communications on the RMW.

Table of Contents

Install

pip install ros2_pyterfaces

Example

Message

from dataclasses import dataclass
from ros2_pyterfaces.idl import IdlStruct, types

@dataclass
class Vector3(IdlStruct, typename="geometry_msgs/msg/Vector3"):
    x: types.float64 = 0.0
    y: types.float64 = 0.0
    z: types.float64 = 0.0

my_msg: Vector3 = Vector3(1,2,3)

# serialization
blob_bytes: bytes = my_msg.serialize()
my_msg_again: Vector3 = Vector3.deserialize(blob_bytes)

# ROS 2 metadata
json_type_description = Vector3.json_type_description()
ros_hash = Vector3.hash_rihs01()

# ROS 2 conversion
ros_msg_type = Vector3.to_ros_type()
ros_msg = my_msg.to_ros()
our_msg: Vector3 = Vector3.from_ros(ros_msg)

Service

Services follow the ROS naming pattern: *_Request, *_Response, *_Event, plus a small wrapper type. The serializable types are the request, response, and event dataclasses. The top-level service type is usually created with make_idl_service(...). If you omit event_type=... (most cases), the factory generates a matching event type for you.

from dataclasses import dataclass
from ros2_pyterfaces import idl

# Same classes definition as Messages for *_Request *_Response
@dataclass
class SetBool_Request(idl.IdlStruct, typename="std_srvs/srv/SetBool_Request"):
    data: bool = False


@dataclass
class SetBool_Response(idl.IdlStruct, typename="std_srvs/srv/SetBool_Response"):
    success: bool = False
    message: str = ""

# Top-level service type
SetBool = idl.make_idl_service(SetBool_Request, SetBool_Response)

# Serialization
some_request: SetBool_Request = SetBool.Request(data=True)
some_response: SetBool_Response = SetBool.Response(success=True, message="yey")

# ROS 2 metadata
json_type_description = SetBool.json_type_description()
ros_hash = SetBool.hash_rihs01()

# ROS 2 conversion
ros_srv_type = SetBool.to_ros_type()

ros_request = some_request.to_ros()
request_again = SetBool.Request.from_ros(ros_request)

ros_response = some_response.to_ros()
response_again = SetBool.Response.from_ros(ros_response)

[!WARNING] Not implemented yet:

  • Actions

Utilities

You can normalize messages to plain nested Python data for comparisons, tests, and snapshots:

from ros2_pyterfaces import idl

plain = idl.message_to_plain_data(my_msg)
plain_ros = idl.message_to_plain_data(my_msg.to_ros(), type(my_msg))
assert plain == plain_ros

There is also a deterministic random message generator for repeatable tests:

from ros2_pyterfaces.geometry_msgs.msg import Vector3
from ros2_pyterfaces.utils.random import random_message

msg = random_message(Vector3)
same_msg_again = random_message(Vector3)
assert msg == same_msg_again

Attribution

The low-level IDL model, serialization behavior, and part of the user API are dependent on (fantastic) Cyclone DDS Python's idl: https://github.com/eclipse-cyclonedds/cyclonedds-python.

Replicated ROS 2 Messages Repos

Included Interfaces

  • ros2_pyterfaces.builtin_interfaces: msg.py
  • ros2_pyterfaces.composition_interfaces: srv.py
  • ros2_pyterfaces.diagnostic_msgs: msg.py, srv.py
  • ros2_pyterfaces.geometry_msgs: msg.py
  • ros2_pyterfaces.lifecycle_msgs: msg.py, srv.py
  • ros2_pyterfaces.nav_msgs: msg.py, srv.py
  • ros2_pyterfaces.rcl_interfaces: msg.py, srv.py
  • ros2_pyterfaces.rosgraph_msgs: msg.py
  • ros2_pyterfaces.sensor_msgs: msg.py, srv.py
  • ros2_pyterfaces.service_msgs: msg.py
  • ros2_pyterfaces.shape_msgs: msg.py
  • ros2_pyterfaces.statistics_msgs: msg.py
  • ros2_pyterfaces.std_msgs: msg.py
  • ros2_pyterfaces.std_srvs: srv.py
  • ros2_pyterfaces.stereo_msgs: msg.py
  • ros2_pyterfaces.test_msgs: msg.py
  • ros2_pyterfaces.type_description_interfaces: msg.py, srv.py
  • ros2_pyterfaces.trajectory_msgs: msg.py
  • ros2_pyterfaces.unique_identifier_msgs: msg.py
  • ros2_pyterfaces.visualization_msgs: msg.py, srv.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ros2_pyterfaces-0.1.2.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ros2_pyterfaces-0.1.2-py3-none-any.whl (41.7 kB view details)

Uploaded Python 3

File details

Details for the file ros2_pyterfaces-0.1.2.tar.gz.

File metadata

  • Download URL: ros2_pyterfaces-0.1.2.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.23

File hashes

Hashes for ros2_pyterfaces-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d6e2a49670da8e88d2995bc1c447f0e4547933766f4d0f01bb60a52b5b6d8a82
MD5 f8faad9a26f317442f1068a4021bf89d
BLAKE2b-256 24cdcd0a956d919e2152279e7a16f2e05492bf774fd4ae646f674dae8fdf35b1

See more details on using hashes here.

File details

Details for the file ros2_pyterfaces-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ros2_pyterfaces-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7f060337184134d1795ded4d943947b742d72971e24619043b655182aadcb7cc
MD5 92837010310003808f8773b04adb8167
BLAKE2b-256 a0d94c60a30f9cb9a151b13abe0f39d78119187ef3534e356394e4dab4d23584

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page