Watching the others

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

10.3CompetingRobot club14 min

Do this lesson in the simulator

In a game the mat is busy. Every game here scores you for what you do, never for shoving somebody else, and most of them put you out for contact. Seeing other robots early is how you stay in.

Where everyone is

Inside a game, others() gives you every robot still driving, with its name and position:

for bot in others():
    print(bot['name'], bot['x'], bot['y'])

That is the game's own view, updated as it runs. On the mat itself, the camera sees the tag on each robot's back, which is what Module 6 taught:

from bugbot import *
connect()

set_cv("apriltag")
# each tag is [id, cx, cy, distance]: ids from 100 up are robots
for tag in robot_tags():
    tid, cx, cy, gap = tag
    print("robot", tid, "at", cx, "across the picture,", round(gap, 1), "cm away")

Run this in the simulator

Use whichever is handier: others() for who is where on the whole mat, the camera for what is in front of you right now.

Giving way

The rule in most games is simple: touching another robot is your fault, whoever moved. Three habits keep you out of trouble:

  • Look before you commit. A quick distance() before a burst of speed costs you nothing.
  • Go round, not through. Sidestep with left() or right(), which BugBot can do without turning.
  • Give way early. Slowing a little when somebody is heading for the same spot is cheaper than a collision.
from bugbot import *
connect()

# the shape of giving way: something ahead and close, step aside instead of stopping dead
if distance() < 25:
    right(50, distance=15)
else:
    forward(60, distance=15)
stop()

Run this in the simulator

Task: through the traffic

Two robots are driving up and down the mat. Get from the start to the green zone without touching either of them, or anything else.

from bugbot import *
connect()

set_cv("apriltag")

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

stop()

Challenges

  1. Print the bearing of the nearest robot every time round the loop, and read the log to see when it got close.
  2. Wait for a gap instead of going round: stop while something is within 30 cm, then carry on.
  3. Cross the mat sideways instead, facing forward the whole way.