Behaviours · Robot club · about 15 min
Look, decide, act, wait, repeat. One action per tick. Counting ticks.
[1 mark]What does every behaviour do, again and again?
[1 mark]Which call suits a behaviour loop, because it does not block?
forward(60)forward(60, distance=20)turn_right(30, angle=90)wait(5)forward(60) starts driving and returns at once. With a distance or angle, your program waits until the move is done.[1 mark]A student leaves wait(0.1) out of a while True behaviour loop. What goes wrong?
[1 mark]What does this program print?
for tick in [0, 9, 10, 19, 20, 35]:
print(tick, "green" if tick % 20 < 10 else "off")0 green 9 green 10 off 19 off 20 green 35 off
tick % 20 counts 0 to 19 and starts again. Under 10 is green, so it is green for the first second of every two. 35 % 20 is 15, so off.
[1 mark]What does this program print?
def choose(d):
if d < 15:
return "back"
elif d < 30:
return "turn"
else:
return "forward"
for d in [10, 15, 29, 30, 80]:
print(d, choose(d))10 back 15 turn 29 turn 30 forward 80 forward
Only the first true test counts. 15 is not under 15, so it falls to the turn. 30 is not under 30, so it goes forward.
[1 mark]In one tick a program calls forward(60) and then turn_right(60). What does the robot do?
if / elif / else.[1 mark]A behaviour loop waits 0.1 s each tick. How many ticks is five seconds?
Keep moving for the whole twenty seconds, at least 120 cm in total, without touching a box or the edge of the mat.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() # drive forward at 60 (keeps going until the next command) forward(60) # pause 5 s (the robot keeps doing what it was told) wait(5) # all motors off stop()
The hint students can ask for: Keep driving for the whole 20 seconds, at least 120 cm in total, without touching a box or the edge. Look, decide, act, wait, repeat.
from bugbot import *
connect()
for tick in range(190):
if distance() < 25:
turn_right(60) # something close: turn away
else:
forward(60) # clear: go
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.