Seeing more · Robot club · about 20 min
Two controllers that never finish: chase a moving tag at a set distance.
[1 mark]What does this program print?
for dist in [50, 30, 20, 12]:
speed = max(0, min(70, (dist - 20) * 4))
print(dist, speed)50 70 30 40 20 0 12 0
50 gives 120, clamped to 70. 30 gives 40. At 20 the error is 0. At 12 it would be -32, and the clamp holds it at 0.
[1 mark]Why is the lowest speed in the follower 0, the max(0, ...)?
[1 mark]With speed = max(0, min(70, (dist - 20) * 4)), what speed does the follower use when the leader is 26 cm away?
[1 mark]How is following a robot different from docking at a marker?
[1 mark]When the leader goes out of view, the lesson just turns right. What is a better recovery?
[1 mark]The task starter always drives at 60 towards robot 101. What goes wrong?
Stay 12 to 35 cm behind the green robot for at least 80% of the run, without touching it. The starter drives at the leader at a fixed speed and rams it. Add the distance controller.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# do this 390 times (tick counts from 0)
for tick in range(390):
seen = [r for r in robots() if r[0] == "green"]
if seen:
# forward, sideways, rotation: -100 to 100 each, until the next command
drive(60, 0, (seen[0][1] - 160) * 120 / 320 * 3)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()The hint students can ask for: The leader drives a loop at a steady speed with its dome lit green. Stay 12 to 35 cm behind it for at least 80% of the run without touching it: steer from its bearing, speed from its distance.
from bugbot import *
connect()
for tick in range(390):
seen = [r for r in robots() if r[0] == 'green']
if not seen:
turn_right(40) # lost it: turn until the green dome is back
else:
cx, dist = seen[0][1], seen[0][3]
bearing = (cx - 160) * 120 / 320
speed = max(0, min(70, (dist - 20) * 4)) # hold about 20 cm behind
drive(speed, 0, bearing * 3)
wait(0.1)
stop()
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.