Today’s mission 5 min
Build one program that lets you pick any mission with the hub buttons, run it, and come straight back to the menu.
By now you have several mission programs. Starting each one from a laptop is slow, and it is easy to start the wrong one. A mission runner lives on the hub. You choose with the arrows, press the centre and the robot goes.
Before you drive today: the centre button will no longer stop the program. The Bluetooth button becomes the emergency stop. Find it on your hub now.
The idea — a menu, and a table of missions 10 min
Three pieces fit together:
- Each mission is a function. File
m1hasdef run():, filem2has its owndef run():, and so on. - A dictionary links symbols to missions.
{"1": m1.run, "2": m2.run}. Notice: no brackets afterrun. You are storing the function, not running it yet. hub_menushows the choices on the hub. Left and right change the symbol; the centre chooses. It returns the symbol you picked.
Then the runner waits for a centre press, so you can place the robot first. Wrap it all in while True and the hub returns to the menu after every mission. You never touch the laptop.
Why move the stop button? After choosing, you place the robot and press the centre button to say “go”. Normally the centre button stops the program, so that press would end it. The runner gives the stop job to another button: hub.system.set_stop_button(Button.BLUETOOTH).
Blocks vs Python 15 min
In SPIKE you could start different stacks from different buttons. That gives you two choices, at most three.
SPIKE word blocks
when [left v] button [pressed v] :: events hat broadcast [mission 1 v] when [right v] button [pressed v] :: events hat broadcast [mission 2 v] when I receive [mission 1 v] :: events hat move [↑ v] for (30) [cm v] :: movement
Pybricks Python
from pybricks.parameters import Button from pybricks.tools import wait while True: pressed = hub.buttons.pressed() if Button.LEFT in pressed: mission_1() elif Button.RIGHT in pressed: mission_2() wait(10)
| Block | Python | Watch out |
|---|---|---|
| when left button pressed | Button.LEFT in hub.buttons.pressed() | pressed() is a set of every button held right now. |
| broadcast / when I receive | mission_1() | A function call does the same job, and you can see where it goes. |
| (nothing) | wait(10) | A loop that checks buttons should rest a little each time. |
Now a menu with as many missions as you like:
SPIKE word blocks
Pybricks Python
from pybricks.parameters import Button from pybricks.tools import hub_menu, wait from base import hub, home_arm import m1 import m2 import m3 hub.system.set_stop_button(Button.BLUETOOTH) MISSIONS = {"1": m1.run, "2": m2.run, "3": m3.run} while True: choice = hub_menu("1", "2", "3") home_arm() while hub.buttons.pressed(): wait(10) while Button.CENTER not in hub.buttons.pressed(): wait(10) MISSIONS[choice]()
| Block | Python | Watch out |
|---|---|---|
| (no block) | hub_menu("1", "2", "3") | Left/right to choose, centre to go. Returns the symbol. |
| (no block) | MISSIONS[choice]() | Look up the function, then the () runs it. |
| (no block) | while Button.CENTER not in … | Wait for "go" while you place the robot. |
| (no block) | set_stop_button(Button.BLUETOOTH) | Your emergency stop is now the Bluetooth button. |
Write it from scratch 20 min
- Turn two missions into files. Take two earlier mission programs. In each, put the moves inside
def run():. Save them asm1andm2. - Make a fake third mission.
m3just beeps and shows a number. It tests the runner without driving. - Write
runnerfrom a blank file. Imports, the stop button, the dictionary, the loop. - Add a ready signal. After each mission, show
Icon.HAPPYand turn the light green, so you know the robot is back in the menu.
m3 — a safe test mission
from base import hub from pybricks.tools import wait def run(): hub.speaker.beep(880, 200) hub.display.number(3) wait(1000)
Code at the top of a mission file runs on import. If m1 has a robot.straight(300) outside def run(), the robot drives when the runner starts, before any menu. Everything that moves goes inside run().
Test and measure 15 min
- Turnaround time. Time from “robot back in the start area” to “next mission started”. With the laptop, then with the runner. Write both down.
- Run 1, 2, 3, 1, 2, 3 without stopping the program. Does every mission start in the same arm position?
- Press the centre button while you place the robot. Does the program keep running? Then press the Bluetooth button in the middle of a mission. Does the robot stop? Then restart the runner from the laptop.
- Ask a teammate to pick a mission number. Can you select it without looking at the hub display for long?
Break it, fix it 10 min
One mistake each.
Bug 1 — the robot drives before the menu appears
# file m1
from base import robot
robot.straight(300)
def run():
robot.turn(90)Bug 2 — every mission runs at the start, then the menu crashes
MISSIONS = {"1": m1.run(), "2": m2.run(), "3": m3.run()}
while True:
choice = hub_menu("1", "2", "3")
MISSIONS[choice]()Bug 3 — KeyError: 1
MISSIONS = {"1": m1.run, "2": m2.run}
while True:
choice = hub_menu(1, 2)
MISSIONS[choice]()Bug 3 is about types. 1 is a number and "1" is text. They look the same on the hub, but a dictionary treats them as different keys.
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.
Add a mission under pressure. Your teacher names one of your earlier programs. In ten minutes:
- turn it into
m4with arun()function; - add it to the menu and the dictionary;
- run missions 4, 1, 4 without touching the laptop.
Done means: all three runs start from the menu, and the hub shows the ready signal after each one.
Engineering journal 5 min
Today’s entry:
- your menu: each symbol and the mission it runs
- turnaround time, laptop versus runner
- where the emergency stop is now, and why it moved
- the rule about code at the top of a mission file