The answersDownload the PDF
Worksheet

5.1 The loop

Behaviours · Robot club · about 15 min

BugBotLab
NameClassDate

What this lesson is about

Look, decide, act, wait, repeat. One action per tick. Counting ticks.

Questions 7 marks in all

  1. [1 mark]What does every behaviour do, again and again?

    1. ALook, decide, act, wait, repeat
    2. BDrive, stop, turn, stop
    3. CWait until the task is finished, then end
    4. DRead every sensor once, then act forever
  2. [1 mark]Which call suits a behaviour loop, because it does not block?

    1. Aforward(60)
    2. Bforward(60, distance=20)
    3. Cturn_right(30, angle=90)
    4. Dwait(5)
  3. [1 mark]A student leaves wait(0.1) out of a while True behaviour loop. What goes wrong?

    1. AThe loop asks the sensors thousands of times with no time passing, and the simulator stops it
    2. BNothing, it just runs faster
    3. CThe robot only does the last tick
    4. DThe robot drives backwards
  4. [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")
  5. [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))
  6. [1 mark]In one tick a program calls forward(60) and then turn_right(60). What does the robot do?

    1. ATurns right: only the last command counts
    2. BDrives forward: only the first command counts
    3. CDrives forward and turns at once
    4. DStops, because the commands clash
  7. [1 mark]A behaviour loop waits 0.1 s each tick. How many ticks is five seconds?

The task: wander

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()

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/5-1-the-loop/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Wander for 20 seconds and print how many times you turned.
  2. Turn left or right depending on which side of the depth grid has more room.
  3. Slow down when something is between 30 and 50 cm away instead of waiting until 25.