Project: the maze
Walls, corners and a goal. Everything in this module in one run.
Do this lesson in the simulatorWalls, corners and a goal. This project uses everything in Module 2: the distance sensor to find walls, turns at the corners, the depth grid to stay off the sides, and correction so the long runs stay straight.
The maze
Two walls make a U-shaped path: up the left channel, across the top, down the middle channel to the goal at the bottom. Look at it in the task view before writing anything.
The plan
# 1. drive up until the top wall is close
# 2. turn right, drive until the right-hand wall is close
# 3. turn right again, drive down into the goal
Three legs, two corners. Each leg is a "drive until distance() is small" loop you wrote in lesson 2.2.
One leg as a function
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def drive_to_wall(gap):
while distance() > gap:
# drive forward at 60 (keeps going until the next command)
forward(60)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
print("wall at", distance(), "cm, position", position())
drive_to_wall(15)
Two legs
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def drive_to_wall(gap):
while distance() > gap:
# drive forward at 60 (keeps going until the next command)
forward(60)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
drive_to_wall(15)
# turn 90 degrees clockwise, then stop
turn_right(30, angle=90)
drive_to_wall(15)
print("second corner at", position())
Staying off the side walls
In a narrow channel the robot's drift takes it into a wall. The grid's edge columns tell you which wall is closer, so nudge away from it inside the leg:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def drive_to_wall(gap):
while distance() > gap:
# 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))
sideways = 0
if left_side < 8:
sideways = 30
elif right_side < 8:
sideways = -30
# forward, sideways, rotation: -100 to 100 each, until the next command
drive(60, sideways, 0)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
drive_to_wall(15)
print("top of the channel, position", position())
Task: the maze
Up the left channel, right along the top, down the middle channel into the goal, without touching either wall. Forty-five seconds.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def drive_to_wall(gap):
while distance() > gap:
# drive forward at 60 (keeps going until the next command)
forward(60)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
drive_to_wall(15)
# turn, second leg, turn, third leg
Where next
The Maze relay competition is this maze with four robots at once, and Module 3 turns "nudge away from the wall" into a proper controller that keeps the robot centred at speed.
Challenges
- Get through the maze in under 25 seconds.
- Write the maze as a list of legs
[("up", 15), ("right", 15), ("down", 15)]and a loop that drives them. - Make the robot solve a maze it has not seen: always keep the left wall close (the left-hand rule).