Blog

Line following with PID control: from theory to a robot that doesn't wobble

Alessandro Panait·

Every Rescue Line robot starts life as a bang-bang controller: line on the left, steer left; line on the right, steer right. It works, and it wobbles like a shopping cart. Wobble costs time, and on a Rescue Line course, wobble is what throws you off the line at gaps and after obstacles.

The idea behind PID

Instead of a binary decision, compute an error: how far is the line from the center of your sensor array? A weighted average across the sensors gives a smooth position value. The PID controller turns that error into a steering correction:

  • P (proportional): steer proportionally to the current error. Big error, big correction.
  • I (integral): accumulate past error. Fixes a robot that consistently rides slightly off-center, e.g. on long curves.
  • D (derivative): react to how fast the error is changing. This is the damping term that kills oscillation.

The output is a single number added to one motor's speed and subtracted from the other's.

Tuning without losing your mind

A method that works in practice:

  1. Set Ki and Kd to zero. Raise Kp until the robot follows the line but visibly oscillates.
  2. Raise Kd until the oscillation dies. If the robot gets twitchy on straights, you went too far.
  3. Add a small Ki only if you see steady-state offset on long arcs. Many Rescue Line robots run perfectly well with Ki = 0.
  4. Now raise the base speed, and re-tune. Gains that work at 50% speed will not work at 100%.

Log your error values or stream them over Bluetooth/serial while driving, watching the error curve tells you more than watching the robot.

Rescue Line specifics

A few things pure theory won't tell you. Calibrate sensors on every field: lighting and tile wear change readings dramatically, so run a calibration sweep before every round. Clamp the integral term (anti-windup), or one lost line will saturate it and send the robot spinning. And gate the PID: at green intersection markers, gaps and obstacles you don't want a correction, you want a state machine that takes over, executes the maneuver, and hands control back to the PID afterwards. Line following is the easy 80%. The state machine around it is the hard 20% that scores points.

Comments (0)