Lessons and the learning game

Robots in Python on the Pi

Build an exploring rover on a Pi 4 in Python: gpiozero Robot, the L298N, sonar and line sensors, a state machine and an autonomous run.

The course

Robots in Python on the Pi is an intermediate Coding course in seven lessons, for anyone who has finished Python from zero. Every lesson drives the same rover: a two-wheel chassis with a 20-slot encoder on each wheel, an L298N on a 7.4 V pack, an HC-SR04 on the nose and a five-channel line tracker underneath. Steps are checked against the chassis's speed, the Pi's pins and what the program prints.

  1. gpiozero Robot: forward and backward, spinning on the spot, part speed and curve_left, and a square driven by the clock, which shows why open loop is not enough.
  2. Motor drivers: one wheel on three plain pins, the H-bridge in reverse, how far a wheel rolls after the power goes (8 slots, 82 mm), PWM on the enable pin, and gpiozero's Motor as those same pins.
  3. Distance sensing: DistanceSensor, the echo timed by hand (5823 microseconds for a meter), stopping before a wall, a proportional slow down, and backing away to turn.
  4. Line sensing: five sensors as a pattern, the tape's position in millimeters, bang-bang steering, proportional steering, and the finish bar.
  5. A state machine: EXPLORE, AVOID on a timer that never freezes the loop, BACKUP when too close, and the whole machine as a table where coming home to base is one more row.
  6. Logging a run: odometry from the encoders, a timestamped CSV, speed from the log and the quantization in it, logging levels, and a JSON summary.
  7. Boss: an autonomous Pi bot: the finale.

The boss

One state machine runs the course with no one at the controls. FOLLOW steers on the tape with a proportional controller. WAIT stops for anything closer than 25 cm and goes again only past 35 cm, which is hysteresis. SEARCH spins toward the side the line was last seen. DONE at the finish bar prints a mission report from a count of every state visited. A three second search timeout and a try/finally that always stops the motors make it fail safe.

What the Pi engine does differently

  • The chassis drives on its own endless floor with no walls and no tape, so the sonar's slider is the obstacle and the tracker's slider is the line. You play the world: drag them while the program runs.
  • DistanceSensor prints gpiozero's PWMSoftwareFallback warning, as it does on a real Pi without pigpio, and its readings trail a change by about a third of a second, from its averaging queue.
  • There are no threads. Encoder callbacks and the sonar's queue run between the program's own steps, at the simulated time of each edge.
  • gpiozero's LineSensor expects a board whose output goes low over the line; this tracker goes high, so the course reads it with DigitalInputDevice.
  • The simulated motors model their lag but not braking current, so a brake stops a wheel in about the same distance as a coast.
  • MicroPython does not keep a dict's key order, so the JSON summary is printed with sort_keys=True.

The gallery has no Pi robot, so the course draws one from the parts the simulator models: chassis2wd, l298n (with its ENA and ENB jumpers pulled, so the Pi owns the enable pins), hcsr04 with a 1 k and 2 k echo divider, and linearray.