Today’s mission 5 min
Four objects, a random order, four drop zones. Scan, decide and deliver in one run, with a program you write today.
This is the Block 3 milestone. There is nothing new to learn. Everything you need is in lessons 13 to 17 and in your own base library. The test is whether you can put it together, on your own, against a clock.
The scoring: 1 point for each object in its correct zone, 1 bonus point if the robot ends back in the start area. Five attempts, shuffled by die each time. Your best three count.
The idea — the shape of a robust mission 10 min
Strong competition programs share one shape. Plan yours on paper before you open the laptop:
- Prepare. Home the arm. Check the gyro is ready.
- Scan. Drive past the slots and fill a list. Do nothing else.
- Decide. Turn the list into a plan. Print it.
- Deliver. Follow the plan, one object at a time.
- Return. Drive back to the start area.
Each step is a function. Each function can be tested alone. When the run fails, you know which step to open.
Guard every step that can go wrong. A bad colour read? Skip that object. A move that might jam? Give it a timeout. A clever program that crashes scores zero. A careful one that skips one object still scores.
Blocks vs Python 15 min
In SPIKE you would split the run into My Blocks. Python functions do the same, with one big difference.
SPIKE word blocks
when program starts :: events hat prepare :: myblocks scan :: myblocks decide :: myblocks deliver :: myblocks go home :: myblocks
Pybricks Python
def main(): prepare() seen = scan() plan = decide(seen) print("plan:", plan) deliver(plan) go_home() main()
| Block | Python | Watch out |
|---|---|---|
| scan (My Block) | seen = scan() | A My Block cannot hand back a value. A function can, with return. |
| decide (My Block) | plan = decide(seen) | In blocks, scan and decide share a global list. In Python the data is passed in plainly. |
| (nothing) | print("plan:", plan) | The plan appears in the terminal before the robot commits to it. |
With return values you can test decide without the robot at all: print(decide(["blue", "red", "none", "green"])). Try a few lists before you drive.
Write it from scratch 20 min
No coach from here. From this section to the end of the lesson, your coach will only ask “What does the terminal say?”.
Open a blank file called m_random. You may import from base and copy from your own earlier programs. Suggested order:
- Write
main()first, calling five functions that only print their own names. Run it. - Fill in
decideand test it with made-up lists. No robot yet. - Fill in
scan. Check the printed list against the real field. - Fill in
deliverfor one object, then for all four. - Fill in
go_home. Addrun()so the mission runner can start it.
Step 1 — the skeleton
def prepare(): print("prepare") def scan(): print("scan") return [] def decide(seen): print("decide") return [] def deliver(plan): print("deliver") def go_home(): print("go home")
Test and measure 15 min
Five scored attempts. Another team rolls the die, sets the field and scores you. Between attempts you may change code, but the clock keeps running.
- Record the shuffled order, the printed list and the score for each attempt.
- Mark which step failed when points were lost: scan, decide, deliver or return.
- Add up your best three. That is your Block 3 score.
Break it, fix it 10 min
These three mistakes are common in milestone programs. Check your own code for each one.
Bug 1 — the plan is always empty
def scan():
seen = []
for slot in range(4):
robot.straight(100)
seen.append(colour_name(eye.color()))
seen = scan()
print(seen)Bug 2 — the robot drives off the table after the scan
def scan():
seen = []
for slot in range(4):
robot.straight(100)
seen.append(colour_name(eye.color()))
return seen
def deliver(plan):
for slot, zone in plan:
robot.straight(100 * slot)
drop_at(zone)Bug 3 — KeyError: 'none'
ZONE = {"red": "A", "blue": "B", "green": "C", "yellow": "D"}
def decide(seen):
plan = []
for slot, colour in enumerate(seen):
plan.append((slot, ZONE[colour]))
return planBug 2 has no error message. After the scan the robot is at the far end, not the start. Every delivery distance was measured from the wrong place. Know where the robot is at the start of every step, and write it as a comment.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. You may use your own earlier programs and the Pybricks documentation, nothing else.
The surprise slot. The coach adds a fifth slot, 100 mm after the fourth. It may be empty. Its object, if any, goes to zone A. In ten minutes:
- change the scan to read five slots;
- change the decide step for the new rule;
- run one scored attempt.
Done means: the empty case does not crash, and the other four objects still score.
Engineering journal 5 min
Today’s entry, the Block 3 summary:
- your five attempts: order, printed list, score, failed step
- your Block 3 score (best three)
- the step that failed most, and your plan to fix it
- what you would ask a coach, and how you answered it yourself