Skip to content

Latest commit

 

History

History
279 lines (182 loc) · 9.04 KB

Common_Errors.md

File metadata and controls

279 lines (182 loc) · 9.04 KB

Common Error Messages – AIDriver RP2040

This appendix lists common error messages you may see while working through the AIDriver challenges, what they mean in plain English, and how to fix them.

Use it as a quick reference when something goes wrong.


1. NameError: name 'driver' is not defined

Example message:

NameError: name 'driver' is not defined

What it means (plain English):
Python can’t find a variable or name called driver.

Likely causes:

  • You created your robot with a different name (e.g. my_robot = AIDriver()) but later used driver.
  • You mis-typed the name (e.g. my_robt vs my_robot).

How to fix:

  1. Find where you created the robot (usually near the top of main.py).

  2. Use exactly the same name everywhere you refer to it.

  3. A simple pattern is:

    my_robot = AIDriver()
    my_robot.drive_forward(200, 200)

2. AttributeError: 'AIDriver' object has no attribute 'backward'

Example message:

AttributeError: 'AIDriver' object has no attribute 'backward'

What it means:
The AIDriver class does not have a function called backward.

Likely causes:

  • You used a name from a flowchart or from another language instead of the actual library function.

  • You wrote:

    my_robot.backward(200, 200)

    instead of:

    my_robot.drive_backward(200, 200)

How to fix:

  • Use the correct function names from the AIDriver library:
    • drive_forward(right_speed, left_speed)
    • drive_backward(right_speed, left_speed)
    • rotate_left(turn_speed)
    • rotate_right(turn_speed)
    • brake()
    • read_distance()

3. SyntaxError: invalid syntax (missing colon)

Example message:

  File "main.py", line 12
    while driver.read_distance() == -1
                                      ^
SyntaxError: invalid syntax

What it means:
Python expected a : or some other symbol at the end of the line.

Likely causes:

  • You forgot the colon (:) at the end of a while, if, for, or def line.

How to fix:

  • Check the line the error points to (e.g. line 12) and make sure it ends with a colon:

    while driver.read_distance() == -1:
        print("Robot too close")

4. IndentationError: unexpected indent / code not indented

Example messages:

IndentationError: unexpected indent

or

IndentationError: expected an indented block

What it means:
Python is confused by how far your lines are indented.

Likely causes:

  • Code inside a while, if, for, or def is not indented at all.
  • Some lines inside the same block are indented differently.

How to fix:

  • For a while loop, the body should be indented, for example:

    while True:
        my_robot.drive_forward(200, 200)
        sleep(0.1)
  • Make sure all lines inside the block start with the same number of spaces.


5. ImportError: no module named 'aidriver'

Example message:

ImportError: no module named 'aidriver'

What it means:
Python cannot find the aidriver library. The library is built into the AIDriver firmware, so this means the robot doesn't have the AIDriver firmware installed.

How to fix:

Press Connect robot. If it says the firmware is missing or needs updating, give your robot to your teacher to install the AIDriver firmware.


6. Robot “does nothing” (no movement, no errors)

What it looks like:

  • Program runs, no error is shown, but the robot does not move.

Likely causes:

  • You never call a movement function inside your loop.
  • Your loop is empty or only has sleep() statements.
  • Speeds set too low (e.g. below ~120).

How to fix:

  • Make sure your while True: loop contains commands like:

    while True:
        my_robot.drive_forward(200, 200)
        sleep(0.1)
  • Use speed values around 200 for reliable motion.


7. Warnings printed by the robot

The robot prints each of these once per run (the simulator shows the same ones):

Warning What it means
Speed 300 is above 255 ... Speeds go from 0 to 255. Above 255 the robot treats the number as a raw PWM value, so 300 is almost stopped.
Speed 50 is in the stopped range (0-80) ... The motors need more power than this to turn. Try 150-220.
hold_state(1000) waits 1000 seconds ... hold_state uses seconds. For one second write hold_state(1), for half a second hold_state(0.5).
hold_state(-1) - time can't be negative ... Check the calculation that gives the time. The robot does not wait at all.
Program ended: motors stopped automatically Your program finished, crashed or was stopped while the motors were running, so the robot braked by itself. Read the lines above it for any error.

8. Robot freezes or keeps rebooting

What it looks like:

  • The board disconnects, restarts, or stops responding while the program is running.

Likely causes:

  • An infinite loop with no sleep() call (CPU never gets a break).
  • Very tight loops that spam the console with prints.
  • Old AIDriver firmware (before version 2026.12.01): every drive, brake and hold_state call was written to event_log.txt, so a fast while True loop filled the robot's storage within minutes. This can happen even when the same code runs perfectly in the simulator. The simulator shows Firmware update needed when you connect a robot like this.

How to fix:

  • If the robot has old firmware, copy your code somewhere safe, then connect GP4 to GND while powering on. This deletes the files on the robot and restores main.py. Then give the robot to your teacher to update the firmware.

  • Always include a small delay in your main loop, for example:

    while True:
        # your logic here
        sleep(0.05)  # 50 ms pause
  • Reduce the number of print() calls inside fast loops.


9. Ultrasonic sensor always reads -1

What it looks like:

  • my_robot.read_distance() returns -1 every time.

Likely causes:

  • Sensor is too close (< 20 mm) or too far (> 2000 mm) from any object.
  • Sensor is pointing into open space. The robot prints No echo – nothing in range?.
  • Wiring issue on TRIG/ECHO pins. The robot prints Echo pin stuck HIGH – check wiring (only after several readings in a row, so a single long echo is not mistaken for a wiring fault).

How to fix:

  1. Place an object 10–50 cm in front of the sensor.

  2. Check the wiring matches the assembly instructions.

  3. Turn on AIDriver debug and watch the messages:

    import aidriver
    from aidriver import AIDriver
    
    aidriver.DEBUG_AIDRIVER = True
    robot = AIDriver()
  4. Run a simple distance test loop and see what the debug messages say.


10. Problems uploading from the simulator

The Robot buttons above the code editor (Connect robot, Upload, Upload & Run, Logs, Stop robot) need Chrome or Edge on a computer.

Message What to do
"Upload needs Chrome or Edge" (in the code editor's header) Open the simulator in Chrome or Edge. Firefox, Safari and phones can't connect to the robot.
"The robot is being used by another program or tab." Close MicroPython Lab, Thonny or any other simulator tab that is connected, then press Connect robot again.
"Connected, but the robot didn't answer like MicroPython." Give your robot to your teacher to install the AIDriver firmware.
"This robot doesn't have the AIDriver firmware" or "This robot's AIDriver firmware is out of date" (the status shows Firmware update needed) Give your robot to your teacher to install or update the firmware. You can still upload, but your code may not work properly until the firmware is updated.
"The robot's program won't stop." Your program is ignoring Stop (for example a try/except around while True). Copy your code somewhere safe. Unplug the robot, connect GP4 to GND and plug it back in: this deletes all files on the robot and restores the default main.py. Remove the wire, then upload again.
"Fix the error on line … before uploading." The simulator found a syntax error. Fix the marked line: the robot would stop at this error.
"The simulator found: …" These are simulator rules (for example only aidriver and time imports). Your code may still work on the robot, so you can choose Upload anyway.
"The upload didn't save correctly, so your old main.py was kept." Press Upload again. The robot still has its previous program.
"The robot's storage is full." Give the robot to your teacher to run GP4 recovery mode (this deletes everything except main.py and the logs).
"The robot was disconnected." Plug the USB cable back in. The simulator reconnects by itself for 10 seconds; after that, press Connect robot.

If you see an error that is not listed here, try to:

  1. Read the first line of the error message.
  2. Note the type (e.g. NameError, SyntaxError, AttributeError).
  3. Use that as a keyword to search in your docs or ask for help.