A line-following robot reads a reflectance sensor array under its nose and steers to keep the black line centered. The sensor array collapses to a single signed lateral error e — the perpendicular offset between the robot's centerline and a look-ahead point on the track. A PID controller turns that error into an angular correction:
e(t) = lateral offset to look-ahead point on line
u(t) = Kp·e(t) + Ki·∫e dt + Kd·(de/dt)
ω = clamp(u, ±ω_max) (turn rate)
vL = v − ω·W/2, vR = v + ω·W/2 (differential drive, wheelbase W)
This simulator runs Ki at a small fixed value (real line-followers usually keep it near zero — the P and D terms dominate) so you can feel the classic trade-off directly:
- Kp too low — the robot cuts corners and drifts off the outside of every turn.
- Kp too high — the robot oscillates violently, snapping side to side ("hunting").
- Kd — damps that oscillation by reacting to how fast the error is changing, letting you push Kp higher before instability sets in.
- Look-ahead distance — a longer look-ahead smooths steering on sharp bends but reacts later; too short and the controller becomes twitchy.
The track itself is generated as a closed polar curve r(θ) = R₀ + Σ Aᵢ·sin(kᵢθ + φᵢ), which guarantees a single, non-self-intersecting loop with a mix of gentle sweeps and sharp bends — a new one every time you press "New Track".