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.
The package can be found here.
Before we get started, we'll do the following:
add_executable
add_dependencies(rdata_pub ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
Now that we have the message that we'll be using built. We can make nodes to use that message.
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.
catkin_make
to build the executable.chmod
.To record messages in a bag file, we use the command rosbag record
. We shall see it's use in this section.
roscore
.rostopic echo /rnumber
rosbag record -a
rosrun test_rosbag rdata_pub
if you're running C++ node.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
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.
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:
rostopic echo /clock
rostopic echo /rnumber
rosbag play --clock 2020-04-14-22-33-09.bag
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
.