Member-only story
Reading ROS Messages from a Bagfile in Python

Chances are if you have worked with ROS, you know how painful it is to write code to decode ROS messages from the recorded bag files. There are only a few tools out there that offer full-fledged functionality to read rosbag files. One such tool that comes to mind is ROS Toolbox in MATLAB from Mathworks. Prior to the 2020a version, the toolbox was a part of the Robotics System Toolbox. However, if you don’t have a license to the Matlab or to the Toolbox, in particular, it is going to be hard. With this in mind, I recently produced a python package bagpy
that enables reading large bag files of any message types. Python is open-source and comes with a rich suite of other data analysis and visualization packages.
Below, I am going to provide some code-snippets on how to use bagpy
to decode ROS Messages. For the purpose of this post, I am going to use OS1–64 v1.13 Sample Data from https://data.ouster.io/sample-data-1.13/OS1-64/index.html that contains data from a vehicle drive in San Francisco. The drive contains information about pedestrians, sidewalks, road signs, trees, and parked cars.
I will use the Jupyter notebook language here.
Install bagpy
package:
!pip install bagpy
Get the sample bag files
!wget https://data.ouster.io/sample-data-1.13/OS1-64/OS1-64_city1.bag
Read the bag file
import bagpy
from bagpy import bagreader
import pandas as pd
import seaborn as sea
import matplotlib.pyplot as plt
import numpy as np
b = bagreader('OS1-64_city1.bag')
Note that when bagreader
is load, you might see a warning Failed to load Python extension for LZ4 support. LZ4 compression will not be available
. This is due to some issues with the ROS library and it is beyond my scope to fix. However, I never had an issue because of this warning.
Check available topics
b.topic_table
Decode messages on every topic
Using the class method message_by_topic
, you can decode ROS messages of every topic type. message_by_topic
creates a CSV with Time and other information as columns in a directory of the same name as…