Today’s mission 5 min
Find a bug by evidence, not by guessing: read the error, print the numbers, time the step.
From today until the mock competition, your coach watches and takes notes. They will not fix anything. At WRO the coach may not help, so the team that can debug alone is the team that finishes its runs.
Guessing is slow. Changing numbers until it works can take an hour. Reading one error message usually takes one minute. Today you learn the one-minute way.
The idea — four places a bug can hide 10 min
When the robot does the wrong thing, the fault is in one of four places. Check them in this order, cheapest first:
- Battery. A low battery makes motors weaker and turns shorter. Print
hub.battery.voltage(). It is in millivolts, so 8000 means 8 volts. Write down what a full battery reads on your hub, and charge well before it drops far below that. - Mechanics. Push every wheel, axle and beam. Is a wheel rubbing? Is a tyre half off? Does the caster catch on the mat? Run the robot slowly and watch it, not the screen.
- Sensors. Print the reading. Is the colour sensor still the right height above the mat? Is the cable in the right port? Is the room brighter than when you calibrated?
- Code. Only now. Read the error if there is one. If there is no error, print the values the code uses and compare them with what you expected.
Reading a traceback. When Python stops, the terminal shows a red message. Read it from the bottom up:
- The last line names the kind of error and what went wrong.
- The line above gives the file and line number. If it names your
basefile, the bug may be in your library, not your mission.
| Error | What Python means | Look for |
|---|---|---|
NameError | I have never heard of this word. | A spelling, a capital letter, or a missing import. |
AttributeError | The thing exists, but not the part after the dot. | A misspelt method, such as distence(). |
TypeError | Wrong kind of value, or wrong number of values. | Text where a number should be, or a missing argument. |
ValueError | Right kind of value, but not allowed. | robot.arc() needs exactly one of angle or distance. |
IndentationError | The spaces at the start of a line are wrong. | A line inside a function or loop that is not indented. |
OSError | The hub refused to do it right now. | Resetting the heading while the drive base is using the gyro. |
Blocks vs Python 15 min
In SPIKE you can show one number at a time on the light matrix. In Pybricks you can print as many numbers as you like, with labels, and read them all after the run.
SPIKE word blocks
when program starts :: events hat reset timer :: sensors move [↑ v] for (30) [cm v] :: movement write (timer :: sensors) :: display
Pybricks Python
from pybricks.tools import StopWatch from base import hub, robot print("battery mV:", hub.battery.voltage()) watch = StopWatch() robot.straight(300) print("straight took", watch.time(), "ms") print("distance", robot.distance(), "mm") print("angle", robot.angle(), "deg") print("heading", hub.imu.heading(), "deg")
| Block | Python | Watch out |
|---|---|---|
| reset timer | watch = StopWatch() | A new StopWatch starts at zero. |
| timer | watch.time() | SPIKE gives seconds. StopWatch gives milliseconds. |
| write (timer) | print("took", watch.time()) | print goes to the terminal, not the hub. Add a label to every number. |
| (nothing) | hub.battery.voltage() | Millivolts. 8000 means 8 volts. |
| (nothing) | robot.distance(), robot.angle() | What the drive base thinks happened. Compare it with a ruler. |
Use the names from your own base file. If yours calls the drive base db, write db.distance().
SPIKE word blocks
Pybricks Python
from pybricks.tools import wait, StopWatch from base import robot, left_eye watch = StopWatch() robot.drive(150, 0) while watch.time() < 2000: print(watch.time(), left_eye.reflection()) wait(50) robot.stop()
| Block | Python | Watch out |
|---|---|---|
| (nothing) | print(watch.time(), left_eye.reflection()) | Print time first, so you can see when the reading changed. |
| wait 0.05 seconds | wait(50) | Without a wait, the terminal floods and the program slows down. |
Write it from scratch 20 min
Write a program called telemetry. It runs one leg of your own route and prints a report. Type it from a blank file.
- Print the battery voltage before anything moves.
- Start a
StopWatchbefore each move, and print its time after the move, with the move’s name. - After every move, print
robot.distance(),robot.angle()andhub.imu.heading(). - During one line-follow, print the reflection every 50 ms.
A good report reads like a story: what the robot was asked to do, and what it measured itself doing. Run it three times and keep all three reports.
A useful pattern
def report(name, watch): print(name, watch.time(), "ms", "dist", robot.distance(), "head", hub.imu.heading())
Test and measure 15 min
Compare the three reports. Answer in your journal:
- Which move took the most time? Is that the move you expected?
- Did the heading after each turn match what you asked for? By how many degrees was it out?
- Did
robot.distance()match a ruler on the mat? If not, is it every run, or only some? - Charge the hub, or swap to a weaker one, and run again. What changed?
Same mistake every run is a setting. A wrong wheel size or a threshold that is off. Different every run is mechanics or light. A slipping wheel, a loose sensor, a shadow. This one question halves the search.
Break it, fix it 10 min
Six programs, one mistake each. Run each one, read the last line of the error and name the error type before you fix it. Time yourselves.
Bug 1
from base import robot def square(): robot.straight(200) robot.turn(90)
Bug 2
from base import robot
robot.straight(300)
robot.turn("90")Bug 3
from base import robot robot.arc(150)
Bug 4
from base import hub, robot robot.use_gyro(True) robot.straight(300) hub.imu.reset_heading(0) robot.turn(90)
Bug 5
from base import robot robot.straight(200) wait(500) robot.straight(-200)
Bug 6
from base import robot
robot.straight(400)
print("went", robot.distence(), "mm")Bug 4 has no typing mistake. The hub will not reset its heading while the drive base is steering with the gyro. Decide where the reset belongs in your program, and why.
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.
The sabotaged robot. While you were away, another team made one change to your robot and one change to your code. It might be a cable in the wrong port, a loose tyre, a changed number or a renamed function.
- Work through the checklist: battery, mechanics, sensors, code.
- Find both changes using evidence: a print, a reading or an error.
- Write down how you found each one before you fix it.
Done means: both faults found and fixed, the route runs, and you can say which piece of evidence led you to each.
Engineering journal 5 min
Today’s entry:
- the four-step checklist, in order, in your own words
- your full-battery voltage, and the voltage where runs went wrong
- the six error types, with one example of each
- the slowest move in your route, and how long it took
- how long you took to find each sabotage fault