Today’s mission 5 min
Drive to a station while the arm gets ready, and stop any job that takes too long.
Time your current pick-up program first. Write the number down. Most of that time the robot is doing one thing while the other motors wait. Today you overlap them, and time it again.
Two minutes is not long. Saving three seconds at five stations is fifteen seconds. That can be one more mission scored.
The idea — tasks that take turns very fast 10 min
The hub has one brain. It cannot really do two things at once. It switches between jobs many times a second, so fast that it looks like both happen together. Each job is called a task.
Pybricks needs four new words for this:
async def— this function is a task, and can share the hub with others.await— “wait for this line to finish, and let other tasks run meanwhile”.multitask(a, b)— run tasks a and b together, and carry on when both are done.run_task(main())— the last line of the file. It starts the whole thing.
The rule you will forget. Inside a task, every move and every wait needs await in front. Forget it, and the program does not wait for that line. Your robot races ahead to the next one.
multitask(…, race=True) is different. It stops as soon as the first task finishes and cancels the rest. Put a wait(3000) in the race, and nothing can run for more than three seconds. That is a timeout.
Blocks vs Python 15 min
In SPIKE you did this with two stacks under two “when program starts” hats. They both start, but neither knows when the other finishes.
SPIKE word blocks
when program starts :: events hat set movement motors to (A+B v) :: movement move [↑ v] for (50) [cm v] :: movement turn on [smile v] :: display when program starts :: events hat (C v) set speed to (50) % :: motors (C v) go to relative position (140) :: motors
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.pupdevices import Motor from pybricks.parameters import Port, Direction, Icon from pybricks.robotics import DriveBase from pybricks.tools import multitask, run_task hub = PrimeHub() left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) arm = Motor(Port.C) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) robot.use_gyro(True) async def main(): await multitask( robot.straight(500), arm.run_target(300, 140), ) hub.display.icon(Icon.HAPPY) run_task(main())
| Block | Python | Watch out |
|---|---|---|
| two "when program starts" stacks | multitask(a, b) | The blocks never meet again. multitask waits for both, then carries on. |
| (nothing) | async def main(): | Any function that uses await must be async. |
| (nothing) | await multitask(…) | No await, no waiting: the smile would appear at once. |
| (nothing) | run_task(main()) | Only once, on the last line. Never inside a task. |
| move ↑ for 50 cm | robot.straight(500) | Still millimetres. |
Now the timeout. SPIKE has nothing like it.
SPIKE word blocks
Pybricks Python
from pybricks.tools import wait, multitask, run_task # robot and sensor are set up as before async def follow_forever(): while True: error = await sensor.reflection() - 50 robot.drive(150, error * 1.2) await wait(10) async def main(): await multitask(follow_forever(), wait(3000), race=True) robot.stop() print("Timed out safely") run_task(main())
| Block | Python | Watch out |
|---|---|---|
| (no block) | race=True | Stops at the FIRST task to finish and cancels the others. |
| (no block) | await sensor.reflection() | Inside a task, sensor reads are awaited too. |
| (no block) | await wait(10) | A loop inside a task must await something, or the other tasks never get a turn. |
Write it from scratch 20 min
Open a blank file called pickup_fast. Do not convert your base library today. Build one new file:
- The setup lines and an empty async main. Put
await robot.straight(200)inside it and run. If it drives, your async skeleton works. - Home the arm with await.
await arm.run_until_stalled(-200, duty_limit=30), thenarm.reset_angle(0). Reset does not move anything, so it needs no await. - Drive and lower together. Use
multitaskto drive to the object while the arm goes toARM_DOWN. - Lift and turn together. Lift to
ARM_CARRYwhile turning towards the drop zone.
The shape of main
async def main(): await arm.run_until_stalled(-200, duty_limit=30) arm.reset_angle(0) await multitask( robot.straight(400), arm.run_target(300, ARM_DOWN), ) await multitask( robot.turn(90), arm.run_target(300, ARM_CARRY), ) await robot.straight(250) await arm.run_target(300, ARM_DOWN) run_task(main())
Only overlap what is safe. Lifting while you drive away is fine. Lowering while you drive into an object can knock it over. Ask: would I be happy if this move finished first?
Test and measure 15 min
- Time the old pick-up program and the new one, three runs each. How many seconds did multitasking save?
- Did accuracy change? Compare where the object lands. If the robot is less accurate, which overlap caused it?
- Test the timeout: lift the robot off the line while it follows. Does it stop after 3 seconds and print the message?
- Change the timeout to 1000 and to 10000. What is the shortest timeout that never cuts off a good run?
Break it, fix it 10 min
One mistake each. Run it, watch what the robot does, and explain the cause before you fix it.
Bug 1 — the arm moves before the robot arrives
async def main():
robot.straight(400)
await arm.run_target(300, ARM_DOWN)
run_task(main())Bug 2 — SyntaxError
def main():
await multitask(
robot.straight(400),
arm.run_target(300, ARM_DOWN),
)
run_task(main())Bug 3 — the timeout never fires
async def follow_forever():
while True:
error = await sensor.reflection() - 50
robot.drive(150, error * 1.2)
async def main():
await multitask(follow_forever(), wait(3000), race=True)
robot.stop()Bug 3 is the sneaky one. A loop with no await never lets the other tasks have a turn. The timer is waiting for a turn that never comes.
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.
Beat your own time. Take your lesson 13 solo program (pick, lift, place). Rewrite it as an async program that:
- overlaps at least two moves safely;
- uses a
race=Truetimeout on any move that could get stuck; - beeps with
await hub.speaker.beep()when it finishes.
Done means: it is faster than the lesson 13 version, and it still places the object correctly three times in a row.
Engineering journal 5 min
Today’s entry:
- old time and new time for the pick-up, three runs each
- which moves you overlapped, and one you decided not to
- the timeout you chose, and the test that proved it
- the await rule, in your own words