A Python tool to convert ROS2 bag files to CSV format, with support for various message types including PointCloud2 data. This tool can process single bags or batch process multiple bags in a directory.
- Multiple Storage Formats: Supports both MCAP and SQLite3 bag formats
- Automatic Format Detection: Automatically detects the storage format based on file extensions
- PointCloud2 Support: Special handling for PointCloud2 messages with proper field parsing
- Batch Processing: Process multiple bag files in a directory at once
- Flexible Output: Flattens nested message structures into CSV columns
- Array Handling: Intelligent handling of arrays and nested structures
- Topic Information: View detailed information about topics and message counts
- Python 3.6+
- ROS2 (tested with ROS2 Jazzy)
- Required Python packages (see
requirements.txt)
-
Clone the repository:
git clone https://github.com/GTEC-UDC/ros2_bag_to_csv.git cd ros2_bag_to_csv -
Install dependencies:
pip install -r requirements.txt
-
Make sure ROS2 is installed and sourced:
source /opt/ros/jazzy/setup.bash # or your ROS2 distribution
python ros2_bag_to_csv.py my_bag_folder --infopython ros2_bag_to_csv.py my_bag_folder --csv /sensor/imu /sensor/cameraProcess multiple bag files in a directory:
python ros2_bag_to_csv.py --batch /path/to/bags/directory --csv /sensor/imu /sensor/camera-
Get information about a bag:
python ros2_bag_to_csv.py rosbag2_2024_01_15-10_30_45 --info
-
Convert IMU and camera topics:
python ros2_bag_to_csv.py rosbag2_2024_01_15-10_30_45 --csv /sensor/imu /sensor/camera/image_raw
-
Batch process multiple bags:
python ros2_bag_to_csv.py --batch ./experiment_data/ --csv /sensor/imu /sensor/lidar
- Timestamp: ROS2 timestamp (nanoseconds)
- Flattened Fields: Message fields are flattened using dot notation (e.g.,
header.stamp.sec) - Array Handling: Arrays are either:
- Indexed (e.g.,
position[0],position[1],position[2]) - Named (for PointField arrays using field names)
- Summarized (for large data arrays)
- Indexed (e.g.,
For an IMU message:
timestamp,header.stamp.sec,header.stamp.nanosec,header.frame_id,angular_velocity.x,angular_velocity.y,angular_velocity.z,linear_acceleration.x,linear_acceleration.y,linear_acceleration.z
1642248645123456789,1642248645,123456789,imu_link,0.01,-0.02,0.05,9.81,0.1,-0.05The tool provides special handling for PointCloud2 messages:
- Parses binary point data using field definitions
- Creates one row per point in the CSV
- Includes header information and point cloud metadata
- Handles different data types (FLOAT32, UINT8, etc.)
- Simple Arrays: Converted to indexed columns
- Complex Arrays: May create multiple CSV rows
- Large Arrays: Truncated with summary information
- PointField Arrays: Use field names instead of indices
- Single bag: Creates a folder named after the bag directory
- Batch mode: Creates a folder structure preserving bag names
- Topic names converted to valid filenames (e.g.,
/sensor/imu→sensor-imu.csv)
The tool supports any ROS2 message type, with special optimizations for:
sensor_msgs/PointCloud2sensor_msgs/Imugeometry_msgs/*std_msgs/*- Custom message types
- Graceful handling of missing topics
- Automatic storage format detection
- Informative error messages
- Continues processing other bags if one fails (in batch mode)
usage: ros2_bag_to_csv.py [-h] [--batch BATCH_DIR] (--info | --csv TOPIC [TOPIC ...]) [bag_directory]
Convert ROS2 bag files to CSV format
positional arguments:
bag_directory Path to the ROS2 bag directory
optional arguments:
-h, --help show this help message and exit
--batch BATCH_DIR Batch mode: process all bags in the specified directory
--info Show information about topics in the bag
--csv TOPIC [TOPIC ...]
Convert specified topics to CSV files
The tool automatically detects the storage format by checking for:
.mcapfiles (MCAP format).db3files (SQLite3 format)- Defaults to MCAP for newer ROS2 distributions
- Deserialization: Convert binary data to ROS2 message objects
- Dictionary Conversion: Transform to ordered dictionary structure
- Flattening: Convert nested structures to flat key-value pairs
- Array Processing: Handle arrays based on content type
- CSV Writing: Output to CSV with proper field ordering
MIT License
Copyright (c) 2025 Group of Electronic Technology and Communications. University of A Coruna.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE