Today’s mission 5 min
Teach the robot to straighten itself up on a black line and against a wall, so every leg starts from a known place.
The gyro knows how far the robot has turned. It does not know where the robot is. Small errors still add up over a run. A square-up wipes them out, because the line and the wall do not move. You did a one-sensor version in Level 4 lesson 6. Today you use both colour sensors from the Expansion Set.
The idea — each side finds the line 10 min
Mount the two colour sensors at the front, one on each side, the same height above the mat. Drive slowly towards a black line that crosses your path.
- Both wheels drive forward slowly.
- When the left sensor sees black, the left motor stops and holds.
- When the right sensor sees black, the right motor stops and holds.
- Both sensors are now on the line, so the robot is square to it.
The two sides must be able to stop separately. So today you drive the motors directly, not the drive base. Stop the drive base first, then use left.run(), right.run() and hold(). Motor speed is in degrees per second, not mm per second.
A wall square-up is simpler. Reverse slowly into a wall. When the wheels cannot turn any more, the back of the robot is flat against it. Stop after a time limit as well, in case the stall is never noticed.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat (A v) start motor [↺ v] :: motors (B v) start motor [↻ v] :: motors repeat until <<(E v) reflection [< v] (20) %? :: sensors> and <(F v) reflection [< v] (20) %? :: sensors>> if <(E v) reflection [< v] (20) %? :: sensors> then (A v) stop motor :: motors end if <(F v) reflection [< v] (20) %? :: sensors> then (B v) stop motor :: motors end end (A v) stop motor :: motors (B v) stop motor :: motors
Pybricks Python
from pybricks.pupdevices import ColorSensor left_eye = ColorSensor(Port.E) right_eye = ColorSensor(Port.F) BLACK = 20 robot.stop() left.run(150) right.run(150) left_done = False right_done = False while not (left_done and right_done): if not left_done and left_eye.reflection() < BLACK: left.hold() left_done = True if not right_done and right_eye.reflection() < BLACK: right.hold() right_done = True wait(5) robot.reset()
| Block | Python | Watch out |
|---|---|---|
| A start motor counterclockwise | left.run(150) | Direction.COUNTERCLOCKWISE in the set-up already flips it. Speed is deg/s. |
| E reflection < 20 %? | left_eye.reflection() < BLACK | Same 0-100 % scale on both. |
| A stop motor | left.hold() | hold() keeps the wheel locked in place. stop() lets it roll. |
| (nothing) | left_done = True | A flag remembers that this side has finished. |
| (nothing) | robot.reset() | Square now, so call this point distance 0, angle 0. |
In the blocks, a stopped motor gets “stopped” again on every pass of the loop. It works, but it is wasteful. The Python flags make each side stop exactly once.
Write it from scratch 20 min
New file, square_up. Set-up from memory, gyro on, plus the two colour sensors. First measure:
- Print both reflections over white mat and over the black line. If the two sensors disagree by more than 10, raise or lower one.
- Choose
BLACKabout a third of the way from black to white.
Now write square_on_line(speed) as a function, like lesson 6. Then write the wall version:
Wall square-up
from pybricks.tools import StopWatch def square_on_wall(speed=-100, limit=2000): clock = StopWatch() robot.drive(speed, 0) wait(300) while not robot.stalled() and clock.time() < limit: wait(10) robot.stop() robot.reset()
Why wait(300) before checking? A robot that is just starting is not moving yet either. Give it time to get going, or it will “stall” the moment it starts.
Test and measure 15 min
Start the robot at a deliberately bad angle to the line: 10°, then 20°, then 30°. Run square_on_line. Print hub.imu.heading() before and after.
- Does it end square at every angle? Where does it fail?
- Try speed 100, 200 and 400. When does it overshoot the line?
- For the wall: does it finish flat every time? Push a pencil behind each back corner to check for a gap.
Write down the fastest speed that squares up correctly ten times out of ten. That is your square-up speed for the season.
Break it, fix it 10 min
One mistake in each. Find it by running and watching.
Bug 1 — it turns instead of squaring
while not (left_done and right_done):
if not left_done and left_eye.reflection() < BLACK:
left.hold()
left_done = True
if not right_done and left_eye.reflection() < BLACK:
right.hold()
right_done = True
wait(5)Bug 2 — it never stops
left.run(150)
right.run(150)
left_done = False
right_done = False
while not (left_done and right_done):
if left_eye.reflection() < BLACK:
left.hold()
if right_eye.reflection() < BLACK:
right.hold()
wait(5)Bug 3 — it squares up, then the next move is wrong
square_on_line(150) hub.imu.reset_heading(0) robot.straight(200)
Bug 2 is a missing flag. The motors stop, but the loop is waiting for two variables that never change. A loop only ends when its condition can become false. Check that something inside it can change.
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.
Line, then wall. Start anywhere in a 20 cm box, at any angle up to 30° to the line.
- Drive forward, square up on the black line.
- Turn 180° with the gyro.
- Reverse into the wall and square up on it.
- Drive out 150 mm and stop.
Done means: five starts chosen by another team, and the robot finishes in the same place each time, within 10 mm.
Engineering journal 5 min
- both sensors’ white and black readings, and your BLACK value
- your square-up speed, and the speed where it failed
- the largest starting angle that still squared up
- why the gyro alone cannot replace a square-up
- where on your WRO mat you could square up