7. Moving to a certain position

Where the robot is, and how to send it somewhere

The problem. drive() and spin() are open-loop: they run the wheels for a time and never check where the robot ended up. On the floor, a wheel slips or a turn comes out at 85 degrees, and nothing corrects it, which is why your step 6 robot does not end exactly where the simulator’s did, and the challenge square does not quite close. goto() is closed-loop: the robot reads its own position all the way and keeps correcting until it is there.

This step builds goto() up one idea at a time, a small script each, as in step 4: where the robot is, sending it somewhere, waiting until it is there, checking how close it got, then a list of waypoints. Every script talks to your simulator first; the last section runs them on your real robot.

Keep the step 3 simulator running, and work in your second terminal, in ~/dotbot-academy with the environment active, next to academy.py.

Where am I: read the position

Save where_am_i.py (below) next to academy.py and run it, python where_am_i.py.

where_am_i.py
import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()  # in mm: x grows right, y grows DOWN the map
print(f"I am at x = {x:.0f} mm, y = {y:.0f} mm")

robot.position() gives where the robot is, as (x, y) in millimetres, measured by the lighthouse (step 2). The origin is the top-left corner of the map, x grows to the right and y grows down (Figure 1). So “300 mm up the map” is (x, y - 300).

(0, 0) x y (x, y - 300) (x, y) 300 mm up
Figure 1: The map’s frame, in millimetres: the origin is the top-left corner and y grows down, so going up the map takes y down.

You should see: the terminal prints two numbers, the robot’s x and y in mm. Hover over robot 000000 on the console map: the coordinates match.

Core go there

robot.goto(x, y) gives the robot a target. The robot drives there by itself and stops once it is within 60 mm of it. Save go_there.py and run it:

go_there.py
import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()
print(f"Start: x = {x:.0f} mm, y = {y:.0f} mm")
robot.goto(x, y - 300)  # 300 mm up the map: y goes down
print("Target sent, script done")

You should see: the terminal prints both lines and the script ends at once, while robot 000000 is still driving up the map. It stops about 300 mm higher, after your script is done.

Important

goto() returns immediately: it only gives the robot its target, and the robot drives there by itself. Your script does not wait unless you make it wait.

Core wait until it is there

robot.arrived() is True once the robot is there. Asking it in a loop, twice a second, makes your script wait for the trip, however long it takes, instead of a time.sleep() that guesses. The robot is still 300 mm up from go_there.py, so this script brings it back down. Save wait_until_there.py and run it:

wait_until_there.py
import time

import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()  # still 300 mm up, where go_there.py sent it
robot.led(0, 0, 255)  # blue while it drives
robot.goto(x, y + 300)  # 300 mm down: back where it started
while not robot.arrived():  # not there yet: check again in half a second
    time.sleep(0.5)
robot.led(0, 255, 0)  # green: there
print("There")

You should see: the robot turns blue, drives 300 mm down the map, back where it started, then turns green, and only then does the terminal print There and the script end.

It bumped into another robot. Drive it clear in the console, or restart the simulator.

Core how close did it get?

goto() stops within 60 mm of the target, not exactly on it. academy.distance(a, b) is the distance in mm between two (x, y) points: between where the robot stopped and where it was sent, it says how close it got. Save how_close.py and run it:

how_close.py
import time

import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()
target = (x, y - 300)  # 300 mm up the map again
robot.led(0, 0, 255)  # blue while it drives
robot.goto(x, y - 300)
while not robot.arrived():
    time.sleep(0.5)
robot.led(0, 255, 0)  # green: there
off = academy.distance(robot.position(), target)  # in mm
print(f"Stopped {off:.0f} mm from the target")

You should see: the robot turns blue, drives 300 mm up the map again, turns green, and the terminal prints how many mm from the target it stopped: a few mm in the simulator.

Core a list of waypoints

Your turn. The robot now stands 300 mm above where it started. Save waypoints.py (below), fill in its two TODOs, and run it: the robot visits three waypoints in turn, 200 mm right, back, then down to where it started, blue while it drives and green at each one.

You should see: the robot drives right, back and down, ending where it started at the beginning of this step, blue on the way and green at each waypoint, and the terminal prints how close it stopped, three times.

waypoints.py
"""Step 7, exercise: a list of waypoints.

After how_close.py the robot stands 300 mm above where it
started. From there, send it through three waypoints in turn:
200 mm right, back here, then down to where it started.
You should see: blue on the way to each waypoint, green at
each one, and how close it stopped, printed three times.
"""

import time

import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()
waypoints = [(x + 200, y), (x, y), (x, y + 300)]  # right, back, down

# 1. Loop over the waypoints. For each one: turn the LED
#    blue, send the robot there and wait until it has
#    arrived, as in how_close.py.
# TODO

# 2. Still in the loop, once it is there: turn it green and
#    print how close it stopped to that waypoint.
# TODO

for target in waypoints: gives you one waypoint per turn, as an (x, y) pair: target[0] is its x and target[1] its y. Inside the loop, write the lines of how_close.py, from the blue LED to the print, with target instead of (x, y - 300).

waypoints.py
"""Step 7, exercise: a list of waypoints.

After how_close.py the robot stands 300 mm above where it
started. From there, send it through three waypoints in turn:
200 mm right, back here, then down to where it started.
You should see: blue on the way to each waypoint, green at
each one, and how close it stopped, printed three times.
"""

import time

import academy

academy.connect("http://localhost:8000")  # your simulator
robot = academy.bot("000000")  # its first robot

x, y = robot.position()
waypoints = [(x + 200, y), (x, y), (x, y + 300)]  # right, back, down

for target in waypoints:
    # 1. Blue, send it there, and wait until it has arrived.
    robot.led(0, 0, 255)
    robot.goto(target[0], target[1])
    while not robot.arrived():
        time.sleep(0.5)
    # 2. Green, and how close it stopped.
    robot.led(0, 255, 0)
    off = academy.distance(robot.position(), target)
    print(f"Stopped {off:.0f} mm from the waypoint")

Core now on the real robot

The same scripts run on your real robot: only the address changes, and the label. In each script, change the two lines at the top, as in step 6:

academy.connect("https://perry-one-indicates-measurements.trycloudflare.com")  # the real controller
robot = academy.bot("A1B2C3")  # the label on your robot

Check that the floor is free about 300 mm above your robot and 200 mm to its right. Then run them in the same order as above, go_there.py, wait_until_there.py, how_close.py, then waypoints.py: each one starts where the one before left the robot.

You should see: the same trips as in the simulator, but not as tidy. The robot curves and corrects on its way, arrived() can come a little earlier or later, and how_close.py prints a few centimetres rather than a few millimetres. It still ends near where it started: unlike step 6’s drive(), goto() checks its position all the way.

Coordinates belong to the controller you talk to. Your simulator and the room’s floor use different maps, so a target computed from robot.position(), like (x, y - 300), works on both, while a fixed one such as goto(1000, 1200) must be read off the map of the controller you are connected to.

  • cannot unpack non-iterable NoneType: position() returned None, so the lighthouse cannot see the robot. Call the instructor.
  • The robot never arrives: it is circling close to the target. Stop the script with Ctrl+C, check that the floor around the target is free, and run it again.