Today’s mission 5 min
Build a colour table for this room, prove it with a 20-reading test, and learn to rebuild it in ten minutes anywhere.
WRO missions often hide their answer in a colour: which block goes where, which zone is open. A robot that reads blue as green scores nothing for that run, however well it drives. Colour reading is a measurement, so today you measure it.
Your table will be wrong at the competition. The light and the mat print are different there. That is fine, if you can measure again quickly during practice time.
The idea — hue, saturation, value 10 min
The Colour Sensor shines its own light and measures what bounces back. Pybricks can give you that as three numbers, called HSV:
- Hue (0–359) — which colour, as a place on a colour wheel. Red is near 0 and 359, green near 120, blue near 220.
- Saturation (0–100) — how strong the colour is. White, grey and black have low saturation.
- Value (0–100) — how bright. Black is low, white is high.
When the room gets darker, value changes a lot and hue hardly moves. That is why hue is the most trustworthy number for a colour.
Then you tell the sensor which colours to expect, using your own measured values. It picks the closest one from your list:
MAT_RED = Color(h=354, s=82, v=58) eye.detectable_colors([MAT_RED, Color.BLUE, Color.WHITE, Color.NONE])
Fewer colours, fewer mistakes. If a mission only uses red, blue and white, list only those (plus Color.NONE for nothing there). Every extra colour is another wrong answer the sensor could pick.
Blocks vs Python 15 min
SPIKE asks “is it red?” with LEGO’s own idea of red. Pybricks lets you replace that idea with your measured red.
SPIKE word blocks
when program starts :: events hat if <(E v) is color [red v]? :: sensors> then turn on [smile v] :: display else if <(E v) is color [blue v]? :: sensors> then write [B] :: display else write [?] :: display end end
Pybricks Python
from pybricks.hubs import PrimeHub from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port, Color, Icon hub = PrimeHub() eye = ColorSensor(Port.E) MAT_RED = Color(h=354, s=82, v=58) MAT_BLUE = Color(h=218, s=78, v=46) eye.detectable_colors([MAT_RED, MAT_BLUE, Color.WHITE, Color.NONE]) seen = eye.color() if seen == MAT_RED: hub.display.icon(Icon.HAPPY) elif seen == MAT_BLUE: hub.display.char("B") else: hub.display.char("?")
| Block | Python | Watch out |
|---|---|---|
| (E) is color red? | eye.color() == MAT_RED | Your red, measured in your room, not LEGO's average red. |
| (no block) | eye.detectable_colors([…]) | Limits the answers to your list. SPIKE always chooses from all its colours. |
| if … else, if … else | if / elif / else | elif keeps it flat. Nested blocks get deep fast. |
| (E) color | eye.hsv() | Three numbers you can print and compare, not just a name. |
The block version is short, but you cannot fix it when it misreads. The Python version you can measure, adjust and test.
Write it from scratch 20 min
Two short programs, both from blank files.
1. colour_probe — the measuring tool. Hold each mission object under the sensor, at the height the robot really sees it, and press the centre button for each reading.
colour_probe
from pybricks.hubs import PrimeHub from pybricks.pupdevices import ColorSensor from pybricks.parameters import Port, Button from pybricks.tools import wait hub = PrimeHub() hub.system.set_stop_button(None) eye = ColorSensor(Port.E) while True: if Button.CENTER in hub.buttons.pressed(): c = eye.hsv() print(c.h, c.s, c.v) hub.speaker.beep() while hub.buttons.pressed(): wait(10) if Button.LEFT in hub.buttons.pressed(): break wait(10)
Turning off the stop button lets you use the centre button as an input. The left button ends the program. Take five readings of every colour, moving the object slightly each time, and write them all down.
2. colour_check — the test. Use the middle of your five readings for each colour. Make the Color(h=…, s=…, v=…) constants, set detectable_colors, then print what the sensor decides every half-second.
When it works, move the constants into base. Then every mission uses the same table, and you rebuild it in one place on competition day.
Test and measure 15 min
- The 20-reading test. Show each object 20 times, in different spots. Count the misreads. Anything above zero needs a reason.
- Height. Read the same red at three heights, for example one, two and three plates higher. Which number changes most: h, s or v?
- Light. Shade the sensor with your hand, then stand by a window. Did the decisions change?
- Moving. Drive slowly over a coloured mat area and print the colour. Does it read correctly at speed 100? At 300?
Break it, fix it 10 min
One mistake in each. Some only show up when you test.
Bug 1 — always says red
MAT_RED = Color(h=354, s=82, v=58) eye.detectable_colors([MAT_RED]) print(eye.color())
Bug 2 — red is never found
MAT_RED = Color(h=354, s=82, v=58)
eye.detectable_colors([MAT_RED, Color.BLUE, Color.NONE])
if eye.color() == Color.RED:
print("red")Bug 3 — TypeError
c = eye.hsv()
print("hue is " + c.h)Bug 1 is a design mistake, not a typing one. The sensor always picks the closest colour in your list. With only one choice, everything is the closest. Always include Color.NONE and the background colour.
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.
New room, new table. Move to a different table or a different part of the room. With only a stopwatch running:
- re-measure every mission colour with your probe;
- update the constants in
base; - run the 20-reading test for each colour.
Done means: zero misreads, in under ten minutes. Write your time down. This is your competition practice-time routine.
Engineering journal 5 min
Today’s entry:
- your colour table: h, s and v for every mission colour
- the sensor height, and a sketch of the mount
- misreads in the 20-reading test, before and after fixing
- your re-calibration time, and the steps in order