Trails and tight spots
A mat that fills up: seeing trails, turning early, keeping to the outside, and turning without stopping.
Do this lesson in the simulatorSome games fill the mat as they go. In Light Cycles every robot leaves a trail, your own included, and touching any of them puts you out. The mat gets smaller every second.
Seeing a trail
A trail on the mat is a line, so the line detector from Module 6 sees it:
from bugbot import *
connect()
set_cv('line')
found = line()
print("line ahead?", bool(found))
if found:
# line() gives [cx, angle]: where it crosses the picture, and which way it heads
cx, angle = found
print("it crosses at", cx, "of 320, heading", angle, "degrees")
In the game itself you also get the whole map: info()['trails'] is every trail as a list of points, your own included. The camera tells you what is 10 to 25 cm ahead; the map tells you what the mat looks like everywhere.
Never drive into a dead end
Two rules will keep you alive longer than any clever plan:
- Turn early. A robot that turns when a trail is 15 cm away needs space it may not have. Turning at 30 cm is cheap.
- Keep to the outside. The middle of the mat fills up first. Robots that hug the edges have somewhere to go for longer.
And one that catches everybody: in Light Cycles you may not sit still. Stopping to think is the same as losing. Turn while moving, with drive():
from bugbot import *
connect()
# forward and turning at the same time: no stopping, no reversing
drive(35, 0, 80)
wait(0.6)
stop()
print("still moving while turning")
drive(forward, sideways, turn) sets all three at once. Module 1 used it for sliding; here it is how you turn a corner without stopping.
Task: thread the gap
Three trails lie across the mat, each with a gap at one end, and the gaps alternate. Drive from the start to the green zone at the top without touching a trail.
from bugbot import *
connect()
set_cv('line')
while position()[1] < 85:
forward(45)
wait(0.1)
stop()
Challenges
- Use
line()to spot the line ahead and steer for the gap rather than driving a fixed path. - Do it again with
drive()only, never stopping. - Count how many times you changed direction. Fewer is usually faster.