Seeing more · Robot club · about 20 min
Gaps: carry on, then search with a state machine.
[1 mark]What does this program print?
readings = [[160, 0], [], [], [170, 2], [], [], [], []]
lost = 0
for seen in readings:
if seen:
lost = 0
else:
lost += 1
print(lost)[1 mark]When the line disappears, why carry straight on for a while before searching?
[1 mark]What does this program print?
for sweep in [0, 14, 15, 29, 30, 45]:
print(sweep, 40 if (sweep // 15) % 2 == 0 else -40)[1 mark]The search slides from side to side instead of turning. Why?
line() return an error[1 mark]Which way should the search try first?
angle said the line was heading[1 mark]The follower switches to search when lost > 8, adding 1 to lost on each tick with no line. On which tick without a line does it switch?
[1 mark]The task starter does if not follow_step(60, 0.4): break. What happens at the gap?
Follow the line through the gap to the green zone, staying within 6 cm of it for at least 60% of the run.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def follow_step(speed=60, gain=0.4):
# [cx, angle] of the line ahead, or [] if none
seen = line()
if not seen:
return False
cx, angle = seen
# forward, sideways, rotation: -100 to 100 each, until the next command
drive(speed, 0, (cx - 160) * gain)
return True
# camera: the line detector
set_cv("line")
while position()[1] < 85:
if not follow_step(60, 0.4):
# leave the loop
break
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()Plan your program here, then type it in and press Run.