Seeing other robots

Tags from 100 up say who and how far; LEDs say what.

6.4Seeing moreRobot club15 min

Do this lesson in the simulator

Every BugBot wears an AprilTag on its body. To your camera another robot is just a tag with a number from 100 up, so everything you learned about tags in Module 4 works on robots: who it is, how far, which way. And its LED is a little coloured light the blob detector can read.

A tag on wheels

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

# camera: the tag detector
set_cv("apriltag")
print(apriltags())

Run this in the simulator

One tag, id 101, about 42 cm away, off to the right. Tags below 100 are markers on the mat; 100 and up are robots. Two shorthands split the list for you: marker_tags() is the markers in view and robot_tags() the robots, each in the same [id, cx, cy, distance] form as apriltags().

Distance and bearing

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

def bearing_of(cx):
    return (cx - 160) * 120 / 320

# camera: the tag detector
set_cv("apriltag")
for tag in apriltags():
    kind = "robot" if tag[0] >= 100 else "marker"
    print(f"{kind} {tag[0]}: {tag[3]} cm away, bearing {round(bearing_of(tag[1]), 1)}")

Run this in the simulator

The same conversion from pixels to degrees as lesson 4.2. Robots move, so these numbers change every time you look, which is the whole point of the next lesson.

What its LED says

A tag says who. The LED says what: a team colour, or a state the robot is broadcasting. The blob detector reads it, one colour at a time:

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

for colour in ["red", "green", "blue", "yellow"]:
    set_cv("blob", colour)
    if blobs():
        print("led:", colour, "at cx", blobs()[0][0])

Run this in the simulator

Only one colour finds anything: the other robot's LED is green. Four colours is about the limit; a bright LED washes out in a camera, so tell them apart by colour, not by shade.

Talking with light

It works both ways. Set your own LED and other robots can read you:

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

# anyone looking at me now sees a red blob
led("red")
print("broadcasting red")

Run this in the simulator

In the games, me.led("red") does the same, and me.blobs on the other robots reports it. A team could agree: green means I have the ball, red means I am coming through.

Task: spot the robot

Print robot 101 at <cm> cm, bearing <degrees> from its tag, and led: <colour> from the blob detector. Do not drive.

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

# camera: the tag detector
set_cv("apriltag")
print(apriltags())

Challenges

  1. Turn until robot 101 is centred in the picture, then print your heading.
  2. Print the robot's position on the mat, from your position, heading, and its distance and bearing. Hint: math.sin and math.cos.
  3. Flash your LED red and green alternately every half second while printing what you see.