In this tutorial, we’ll learn the basics of rosbag. rosbag is a tool that enables you to record messages that are published to a ROS topic. You can also replay the messages you recorded using rosbag. The primary use cases for rosbags are testing and troubleshooting your robotics applications as well as developing new functionality.
Let’s see how to record and replay messages using rosbags. The official tutorial is here at the ROS website, but we’ll run through the steps of a basic example below.
Most Common rosbag Commands
The main component of rosbags is the bag file. A bag file is a formatted file that contains timestamped ROS messages.
The syntax for creating a bag file is as follows:
rosbag record -O filename.bag topic-names
For example, if you want to record messages that are published to the /turtle1/cmd_vel and /turtle1/pose topics, you would type this command:
rosbag record -O filename.bag /turtle1/cmd_vel /turtle1/pose
If you want to record the messages of all published topics that are currently active, you would use the following command:
rosbag record -a
You don’t want to use the command above in large, complex robotics applications because your files can get quite large, especially if your application has a vision system that publishes data from a camera.
The syntax for replaying the messages that were recorded to a bag file is as follows:
rosbag play filename.bag
If you want to inspect a bag file, the syntax for doing this is:
rosbag info filename.bag
Let’s look at an example of how to record and replay messages using rosbags.
Open a new terminal window, and launch ROS.
In another terminal tab, type the following command to launch the turtle simulation:
rosrun turtlesim turtlesim_node
You should see a screen with a turtle pop up.
In another terminal tab, type the following command to get a turtle to repeatedly move in a square-shaped pattern:
rosrun turtlesim draw_square
Open another terminal tab, and check out the topics that are currently active:
rostopic list -v
We can see the list of published topics.
Let’s record the messages that are publishing to the /turtle1/cmd_vel and /turtle1/pose topics. We’ll store these messages in a bag file.
In a new terminal tab, create a new folder:
Move inside the directory you just created.
rosbag record -O turtle_square_sim.bag /turtle1/cmd_vel /turtle1/pose
Let the system run for about a minute.
Press CTRL + C to stop recording.
Go back to the terminal where you launched the draw_square node (don’t shutdown turtlesim or the ROS Master though).
Press CTRL + C to get the turtle to stop drawing.
Now, let’s replay the messages we recorded. In a new terminal tab, type:
rosbag play turtle_square_sim.bag
You’ll see that, as expected, the messages we recorded are being replayed. The turtle’s path will not exactly replicate the original path due to differences in the timing and initial pose of the robot.
That’s it for the basics of rosbags. Keep building!