Today’s mission 5 min
Turn your favourite moves into functions, then write a whole route from a blank file, alone.
In EV3 Level 4 you built My Blocks so a long program read like a plan. Python functions do the same job, and one more: they can hand a value back. Today ends Block 1 with a milestone run.
The idea — name a job, then call it 10 min
def drive_to_line(speed):defines a function calleddrive_to_line. Everything indented under it is its body. Defining it does not run it.drive_to_line(250)calls it. Now the body runs, withspeedset to 250.speedis a parameter — the input slot on a My Block.return averagehands a value back to whoever called the function, and ends it.
A competition file has three parts, always in this order:
- Setup — imports, motors, sensors, the drive base.
- Functions — every
def. - The run — short lines that call the functions, so it reads like the route plan.
Blocks vs Python 15 min
SPIKE word blocks
define drive to line (speed) :: myblocks set movement speed to (speed :: myblocks) % :: movement start moving (straight: 0) :: movement wait until <(E v) reflection [< v] (50) %? :: sensors> stop moving :: movement when program starts :: events hat drive to line (40) :: myblocks move [↓ v] for (5) [cm v] :: movement drive to line (20) :: myblocks
Pybricks Python
BLACK_LINE = 50 def drive_to_line(speed): robot.drive(speed, 0) while left_eye.reflection() > BLACK_LINE: wait(10) robot.brake() drive_to_line(250) robot.straight(-50) drive_to_line(120)
| Block | Python | Watch out |
|---|---|---|
| define drive to line (speed) | def drive_to_line(speed): | No spaces in a Python name: use underscores. |
| speed (the pink input) | speed | Inside the function it is an ordinary variable. |
| drive to line (40) | drive_to_line(250) | % vs mm per second again. |
| (blocks anywhere on the canvas) | (def above the call) | Python reads top to bottom. Define first, call after. |
SPIKE word blocks
Pybricks Python
def average_reflection(sensor, samples): total = 0 for i in range(samples): total = total + sensor.reflection() wait(5) return total / samples white = average_reflection(left_eye, 10) print("white reads", white)
| Block | Python | Watch out |
|---|---|---|
| (no block) | return total / samples | The answer comes back to the line that called it. |
| (no block) | average_reflection(left_eye, 10) | The sensor itself can be an input. Same function, either eye. |
Averaging ten readings gives a steadier number than one. You will use this function in lesson 9 and again in lesson 15.
Write it from scratch 20 min
New file: moves. Setup at the top, functions in the middle, the run at the bottom.
- Write four functions:
forward(mm),left(),right()anddrive_to_line(speed).leftandrighteach turn 90 degrees. - Write
average_reflectionand print the average for white and for black. - Write the run using only your function names. Read it aloud. It should sound like directions.
The shape of every competition file
# ---- setup ---- from pybricks.hubs import PrimeHub from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Direction from pybricks.robotics import DriveBase from pybricks.tools import wait hub = PrimeHub() left_motor = Motor(Port.A, Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.B) robot = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112) left_eye = ColorSensor(Port.E) # ---- functions ---- def forward(mm): robot.straight(mm) def left(): robot.turn(-90) def right(): robot.turn(90) # ---- the run ---- forward(300) right() forward(200) left()
Put your calibrated numbers from lesson 4 in the DriveBase line, not 56 and 112.
Test and measure 15 min
- Print
average_reflection(left_eye, 1)five times, thenaverage_reflection(left_eye, 20)five times. Which set of numbers is steadier? - Give
forwarda second parameter,speed, with a default:def forward(mm, speed=300):. Call it both asforward(200)andforward(200, 600). Userobot.settings(straight_speed=speed)inside. - Change the turn angle in
right()once. How many places in the run did that fix? That is why functions exist.
Break it, fix it 10 min
Bug 1
forward(300)
def forward(mm):
robot.straight(mm)Bug 2 — prints None
def average_reflection(sensor, samples):
total = 0
for i in range(samples):
total = total + sensor.reflection()
total / samples
print(average_reflection(left_eye, 10))Bug 3
def drive_to_line(speed):
robot.drive(speed, 0)
while left_eye.reflection() > 50:
wait(10)
robot.brake()
drive_to_line()None means “no answer was handed back”. A function without return still runs, but gives None. If you print a function’s result and see None, look for the missing return.
Milestone — the blank-file route 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page and every earlier program. You may use your journal numbers and the Pybricks documentation, nothing else.
Your coach tapes a start box, three crosses (1, 2, 3) and a black line on the floor, and shows you the order. In a new, empty file, write milestone1, which:
- starts in the box and visits crosses 1, 2 and 3 in order;
- beeps on each cross;
- finishes by driving to the black line with your
drive_to_linefunction, and stops on it; - uses functions, so the run is no more than ten lines;
- has setup, functions and the run in that order.
Done means: two runs in a row, each cross touched by the robot’s centre, finished in under 12 minutes from the blank file.
Engineering journal 5 min
- the three parts of a competition file, in order
- each function you wrote, and what its parameters mean
- the steadiness test: 1 sample against 20
- the milestone: time taken, runs needed, what went wrong first
- Block 1 review: which Python idea still feels harder than its block?