Today’s mission 5 min
Gather every move you have built into one library, then use it to run a timed route on the mat.
You now own a lot of code: set-up, gyro turns, smooth straights, square-ups, a PID follower. It is spread over eight programs. Today it goes into one program called base. From now on, every mission starts by importing it. Fix a bug in base once, and every mission is fixed.
The score is your worst run. Three timed runs of the route. A fast run and two crashes scores a crash. This is how to think about WRO from now on.
The idea — a library and an import 10 min
In Level 4 you made My Blocks so a long route read like a list of jobs. A library goes one step further. It is a whole program of functions that other programs can borrow.
baseholds the set-up and the functions. It never moves the robot by itself.- A mission program starts with
from base import robot, gyro_turn, follow_for. Then it uses them as if they were written there. - Importing a program runs it from top to bottom. That is how
robotgets set up. It is also why a test move left inbasewould run every time.
Good function names make a mission read like the plan on your whiteboard. Choose names a teammate would guess without asking.
Blocks vs Python 15 min
SPIKE word blocks
define gyro turn (angle) set yaw angle to (0) :: sensors start moving [↻ v] :: movement wait until <([yaw v] angle :: sensors) > (angle)> stop moving :: movement when program starts :: events hat gyro turn (90) :: myblocks move [↑ v] for (30) [cm v] :: movement gyro turn (45) :: myblocks
Pybricks Python
def gyro_turn(angle): robot.turn(angle) gyro_turn(90) robot.straight(300) gyro_turn(45)
| Block | Python | Watch out |
|---|---|---|
| define gyro turn (angle) | def gyro_turn(angle): | Same idea. The input is a parameter. |
| gyro turn (90) | gyro_turn(90) | Python names cannot contain spaces. Use underscores. |
| (only turns right) | robot.turn(angle) | A negative angle turns left in Pybricks. The block needs a second version. |
SPIKE word blocks
Pybricks Python
# the program called "route" from base import robot, gyro_turn, follow_for, square_on_line square_on_line(150) follow_for(600, 200) gyro_turn(90) robot.straight(250)
| Block | Python | Watch out |
|---|---|---|
| (no block) | from base import gyro_turn | Borrows the function from the program called base. |
| (no block) | import a name base does not have | ImportError. Check the spelling in both programs. |
Write it from scratch 20 min
New program, base. Copy nothing. Rewrite each part from your earlier programs, improving names as you go. It should contain:
- Set-up: imports, hub, motors, both colour sensors, drive base with your measured wheel and axle, your chosen settings,
use_gyro(True). - Constants:
TARGET,BLACK,KP,KI,KDat the top, with a comment saying when you measured them. - Functions:
gyro_turn,square_on_line,square_on_wall,follow_for,follow_to_junction. - One PID step shared by both followers, so the formula exists once.
One PID step, used by both followers
def pid_step(state, speed): error = left_eye.reflection() - TARGET state[0] = state[0] + error derivative = error - state[1] robot.drive(speed, KP * error + KI * state[0] + KD * derivative) state[1] = error def follow_for(distance, speed=150): state = [0, 0] start = robot.distance() while robot.distance() - start < distance: pid_step(state, speed) wait(10) robot.stop()
state is a list holding the integral and the last error. Each follow makes a fresh one, so nothing carries over. Then make a second program, test_base, that imports each function and tries it once.
Test and measure — the milestone run 15 min
Your teacher marks a route on the mat with at least: one square-up, one line follow to a junction, one follow for a distance and two gyro turns. Write the program route using only functions from base.
- Three timed runs. Record the time and the finish error.
- The score is the worst of the three.
- Between runs you may change constants in
base, but you must write down every change.
Break it, fix it 10 min
Bug 1 — the robot moves when the route only imports
# the last lines of base
def gyro_turn(angle):
robot.turn(angle)
gyro_turn(90)Bug 2 — route will not start
# base defines: def gyro_turn(angle): from base import robot, gyroturn gyroturn(90)
Bug 3 — follow_for crashes inside base
# in base: no colour sensors are set up
def follow_for(distance, speed=150):
state = [0, 0]
start = robot.distance()
while robot.distance() - start < distance:
pid_step(state, speed)
wait(10)
robot.stop()
# in route:
from pybricks.pupdevices import ColorSensor
from pybricks.parameters import Port
left_eye = ColorSensor(Port.E)
from base import follow_for
follow_for(500)Bug 3 is about where a name lives. A function in base only sees names made in base. Making left_eye in route does not help it. Everything the library uses must be set up in the library.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
The unseen route. Another team draws you a new route on the mat. You have ten minutes.
- Plan it on paper as a list of function calls.
- Write
route2using onlybase. You may not editbaseduring these ten minutes. - Run it once for real.
Done means: the route finishes within 30 mm of the mark on its first or second run, and every line of route2 after the import is one function call.
Engineering journal 5 min
- the list of functions in base, with one line on what each does
- every constant, and the date you measured it
- the three milestone times, the finish errors and your score
- each change made between runs, and whether it helped
- the rule: base never moves the robot by itself