# Convert ROS1 bag to ROS2 bag

For rosbag files recorded in ROS1 environment, we cannot use them directly in ROS2 environment. There are some discussions on workarounds. In summary, there are two ways, as summarized in [this article](https://docs.ros.org/en/noetic/api/ov_core/html/dev-ros1-to-ros2.html).

## Solution 1: Install `rosbag_v2` plugin and `ros1_bridge`

* Requirement: Ubuntu 20 or below, where we have both ROS1 and ROS2 available.
* Follow the instructions in [ros1\_bridge](https://github.com/ros2/ros1_bridge) and [rosbag\_v2](https://github.com/ros2/rosbag2_bag_v2) to install.

```
sudo apt-get install ros-$ROS2_DISTRO-ros2bag ros-$ROS2_DISTRO-rosbag2*
sudo apt install ros-$ROS2_DISTRO-rosbag2-bag-v2-plugins
```

* We can then playback ros1 bag files in a ros2 environment.

```
source_ros1
source_ros2
ros2 bag play -s rosbag_v2 V1_01_easy.bag
```

Additional comments: For Ubuntu 22 or above environment, according to [ros2 official docs](https://docs.ros.org/en/humble/How-To-Guides/Using-ros1_bridge-Jammy-upstream.html), it may be possible to install ros1\_bridge alongside ros2, if ros2 is installed from source. In practice, I did not run through this successfully and got stuck at the `ros-core-dev : Depends: catkin but it is not installable` error.

References:

* <https://robotics.stackexchange.com/questions/101945/playback-ros1-bag-files-in-ros2>
* <https://answers.ros.org/question/402302/playback-ros1-bag-files-in-ros2/>
* <https://docs.ros.org/en/humble/How-To-Guides/Using-ros1_bridge-Jammy-upstream.html>

## Solution 2: Use `rosbags` to convert bag files (Recommended)

* This is a python package that does not depend on ros environment. We can convert a ros1 bag file into ros2 bag by running the following commands.

```
pip3 install rosbags>=0.9.11
rosbags-convert --src V1_01_easy.bag --dst <ros2_bag_folder>
```

References:

* <https://gitlab.com/ternaris/rosbags>
* <https://docs.ros.org/en/noetic/api/ov_core/html/dev-ros1-to-ros2.html>
