Skip to content

Instantly share code, notes, and snippets.

@IamPhytan
Last active December 22, 2022 17:09
Show Gist options
  • Save IamPhytan/6922dfde2343c67686ea54215aeda368 to your computer and use it in GitHub Desktop.
Save IamPhytan/6922dfde2343c67686ea54215aeda368 to your computer and use it in GitHub Desktop.
MCAP Cookbook

MCAP

MCAP cheatsheet

Cookbook

Python

Get informations about a mcap

import sys

from mcap.reader import make_reader

filename = None
mcapfile = filename or sys.argv[1]

with open(filename, "rb") as f:
    reader = make_reader(f)

    header = reader.get_header()
    msg_profile = header.profile
    mcap_version = header.library

    summary = reader.get_summary()
    statistics = summary.statistics
    schemas = summary.schemas
    channels = summary.channels

    start = statistics.message_start_time
    end = statistics.message_end_time

    # schemas : {id: Schema(id=id,data=msg_definition, encoding='ros1msg'/'ros2msg', name='sensor_msgs/msg/Imu')}
    # channels : {id: Channel(id=id,topic='/topic', message_encoding='cdr', metadata={'offered_qos_profiles': '{}'}, schema_id=schema_id)}

    for schema, channel, message in reader.iter_messages(topics=["/velocity"]):
        topic = channel.topic
        message_type = schema.name
        message_data = message.data

ROS1 messages from mcap

import sys

from mcap_ros1.reader import read_ros1_messages

for msg in read_ros1_messages(sys.argv[1]):
    topic = msg.topic
    if topic == "/topic":
        msg_object = msg.ros_msg
        field = msg_object.field_name
import sys

from mcap_ros2.reader import read_ros2_messages

for msg in read_ros2_messages(sys.argv[1]):
    topic = msg.topic
    if topic == "/topic":
        msg_object = msg.ros_msg
        field = msg_object.field_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment