Getting round things
Detect, sidestep, continue: the first obstacle avoidance.
Do this lesson in the simulatorStopping in front of a wall is good. Getting past it is better. This lesson builds the first real behaviour: notice an obstacle, step round it, carry on.
The plan
# 1. drive until something is close
# 2. step sideways until the way ahead is clear
# 3. drive past it
# 4. step back to the original line (optional) and continue
Each step is something you already know. The new part is joining them with the sensor as the judge of when each step is done.
Step 1: approach
# 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 20 cm
while distance() > 20:
# 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()
print("something ahead at", distance(), "cm")
Step 2: sidestep until clear
# 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 20 cm
while distance() > 20:
# 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()
print("box ahead")
# as long as something is closer than 60 cm
while distance() < 60:
# slide right at 50
right(50)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
# a little further: the body is wider than the sensor's beam
right(50, distance=6)
print("clear now, at x =", position()[0])
The second loop strafes right until the sensor sees a long way again. The robot never turned, so "ahead" still means the same direction.
Step 3: past and on
# 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 20 cm
while distance() > 20:
# 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()
print("box ahead")
# as long as something is closer than 60 cm
while distance() < 60:
# slide right at 50
right(50)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
# a little further: the body is wider than the sensor's beam
right(50, distance=6)
# drive forward at 60 for 35 cm, then stop
forward(60, distance=35)
print("past it, at", position())
Which way to step?
Stepping right always works only if there is room on the right. The depth grid from lesson 2.3 tells you which side has more room:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# 64 distances, 8 rows of 8
grid = tof_grid()
# the level rows
left_side = min(grid[row * 8 + 0] for row in (2, 3))
right_side = min(grid[row * 8 + 7] for row in (2, 3))
if right_side >= left_side:
print("stepping right")
else:
print("stepping left")
Task: dodge the box
Drive until the box is close, print box ahead, step sideways until the way is clear, go past, and finish in the green zone without touching the box.
# 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 20 cm
while distance() > 20:
# 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()
print("box ahead")
Challenges
- After passing the box, step back to the original line so the robot ends where it would have without the box.
- Choose the side with more room using the grid.
- Handle two boxes in a row with a loop around the whole behaviour.