Behaviours · Robot club · about 15 min
Two things at once, remembered ticks, timeouts.
[1 mark]Why can a program not blink the LED with wait(0.5) while it drives to the zone?
wait(0.5) holds up the whole loop, so the driving stops being updatedwait turns the LED offwait only accepts whole numbers[1 mark]What does this program print?
on = False
changes = 0
for ticks in range(20):
if ticks % 5 == 0:
on = not on
changes += 1
print(changes, on)4 False
Ticks 0, 5, 10 and 15 each flip the toggle. Four flips from False brings it back to False.
[1 mark]The loop waits 0.1 s a tick. To toggle the LED every half second, every how many ticks should it toggle?
ticks % 5 == 0.[1 mark]What does this program print?
started = None
for ticks in range(40):
if started is None and ticks == 20:
started = ticks
print("start", ticks)
elif started is not None and ticks - started >= 10:
print("stop", ticks)
started = Nonestart 20 stop 30
The timer remembers tick 20. ticks - started is the time since then, and it reaches 10 at tick 30, one second later.
[1 mark]In started_turning = None, what does None mean?
None says there is no event to count from. When the turn starts, it holds the tick it started on.[1 mark]A search spins while ticks < 30 and gives up if nothing is found. Why is this timeout important?
[1 mark]What is the advantage of clock() - started_at >= 1.0 over counting ticks?
Drive into the green zone while the LED blinks green and off every half second, at least eight changes.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# the LED: green
led("green")
# drive forward at 60 for 70 cm, then stop
forward(60, distance=70)
# the LED: off
led("off")The hint students can ask for: Drive to the green zone while the LED blinks green and off every half second. Two things at once means a tick loop and a tick counter, not two waits.
from bugbot import *
connect()
ticks = 0
on = False
while position()[1] < 70:
forward(60)
if ticks % 5 == 0: # every 5 ticks = every half second
on = not on
led("green" if on else "off")
ticks += 1
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.