Seeing more · Robot club · about 20 min
cx to rotation, the angle as a look-ahead, speed on the straights.
[1 mark]follow_step uses rotation (cx - 160) * 0.4. What rotation does a line at cx = 100 give?
[1 mark]The rotation for a line at cx = 100 is negative. What does the robot do?
[1 mark]A student writes drive(60, 0, (160 - cx) * 0.4). What happens?
[1 mark]What does this program print?
def rotation(cx, angle):
return (cx - 160) * 0.3 + angle * 1.5
print(rotation(160, 10))
print(rotation(200, -8))15.0 0.0
The first line is centred but bending right, so the angle term starts the turn early. In the second, the two terms cancel out.
[1 mark]Adding angle * 1.5 to the rotation is the beginning of which kind of control?
cx says where the line is, angle says where it is going, so the robot starts turning before it drifts off-centre.[1 mark]At each bend the line swings well off-centre, up to 85 pixels, before the robot has turned. What does that mean?
[1 mark]What does this program print?
for angle in [0, 5, 20, -25]:
speed = 80 if abs(angle) < 10 else 40
print(angle, speed)0 80 5 80 20 40 -25 40
A small angle means a straight, so go fast. abs makes a bend to the left count the same as one to the right.
Follow the line to the green zone, staying within 6 cm of it for at least 85% of the run.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() # drive forward at 60 for 90 cm, then stop forward(60, distance=90)
The hint students can ask for: Follow the line to the green zone at the far end, staying within 6 cm of it. Steer from cx: the line left of centre means turn left.
from bugbot import *
connect()
def follow_step(speed=60, gain=0.4):
# one tick of line following: steer from where the line crosses the picture
seen = line()
if not seen:
return False
cx, angle = seen
error = cx - 160 # pixels left (-) or right (+) of centre
drive(speed, 0, error * gain)
return True
set_cv('line')
while position()[1] < 85:
if not follow_step(60, 0.4):
forward(30)
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.