Navigation & Path Planning

Week 11 • CMPSC 304 Robotic Agents

Press Space or to advance

From Mapping to Navigation

Wednesday
SLAM & Mapping
Today
Navigation
P4 Parts 1-2
Map & Navigate

Wednesday: SLAM

  • Built maps from lidar data
  • Occupancy grids, scan matching
  • Output: a map file

Today: Navigation

  • Use the map to navigate
  • Localization, path planning, obstacle avoidance
  • Output: autonomous movement

The Navigation Problem

Given: a map and a goal position
Find: a safe path from the robot's current position to the goal
Execute: follow the path while avoiding obstacles
  • Where am I? → Localization
  • Where do I want to go? → Goal specification
  • How do I get there? → Path planning
  • How do I avoid obstacles? → Local planning / obstacle avoidance

Localization: Where Am I?

AMCL (Adaptive Monte Carlo Localization) is a particle filter that estimates the robot's position on a known map.
  • Particles = many guesses of where the robot might be
  • Each particle is a possible (x, y, θ) pose
  • Compare each particle's expected lidar scan to the actual scan
  • Particles that match well survive; poor matches are eliminated
  • Over time, particles converge on the true position
In RViz: You'll see a cloud of green arrows around the robot. Initially spread out, they converge as the robot moves and gets more sensor data.

Setting the Initial Pose

Why you must set the initial pose: AMCL doesn't know where the robot starts. You give it a hint by clicking in RViz.
  1. Click the "2D Pose Estimate" button in the RViz toolbar
  2. Click on the map where the robot is
  3. Drag to set the direction the robot is facing
  4. The particle cloud should jump to that location and then converge
What if you get it wrong? AMCL may still converge to the correct position as the robot moves and gets more scan data — but it takes longer. Set it as accurately as you can.

Costmaps: The Planner's View

A costmap is a grid where each cell has a cost — how expensive it is for the robot to be there.
CostMeaningVisual
0Free space — safe to traverseNo color
1–252Increasing cost (near obstacles)Blue → red gradient
253Inscribed — robot center would hit obstacleCyan ring
254Lethal — occupied cellPurple/pink
255UnknownNo data
Inflation layer: Obstacles are "inflated" — expanded by the robot's radius — so the planner can treat the robot as a point. A cell 30 cm from a wall gets a higher cost than one 1 m away.

Global vs. Local Costmap

Global Costmap

  • Covers the entire map
  • Based on the static map you saved
  • Updated slowly (mostly static)
  • Used for long-range planning
  • Finds the overall route

Local Costmap

  • Small area around the robot
  • Updated in real-time with lidar
  • Includes dynamic obstacles
  • Used for immediate decisions
  • Adjusts the path on the fly
Why two costmaps? The global costmap gives the "big picture" route. The local costmap handles the messy reality of moment-to-moment driving.

Path Planning: Global Planner

The global planner finds a path from the robot's current position to the goal on the global costmap.
  • Nav2 uses NavFn (navigation function) by default — a variant of Dijkstra's algorithm
  • Treats the costmap as a graph where each cell is a node
  • Edge weights = cell costs
  • Finds the lowest-cost path (not just shortest distance — avoids getting close to obstacles)
In RViz: The global plan appears as a thin line from robot to goal. It may curve around obstacles and stay away from walls due to the inflation layer.

Path Following: Local Planner

The local planner (controller) generates velocity commands to follow the global path while reacting to real-time sensor data.
  • Nav2 uses DWB (Dynamic Window Based) controller by default
  • Simulates many possible velocity commands forward in time
  • Scores each trajectory: does it follow the path? avoid obstacles? reach the goal?
  • Picks the best-scoring trajectory and sends velocity commands
Global
Path
Local
Planner
/cmd_vel
Commands
Robot
Moves

Nav2: The Full Picture

Nav2 (Navigation 2) is the ROS2 navigation framework that combines all these components.
ComponentRoleROS2 Node
Map ServerLoads and serves the static mapmap_server
AMCLLocalizes robot on the mapamcl
Global CostmapFull-map cost gridglobal_costmap
Local CostmapReal-time local cost gridlocal_costmap
Planner ServerGlobal path planningplanner_server
Controller ServerLocal path followingcontroller_server
BT NavigatorCoordinates everythingbt_navigator

Sending Navigation Goals

You can send goals two ways:

RViz (Manual)

  1. Click "Nav2 Goal" button
  2. Click where you want to go
  3. Drag to set orientation
  4. Robot plans and drives

Great for testing and exploration

Code (Programmatic)

  • Use NavigateToPose action
  • Send goals from a Python node
  • Chain multiple waypoints
  • React to results automatically

Required for P4 Part 3

What Happens When Things Go Wrong?

Nav2 has built-in recovery behaviors for when the robot gets stuck.
  • Clear costmap: Reset the costmap to remove ghost obstacles from old sensor data
  • Spin: Rotate in place to get new sensor readings
  • Back up: Drive backwards to recover from a tight space
  • Wait: Pause and hope a dynamic obstacle moves
If all recovery behaviors fail: Nav2 aborts the goal and reports failure. Your code needs to handle this — goals don't always succeed! This is very common with physical robots.

Using Nav2 with TurtleBot3

# Terminal 1: Launch Gazebo
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py

# Terminal 2: Launch Nav2 with your map
ros2 launch turtlebot3_navigation2 navigation2.launch.py map:=$HOME/map.yaml
  1. Wait for Nav2 to fully start (watch terminal for "Creating bond timer")
  2. Set initial pose with "2D Pose Estimate" in RViz
  3. Send a goal with "Nav2 Goal" in RViz
  4. Watch the robot plan and navigate!
Important: You must use the map you built in the SLAM exercise. Nav2 needs a pre-built map — it does not build one itself.

Textbook Connection: IAR Chapter 14

  • Section 14.1: The path planning problem — configuration space, free space, obstacles
  • Section 14.2–14.3: Graph-based planners — Dijkstra, A*, which Nav2's planner builds on
  • Section 14.5: Sensor-based planning — the local planner's job, reacting in real-time
Key insight: Path planning balances optimality (find the best path) with feasibility (can the robot actually execute it?) and reactivity (can it handle surprises?). Nav2 uses a layered approach: optimal global plan + reactive local controller.

Summary

ConceptWhat It Does
AMCLParticle filter — localizes robot on a known map
CostmapGrid with obstacle costs + inflation for safe planning
Global PlannerFinds optimal path on the full map (Dijkstra/A*)
Local PlannerFollows path reactively, avoids dynamic obstacles
Nav2Framework that combines all navigation components
RecoveryBuilt-in behaviors for when the robot gets stuck
Now let's navigate! → Activity 7