Important ROS Commands – ROS Bag
As described in ROS Terminologies blogs, ROS bags are used to record the data and reproduce the same environment when it is needed. Other than recording and playing the data, ROS bag commands are also used to compress and decompress the recorded data. Here we discuss ROS bag commands as follows
ROS Bag Command “rosbag”
First we check the sub-commands which can be used with the “rosbag” command.
Run following command in the terminal
rosbag -h
We can see the arguments that can be used with “rosbag” command in figure below.
The general structure of the command is as follows
rosbag <sub-command> [options] [arguments]
Now we will go through these commands one by one.
Note: The order of the commands in above list may be different from the order of the commands in our description. This is because, we will follow the order which is convenient to explain and execute the commands.
Close all terminals, and open three new terminals to run the following three commands in each terminal separately
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
“rosbag record” command
The “rosbag record” command is used to record the data for a given topic. The structure of the command is as follows
rosbag record [Option] [Topic _Name]
The topic for which we want to record the data should be running.
First we check which topics are currently being used in the ROS network
Run the command
rostopic list
we can see following topics are currently in use
/rosout /rosout_agg /turtle1/cmd_vel /turtle1/color_sensor /turtle1/pose
We can run the “rosbag record” command to start recording the data for the topic “/turtle1/cmd_vel”
Run in terminal
rosbag record /turtle1/cmd_vel
We can see that the data will start to be recorded in the file “2023-02-20-20-11-58.bag”.
The name of the file is given automatically with the extension of file as “.bag”.
Here, the name of the file is actually same as the current date and time.
However, we can specify the name of the bag file as well. Here we do
Run the command
rosbag record -O TurtleMovement.bag /turtle1/cmd_vel
In above command “-O” means, we will specify output (file name), and “TurtleMovement.bag” is the name of recorded file with extension “.bag”.
Now open the terminal window where “turtle_teleop_key” node is running. Use arrow keys to move the turtle.
The messages on the topic “turtle1/cmd_vel” will be recorded. To stop the recording, press “Ctrl+c”. The recording will be stopped and the data will be saved in the file “TurtleMovement.bag”. The recorded file will be automatically saved on the current path in the storage drive.
We can see the above three commands and their output in figure below
The picture is showing, rostopic list command, rostopic record command without name and with name.
The movement we recorded is shown in the figure below
In order to save the data for all topics, the option “-a” is used with “rasbag record” command as follows
Run the command
rosbag record -a
This will start recording data for all topics
“rosbag info” command
Using the command “rosbag info”, we can check the information of a bag file.
Ths structure of the command is as follows
rosbag info [File_Name]
We can check the information of the recently saved file.
Run the command
rosbag info TurtleMovement.bag
The information of the bag file is shown in picture below
Command to play a bag file “rosbag play”
The command “rosbag play” is used to play the recorded bag file.
The structure of the command is as follows
rosbag play [Bag File_Name]
We run the previously recorded bag “TurtleMovement.bag” file using this command
Run in terminal
rosbag play TurtleMovement.bag
We can see that the movement of the turtle is exactly the same as we recorded its movement.
However, for the same movement “turtlesim_node” should be restarted and initialized again. Because same trajectory will be obtained if starting position and orientation of the turtlebot is same.
Command to compress bag file “rosbag compress”
A bag file recorded for a small period of time will be of small size, which may not be an issue. However, if a bag file is recorded for longer duration then it will be of bigger size which will take a lot of storage. In order to avoid problem of storage, the bag file may be compressed using the command “rosbag compress”.
The structure of the command is as follows
rosbag compress [Bag File_Name]
we compress the already recorded bag file “TurtleMovement.bag”
Run in terminal
rosbag compress TurtleMovement.bag
The command has compressed the file to 12.4 KB, which was originally 44.4 KB.
At the same time, the command “rosbag compress” saved the uncompressed (original) file with the name of “TurtleMovement.orig.bag” as can be viewed in the directory.
If you play the compressed file now using “rosbag play” command, it will give the same result as it gave in case of playing uncompressed file.
Command to decompress bag file “rosbag decompress”
The command “rosbag decompress is used to decompress a compressed bag file. The command will restore the file in its original state.
The structure of the command is as follows
rosbag decompress [Bag File_Name]
we decompress the already compressed bag file “TurtleMovement.bag”
Run in terminal
rosbag decompress TurtleMovement.bag
if we delete the previously created “TurtleMovement.orig.bag” file from the directory, the command “rosbag decompress” will save original file with name “TurtleMovement.orig.bag” in compressed state, while the the file “TurtleMovement.bag” will be decompressed.
However, if both “TurtleMovement.bag” and “TurtleMovement.orig.bag” already exist at the same path. It means that both compressed and uncompressed bag files are present. The message will be displayed accordingly as show in figure below
The remaining subcommands with the command “rosbag”, which are not very important, are not frequently used. Therefore, we are skipping them. You can practice them by yourself if needed.