Back to Blog
Migrating from ROS to ROS2: Lessons Learned

Migrating from ROS to ROS2: Lessons Learned

10 min read
ROS
Software
Best Practices

Migrating from ROS to ROS2: Lessons Learned

After successfully migrating a production robotics system from ROS1 to ROS2, here are the key insights and best practices we discovered.

Why Migrate to ROS2?

ROS2 offers several significant advantages over ROS1:

Performance Improvements

  • Real-time capabilities - Better deterministic behavior
  • Improved security - Built-in security features
  • Cross-platform support - Windows, macOS, and Linux
  • Modern C++ features - C++14/17 support

Architectural Benefits

  • Distributed architecture - Better scalability
  • Quality of Service (QoS) - Configurable communication policies
  • Lifecycle management - Better node state control
  • Parameter handling - Improved parameter system

ROS Comparison

Migration Challenges

1. API Changes

Many ROS1 APIs have changed significantly in ROS2:

Node Creation: ```cpp // ROS1 ros::NodeHandle nh;

// ROS2 rclcpp::Node::SharedPtr node = rclcpp::Node::make_shared("my_node"); ```

Publisher/Subscriber: ```cpp // ROS1 ros::Publisher pub = nh.advertise<std_msgs::String>("topic", 1000);

// ROS2 auto pub = node->create_publisher<std_msgs::msg::String>("topic", 10); ```

2. Build System Updates

ROS1 (catkin): ```bash catkin_make catkin build ```

ROS2 (colcon): ```bash colcon build colcon build --packages-select my_package ```

3. Parameter Handling

ROS2 introduces a more robust parameter system:

```cpp // ROS2 Parameter Declaration node->declare_parameter("my_param", "default_value");

// ROS2 Parameter Access std::string value = node->get_parameter("my_param").as_string(); ```

Migration Strategy

Phase 1: Assessment

  • Inventory existing packages - List all ROS1 packages
  • Identify dependencies - Map package relationships
  • Assess complexity - Determine migration effort
  • Plan timeline - Set realistic milestones

Phase 2: Preparation

  • Update development environment - Install ROS2
  • Learn new APIs - Train development team
  • Set up CI/CD - Prepare automated testing
  • Create migration tools - Develop helper scripts

Phase 3: Incremental Migration

  • Start with simple packages - Begin with basic functionality
  • Migrate dependencies first - Handle shared libraries
  • Test thoroughly - Verify functionality at each step
  • Maintain parallel development - Keep ROS1 version working

Phase 4: Testing and Validation

  • Unit testing - Test individual components
  • Integration testing - Test system interactions
  • Performance testing - Verify performance requirements
  • User acceptance testing - Validate with end users

Best Practices

Development Workflow

  1. Start with simple packages - Build confidence with easy wins
  2. Use migration tools - Leverage automated conversion tools
  3. Maintain parallel development - Keep both versions working
  4. Document changes thoroughly - Track all modifications

Code Organization

```cpp // Use modern C++ features class RobotController : public rclcpp::Node { public: RobotController() : Node("robot_controller") { // Initialize parameters this->declare_parameter("max_speed", 1.0);

    // Create publishers/subscribers
    cmd_vel_pub_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
    
    // Set up timers
    control_timer_ = this->create_wall_timer(
        std::chrono::milliseconds(100),
        std::bind(&RobotController::controlLoop, this)
    );
}

private: void controlLoop() { // Control logic here }

rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_pub_;
rclcpp::TimerBase::SharedPtr control_timer_;

}; ```

Testing Strategy

  • Unit tests - Test individual functions
  • Integration tests - Test package interactions
  • System tests - Test complete functionality
  • Performance tests - Verify timing requirements

Common Pitfalls

Memory Management

  • Shared pointers - Use smart pointers properly
  • Lifecycle management - Understand node lifecycle
  • Resource cleanup - Properly clean up resources

Timing Issues

  • Clock synchronization - Handle different time sources
  • Timer precision - Understand timer behavior
  • QoS settings - Configure appropriate QoS

Parameter Handling

  • Parameter types - Understand parameter type system
  • Default values - Always provide defaults
  • Validation - Validate parameter values

Performance Considerations

Communication

  • QoS policies - Configure appropriate QoS
  • Message serialization - Optimize message handling
  • Network configuration - Tune network settings

Real-time Performance

  • Priority scheduling - Set appropriate priorities
  • Memory allocation - Avoid dynamic allocation in real-time
  • Lock-free programming - Use lock-free data structures

Conclusion

While ROS2 migration requires significant effort, the benefits in terms of performance, security, and maintainability make it worthwhile for production systems. The key is to approach it systematically, starting with simple packages and gradually building up to more complex systems.

Key Takeaways:

  • Plan thoroughly before starting
  • Use incremental migration approach
  • Invest in testing and validation
  • Train your development team
  • Document everything

The migration process, while challenging, ultimately results in a more robust and maintainable robotics system that can take advantage of modern software engineering practices.