Working with ROS 1 Messages

Writer Example

import sys

from mcap_ros1.writer import Writer
from std_msgs.msg import String

with open(sys.argv[1], "wb") as f:
    ros_writer = Writer(f)
    for i in range(0, 10):
        ros_writer.write_message("/chatter", String(data=f"string message {i}"))
    ros_writer.finish()

Reader Example

import sys

from mcap_ros1.decoder import DecoderFactory

from mcap.reader import make_reader


def main():
    with open(sys.argv[1], "rb") as f:
        reader = make_reader(f, decoder_factories=[DecoderFactory()])
        for schema, channel, message, ros_msg in reader.iter_decoded_messages():
            print(f"{channel.topic} {schema.name} [{message.log_time}]: {ros_msg}")


if __name__ == "__main__":
    main()