ROS 2 Lyrical Luth · Lesson 64 of 86
Obstacle avoidance
Costmap obstacle layers, the local planner's role and the collision monitor.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain obstacle avoidance 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
Obstacle avoidance happens at several levels: the global planner avoids known obstacles; the local costmap's obstacle layer marks new ones from the lidar or depth camera; the controller steers around them; and the collision monitor is a last safety layer that slows or stops the robot if something enters a defined area, independent of the planner.
The stopping distance is v² / (2 a): at 0.5 m/s with a deceleration of 0.5 m/s² the robot needs 0.25 m to stop. Make safety zones larger than that plus a margin for latency. Test with people-sized and low obstacles that the sensors may not see.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the obstacle avoidance 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
obstacle_layer:
# Costmap layer that marks obstacles.
plugin: "nav2_costmap_2d::ObstacleLayer"
# The layer implementation.
observation_sources: scan
# Which sensors feed it.
scan:
# Settings for that source.
topic: /scan
# Lidar topic.
data_type: "LaserScan"
# Message type.
marking: true
# Mark obstacles the sensor sees.
clearing: true
# Clear cells where the ray passes through.
obstacle_max_range: 2.5
# Ignore obstacles farther than 2.5 m.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 Obstacle avoidance. Expected result: stop_dist=0.25 safe=false
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 <iostream>
int main() {
const double v = 0.5, a = 0.5, obstacle_distance = 0.2;
// TODO: stopping distance = v*v / (2*a); the robot is safe only if the obstacle is farther than that
std::cout << "stop_dist=" << stop_dist << " safe=" << std::boolalpha << safe << '\n';
return 0;
}Expected output
stop_dist=0.25 safe=falseStuck? Show the full solution (try it yourself first)
#include <iostream>
int main() {
const double v = 0.5, a = 0.5, obstacle_distance = 0.2;
const double stop_dist = v * v / (2.0 * a);
const bool safe = obstacle_distance > stop_dist;
std::cout << "stop_dist=" << stop_dist << " safe=" << std::boolalpha << safe << '\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
Place a box in the robot's path after navigation starts and observe how the local costmap and controller react. Compute the stopping distance for your robot's speed and deceleration and compare it with the lidar range.
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.