6. My script moves a real robot
The same script, on the floor
The problem. Your step 4 script drives a simulated robot on your laptop. The simulator promised that the same code runs unchanged on the real controller: here you check that promise, on one of your two robots.
This step runs the scripts of step 4 again, in the same order: light it, drive it, bring it back, turn it, and the square if there is time. Only two lines change, the two you met in step 5: the controller’s address, and the label of your robot.
Work in ~/dotbot-academy, with the environment active, next to academy.py. Your simulator can stay running: nothing here talks to it.
Before you run anything
- Pick one of your two robots, and keep its label from step 5 at hand.
- The robot faces the top of the floor. Check that the floor is free about 300 mm in front of it and to its left.
- Put a small mark beside it, a coin or a piece of paper, out of its way: you will want to know where it started.
Hello: light it
Save real.py (below) next to academy.py. It is step 4’s first hello.py, with the two lines that change at the top: set BASE_URL to the instructor’s controller, as in connect.py, and LABEL to your robot’s label. Then run it:
real.py
"""Step 6: my script moves a real robot.
Step 4's hello.py, with the two lines that change on the floor at the top:
the instructor's controller and the label on your robot.
You should see: your robot on the floor turns red, then green, then blue,
one second apart, and so does its dot on the instructor's map.
"""
import time
import academy
BASE_URL = "http://192.168.1.10:8000" # TODO: the instructor's controller
LABEL = "A1B2C3" # TODO: the label on one of your two robots
academy.connect(BASE_URL)
robot = academy.bot(LABEL)
robot.led(255, 0, 0) # red: red, green, blue, each 0 to 255
time.sleep(1)
robot.led(0, 255, 0) # green
time.sleep(1)
robot.led(0, 0, 255) # bluepython real.pyYou should see: your robot turns red, then green, then blue, one second apart, on the floor and as a dot on the instructor’s map.
Core drive it, and bring it back
Add the same lines as in step 4, Core: drive it, to the end of real.py: forward at 50 mm/s for 2 seconds, then back the same way. Run it again.
You should see: the robot drives about 100 mm up the floor, then backs up and stops close to your mark. Close, not exactly on it: in the simulator it came back within a few millimetres.
Core turn it
Add the spin from step 4, Core: turn it: to the left, at 50 mm/s, for 5 seconds. Run it again.
You should see: after the drive there and back, the robot turns left on the spot, about one full turn. Does it end facing the same way as your simulated robot did?
real.py
"""Step 6: my script moves a real robot.
Step 4's hello.py, drive, back and spin, with the two lines that change on
the floor at the top: the instructor's controller and the label on your robot.
You should see: your robot changes color, drives about 100 mm up the floor,
backs up close to where it started, then turns left on the spot, about one
full turn.
"""
import time
import academy
BASE_URL = "http://192.168.1.10:8000" # TODO: the instructor's controller
LABEL = "A1B2C3" # TODO: the label on one of your two robots
academy.connect(BASE_URL)
robot = academy.bot(LABEL)
robot.led(255, 0, 0) # red: red, green, blue, each 0 to 255
time.sleep(1)
robot.led(0, 255, 0) # green
time.sleep(1)
robot.led(0, 0, 255) # blue
robot.drive(speed=50, seconds=2) # 50 mm/s for 2 s: about 100 mm
robot.drive(speed=-50, seconds=2) # back the same 100 mm, to the start
robot.spin(speed=-50, seconds=5) # left, on the spot: about a full turnChallenge: a square
The instructor may skip it. Step 7 needs nothing from it.
Take your square.py from step 4, the challenge, and change its BASE_URL and LABEL as in real.py, nothing else. Check that the floor is free about 300 mm to the robot’s left too, then run python square.py.
You should see: the robot drives four sides of about 200 mm, a new color on each, turning left at each corner. It does not end where it started, and maybe not facing the same way.
QUARTER_TURN again
The value that made a square in the simulator is not the one for this robot on this floor. Watch the first corner: too wide, make QUARTER_TURN a little smaller; too tight, a little larger. Then run it again.
What differs from the simulator
The code is the same; the robot is not. A few things you may have noticed:
- It drifts.
drive()andspin()run the wheels for a time and never check where the robot ended up. Each small error adds up, so the square does not close and the trip back stops near the start, not on it. QUARTER_TURNneeds re-tuning. A turn for a time gives a different angle on each robot and each floor.- The wheels slip. On a smooth or dusty floor, when it starts and when it stops, a wheel turns without the robot moving quite as far. The simulator’s wheels never slip.
- Charge runs down. A DotBot runs on supercapacitors, not a battery. A robot low on charge moves sluggishly, or stops answering: tell the instructor, it goes on the charger.
- The dot on the map is measured. In the simulator it is computed; here the lighthouse measures it, so it jitters a little even when the robot stands still.
Step 7 answers the drift: it sends the robot to a position, and the robot checks where it is until it gets there.
LookupError: 0 robots match: the label has a typo, or the robot is off. Check it in the console’s List view.ConnectionError:BASE_URLis wrong, or your laptop is not on the workshop Wi-Fi.- The robot lights up but does not move, or stops halfway: it may be low on charge. Call the instructor.