8. My two robots at once
From one robot to a swarm
The problem. drive() returns only once its robot has stopped, so a script that drives two robots moves them one after the other instead of together.
There is no docs section for this step: it is the workshop’s own. The answer is goto(), from step 7: it returns at once, so both robots can be on their way at the same time, with nothing else to learn.
Both robots, together
Save two.py (below) next to academy.py. BASE_URL is the instructor’s controller, as in step 6; set LABELS to the labels of your two robots. Check that the floor about 300 mm in front of both robots is free, then run it:
python two.pytwo.py
"""Step 8: my two robots at once.
goto() returns at once, so send each robot its target first, then check on
both of them in one loop.
You should see: robot A turns red and robot B blue, both leave together,
drive 300 mm up the map, and turn green once both are there.
"""
import time
import academy
BASE_URL = "https://perry-one-indicates-measurements.trycloudflare.com" # the instructor's controller, as in step 6
LABELS = ["A1B2C3", "D4E5F6"] # TODO: the labels on your two robots
academy.connect(BASE_URL)
robot_a, robot_b = academy.swarm(LABELS)
robot_a.led(255, 0, 0) # A is red
robot_b.led(0, 0, 255) # B is blue
xa, ya = robot_a.position()
xb, yb = robot_b.position()
# 1. Send every robot its target: each goto() returns at once.
robot_a.goto(xa, ya - 300)
robot_b.goto(xb, yb - 300)
# 2. Then check on all of them in one loop, until every one has arrived.
while not (robot_a.arrived() and robot_b.arrived()):
time.sleep(0.5)
robot_a.led(0, 255, 0) # green: both are there
robot_b.led(0, 255, 0)You should see: robot A turns red and robot B blue, both leave at the same moment, drive 300 mm up the map, and turn green once both are there.
That robot bumped into another one or got stuck. Drive it clear in the console, and run the script again.
This is how swarm programs are written: first send every robot its target, then check on all of them in one loop until every one has arrived. With twenty robots instead of two, the script grows by one line per robot, and the trip takes about as long.
Core up and back, together
Your turn. Give each robot a short list of waypoints instead of one target: 300 mm up, then back to where it started. Both robots go to their next waypoint together, and only once both have arrived do they go on to the one after.
You should see: both robots go up together, come back together, and turn green once both are home.
Write each robot’s route as a list, route_a = [(xa, ya - 300), (xa, ya)], and the same for B. Then for target_a, target_b in zip(route_a, route_b): gives you one waypoint of each per turn: send both, then wait for both, with the same while loop as in two.py.
two.py
"""Step 8: my two robots at once, up and back.
Each robot gets a short list of waypoints, up 300 mm and back home; both
robots go to their next waypoint together, and the loop waits for both.
You should see: both robots leave together, go up, come back together, and
turn green once both are home.
"""
import time
import academy
BASE_URL = "https://perry-one-indicates-measurements.trycloudflare.com" # the instructor's controller, as in step 6
LABELS = ["A1B2C3", "D4E5F6"] # TODO: the labels on your two robots
academy.connect(BASE_URL)
robot_a, robot_b = academy.swarm(LABELS)
robot_a.led(255, 0, 0) # A is red
robot_b.led(0, 0, 255) # B is blue
xa, ya = robot_a.position()
xb, yb = robot_b.position()
route_a = [(xa, ya - 300), (xa, ya)] # up, then back home
route_b = [(xb, yb - 300), (xb, yb)]
for target_a, target_b in zip(route_a, route_b):
robot_a.goto(*target_a)
robot_b.goto(*target_b)
while not (robot_a.arrived() and robot_b.arrived()):
time.sleep(0.5)
robot_a.led(0, 255, 0) # green: both are home
robot_b.led(0, 255, 0)