The answersDownload the PDF
Worksheet

6.8 Project: the delivery

Seeing more · Robot club · about 30 min

BugBotLab
NameClassDate

What this lesson is about

Follow a line to a marker and back through traffic.

Questions 6 marks in all

  1. [1 mark]Why does the delivery program keep calling set_cv inside the loop?

    1. AThe camera runs one detector at a time, and it needs both lines and tags
    2. Bset_cv refreshes the picture
    3. CEach detector only works once
    4. DIt makes the robot drive straighter
  2. [1 mark]Put the delivery states in order.

    Number the lines 1 to 4 to put them in the right order.

    1. turn
    2. done
    3. home
    4. out
  3. [1 mark]The give way check comes after the state logic in each tick. Why there?

    1. AIts stop() overrides whatever the state asked for
    2. BIt has to run before set_cv("line")
    3. CThe state logic would undo the stop otherwise
    4. DChecks at the end of a loop run faster
  4. [1 mark]What does this program print?

    def facing_back(h, h0):
        return abs((h - h0 - 180 + 180) % 360 - 180) < 20
    
    for h in [0, 90, 170, 185, 200]:
        print(h, facing_back(h, 0))
  5. [1 mark]What is h0 for?

    1. AIt remembers the heading when marker 7 was found, so the robot knows when it faces back
    2. BIt is the heading at the start of the program
    3. CIt stores the line's angle
    4. DIt counts ticks in the turn state
  6. [1 mark]A student reads the tags with set_cv("apriltag") but forgets to switch back to set_cv("line"). What will follow_step do?

    1. AReturn False every tick, because the line detector is off
    2. BFollow the line as normal
    3. CFollow the tags instead of the line
    4. DRaise an error on the first tick

The task: the delivery

Follow the line to marker 7, print found 7, and follow it home without touching the red robot.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def follow_step(speed=60, gain=0.4):
    # [cx, angle] of the line ahead, or [] if none
    seen = line()
    if not seen:
        return False
    cx, angle = seen
    # forward, sideways, rotation: -100 to 100 each, until the next command
    drive(speed, 0, (cx - 160) * gain)
    return True

# camera: the line detector
set_cv("line")
# again and again, for ever
while True:
    if not follow_step(55, 0.4):
        # leave the loop
        break
    # pause 0.1 s (the robot keeps doing what it was told)
    wait(0.1)
# 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/6-8-project-delivery/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Make the give-way rule use the prediction from lesson 6.7 instead of stopping for every close robot.
  2. Blink the LED green on the way out and blue on the way home.
  3. Time the whole delivery and print it. Then make it faster on the straights.