{"id":7397,"library":"mcap-ros2-support","title":"MCAP ROS2 Support","description":"mcap-ros2-support provides utilities to seamlessly integrate ROS2 messages with the MCAP data logging format. It allows for efficient recording and playback of ROS2 data within MCAP files, handling schema definition and message serialization/deserialization. The current version is 0.5.7, and the project (including core mcap, CLI, and other support libraries) has a frequent release cadence.","status":"active","version":"0.5.7","language":"en","source_language":"en","source_url":"https://github.com/foxglove/mcap","tags":["ros2","mcap","robotics","data logging","foxglove"],"install":[{"cmd":"pip install mcap-ros2-support","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core MCAP library for writing/reading MCAP files.","package":"mcap","optional":false},{"reason":"Provides ROS2 message runtime definitions for Python.","package":"rosidl_runtime_py","optional":false},{"reason":"Needed for fast RTPS type support, often a transitive dependency.","package":"rosidl_typesupport_fastrtps_cpp","optional":true}],"imports":[{"note":"The package name for the writer module is `mcap_ros2`, not `mcap_ros2_support`.","wrong":"from mcap_ros2_support.writer import Ros2RawWriter","symbol":"Ros2RawWriter","correct":"from mcap_ros2.writer import Ros2RawWriter"},{"note":"The package name for the reader module is `mcap_ros2`, not `mcap_ros2_support`.","wrong":"from mcap_ros2_support.reader import Ros2Reader","symbol":"Ros2Reader","correct":"from mcap_ros2.reader import Ros2Reader"},{"note":"This is from the core `mcap` library, not `mcap-ros2-support`.","symbol":"McapWriter","correct":"from mcap.mcap_writer import McapWriter"}],"quickstart":{"code":"import os\nfrom pathlib import Path\nfrom mcap.mcap_writer import McapWriter\nfrom mcap_ros2.writer import Ros2RawWriter\nfrom mcap_ros2.reader import Ros2Reader\n\n# Assuming a ROS2 message type is available, e.g., from an installed ROS2 package\n# For demonstration, we'll mock a simple message structure for 'std_msgs/msg/String'\n# In a real scenario, you would import the actual message type and populate it.\nclass MockStringMsg:\n    _TYPE = 'std_msgs/msg/String'\n    _HAS_HEADER = False\n    _LAYOUT = [\n        ('data', 'string'),\n    ]\n    _REGISTERED_TYPES = {}\n    _CDR_TYPES = {}\n\n    def __init__(self, data=''):\n        self.data = data\n\n    def __repr__(self):\n        return f\"MockStringMsg(data='{self.data}')\"\n\n    @staticmethod\n    def get_fields_and_field_types():\n        return [('data', 'string')]\n\n    def get_type_description_interfaces(self):\n        # Simplified mock for the purpose of the quickstart\n        return {'type_description': 'string data'}\n\n# Write a simple MCAP file with a ROS2 message\noutput_file = Path('ros2_messages.mcap')\nwith open(output_file, 'wb') as f,\n     McapWriter(f) as mcap_writer,\n     Ros2RawWriter(mcap_writer) as ros2_writer:\n\n    # In a real ROS2 environment, you'd create actual ROS2 messages\n    # from std_msgs.msg import String\n    # msg = String(data='Hello ROS2 from MCAP!')\n    mock_msg = MockStringMsg(data='Hello ROS2 from MCAP!')\n    topic = '/chatter'\n    log_time_ns = 123456789\n\n    # The Ros2RawWriter can infer the schema from a ROS2 message object.\n    # For this mock, we ensure the mock_msg has the necessary _TYPE and field info.\n    ros2_writer.write_message(\n        topic=topic,\n        message=mock_msg,\n        log_time_ns=log_time_ns,\n        publish_time_ns=log_time_ns\n    )\n\nprint(f\"Wrote ROS2 message to {output_file}\")\n\n# Read the MCAP file and verify\nwith open(output_file, 'rb') as f:\n    reader = Ros2Reader(f)\n    for msg, schema, channel in reader.iter_decoded_messages():\n        print(f\"Read message from topic '{channel.topic}': {msg}\")\n        assert msg.data == 'Hello ROS2 from MCAP!'\n        break # Only read one message for this example\n\n# Clean up the created file\nos.remove(output_file)\nprint(f\"Cleaned up {output_file}\")\n","lang":"python","description":"This quickstart demonstrates how to write and read a ROS2 message to and from an MCAP file using `mcap-ros2-support`. It mocks a `std_msgs/msg/String` for demonstration, but in a live ROS2 environment, you would import actual message types. It uses `Ros2RawWriter` to encode ROS2 messages into the MCAP format and `Ros2Reader` to decode them back. The core `McapWriter` is used for the underlying MCAP file operations."},"warnings":[{"fix":"Ensure all ROS2 message packages are correctly installed in your Python environment (`pip install ...` for Python packages, or `colcon build` for ROS2 workspaces).","message":"Deserializing ROS2 messages requires the corresponding `rosidl_runtime_py` and `rosidl_typesupport_fastrtps_cpp` (or other type support) packages for the message types to be installed and available in the Python environment where the MCAP file is being read. Missing these will result in deserialization errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update to `mcap-ros2-support>=0.5.7` to use string topics, or ensure topic inputs match previous API expectations for older versions.","message":"Prior to v0.5.7, specifying topics might have had stricter type requirements. As of v0.5.7, topics can be specified as strings, providing more flexibility, indicated by 'allow topics specified as string' in the changelog.","severity":"breaking","affected_versions":"<0.5.7"},{"fix":"Always use `from mcap_ros2.<reader|writer> import <Symbol>` for the ROS2 specific components.","message":"The `mcap_ros2` package (where `Ros2RawWriter` and `Ros2Reader` reside) is a sub-package within the `mcap-ros2-support` distribution. The import paths use `mcap_ros2` directly, which can be confusing for users expecting `mcap_ros2_support`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `mcap-ros2-support` is installed via `pip install mcap-ros2-support` and use the correct import path: `from mcap_ros2.<reader|writer> import ...`.","cause":"Incorrect import path or `mcap-ros2-support` not installed.","error":"ModuleNotFoundError: No module named 'mcap_ros2'"},{"fix":"Ensure you are passing a properly instantiated ROS2 message object (e.g., `String(data='...')`) or an object that mimics the ROS2 message interface expected by `mcap_ros2` (with attributes like `_TYPE`, `_HAS_HEADER`, `get_fields_and_field_types`, etc.).","cause":"Attempting to pass raw bytes (or an object without ROS2 message metadata) to `Ros2RawWriter.write_message` where a ROS2 message object is expected.","error":"AttributeError: 'bytes' object has no attribute '_TYPE'"},{"fix":"Verify that all ROS2 packages containing the message types used in the MCAP file are installed and accessible in your Python environment. This often means having a ROS2 workspace sourced or relevant `rosidl_runtime_py` dependencies installed.","cause":"Attempting to read a ROS2 message from an MCAP file where the corresponding ROS2 message type definition is not available in the current Python environment.","error":"mcap.exceptions.McapError: Message type not found for schema id ..."}]}