[[rosbag]] has both [[#cpp_api|C++]] and [[#py_api|Python]] APIs for reading messages from and writing messages to bag files. See the [[rosbag/Cookbook|rosbag Cookbook]] for useful code snippets using the APIs. Note that the rosbag API's are ''not thread-safe'' for reading and writing any given bag file. Users of the APIs must ensure no concurrent input/output operations are performed on different threads. <> <> == C++ API == The `rosbag` C++ API works on the premise of creating "views" of one or more bags using "queries". A Query is an abstract class which defines a function that filters whether or not the messages from a connection are to be included. This function has access to topic_name, datatype, md5sum, message definition as well as the connection header. Additionally, each Query can specify a start and end time for the range of times it includes. Multiple queries can be added to a View, including queries from different bags. The View then provides an iterator interface across the bags, sorted based on time. For more information, see the [[http://www.ros.org/doc/api/rosbag/html/c++/|C++ Code API]]. Example usage for write: {{{#!cplusplus #include #include #include rosbag::Bag bag; bag.open("test.bag", rosbag::bagmode::Write); std_msgs::String str; str.data = std::string("foo"); std_msgs::Int32 i; i.data = 42; bag.write("chatter", ros::Time::now(), str); bag.write("numbers", ros::Time::now(), i); bag.close(); }}} Example usage for read: {{{#!cplusplus #include #include #include #include #include #define foreach BOOST_FOREACH rosbag::Bag bag; bag.open("test.bag", rosbag::bagmode::Read); std::vector topics; topics.push_back(std::string("chatter")); topics.push_back(std::string("numbers")); rosbag::View view(bag, rosbag::TopicQuery(topics)); foreach(rosbag::MessageInstance const m, view) { std_msgs::String::ConstPtr s = m.instantiate(); if (s != NULL) std::cout << s->data << std::endl; std_msgs::Int32::ConstPtr i = m.instantiate(); if (i != NULL) std::cout << i->data << std::endl; } bag.close(); }}} Another C++ example, using C++11: {{{#!cplusplus #include #include #include rosbag::Bag bag; bag.open("test.bag"); // BagMode is Read by default for(rosbag::MessageInstance const m: rosbag::View(bag)) { std_msgs::Int32::ConstPtr i = m.instantiate(); if (i != nullptr) std::cout << i->data << std::endl; } bag.close(); }}} <> == Python API == The Python API is similar, except that the "query" is specified as optional arguments to the `read_messages` function, which returns the "view" as a generator. For more information, see [[http://www.ros.org/doc/api/rosbag/html/python|Python Code API]] Example usage for write: {{{#!python import rosbag from std_msgs.msg import Int32, String bag = rosbag.Bag('test.bag', 'w') try: s = String() s.data = 'foo' i = Int32() i.data = 42 bag.write('chatter', s) bag.write('numbers', i) finally: bag.close() }}} Example usage for read: {{{#!python import rosbag bag = rosbag.Bag('test.bag') for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']): print(msg) bag.close() }}} In line 4, the loop prints all the data that consists of: * `topic`: the topic of the message * `msg`: the message * `t`: time of message. The time is represented as a [[http://wiki.ros.org/rospy|rospy]] [[http://docs.ros.org/api/rospy/html/rospy.rostime.Time-class.html|Time]] object (`t.secs`, `t.nsecs`) See the [[rosbag/Cookbook|rosbag Cookbook]] for useful code snippets using the APIs. == Using bagpy to decode rosbag files == `bagpy` provides a wrapper class `bagreader` written in python that provides an easy to use interface for reading bag files recorded by rosbag record command. This wrapper class uses ROS's python API `rosbag` internally to perform all operations. Example usage for decoding bagfile: {{{#!python import bagpy from bagpy import bagreader import pandas as pd b = bagreader('test.bag') # replace the topic name as per your need LASER_MSG = b.message_by_topic('/vehicle/front_laser_points') LASER_MSG df_laser = pd.read_csv(LASER_MSG) df_laser # prints laser data in the form of pandas dataframe }}} Link to the `bagpy` repository: https://github.com/jmscslgroup/bagpy See this [[https://rahulbhadani.medium.com/reading-ros-messages-from-a-bagfile-in-python-b006538bb520|post]] for an extended tutorial.