Control · Robot club · about 15 min
The error and its sign, wrapping headings, bang-bang control, overshoot.
[1 mark]What does this program print?
def wrapped(h):
return (h + 180) % 360 - 180
print(wrapped(200))
print(wrapped(350))-160 -10
Headings wrap round. 200 degrees is the same as 160 the other way, and 350 is the same as 10 to the left, so they become -160 and -10.
[1 mark]A controller uses error = target - measured. The target heading is 90 and the robot faces 60. What is the error? Give a whole number.
[1 mark]What is bang-bang control?
[1 mark]The bang-bang loop calls stop() when the error is under 2 degrees, but the robot ends further off. Why?
[1 mark]What does this program print?
for error in [30, -10]:
speed = 60 if abs(error) > 25 else 20
print(error, speed)30 60 -10 20
abs() ignores the sign. 30 is more than 25 so it gets 60, while -10 is only 10 away so it gets the slow speed of 20.
[1 mark]What does break do inside while True:?
while True would repeat for ever. break is how you get out once the error is small enough.[1 mark]What is the problem with the crude fix: fast when far, then short slow bursts with a pause?
Spin until the robot faces 0 within 3 degrees, printing error: and the error every time round the loop. Do not drive anywhere.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
print("heading:", heading())The hint students can ask for: Spin until heading() is within 3 degrees of 0, printing error: and the error each time round the loop. Do not drive anywhere.
from bugbot import *
connect()
def wrapped(h):
return (h + 180) % 360 - 180
while True:
error = wrapped(heading())
print('error:', round(error, 1))
if abs(error) <= 2:
break
speed = 60 if abs(error) > 25 else 20 # fast when far, careful when close
if error > 0:
turn_left(speed)
else:
turn_right(speed)
wait(0.1)
if abs(error) <= 25:
stop()
wait(0.3) # let it settle before looking again
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.