AIT-Assistive-Autonomous-Systems / ros2bag_tools

Tool extensions for ros2bag cli
122 stars 34 forks source link

Pipeline-Status

Workflow Branch Status
Build and test rosci
Lint roslint

ROS2 bag tools

ros2bag_tools adds verb extensions to the ros2bag cli.

Verb Usage
add add new topic, with messages aligned to existing topic
cut cut time slice by wall time or duration offset
drop drop X out of every Y messages of a topic
export export data to other formats, see export
extract extract topics by name
plot plot message data to a new window, see plot
process chain multiple filters, see chaining
prune remove topics without messages
reframe change frame_id on messages with headers
rename change name of a topic
replace replace messages of a specific topic with message data specified in a yaml file
restamp for all messages with headers, change the bag timestamp to their header stamp
summary print summary on data to stdout
sync output synchronized bundles of messages using the ApproximateTimeSynchronizer
video show or write video of image data

You can check detailed usage information with ros2 bag $VERB --help. Bold verbs support chaining as described in chaining.

chaining

Each command writes a new output bag on disk. If you need to chain multiple commands together, you can use ros2 bag process to process all messages in-memory and write only one output bag. To use it, create a config file that contains one line per command, with the same arguments as the cli interface.

For example create a file named process.config containing those lines:

cut --duration 10
extract -t /image_rect
restamp

and run ros2 bag process -c process.config in.bag -o out.bag to use only the first 10 seconds, keep only the /image_rect topic and restamp the remaining messages in one go.

export

Exporting is currently possible for:

Run ros2 bag export --in $BAG_PATH -t $TOPIC_NAME $EXPORTER to see available options.

synchronized export

ros2 bag export supports preprocessing of messages before exporting, using the filters described above. In addition, a list of exporters can be passed, to export messages from multiple topics simultaneously while reading the data only once.

For instance, this can be used to export synchronized images in the presence of frame drops:

# filters.config
# same syntax as described in chaining
sync -t /left/image_rect /right/image_rect
# export.config
# syntax: TOPIC EXPORTER_NAME EXPORTER_ARGS..
/left/image_rect image --dir cam_left
/right/image_rect image --dir cam_right
# write only those images in /left/image_rect and /right/image_rect with header.stamp synchronized
# by the default tolerance to the cam_left and cam_right directories respectively
ros2 bag export -f filters.config -c export.config

plot

Quickly plot timeseries from message data using matplotlib. You can specify one topic + field per time series.

Example:

# plot header stamps on x axis, range value on y
ros2 bag plot rosbag2_tools/test/range.bag -t /range.range

rosbag2_tools

This packages provides python utilities to implement the verbs, but can be used independently.

Iterate message data using BagView.

from rosbag2_tools.bag_view import BagView
for _, range_msg, _ in BagView('rosbag2_tools/test/range.bag'):
    print(range_msg.range)
# =>
# 10.0
# 20.0

Read topics as pandas data frames.

from rosbag2_tools.bag_view import BagView
from rosbag2_tools.data_frame import read_data_frames
bag_view = BagView('rosbag2_tools/test/range.bag')
dfs = read_data_frames(bag_view, {'/range': ['range']})
dfs['/range']
# =>
#    range                  header.stamp
# 0   10.0 1970-01-01 00:00:00.000000090
# 1   20.0 1970-01-01 00:00:00.000000190

Current limitations