The worksheetDownload the PDF
Answers

10.3 Watching the others

Competing · Robot club · about 14 min

BugBotLab

What this lesson is about

others() and the camera tags, and giving way early so contact never costs you the game.

Questions 6 marks in all

  1. [1 mark]In the traffic task, a student calls others() to find the moving robots. What happens?

    1. AIt does not work: others() is only there in games, so in a task use the camera
    2. BIt lists both robots with their positions
    3. CIt returns what the camera can see
    4. DIt sends a message asking where they are
    Answer: A. others() is the game's own view. In a task, robots() shows what is in front of you, each one found by its dome colour.
  2. [1 mark]What does this program print?

    import math
    bots = [{"name": "red-1", "x": 30, "y": 40}, {"name": "blue-2", "x": -6, "y": 8}, {"name": "blue-3", "x": 20, "y": 0}]
    closest, best = None, 1e9
    for bot in bots:
        d = math.hypot(bot["x"], bot["y"])
        if d < best:
            closest, best = bot["name"], d
    print(closest, best)
    Answer:
    blue-2 10.0

    Measured from (0, 0) the distances are 50, 10 and 20, so blue-2 is closest at 10.0 cm.

  3. [1 mark]You touch another robot that drove into you. Whose fault is it, in most games?

    1. AYours: touching another robot is your fault, whoever moved
    2. BTheirs, because they moved
    3. CNobody's
    4. DWhoever has the lower score
    Answer: A. That is why seeing other robots early and giving way matters.
  4. [1 mark]Which habits keep you out of trouble on a busy mat?

    Tick every answer that is true.

    1. ALook before you commit
    2. BGo round with left() or right()
    3. CGive way early
    4. DSpeed up to get past before they arrive
    5. EPush through, since they moved into you
    Answer: A, B, C. A quick look, a sidestep and a little slowing cost almost nothing. A collision costs the game.
  5. [1 mark]robots() comes back as [['red', 160, 118, 35.0]]. What is 35.0?

    1. AHow far away that robot is, in centimetres
    2. BIts bearing in degrees
    3. CHow big its dome looks, in pixels
    4. DIts id
    Answer: A. Each robot is [colour, cx, cy, distance], nearest first. The bearing comes from cx: (cx - 160) * 120 / 320.
  6. [1 mark]What does this program print?

    for gap in [40, 25, 12]:
        if gap < 25:
            print(gap, "step aside")
        else:
            print(gap, "carry on")
    Answer:
    40 carry on
    25 carry on
    12 step aside

    gap < 25 is false when the gap is exactly 25, so only 12 is close enough to step aside.

The task: through the traffic

Two robots are driving back and forth across the mat, 35 cm and 55 cm ahead of you. Get from the start to the green zone without touching either of them, or anything else.

from bugbot import *
connect()

while position()[1] < 80:
    forward(50)
    wait(0.1)

stop()

The hint students can ask for: Two robots cross your path, one red and one blue. Watch their domes with robots() and wait or go round.

A solution

from bugbot import *
connect()
def robot_near():
    return any(r[3] < 30 for r in robots())
while position()[1] < 76:
    if robot_near():
        stop()
    else:
        forward(45)
    wait(0.1)
stop()

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