Today’s mission 5 min
Set up your robot as a Pybricks DriveBase and calibrate it until 1000 mm really is 1000 mm and a full turn really is 360 degrees.
This is your competition robot for the rest of the level. Left motor on A, right motor on B, colour sensors on E and F, C and D kept free for attachments. Write the ports on a label stuck to the hub. You will thank yourself in lesson 23.
The idea — tell Pybricks how big your robot is 10 min
In blocks you told SPIKE how far one motor rotation moves the robot. In Pybricks you give two measurements, and it does the maths for you:
- wheel_diameter — the wheel’s width across, in millimetres. The SPIKE Prime wheel is about 56 mm.
- axle_track — the distance between the two places where the wheels touch the floor, in millimetres. Measure yours with a ruler. Many SPIKE robots are around 112 mm.
From those two numbers, the drive base can:
robot.straight(300)— drive 300 mm forward. A minus number reverses.robot.turn(90)— turn the robot 90 degrees on the spot.robot.settings(straight_speed=300)— set the speed in mm per second.
Why the left motor is COUNTERCLOCKWISE. The two motors face opposite ways. To drive forward, one must turn clockwise and the other counterclockwise. Telling Pybricks which is which means a positive number always means forward.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat set movement motors to (A+B v) :: movement set 1 motor rotation to (17.6) [cm v] moved :: movement set movement speed to (50) % :: movement move [↑ v] for (30) [cm v] :: movement move (right: 100) for (180) [degrees v] :: movement move [↑ v] for (30) [cm v] :: movement move (left: -100) for (180) [degrees v] :: movement
Pybricks Python
from pybricks.pupdevices import Motor from pybricks.parameters import Port, Direction from pybricks.robotics import DriveBase left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) robot.settings(straight_speed=250) robot.straight(300) robot.turn(90) robot.straight(300) robot.turn(-90)
| Block | Python | Watch out |
|---|---|---|
| set movement motors to A+B | DriveBase(left, right, …) | Pybricks also needs which way each motor faces. |
| set 1 motor rotation to 17.6 cm | wheel_diameter=56 | Give the diameter in mm; Pybricks works out the rest. |
| set movement speed to 50 % | settings(straight_speed=250) | Percent vs mm per second. |
| move ↑ for 30 cm | robot.straight(300) | cm vs mm: multiply by 10. |
| move right: 100 for 180 degrees | robot.turn(90) | SPIKE counts MOTOR degrees. Pybricks counts ROBOT degrees. |
| move left | robot.turn(-90) | The sign chooses the side. Find out which side is positive. |
The SPIKE turn above only turns about 90 degrees on one robot with one wheel spacing. Change the robot and the number is wrong.robot.turn(90) is 90 degrees on every robot, once you have measured it.
Write it from scratch 20 min
New file: calibrate. You will keep this file all level.
- Measure. Write your wheel diameter and axle track in the journal before typing anything.
- Set up the drive base from a blank file: imports, two motors, one
DriveBase. - Straight test. Put the robot behind a line on the floor. Run
robot.straight(1000). Measure how far it really went, in mm. - Turn test. Line the robot up with a straight edge. Run
robot.turn(360)three times. Did it come back to the edge, or past it?
Fix the numbers with two formulas
# Straight: asked for 1000, it really went 'measured' mm. # new wheel_diameter = old wheel_diameter * measured / 1000 # # Turn: asked for 360, it really turned 'actual' degrees. # new axle_track = old axle_track * 360 / actual # # Example: drove 985 mm -> 56 * 985 / 1000 = 55.2 # turned 370 deg -> 112 * 360 / 370 = 109 robot = DriveBase(left, right, wheel_diameter=55.2, axle_track=109)
Change one number, test again, write the result down. Stop when the straight test is within 5 mm and the turn test comes back to the edge.
Why the turn formula works. If the robot turns too far, Pybricks thinks the wheels are further apart than they are, so it turns the motors too much. A smaller axle track turns them less.
Test and measure 15 min
- After calibration, run
robot.straight(1000)three times. Record all three distances. How much do they vary? - Try
straight_speedof 150, 400 and 700. Does the distance change at high speed? Write down the fastest speed that still lands within 10 mm. - Add
print(robot.distance(), robot.angle())after a move. Does the robot’s own count agree with your ruler? - Find out which way
turn(90)goes. Draw an arrow in your journal.
Break it, fix it 10 min
None of these three shows an error. Each robot does the wrong thing. Predict what it does before you run it.
Bug 1 — the robot spins instead of driving
left = Motor(Port.A) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) robot.straight(300)
Bug 2 — the robot drives off the table
left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=5.6, axle_track=11.2) robot.straight(300)
Bug 3 — the robot hardly moves
left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) # drive 30 cm to the box robot.straight(30)
Ask: is it a unit? When a move is ten times too big or too small, the bug is almost always cm written where mm was needed.
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.
Back where you started. Mark a cross on the floor with tape. Write square, which:
- drives a square with 400 mm sides and stops on the cross;
- then drives a triangle with 400 mm sides and stops on the cross;
- beeps once at every corner;
- uses a loop, not eight copied lines.
Done means: after both shapes, the robot’s centre is within 20 mm of the cross, two runs in a row.
Engineering journal 5 min
- your robot’s ports, labelled on a sketch
- your measured and your calibrated
wheel_diameterandaxle_track - the three straight-test distances after calibration
- which way a positive turn goes
- the fastest speed that stayed accurate
Keep these numbers safe. Every program for the rest of the level starts with them. If the robot is rebuilt, they must be measured again.