Control · Robot club · about 15 min
Speed from the remaining distance, the dead band, clamping.
[1 mark]The bang-bang park drives at 60 until distance() is 22 or less, then stops. Where does it end?
[1 mark]Target 22, gain 4, and distance() reads 32. What speed does speed = (distance() - target) * 4 ask for?
[1 mark]The plain proportional park stalls a little short of the target. Why?
[1 mark]What does this program print?
for value in [4, 30, 250]:
print(max(16, min(60, value)))16 30 60
Clamping keeps a number between two limits. 4 is raised to 16, 30 is fine, and 250 is capped at 60.
[1 mark]In the clamped park, the robot overshoots and distance() reads 18. What does it do?
error = distance() - target
speed = max(16, min(60, abs(error) * 4))
if error > 0:
forward(speed)
else:
backward(speed)[1 mark]Put the three lines of every P controller in order.
Number the lines 1 to 3 to put them in the right order.
output = clamp(error * gain)apply(output)error = target - measurederror = target - measured output = clamp(error * gain) apply(output)
Measure the error, work out how hard to push, then push. Steering and speed are the same loop with a different sensor and motor.
Stop with the front of the robot 20 to 24 cm from the wall, the green band, without touching the wall.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# as long as the thing ahead is further than 22 cm
while distance() > 22:
# drive forward at 60 (keeps going until the next command)
forward(60)
# 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: Stop with the front of the robot 20 to 24 cm from the wall: the green band. Make the speed depend on how far you still have to go, and remember the motors do nothing below about 15.
from bugbot import *
connect()
target = 22
while True:
error = distance() - target
if abs(error) < 1:
break
speed = max(16, min(60, abs(error) * 4))
if error > 0:
forward(speed)
else:
backward(speed)
wait(0.05)
stop()
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.