Navigazione waypoint ROS2 con ripianificazione dinamica tramite Nav2

In this tutorial, you’ll learn how to implement waypoint-based navigation in ROS2 using the Nav2 stack, enhanced with dynamic replanning capabilities. This setup enables your robot to follow a sequence of predefined goals and recalculate the path if unexpected obstacles appear. It’s ideal for autonomous patrol or area coverage in indoor environments. 

 📝 Requirements 

  • – ROS2 Humble or Foxy installed 
    – Nav2 stack (`nav2_bringup`) configured 
    – A robot simulation (e.g., TurtleBot3 in Gazebo) 
    – Pre-built map and AMCL localization 
    RViz installed for interaction 

Step 1: Define Waypoints 

Create a list of waypoints in a YAML file with the format expected by `FollowWaypoints` behavior. Each waypoint should include a position (x, y) and orientation (quaternion z, w). 

Example: waypoints.yaml 

– poses: 
  – position: 
      x: 2.0 
      y: 1.0 
    orientation: 
      z: 0.0 
      w: 1.0 
  – position: 
      x: 0.5 
      y: 3.0 
    orientation: 
      z: 0.0 
      w: 1.0 

🖥️Step 2: Launch Nav2 and Load Waypoints 

Launch your standard Nav2 bringup with the robot localized on a known map. Then use a script or custom node to load the YAML and publish goals sequentially to `FollowWaypoints` action server. 

d635522c-38a7-48a0-97c7-42ac8d25b61b

Step 3: Enable Dynamic Replanning 

To ensure the robot adapts to unexpected obstacles, activate recovery and planner plugins. Make sure your `planner_server` has recovery behaviors like `Spin`, `ClearCostmap`, and the controller allows replanning. 

 

 In your behavior tree (bt_navigator.xml): 

 

<RecoveryNode name=”NavigateRecovery”> 
  <ClearEntireCostmap/> 
  <Spin recovery_spin_1/> 
  <BackUp recovery_backup/> 
</RecoveryNode> 

🛠️Step 4: Test the Patrol Behavior 

Place a dynamic obstacle (e.g., a box) in the robot’s path during navigation. Nav2 should replan or attempt a recovery behavior and continue toward the next waypoint. Observe transitions in RViz and logs. 

🛠️Step 5: Troubleshooting 

– Waypoints not loading:

check YAML structure and action interface. 
– Robot doesn’t replan: verify recovery plugins are configured and active. 
– Navigation freezes:

look at BT logs and verify conditions for fallback nodes. 
– Path too close to obstacle:

adjust inflation and costmap parameters. 

Want to learn more?

You can find a detailed explaination of the autonomous navigation in the "Self Driving and ROS - Learn by Doing! Odometry and Control" course
DISCOUNT
en_US