Build a mini robotBeginnerLesson 2675 min read

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.

Lesson in motion

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.

1/6
In simple words
Think of a goat walking through a village lane. It walks forward until something is in the way: a bicycle, a cart, a person. Then it turns and tries another way. It does not need a map of the whole village. A simple obstacle-avoiding robot works the same way: go forward, and when something is close, turn.
Robot vacuum cleaners, toy robots and many student robots use reactive obstacle avoidance. They do not plan a full route. They react to what the sensors say right now. This is simple and often good enough to explore a room without crashing.

The basic rule

  1. 1

    Look

    Read the distance straight ahead.
  2. 2

    Far enough?

    If nothing is closer than a safe distance (for example 30 cm), drive forward.
  3. 3

    Too close?

    Stop driving forward, and turn on the spot until the path ahead is clear.
  4. 4

    Repeat

    Check again every step.

Choosing the safe distance

The safe distance must be bigger than the distance the robot needs to stop and turn. It depends on speed, the size of the robot and how fast the sensor reacts. In our simulator the distance is measured from the middle of the robot, which is 5 cm from its edge, so a safe distance of 30 cm leaves a gap of about 25 cm.
Safe distanceWhat happens
Too small (for example 8 cm)The robot starts turning when it is already touching the obstacle
About rightThe 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

A robot that works from one starting point may be lucky. Test from several starting points and headings, and measure more than one thing: did it crash, how long did it survive, and how far did it actually travel? A robot that stands still never crashes, but it is not useful.
Weak test
  • One start, one run
  • Only checks "did not crash"
  • Obstacles moved until it works
Stronger test
  • Several starts and headings
  • Checks no crash and distance travelled
  • Same obstacle layout for every version of the code
Danger
A real robot that fails to stop can hit feet, pets or table legs, or drive off a table or down stairs. Test on the floor, in a clear area, at low speed, with an adult nearby for younger learners. Keep long hair, loose sleeves and fingers away from spinning wheels. Keep a way to switch off power quickly.
Try this. In the lab, change the turn to always go left, then always right. Does the robot get stuck in a different place? What does that tell you about testing from only one start?

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.

~25 min

The problem

This runs in your browser. The room has five round obstacles (red). The robot starts near a corner.

  1. Run the starter. The robot drives straight into an obstacle.
  2. Change decide so that when s["distance"] is below a safe distance, the robot turns on the spot, and otherwise it drives forward.
  3. Goal: for 500 steps, no crash, and the robot travels at least 300 cm. The check tries four different starts.
  4. 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.

Output

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.

Please sign in to continue.

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.

Sign in to take the test and save your progress