The worksheetDownload the PDF
Answers

4.1 What the camera sees

Vision · Robot club · about 15 min

BugBotLab

What this lesson is about

set_cv, the tag list, pixels and distance, empty lists.

Questions 7 marks in all

  1. [1 mark]A program calls apriltags() before it has called set_cv. What does it get?

    1. AAn empty list, because no detector is switched on
    2. BEvery tag the camera can see
    3. CAn error saying the camera is off
    4. DThe tags from the last program that ran
    Answer: A. The camera runs one detector at a time, and until you pick one the vision functions return nothing. Always start with set_cv("apriltag").
  2. [1 mark]Each entry in apriltags() is [id, cx, cy, distance]. What is tag[3]?

    1. AHow far away the tag is, in cm
    2. BThe number printed on the tag
    3. CHow far across the picture it is, in pixels
    4. DHow far down the picture it is, in pixels
    Answer: A. Lists count from 0, so index 3 is the fourth number: the distance in cm.
  3. [1 mark]The picture is 320 pixels wide. What cx does a tag straight ahead of the robot have?

    Answer: 160. Half of 320 is 160, so cx = 160 is dead centre. Smaller is left, bigger is right.
  4. [1 mark]What does this program print?

    tag = [7, 160, 120, 45.0]
    print(f"marker {tag[0]} at {tag[3]} cm")
    Answer:
    marker 7 at 45.0 cm

    tag[0] is the id and tag[3] is the distance, so this is exactly the line the task asks for.

  5. [1 mark]The robot turns away so no tag is in view, then runs tag = apriltags()[0]. What happens?

    1. AAn error, because there is no first item in an empty list
    2. Btag becomes 0
    3. Ctag becomes an empty list
    4. DIt waits until a tag comes into view
    Answer: A. An empty list has no item 0, so Python stops with an error. Check the list is not empty before you read from it.
  6. [1 mark]Which of these are true of the list apriltags() gives you?

    Tick every answer that is true.

    1. AThe nearest tag comes first
    2. BThere is one entry for each tag it can read
    3. Ccx is measured in cm
    4. DAn empty list means something has gone wrong
    Answer: A, B. Tags come nearest first, one entry each. cx is in pixels, and an empty list just means no tag is in view.
  7. [1 mark]Marker 7 is 45 cm away. The robot drives forward 8 cm towards it. About what distance, in cm, should the next apriltags() report?

    Answer: 37 (accept within 1). 45 minus 8 is 37, and that is what the camera reports after the first 8 cm.

The task: find the marker

Switch on the tag detector and print marker 7 at 45.0 cm using the id and distance from the list. Do not drive.

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

print(apriltags())

The hint students can ask for: Switch the camera to the tag detector, read the tag in front of you, and print marker 7 at 45.0 cm (id and distance from the list). Do not drive.

A solution

from bugbot import *
connect()
set_cv('apriltag')
tags = apriltags()
tag = tags[0]
print('marker', tag[0], 'at', tag[3], 'cm')

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.