Seeing other robots
Tags from 100 up say who and how far; LEDs say what.
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
A BugBot wears no marker. What it has is the dome on its head, lit from inside, and that is what another robot's camera picks out: one big patch of colour, readable from any side, at any angle, from right across the mat. Markers on the mat still carry tags. Robots carry colour.
Why colour and not a tag
A tag has to be square-on enough to decode, and it has to be big enough in the picture. A robot turns, tilts as it drives and is often far away, so a tag on its side is readable about half the time. A lit dome is a blob of colour: the camera finds it in one pass, it looks the same from every side, and it still reads when it is only a few pixels across. That is why the robot you are looking at has a dome and no tag.
Finding one
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# every robot in view, nearest first
print(robots())
One robot: ['green', 195, 118, 42.7]. The colour of its dome, then cx and cy, where the dome sits in the picture, then how far away it is in centimetres. Same shape as a tag reading, but the first item is a colour instead of a number, because that is what a robot broadcasts about itself.
A robot with its LED off is not in the list. Off means invisible.
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
for colour, cx, cy, dist in robots():
print(f"{colour}: {dist} cm away, bearing {round(bearing_of(cx), 1)}")
The same conversion from pixels to degrees as lesson 4.2: the picture is 320 pixels wide across 120 degrees, so 160 is straight ahead. Robots move, so these numbers change every time you look, which is the whole point of the next lesson.
Telling them apart
The colour is the name. In a game every robot is given a different one, so robots() tells you which is which without anything written on them:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
for colour, cx, cy, dist in robots():
if colour == "green":
print("that is the green one, at", dist, "cm")
Four or five colours is about the limit. A lit dome washes out towards white in a camera, so tell robots apart by which colour, never by how bright.
Talking with light
It works both ways. Set your own dome and everyone else 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 red
led("red")
print("broadcasting red")
In the games, me.led("red") does the same, and the other robots see it in their own robots(). A team could agree: green means I have the ball, red means I am coming through.
Task: spot the robot
Print robot at <cm> cm, bearing <degrees> and dome: <colour>, both from robots(). Do not drive.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# every robot in view, nearest first
print(robots())
Challenges
- Turn until the green robot is centred in the picture, then print your heading.
- Print the robot's position on the mat, from your position, heading, and its distance and bearing. Hint:
math.sinandmath.cos. - Flash your own dome red and green alternately every half second while printing what you see.