1"use strict";(globalThis.webpackChunkwebsite||=[]).push([[447],{9145(e,n,r){r.r(n),r.d(n,{assets:()=>d,contentTitle:()=>o,default:()=>h,frontMatter:()=>a,metadata:()=>s,toc:()=>c});const s=JSON.parse('{"id":"guides/python/ros1","title":"Reading and writing ROS 1","description":"Read and write MCAP files with ROS 1 message data in Python.","source":"@site/docs/guides/python/ros1.md","sourceDirName":"guides/python","slug":"/guides/python/ros1","permalink":"/guides/python/ros1","draft":false,"unlisted":false,"editUrl":"https://github.com/foxglove/mcap/tree/main/website/docs/guides/python/ros1.md","tags":[],"version":"current","frontMatter":{"description":"Read and write MCAP files with ROS 1 message data in Python."},"sidebar":"guidesSidebar","previous":{"title":"Reading and writing Protobuf","permalink":"/guides/python/protobuf"},"next":{"title":"Reading and writing ROS 2","permalink":"/guides/python/ros2"}}');var i=r(2540),t=r(3023);const a={description:"Read and write MCAP files with ROS 1 message data in Python."},o="Reading and writing ROS 1",d={},c=[{value:"Reading",id:"reading",level:2},{value:"Writing",id:"writing",level:2},{value:"Important links",id:"important-links",level:2}];function l(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,t.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"reading-and-writing-ros-1",children:"Reading and writing ROS 1"})}),"\n",(0,i.jsxs)(n.p,{children:["To start writing Python code that reads and writes ROS 1 data in MCAP, install the ",(0,i.jsxs)(n.a,{href:"https://github.com/foxglove/mcap/tree/main/python/mcap-ros1-support",children:[(0,i.jsx)(n.code,{children:"mcap-ros1-support"})," helper library"]}),"."]}),"\n",(0,i.jsx)(n.h2,{id:"reading",children:"Reading"}),"\n",(0,i.jsxs)(n.p,{children:["Read ROS1 messages from an MCAP file using the ",(0,i.jsx)(n.code,{children:"mcap_ros1.reader"})," module:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'import sys\n\nfrom mcap_ros1.reader import read_ros1_messages\n\nfor msg in read_ros1_messages(sys.argv[1]):\n print(f"{msg.topic}: {msg.ros_msg}")\n'})}),"\n",(0,i.jsx)(n.h2,{id:"writing",children:"Writing"}),"\n",(0,i.jsxs)(n.p,{children:["Import the necessary packages from Python and a ",(0,i.jsx)(n.code,{children:"Writer"})," from ",(0,i.jsx)(n.a,{href:"https://github.com/foxglove/mcap/tree/main/python/mcap-ros1-support",children:(0,i.jsx)(n.code,{children:"mcap-ros1-support"})}),":"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:"import sys\nfrom std_msgs.msg import String\nfrom mcap_ros1.writer import Writer\n"})}),"\n",(0,i.jsx)(n.p,{children:"Open a file and create a writer:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:'with open(sys.argv[1], "wb") as f:\n ros_writer = Writer(f)\n'})}),"\n",(0,i.jsxs)(n.p,{children:['Finally, specify the channel ("/chatter") and its schema (',(0,i.jsx)(n.code,{children:"String"}),') you will be using before publishing your messages ("string message 0", "string message 1", "string message 2", etc.):']}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-python",children:' for i in range(0, 10):\n ros_writer.write_message("/chatter", String(data=f"string message {i}"))\n ros_writer.finish()\n'})}),"\n",(0,i.jsx)(n.h2,{id:"important-links",children:"Important links"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"https://github.com/foxglove/mcap/tree/main/python/mcap",children:"MCAP Python library"})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsxs)(n.a,{href:"https://github.com/foxglove/mcap/tree/main/python/mcap-ros1-support",children:[(0,i.jsx)(n.code,{children:"mcap-ros1-support"})," helper library"]})}),"\n",(0,i.jsx)(n.li,{children:(0,i.jsx)(n.a,{href:"https://github.com/foxglove/mcap/tree/main/python/examples/ros1",children:"Example code"})}),"\n"]})]})}function h(e={}){const{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(l,{...e})}):l(e)}},3023(e,n,r){r.d(n,{R:()=>a,x:()=>o});var s=r(3696);const i={},t=s.createContext(i);function a(e){const n=s.useContext(t);return s.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),s.createElement(t.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.