Today’s mission 5 min
Measure how much your hub’s gyro drifts, then make every turn land on its heading, not just near it.
In Level 4 block 3 you fought the EV3 gyro: a separate sensor, drift, lag and a reset routine. The SPIKE Prime hub has its gyro built in. Pybricks reads it for you and can steer with it automatically. Today you find out how good it is, with numbers.
Why it matters at WRO. A turn that is 3° out puts the robot about 5 cm off target after one metre. Every leg after that starts from the wrong place. Accurate turns are the cheapest points on the mat.
The idea — heading, drift and readiness 10 min
The hub measures how fast it is spinning, many times a second, and adds it up. That total is the heading: how far the robot has turned since the program started. In Pybricks it is hub.imu.heading(), in degrees.
- Clockwise is positive. Turn right and the heading goes up.
robot.turn(90)turns right. - It does not wrap. Two full turns right reads 720, not 0. SPIKE’s yaw block jumps from 179 to −180.
- It drifts. Small errors add up, so a still robot slowly “turns”. Today you measure how much.
- It must start still. The hub learns what “not spinning” looks like while it is still.
hub.imu.ready()isTrueonce it has.
The big step is one line: robot.use_gyro(True). After it, straight() holds its heading and turn() stops when the gyro says the turn is done. In Level 4 you wrote that correction yourself. Now it is built in.
Blocks vs Python 15 min
A gyro turn of 90° to the right. In SPIKE you build it yourself: zero the yaw, start turning, wait for the angle, stop. In Pybricks the drive base does all four steps.
SPIKE word blocks
when program starts :: events hat set movement motors to (A+B v) :: movement set yaw angle to (0) :: sensors start moving [↻ v] :: movement wait until <([yaw v] angle :: sensors) > (90)> stop moving :: movement
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.pupdevices import Motor from pybricks.parameters import Port, Direction from pybricks.robotics import DriveBase from pybricks.tools import wait hub = PrimeHub() left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) while not hub.imu.ready(): wait(10) robot.use_gyro(True) robot.turn(90) print("heading", hub.imu.heading())
| Block | Python | Watch out |
|---|---|---|
| set yaw angle to 0 | (program start) | The heading starts at 0 when the program starts. Keep the robot still. |
| (nothing) | while not hub.imu.ready(): wait(10) | Waits until the gyro has settled. SPIKE hides this step. |
| start moving right … wait until yaw > 90 … stop | robot.use_gyro(True) then robot.turn(90) | Pybricks slows down before 90 and holds there. The blocks overshoot. |
| yaw angle (pitch/roll/yaw reporter) | hub.imu.heading() | Yaw wraps at 180. Heading keeps counting: 450 means a turn and a quarter. |
The blocks stop after the robot passes 90°, so it always overshoots a little. You fixed that in Level 4 with a slower finish. Pybricks plans the slow-down for you.
Write it from scratch 20 min
New file, gyro_lab. Start from a blank page. Type the robot set-up from memory: imports, hub, two motors, drive base. Check it against lesson 4 only if it will not run.
Part 1 — watch the heading. Print the heading twice a second for ten seconds. Turn the robot slowly by hand.
Part 1 — after the set-up lines
while not hub.imu.ready(): wait(10) for i in range(20): print(hub.imu.heading()) wait(500)
Turn it right a quarter turn. Does the number go up or down? Now open the same robot in the SPIKE App, if you have it, and check which way its yaw goes. Write both in your journal.
Part 2 — the square. Drive a 300 mm square: four straights and four turns of 90°. Run it with robot.use_gyro(False), then with robot.use_gyro(True). Print the heading at the end of each.
Part 2
robot.use_gyro(True) for side in range(4): robot.straight(300) robot.turn(90) print("end heading", hub.imu.heading())
A perfect square ends on heading 360. The difference from 360 is your error for the whole square.
Test and measure 15 min
Fill this table in your journal. Three runs each.
- Drift. Robot still on the table for 60 seconds. Print the heading every 10 seconds. How many degrees per minute?
- Square, gyro off. End heading, and how far the robot finished from its start mark, in mm.
- Square, gyro on. The same two numbers.
- Bad start. Press start and nudge the robot in the first second. Run the square. What changed?
A WRO run lasts two minutes. Multiply your drift by two. If it is under 1°, drift is not your problem: the start is.
Break it, fix it 10 min
One mistake in each. Run it, read the terminal and fix it alone. Assume the usual set-up lines are above each one.
Bug 1
robot.use_gyro(true) robot.turn(90)
Bug 2
robot.use_gyro(True) robot.straight(300) hub.imu.reset_heading(0) robot.turn(90)
Bug 3
robot.use_gyro(True)
robot.turn(90)
print("heading", hub.imu.heading)Bug 2 is the one you will meet at a competition. While the drive base is using the gyro, Pybricks will not let you reset the heading underneath it. Use robot.reset() to zero the drive base instead, or reset the heading before use_gyro(True). Bug 3 prints something strange rather than crashing: a method without its brackets is the method itself, not its value.
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 triangle. From a start mark, drive a triangle with sides of 400 mm and come back to the mark, facing the way you started.
- Work out the turn angle yourself. It is not 60°.
- Wait for the gyro to be ready before the first move.
- Print the end heading and the error from a perfect finish.
Done means: three runs in a row finish within 15 mm of the mark and 2° of the start heading.
Engineering journal 5 min
- which way heading counts, and which way SPIKE yaw counts
- your drift in degrees per minute
- the square table: gyro off against gyro on
- what happens if the robot moves in the first second
- how to zero the drive base when the gyro is in use