Control: PID Steering
Feedback control from first principles: measure the error, steer to reduce it, and tune the proportional, derivative and integral terms by watching the car. Then write your own PID controller in Python and drive a full lap.
Softmaxima Simulator StudioTry this topic's code in the Studio: it opens with the PID controller project, ready to run.OpenIn plain words
Think of steering to stay in the middle of a lane. The further off you are, the more you turn back (P, for proportional). If you are drifting back fast, you ease off early so you do not overshoot (D, for derivative). If a crosswind keeps pushing you aside, you hold a little extra steering all the time (I, for integral). Together they make the PID controller, used in almost every machine that holds a value steady.
By the end of this topic you will be able to tune a controller that keeps the car in its lane, and write your own in Python.
Start with: the demo Try it: tune the controller: move one slider at a time.
Feedback control
A controller compares what is happening with what should happen, and acts on the difference. That difference is the error. For lane keeping, the main error is the cross-track error (CTE): how far the car is from the lane centre, positive to the left in our simulator.
This loop of measure → compare → act, repeated many times a second, is feedback control. It is everywhere: cruise control, thermostats, drones, factory robots. Its strength is that it corrects mistakes without needing a perfect model: a gust of wind or a slope pushes the car off, the error grows, and the controller pushes back.
P, I and D
The PID controller is the most widely used controller in the world. It adds three terms, each with a gain (a number you choose):
- P, proportional: steer back in proportion to the error. Too small and the car drifts; too large and it overshoots the centre and swings from side to side. Alone, P always wobbles, because the car reaches the centre still pointing across it.
- D, derivative: respond to how fast the error is changing. As the car swings back towards the centre, the error is shrinking fast, so D eases off the steering early. It acts as a damper and stops the wobble.
- I, integral: add up the error over time. A small steady error that P is too weak to remove (for example a constant bend, or a car that pulls to one side) keeps growing in the integral until it is corrected. Too much I causes slow oscillation, so it is usually small.
The minus sign is because the error is positive when the car is too far left, so the steering must be to the right (negative).
Try it: tune the controller
The car starts 1.2 m off the lane centre. Try P only first and watch it wobble and crash, then add D, then a little I. Change the speed too: gains that work at 40 km/h can fail at 60.
Interactive: this part runs in your browser.
How engineers tune PID
- Set I and D to zero. Raise Kp until the car follows the lane but starts to oscillate.
- Raise Kd until the oscillation is damped: the car returns to the centre smoothly with little overshoot.
- Add a small Ki only if a steady offset remains, for example in long bends.
- Test at every speed and on every kind of road. Because turning rate grows with speed, the same gains feel stronger at high speed. Real systems change the gains with speed, which is called gain scheduling.
Two practical details: compute the derivative of the measurement and filter it, because sensor noise makes a raw derivative jumpy (you will meet noisy sensors in Part 3); and limit the integral so it cannot grow huge while the car is stuck or the steering is at its limit (anti-windup).
Challenge: write your own PID controller
Your code runs in Python in this page and drives the car for a full lap. The first run may take a few seconds while Python loads.
This runs your code in the simulator, as described in The Simulator: Run Your Code. That page also lets you try any track, keep your code, and download the simulator to run on your own computer.
Interactive: this part runs in your browser.
Quiz
1. With P control only, the car keeps swinging from side to side. Which term fixes this?
2. On a long, steady bend the car settles 0.3 m outside the lane centre and stays there. Which term removes this?
3. CTE is +0.5 m (the car is left of centre). Which way should it steer?
4. Gains tuned at 40 km/h make the car wobble at 80 km/h. Why?
📚 Further reading
Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.
- Åström & Murray: Feedback Systems (free online) ↗ a free, readable textbook on feedback and PID control