Avoiding obstacles
Use the distance sensor to wander around a room full of obstacles without touching any. Learn simple avoidance rules, why robots get stuck, and how to test fairly.
In 60 seconds
Avoiding obstacles
Use the distance sensor to wander around a room full of obstacles without touching any. Learn simple avoidance rules, why robots get stuck, and how to test fairly.
The basic rule
- 1
Look
Read the distance straight ahead. - 2
Far enough?
If nothing is closer than a safe distance (for example 30 cm), drive forward. - 3
Too close?
Stop driving forward, and turn on the spot until the path ahead is clear. - 4
Repeat
Check again every step.
Choosing the safe distance
| Safe distance | What happens |
|---|---|
| Too small (for example 8 cm) | The robot starts turning when it is already touching the obstacle |
| About right | The robot turns away with room to spare |
| Too large (for example 90 cm) | The robot turns all the time and never explores narrow spaces |
Ways robots get stuck
- Always turning the same way can trap the robot in a corner, turning back and forth between two walls. Some robots pick a random direction, or turn the other way if the first turn did not work.
- The sensor only looks ahead. While turning, the side of the robot can scrape an obstacle the sensor never saw. The cone of the sensor helps a little; side sensors help more.
- Spinning forever. If the robot turns on the spot whenever anything is within the safe distance, and it is in a very tight space, it may spin without ever moving. Measure progress, not just "no crash".
Testing fairly
- One start, one run
- Only checks "did not crash"
- Obstacles moved until it works
- Several starts and headings
- Checks no crash and distance travelled
- Same obstacle layout for every version of the code
Watch and read more
Lab
An obstacle-avoiding robot that explores a cluttered room for 500 steps from several starts, never crashes, and keeps moving.
The problem
This runs in your browser. The room has five round obstacles (red). The robot starts near a corner.
- Run the starter. The robot drives straight into an obstacle.
- Change
decideso that whens["distance"]is below a safe distance, the robot turns on the spot, and otherwise it drives forward. - Goal: for 500 steps, no crash, and the robot travels at least 300 cm. The check tries four different starts.
- Try safe distances of 10, 30 and 80 cm. Write down what happens with each.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Your robot passes all four starts. Someone moves one obstacle 10 cm and it crashes. What does this tell you, and what would you do?Reveal
The rules were tuned to one layout and are fragile. Test on many layouts, including new ones you did not tune for, before you trust the code. Then make the rules more robust: a bigger safe distance, backing up before turning, or turning away from the side where the obstacle is closer, if you add side sensors.
Q2Why is "survived 500 steps without crashing" not enough to call a real robot safe?Reveal
The test only covers the situations it tried. Real rooms have moving people, pets, stairs, thin legs and soft objects that the sensor may not see. Safety needs slow speeds, a quick way to cut power, supervision, and testing in the real place, not only in a simulator.
Questions people ask
Is reactive avoidance enough for a real delivery robot?
No. Real delivery and warehouse robots also use maps, planned routes, several kinds of sensors and strict safety systems. Reactive rules are a good first layer and a good way to learn, but they cannot plan a route to a goal on their own.
Should the robot back up before turning?
If the obstacle is very close, backing up a little first gives room to turn without scraping it. Many simple robots do "stop, back up briefly, turn, go". Add it if your robot gets too close before it reacts.
Why measure distance travelled?
Because "no crash" alone is easy to fake: a robot that just spins on the spot never crashes. Distance travelled shows the robot is actually exploring.
What about obstacles that move, like people?
Moving obstacles are much harder. The robot must react faster and leave bigger gaps. Small hobby robots should move slowly around people, and never be used where they could trip someone.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.