OpenCV Gazebo ROS 1 to ROS 2 Migration

ROS 1 to ROS 2 Migration: OpenCV with Gazebo
This is a documentation of the successful migration of the pkg_cv_ros_tutorial_by_dhanuzch repository from ROS 1 to ROS 2 (Foxy and newer), following the official ROS 2 Migration Guide.
This repository was originally a ROS 1 package used in the “Using OpenCV with Gazebo in ROS” tutorial series by @dhanuzch. It has now been ported and is available at:
GitHub Repository: pkg_cv_ros_tutorial_by_dhanuzch
The migration includes updating package manifests, the build system (ament_cmake), Python source code for nodes, and transitioning ROS 1 XML launch files to ROS 2 Python launch files.
Proposed Changes
Phase 1: Package Manifest and Build System
Ensure the package’s foundation is fully ROS 2 compliant. Update dependencies and build configurations to use ament_cmake.
package.xml
- Changed
<package format="2">to at least format 3 for ROS 2 compatibility. - Replaced
<buildtool_depend>catkin</buildtool_depend>with<buildtool_depend>ament_cmake</buildtool_depend>. - Replaced occurrences of
rospywithrclpy. - Added
sensor_msgsandcv_bridgeas<exec_depend>. - Added
<export><build_type>ament_cmake</build_type></export>.
CMakeLists.txt
- Updated
cmake_minimum_requiredtoVERSION 3.8. - Replaced
find_package(catkin ...)withfind_package(ament_cmake REQUIRED). - Used
install(PROGRAMS ...)to install the Python scripts intolib/${PROJECT_NAME}so they can be executed natively. - Replaced
catkin_package()withament_package().
Phase 2: Python Node Migration
Migrate the ROS 1 specific Python code (rospy) to the ROS 2 API (rclpy).
scripts/camera_read.py & scripts/decode_qr.py
- Changed
import rospytoimport rclpyand includedfrom rclpy.node import Node. - Ensured script node classes inherit natively from
Nodeand updated constructors viasuper().__init__(). - Replaced
rospy.Subscriberwithself.create_subscription(). - Built modern logging (
self.get_logger().info()). - Updated
main()execution blocks to leveragerclpy.spin().
Phase 3: Launch Files and Testing
Recreate the environment execution flow using ROS 2 Python launch files and conduct final system integration and testing.
launch/1_world_and_script.launch.py
- Created a modern ROS 2 Python launch script importing logic to extend
GAZEBO_MODEL_PATH. - Added
Nodeexecution actions sequentially alongside simulation boots.
launch/2_world_and_script.launch.py
- Converted the legacy target to spawn the QR-decoding process seamlessly via concurrent
ExecuteProcessdependencies!
Verification Plan
Automated/Compilation Tests
- Run
colcon build --symlink-installfrom the workspace root. - Ensure the package compiles cleanly without CMake or ament errors.
- Verify that
source install/setup.bashmakes the package correctly visible toros2 pkg list.
System Integration & Manual Verification
To manually test:
- Initiate the simulation using
ros2 launch pkg_cv_ros_tutorial_by_dhanuzch 1_world_and_script.launch.py. - Confirm that the
cv2.imshowwindow appears and natively resolves drone camera outputs. - Test
2_world_and_script.launch.pyto confirm QR decoding highlights!