The answersDownload the PDF
Worksheet

9.6 Sharing what you see

Talking to each other · Robot club · about 20 min

BugBotLab
NameClassDate

What this lesson is about

Telling a robot without a camera where the ball is.

Questions 6 marks in all

  1. [1 mark]Which message is useful to the Fetcher, which is standing somewhere else?

    1. Aball at 20,30
    2. Bred blob at cx 210, 40 px wide
    3. Cball in my camera
    4. Dturn right 15
  2. [1 mark]What does this program print?

    import math
    
    def bearing_of(cx):
        return (cx - 160) * 120 / 320
    
    x, y, heading = 10, 0, 0
    cx, width = 160, 23
    dist = 4 * 92 / max(width, 1)
    h = math.radians(heading + bearing_of(cx))
    bx, by = x + math.sin(h) * dist, y + math.cos(h) * dist
    print(f"ball at {bx:.0f},{by:.0f}")
  3. [1 mark]The blob's cx is 200. Using bearing_of(cx) = (cx - 160) * 120 / 320, what is its bearing in degrees?

  4. [1 mark]Why is the direction to the ball heading() + bearing_of(cx)?

    1. AThe bearing is measured from where the camera points, so adding the heading turns it into a direction on the mat
    2. BTo make the number positive
    3. CBecause the camera is on the back of the robot
    4. DThe heading is always zero
  5. [1 mark]Why does the code use max(width, 1) when working out the distance?

    1. ASo it never divides by zero
    2. BSo the ball is never closer than 1 cm
    3. CTo round the width to a whole number
    4. DBecause the width is measured in metres
  6. [1 mark]The robot faces up the mat and the red ball is right of centre in the picture. Roughly where is the ball?

    1. AUp the mat and to the right, at larger x
    2. BUp the mat and to the left, at smaller x
    3. CBehind the robot
    4. DExactly straight ahead

The task: share what you see

Find the red ball with the camera, work out where it is, and send ball at <x>,<y> within 10 cm of the truth. The Fetcher does the rest.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# camera: look for red blobs
set_cv('blob', 'red')
# until a blob of that colour is in view
while not blobs():
    # spin clockwise on the spot at 40
    turn_right(40)
    # pause 0.1 s (the robot keeps doing what it was told)
    wait(0.1)
# all motors off
stop()
print(blobs())

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

QR code
Do it on the robot
www.bugbotlab.com/learn/9-6-sharing-what-you-see/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Send the distance from the Fetcher to the ball as well (the Fetcher starts 38 cm to your right and 8 cm behind you).
  2. Add a second ball of another colour in free play and report both.
  3. Keep watching: if the ball is pushed, send the new position.