ROS 2 Lyrical Luth · Lesson 58 of 86
Costmaps: global and local
Layers, inflation, robot footprint and cost values.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain costmaps: global and local in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A costmap turns the map and live sensors into a grid of costs (0 free … 254 lethal, 255 unknown). The global costmap covers the whole map for long-range planning; the local costmap is a small window that moves with the robot for obstacle avoidance. Each is built from layers: static, obstacle (from lidar or depth) and inflation.
The inflation layer makes cells near obstacles expensive so paths keep a safe distance: cost = 252 · exp(−k · (distance − inscribed_radius)) for distances beyond the inscribed radius. The robot's footprint or radius, the resolution and inflation radius are the most important settings.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the costmaps: global and local example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
local_costmap:
# The small, moving window used by the controller.
local_costmap:
# Node section.
ros__parameters:
# ROS parameters.
update_frequency: 5.0
# Refresh five times per second.
rolling_window: true
# The window follows the robot.
width: 3
# Three metres wide.
height: 3
# Three metres high.
resolution: 0.05
# 5 cm cells.
robot_radius: 0.22
# Treat the robot as a circle of 22 cm radius.
plugins: ["obstacle_layer", "inflation_layer"]
# Layers combined into this costmap.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · Mobile robot
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Costmaps: global and local. Expected result: 102
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <cmath>
#include <iostream>
int main() {
const double inscribed_radius = 0.2, cost_scaling_factor = 3.0, distance = 0.5;
// TODO: Nav2 inflation cost: 252 * exp(-cost_scaling_factor * (distance - inscribed_radius)), truncated to an integer
std::cout << cost << '\n';
return 0;
}Expected output
102Stuck? Show the full solution (try it yourself first)
#include <cmath>
#include <iostream>
int main() {
const double inscribed_radius = 0.2, cost_scaling_factor = 3.0, distance = 0.5;
const int cost = static_cast<int>(252.0 * std::exp(-cost_scaling_factor * (distance - inscribed_radius)));
std::cout << cost << '\n';
return 0;
}Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Configure global and local costmaps for a 0.22 m radius robot, view both in RViz2, change the inflation radius from 0.3 to 0.8 m, and describe how the planned path changes.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.