rosbag2_storage_mcapを使う
はじめに
こんにちは。Yoshimuraです。
https://github.com/ros2/rosbag2/pull/1163でrosbag2_storage_mcapパッケージが追加され、rosbag2のストレージとしてMCAPが選択できるようになりました。この記事ではrosbag2_storage_mcapの使い方を紹介します。
動作確認環境
動作確認に用いた環境は以下の通りです。
- Ubuntu 22.04
- ROS2 Humble
- rosbag2_storage_mcap 0.6.0
MCAPとは
MCAP(https://mcap.dev/index.html)はタイムスタンプ付きの異なる種類データを記録するためのデータフォーマットで、構造化データ、非構造化データ(ROS2、Protobuf、JSON Schemaなど)の複数ストリームを1つのファイルに記録できます。MCAPのファイルフォーマット詳細はhttps://mcap.dev/specification/index.htmlを参照ください。また、ROS 2 Iron Irwiniからrosbag2のデフォルトストレージがMCAPとなる予定である旨がhttps://foxglove.dev/blog/mcap-as-the-ros2-default-bag-formatでアナウンスされています。
MCAPのメリット
https://mcap.dev/index.htmlの「Benefits」に書かれているものから抜粋したMCAPのメリットを以下に示します。
- 書き出しのパフォーマンスがよい
- 記録時にデータが破損した場合の復旧が容易
- ファイル全体をスキャンせずにデータを抽出できる
- デコードのために余分なパッケージ依存がない
rosbag2_storage_mcapインストール
以下のコマンドを実行してrosbag2_storage_mcapパッケージをインストールします。
$ sudo apt install ros-humble-rosbag2-storage-mcap
MCAP形式でrosbag2を記録する
以下のコマンドを実行して全てのトピックをMCAPで保存します。-sオプションはストレージタイプの指定です。詳細なオプションはros2 bag -hで確認ください。
$ ros2 bag record -s mcap --all
rosbag2ファイルサイズの比較(SQLite3 vs MCAP)
rosbag2_storage_mcapの場合、https://github.com/ros2/rosbag2/blob/humble/rosbag2_storage_mcap/README.md#writer-configurationにある通り、圧縮に関して以下の指定ができます。
- compression
- None、Lz4、Zstdのいずれかを指定できる
- compressionLevel
- Fastest、Fast、Default、Slow、Slowestのいずれかを指定できる
ここでは以下の条件でrosbag2ファイルサイズを確認しました。
- SQLite3(ROS2 Humbleデフォルト):無圧縮
- SQLite3(ROS2 Humbleデフォルト):Zstd圧縮
- MCAP:Zstd(fast)
- MCAP:Zstd(default)
- MCAP:Zstd(slow)
- MCAP:Lz4(fast)
各条件で生成されたrosbag2のファイルサイズを下表に示します。ただし、MCAP+Lz4(default)とMCAP+Lz4(slow)のケースはrosbag2ファイル保存時にエラーとなったため計測対象から除外しました。
RGB-Dデータ
RGB-Dデータ(詳細はAppendix参照)を使った場合の結果を下表に示します。MCAP+Zstd(slow)ではSQLite3+Zstdと同等のファイルサイズになっていることがわかります。
| storage type | compression | size(MB) |
|---|---|---|
| SQLite3 | 無圧縮 | 1359 |
| SQLite3 | Zstd | 408 |
| MCAP | Zstd(fast) | 500 |
| MCAP | Zstd(default) | 472 |
| MCAP | Zstd(slow) | 393 |
| MCAP | Lz4(fast) | 633 |
Autoware sample rosbag2
Autoware sample rosbag2(詳細はAppendix参照)を使った場合の結果を下表に示します。MCAP+Zstd(slow)ではSQLite3+Zstdと同等のファイルサイズになっていることがわかります。
| storage type | compression | size(GB) |
|---|---|---|
| SQLite3 | 無圧縮 | 7.9 |
| SQLite3 | Zstd | 2.8 |
| MCAP | Zstd(fast) | 3.1 |
| MCAP | Zstd(default) | 2.7 |
| MCAP | Zstd(slow) | 2.8 |
| MCAP | Lz4(fast) | 3.6 |
MCAP CLIツール
MCAPを扱うためのCLIツールを紹介します。https://github.com/foxglove/mcap/blob/releases/cpp/v0.6.0/go/cli/mcap/README.mdからダウンロードします。実行オプションは以下の通りです。
Usage:
mcap [command]
Available Commands:
add Add records to an existing MCAP file
cat Cat the messages in an MCAP file to stdout
completion Generate the autocompletion script for the specified shell
compress Compress data in an MCAP file
convert Convert a bag file to an MCAP file
doctor Check an MCAP file structure
filter Copy some filtered MCAP data to a new file
get Get a record from an MCAP file
help Help about any command
info Report statistics about an MCAP file
list List records of an MCAP file
merge Merge a selection of MCAP files by record timestamp
recover Recover data from a potentially corrupt MCAP file
version Output version information
Flags:
--config string Config file (default is $HOME/.mcap.yaml)
-h, --help help for mcap
Use "mcap [command] --help" for more information about a command.
ここでは以下のコマンドを紹介します。
- info
- cat
- doctor
infoコマンド
rosbag2に記録されている情報のサマリーを表示します。実行例は以下の通りです。
$ mcap info zstd_default_0.mcap
library: libmcap 0.7.0
profile: ros2
messages: 5326
duration: 47.452457991s
start: 2023-01-05T16:28:57.516754852+09:00 (1672903737.516754852)
end: 2023-01-05T16:29:44.969212843+09:00 (1672903784.969212843)
compression:
zstd: [1194/1194 chunks] [1356 MB/470 MB (65.29%)] [9.90 MB/sec]
channels:
(1) /rosout 35 msgs (0.74 Hz) : rcl_interfaces/msg/Log [ros2msg]
(2) /events/write_split 0 msgs (0.00 Hz) : rosbag2_interfaces/msg/WriteSplitEvent [ros2msg]
(3) /events/read_split 0 msgs (0.00 Hz) : rosbag2_interfaces/msg/ReadSplitEvent [ros2msg]
(4) /parameter_events 0 msgs (0.00 Hz) : rcl_interfaces/msg/ParameterEvent [ros2msg]
(5) /camera/depth/image_rect_raw 661 msgs (13.93 Hz) : sensor_msgs/msg/Image [ros2msg]
(6) /camera/aligned_depth_to_color/camera_info 661 msgs (13.93 Hz) : sensor_msgs/msg/CameraInfo [ros2msg]
(7) /camera/depth/metadata 661 msgs (13.93 Hz) : realsense2_camera_msgs/msg/Metadata [ros2msg]
(8) /camera/extrinsics/depth_to_depth 1 msgs (0.02 Hz) : realsense2_camera_msgs/msg/Extrinsics [ros2msg]
(9) /camera/aligned_depth_to_color/image_raw 661 msgs (13.93 Hz) : sensor_msgs/msg/Image [ros2msg]
(10) /camera/color/camera_info 661 msgs (13.93 Hz) : sensor_msgs/msg/CameraInfo [ros2msg]
(11) /camera/color/image_raw 661 msgs (13.93 Hz) : sensor_msgs/msg/Image [ros2msg]
(12) /camera/color/metadata 661 msgs (13.93 Hz) : realsense2_camera_msgs/msg/Metadata [ros2msg]
(13) /camera/depth/camera_info 661 msgs (13.93 Hz) : sensor_msgs/msg/CameraInfo [ros2msg]
(14) /tf_static 1 msgs (0.02 Hz) : tf2_msgs/msg/TFMessage [ros2msg]
(15) /camera/extrinsics/depth_to_color 1 msgs (0.02 Hz) : realsense2_camera_msgs/msg/Extrinsics [ros2msg]
(16) /camera/imu 0 msgs (0.00 Hz) : sensor_msgs/msg/Imu [ros2msg]
attachments: 0
metadata: 0
catコマンド
記録されているトピック内容のサマリーを標準出力に表示します。実行例は以下の通りです。
$ mcap cat zstd_default_0.mcap
1672903737516754852 /rosout [rcl_interfaces/msg/Log] [0 1 0 0 57 124 182 99 88 237]...
1672903737516870652 /rosout [rcl_interfaces/msg/Log] [0 1 0 0 57 124 182 99 108 241]...
≪中略≫
1672903741204468335 /camera/aligned_depth_to_color/camera_info [sensor_msgs/msg/CameraInfo] [0 1 0 0 61 109 182 99 0 8]...
1672903741204537835 /camera/depth/metadata [realsense2_camera_msgs/msg/Metadata] [0 1 0 0 61 109 182 99 0 8]...
1672903741204545735 /tf_static [tf2_msgs/msg/TFMessage] [0 1 0 0 4 0 0 0 28 109]...
1672903741204576835 /camera/extrinsics/depth_to_color [realsense2_camera_msgs/msg/Extrinsics] [0 1 0 0 0 0 0 192 247 255]...
≪中略≫
doctorコマンド
rosbag2ファイルが破損していないかをチェックします。実行例は以下の通りです。今回用いたデータは破損していないためエラーメッセージは出ていません。
$ mcap doctor zstd_default_0.mcap
Examining zstd_default_0.mcap
注意点
執筆時点ではrqt_bagがMCAPをサポートしていないようで、rqt_bagでrosbag2(MCAP)を開こうとすると以下のエラーメッセージが出ます。
sqlite3.DatabaseError: file is not a database
Rolling向けの対策は行われているため、今後、Humble向けにバックポートされる可能性があります。
- https://github.com/ros-visualization/rqt_bag/pull/126
- https://github.com/ros-visualization/rqt_bag/issues/121
No storage plugin found with id ’mcap’という警告メッセージ
https://github.com/ros2/rosbag2/issues/1195にて報告されています。Rolling向けだとこのメッセージは消されていますが、Humbleでどう対応するか検討中となっているようです。
今後の予定
- ベンチマーク
- MCAP API
- Python、C++などからMCAPを読み書きする
- MCAPデータの可視化
Appendix
ファイルサイズ確認に用いた入力のrosbag2ファイル
ファイルサイズ確認に用いた入力のrosbag2ファイル(SQLite3、無圧縮)は以下の通りです。
RGB-Dデータ
$ ros2 bag info rosbag2_2023_01_05-15_25_01/
Files: rosbag2_2023_01_05-15_25_01_0.db3
Bag size: 1.3 GiB
Storage id: sqlite3
Duration: 44.66s
Start: Jan 5 2023 15:25:01.719 (1672899901.719)
End: Jan 5 2023 15:25:45.786 (1672899945.786)
Messages: 5301
Topic information: Topic: /events/write_split | Type: rosbag2_interfaces/msg/WriteSplitEvent | Count: 0 | Serialization Format: cdr
Topic: /camera/imu | Type: sensor_msgs/msg/Imu | Count: 0 | Serialization Format: cdr
Topic: /camera/aligned_depth_to_color/camera_info | Type: sensor_msgs/msg/CameraInfo | Count: 661 | Serialization Format: cdr
Topic: /camera/depth/metadata | Type: realsense2_camera_msgs/msg/Metadata | Count: 661 | Serialization Format: cdr
Topic: /camera/extrinsics/depth_to_depth | Type: realsense2_camera_msgs/msg/Extrinsics | Count: 1 | Serialization Format: cdr
Topic: /camera/aligned_depth_to_color/image_raw | Type: sensor_msgs/msg/Image | Count: 661 | Serialization Format: cdr
Topic: /camera/color/camera_info | Type: sensor_msgs/msg/CameraInfo | Count: 661 | Serialization Format: cdr
Topic: /camera/color/image_raw | Type: sensor_msgs/msg/Image | Count: 661 | Serialization Format: cdr
Topic: /rosout | Type: rcl_interfaces/msg/Log | Count: 10 | Serialization Format: cdr
Topic: /camera/color/metadata | Type: realsense2_camera_msgs/msg/Metadata | Count: 661 | Serialization Format: cdr
Topic: /camera/depth/camera_info | Type: sensor_msgs/msg/CameraInfo | Count: 661 | Serialization Format: cdr
Topic: /camera/depth/image_rect_raw | Type: sensor_msgs/msg/Image | Count: 661 | Serialization Format: cdr
Topic: /parameter_events | Type: rcl_interfaces/msg/ParameterEvent | Count: 0 | Serialization Format: cdr
Topic: /tf_static | Type: tf2_msgs/msg/TFMessage | Count: 1 | Serialization Format: cdr
Autoware sample rosbag2
https://gitlab.com/autowarefoundation/autoware.ai/autoware/-/wikis/ROSBAG-Demoにあるsample_moriyama_150324.bag2を使用。
$ ros2 bag info sample_moriyama_150324.bag2/
Files: sample_moriyama_150324.bag2/sample_moriyama_150324.bag2_0.db3
Bag size: 7.9 GiB
Storage id: sqlite3
Duration: 479.172s
Start: Feb 21 2020 02:04:31.772 (1582218271.772)
End: Feb 21 2020 02:12:30.944 (1582218750.944)
Messages: 16768
Topic information: Topic: /clock | Type: rosgraph_msgs/msg/Clock | Count: 0 | Serialization Format: cdr
Topic: /nmea_sentence | Type: nmea_msgs/msg/Sentence | Count: 11980 | Serialization Format: cdr
Topic: /points_raw | Type: sensor_msgs/msg/PointCloud2 | Count: 4788 | Serialization Format: cdr
参考URL
-
https://github.com/ros2/rosbag2/pull/1160
- rollingでデフォルトストレージをMCAPにするPR
-
https://vimeo.com/showcase/9954564/video/767149733
- ROSCon 2022の動画
-
http://download.ros.org/downloads/roscon/2022/MCAP%20A%20Next-Generation%20File%20Format%20for%20ROS%20Recording.pdf
- ROSCon 2022のスライド
-
https://foxglove.dev/blog/announcing-the-mcap-storage-plugin-for-ros2
- rosbag2_storage_mcapのブログ記事
-
https://foxglove.dev/blog/mcap-as-the-ros2-default-bag-format
- ROS 2 Iron Irwiniからrosbag2のデフォルトストレージがMCAPになる旨のアナウンス
-
https://foxglove.dev/blog/plotjuggler-adds-support-for-mcap
- PlotJugglerでMCAPサポート追加した旨のブログ記事
-
https://mcap.dev/performance-comparison/rosbag2-plugin-comparison.html
- ベンチマーク結果
Discussion