Search this site
Embedded Files
No Boredom
  • Home
    • Computer Geek
    • Learning
    • Math Geek
    • Projects
No Boredom
  • Home
    • Computer Geek
    • Learning
    • Math Geek
    • Projects
  • More
    • Home
      • Computer Geek
      • Learning
      • Math Geek
      • Projects

Using rosbag

< Back to ROS Intermediate tutorials
What is rosbag?
Prerequisites
Building the message
Making the nodes
Recording messages
Getting information about a bag file
Playing Messages
Additional Notes

What is rosbag?

Official package

In order to log the messages exchanged into a file, ROS has a solution and it's called rosbag. Say you have nodes publishing messages on various topics and want to record them so that you can play them just as they are at a later time, rosbag allows you to do just that. It essentially can be made to subscribe to all the topics in the currently running session and then it can store all the messages published on those topics. Best part is that it stores all this in a single compressed file called a bag file (they have a ".bag" extension).

In this page, we'll explore how to log messages and play them using rosbag.

For this tutorial, we'll be publishing random numbers with a timestamp, record them while being sent, play them back and then verify the results.

Prerequisites

The package can be found here.

Building the message

Before we get started, we'll do the following:

  • All the work in this page is done in a separate package called "test_rosbag" (source code available on github here)
  • Create a custom message type which will send a header (type std_msgs/Header, info here) and a number (type std_msgs/Int16, info here).
  • Build the package with only the message (no source code for nodes)
    • If you want to build all of them together, then it's important to specify in the CMakeLists.txt file that the messages have to be built before nodes (so that headers are generated and no errors occur). For this, add the following line after add_executable
      • add_dependencies(rdata_pub ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
    • If you've done this (or this is already done), then you don't need to build the nodes again, you've already built the C++ nodes.

Now that we have the message that we'll be using built. We can make nodes to use that message.

Making the nodes

C++ Node codePython Node code

Now, we have to make a simple publisher using C++ or Python to publish a random number with a timestamp at some fixed frequency. The code for C++ is here and for Python is here.

  • Note that for a C++ node, you'll be required to make a few changes in the CMakeLists.txt file (final file here).
    • After modifying the CMakeLists.txt, the workspace will require a catkin_make to build the executable.
    • No need to do this, if the CMakeLists.txt already had the dependency to build messages prior to the nodes.
  • Note that for a Python node, you'll be required to make the script executable using chmod.

Recording messages

To record messages in a bag file, we use the command rosbag record. We shall see it's use in this section.

  1. Launch the roscore.
  2. Inspect messages on the topic /rnumber using rostopic echo /rnumber
  3. Now, run the command to record all data in a bag file. The command to do this is as follows
    • rosbag record -a
    • Make sure you run it in a directory where you can easily find the ".bag" file generated.
  4. Now, we can run the node and see data on the rostopic session.
    • Run the node using rosrun test_rosbag rdata_pub if you're running C++ node.
    • Run the node using rosrun test_rosbag RandomDataPublisher.py if you're running Python node.

The results are seen below.

Output of the node

Output of rosbag

You can end the recording using Ctrl + C combination. You must see a file bearing the mentioned name in the working directory of this command (yours will be different). This is the bag file.

Output of rostopic

Getting information about a bag file

To get information about a bag file, use the command rosbag info. It gives information about the file like duration, start, stop, size (in terms of storage used as well as number of messages), types of messages stored, topic names under which messages are stored and also the number of messages. The command shown below will give the information about the bag file generated by running the above record command:

  • rosbag info 2020-04-14-22-33-09.bag

The output is shown below

Output of rosbag info command

This way, we can know mode about a bag file. This can be treated as the file metadata.

Playing Messages

In order to play messages from a bag file, we use the command rosbag play. The bag file is played with messages being published to the same topics through which they were stored in the bag file. The contents of the messages are the same. If you want even the time stamps so that you could use a simulated time, then you could use the --clock parameter. This will publish the timestamps of the time when the bag file was being created.

To understand this better, do the following:

  1. Set up channels to receive data and verify, using rostopic. Run the following commands in separate terminals:
    • rostopic echo /clock
      • To get the time stamps published by rostopic. Since we've used the --clock parameter in the play command, we'll see timestamps here. You'll see that they're published at approximately 100 Hz.
    • rostopic echo /rnumber
      • Here, we expect to see the data which we had earlier published through our node made earlier.
  2. Play the contents of the bag file
    • rosbag play --clock 2020-04-14-22-33-09.bag
      • The name of your bag file will be different, use that name.

The output of the commands above can be found below

Data published to /clock topic

Output of the rosbag play command

Data published to /rnumber topic

Notice that the contents of the messages and the clock are the same as when they where while they were being recorded by rosbag.

Additional Notes

  • Official wiki of rosbag
  • rqt_bag, a GUI tool made using rqt for inspecting bag files.
  • rosbag has APIs that let you open and modify ".bag" files using your own C++ or Python programs. The code API can be found here.
  • Latest package on GitHub.
< Back to ROS Intermediate tutorials
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse