The distance sensor

distance() and loops that react to it.

2.2SensingRobot club15 min

Do this lesson in the simulator

So far the robot has driven blind. Now it looks ahead. On its front is a time-of-flight sensor: it fires invisible light and times the reflection. distance() tells you how far it is to the nearest thing straight ahead.

distance()

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("ahead:", distance(), "cm")
# drive forward at 60 for 20 cm, then stop
forward(60, distance=20)
print("ahead now:", distance(), "cm")

Run this in the simulator

There is a wall ahead in this scene, so the first reading is the distance to it. Drive 20 and it drops by about 20. On an empty mat the far edge counts as a wall.

A loop that reacts

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# as long as the thing ahead is further than 30 cm
while distance() > 30:
    # drive forward at 40 (keeps going until the next command)
    forward(40)
    # pause 0.1 s (the robot keeps doing what it was told)
    wait(0.1)
# all motors off
stop()
print("stopped", distance(), "cm from the wall")

Run this in the simulator

The while asks the sensor before every repeat. Nothing in the program knows how far the wall is; the robot finds out by looking. That is the difference between a program and a robot.

Two details matter. wait(0.1) gives the robot time to move before the next look and keeps the safety timer fed. And the robot coasts after stop(), so at speed 40 it stops a little closer than 30 cm.

Slowing down as you get close

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# as long as the thing ahead is further than 15 cm
while distance() > 15:
    if distance() > 40:
        # drive forward at 90 (keeps going until the next command)
        forward(90)
    else:
        # drive forward at 30 (keeps going until the next command)
        forward(30)
    # pause 0.1 s (the robot keeps doing what it was told)
    wait(0.1)
# all motors off
stop()
print("stopped", distance(), "cm from the wall")

Run this in the simulator

Fast while far, slow when close: the trick from lesson 1.5, driven by the sensor instead of by distance.

Following the wall's distance

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# do this 5 times (i counts from 0)
for i in range(5):
    print(f"step {i}: wall in {distance()} cm")
    # drive forward at 50 for 8 cm, then stop
    forward(50, distance=8)

Run this in the simulator

Task: stop before the wall

Drive up to the wall and stop in the green band in front of it, without touching it.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

# as long as the thing ahead is further than 30 cm
while distance() > 30:
    # drive forward at 50 (keeps going until the next command)
    forward(50)
    # pause 0.1 s (the robot keeps doing what it was told)
    wait(0.1)
# all motors off
stop()

Adjust the number and the speed until the robot stops inside the band every time.

Challenges

  1. Stop within 10 cm of the wall at the fastest speed you can manage.
  2. Drive to the wall, turn 180, drive to the other wall, and print both distances.
  3. Make the LED change from green to orange to red as the wall gets closer.