(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Producing filtered bag files

Description: This tutorial will cover using rosbag filter to filter bag files into new bag files using topic and data information

Keywords: rosbag record,rosbag play,rosbag filter

Tutorial Level: INTERMEDIATE

Next Tutorial: Recording and playing back data

In the previous tutorial we saw how rosbag record could be used to only record a subset of the arguments. But suppose that you or someone else has made a bag file using rosbag record -a on a running system, logging all topics to a single bag file. You would like to test one of the nodes in this system using the an input stream from the bag file. But you do not also want to replay the messages that your test node emitted at the time the recording was made. Nor do you want to inject all the other old messages from the bagfile into your system. The command rosbag filter exists to produce new bag files from old bag files using python filter expressions.

To see an example of this, either execute section 1 of the previous tutorial in order to create a bag file using turtle_teleop, or move to the directory that contains the bag file created during this tutorial. Running rosbag info on the bag file should show both the input into turtlesim (/turtle1/command_velocity) and also the outputs of the turtlesim node (/turtle1/pose and /turtle1/color_sensor). We are going to make a new bag file containing only the input to turtlesim.

The command we use to manipulate existing bag files is called rosbag filter:

Usage: rosbag filter: INBAG OUTBAG EXPRESSION

EXPRESSION can be any Python-legal expression.

The following variables are available:
 * topic: name of topic
 * m: message
 * t: time of message (t.secs, t.nsecs)

To create our new bag file we want to filter on the basis of topic name - we can see from the usage instructions that the variable "topic" can be used in the filtered expression. We want to filter the messages which have a topic name of /turtle1/command_velocity. Execute the following command:

rosbag filter <your bagfile> turtlecom.bag 'topic == "/turtle1/command_velocity"'

Now run rosbag info on turtlecom.bag. You should see a single topic for command_velocity.

The python filter functionality can extend beyond separating out messages by topic name - we can also filter based on the actual data in the message. For instance, suppose that my original teleop of turtlesim produced the following behavior:

  • jagged.png

Suppose further that we want to test a control algorithm on only left turns in turtlesim. The following command will pass turtlecom.bag through a filter that removes any references to commands with right (negative in angular velocity) values:

rosbag filter turtlecom.bag left_only.bag "m.angular >= 0"

The result if we play it through turtlesim exhibits only left turns:

  • left.png

Wiki: rosbag/Tutorials/Producing filtered bag files (last edited 2014-07-23 08:23:07 by YoshimaruTanaka)