Planners and controllers cannot read a raw camera frame or LiDAR scan directly. Nav2 translates maps and live sensor readings into a unified grid system called a Costmap. Every cell in this 2D grid is assigned a value from 0 (totally free) to 254 (lethal obstacle).

The Two Core Costmaps

Nav2 maintains two completely distinct costmaps to serve the Planners and Controllers:

  1. Global Costmap

    • this is used by the Planner Server
    • It covers the full extent of the known environment map
    • It is static and locked to the world coordinate origin, used for long-term path planning around permanent structures
  2. Local Costmap

    • It is used by the Controller Server
    • It is a small "rolling window" grid (e.g., 3m x 3m) centered on the robot
    • It moves along with the robot as it drives, used for fast, real-time reactive obstacle avoidance

The Layered Costmap

Both the global and local costmaps are built by stacking separate data layers on top of each other:

  1. Static Layer reads the occupancy grid file (.yaml/.pgm) previously saved by the previous SLAM workflow to establish the unchanging architecture of the space.
  2. Obstacle/Voxel Layer: Takes live topics like /scan (LiDAR) or depth data to instantly mark cells where obstacles are currently detected, and clears them using Raycasting once they disappear.
  3. Inflation Layer: Expands lethal obstacle cells outward by a configurable safety radius. It introduces a gradient of decreasing cost moving away from walls. This prevents the planner from scraping right against corners, ensuring a safe buffer zone.