The worksheetDownload the PDF
Answers

6.1 Lines

Seeing more · Robot club · about 15 min

BugBotLab

What this lesson is about

The line detector: cx and angle, and the empty list.

Questions 6 marks in all

  1. [1 mark]With set_cv("line") on, what does line() give when a line is in view?

    1. A[cx, angle]
    2. B[cx, cy, distance]
    3. C[id, cx, cy, distance]
    4. D[cx, cy, area, x0, y0, x1, y1, aspect]
    Answer: A. Two numbers: where the line crosses the picture, and which way it is heading.
  2. [1 mark]The robot turns right by 20 degrees while looking at a line that runs straight ahead. What happens to the line's angle?

    1. AIt goes negative: the line seems to swing left
    2. BIt goes positive: the line seems to swing right
    3. CIt stays 0
    4. DIt becomes []
    Answer: A. It is you that turned. Turn clockwise and the line appears to head off to your left.
  3. [1 mark]Ten centimetres ahead, one pixel is about 0.108 cm. A line has cx = 197. About how many cm to the right of centre is it, to one decimal place?

    Answer: 4.0 (accept within 0.2). 197 minus 160 is 37 pixels, and 37 times 0.108 is about 4 cm.
  4. [1 mark]The detector sees about 35 cm across the strip 10 cm ahead. About how many cm to the side can the line be before it drops out of view?

    Answer: 17.5 (accept within 0.5). Half of 35 is about 17 either side of centre. Further than that and line() is [].
  5. [1 mark]What does this program print?

    for seen in [[197, 5], []]:
        if seen:
            cx, angle = seen
            print(f"line at cx {cx}, angle {angle}")
        else:
            print("no line")
    Answer:
    line at cx 197, angle 5
    no line

    An empty list counts as false, so the second reading prints the fallback. Every line program has to cope with [].

  6. [1 mark]The task starter just calls print(line()). What is it missing?

    1. Aset_cv("line"), to switch on the line detector
    2. Bset_cv("apriltag")
    3. Cforward(50), because the line detector only works when moving
    4. DNothing, line() always works
    Answer: A. The camera runs one detector at a time, and you have to pick it first, just as with tags in Module 4.

The task: see the line

Print line at cx <cx>, angle <angle> from the detector. Do not drive.

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

print(line())

The hint students can ask for: Switch the camera to the line detector and print where the line is, like line at cx 197, angle 0.0. Do not drive.

A solution

from bugbot import *
connect()
set_cv('line')
cx, angle = line()
print(f'line at cx {cx}, angle {angle}')

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