Working With rosbags in ROS Noetic

2020-01-07-152345

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

Example

Let’s look at an example of how to record and replay messages using rosbags.

Open a new terminal window, and launch ROS.

roscore

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.

1-screen-with-a-turtleJPG

In another terminal tab, type the following command to get a turtle to repeatedly move in a square-shaped pattern:

rosrun turtlesim draw_square
2-draw-squareJPG

Open another terminal tab, and check out the topics that are currently active:

rostopic list -v

We can see the list of published topics.

3-list-of-topicsJPG

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:

mkdir ~/bagfiles

Move inside the directory you just created.

cd ~/bagfiles

Start recording.

rosbag record -O turtle_square_sim.bag /turtle1/cmd_vel /turtle1/pose
4-recordingJPG

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:

cd ~/bagfiles
rosbag play turtle_square_sim.bag 
5-data-outputJPG

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.

6-visual-output-bagfilesJPG

That’s it for the basics of rosbags. Keep building!