PID Control, One Term at a Time
- 0 views
- Last updated
- Engineering
A cruise control has to hold a hundred kilometres an hour while the road tilts under it. This lecture builds the controller that does it, one term at a time, on a single response curve you watch the whole way through: proportional action, which reacts to the error now and always leaves a steady gap; integral action, which accumulates the error's history and finally erases that gap, at the cost of overshoot and windup; and derivative action, which reads the slope of the error and leans against the approach. The three are then assembled into the standard PID law and tuned, with each gain pushed too far in turn, so the trade between rise time, overshoot and noise is something you see rather than something you are told.
Every machine that has to hold something steady is solving the same problem. A cruise control holding a speed. A thermostat holding a room temperature. The world knocks it off target, and it has to push back, by exactly the right amount and no more. So here is the question this whole lecture answers. How does a machine hold a setpoint that the world keeps knocking it off? PID answers with three corrections: the present error, its accumulated past, and its direction of travel. Let's make it concrete. A car on a road, and a driver who sets the cruise control to one hundred kilometres an hour. Left alone with no extra throttle, this car rolls along at sixty. And it does not respond instantly. Ask for more throttle, and the engine takes about a second to deliver the extra pull, and the car takes a few seconds more to work that pull into speed. Every real plant has that lag, and it is the reason control is hard at all. That target has a name. It's the setpoint, r. What the car is actually doing at each instant we'll call v of t. And the whole job of a controller is to make v equal r, and then keep it there. Now, the naive fix. Guess the throttle. Find the setting that holds a hundred on flat road, push the pedal to exactly there, and leave it. And on flat road, it works. And then, fifteen seconds in, the road tilts up. The grade swallows part of the engine's pull, and the speed sags. The throttle does not move, because nothing in this machine is looking at the speed. It settles twenty kilometres an hour below target, and it stays there for good. And notice where the fault is. The machine is not wrong about the throttle. It is wrong because it has no idea what the speed is doing. Nothing measures the output, so nothing can react to it. That is the whole disease of an open loop. It guesses, and it never checks. So let's close the loop. Measure the speed, subtract it from the setpoint, and call that difference the error. The error is the only thing our controller gets to look at. So everything from here is one question. Given the error, what throttle should I command? That is u equals some function of e, and the rest of this lecture is filling that function in. There are three classic answers, and they stack. Proportional, which reacts to the error right now. Integral, which remembers the whole history of the error. And derivative, which watches where the error is heading.
So, the first answer, and it is the one everybody invents for themselves. Look at the error right now, and push back in proportion to it. Big error, big correction. Small error, small correction. Nothing else. In symbols, that is u equals K p times e. One number to pick, the proportional gain, and that is the entire controller. So let's watch it drive. Same car, same hill at fifteen seconds. We start with the gain set to one: the moment we switch on, the error is forty, so the controller asks for forty units of throttle, and the car pulls away. Notice the shape it makes. It starts flat, because the engine has not delivered anything yet. Then it swings up hard while the error is big. And it flattens out as the error shrinks, because the correction shrinks with it. And then it stops. Eighty kilometres an hour. Twenty short of the setpoint, and it will sit there all day. The controller is running, the loop is closed, and the car is still twenty short. And this is not a bug you can tune away. It is arithmetic. This controller commands a throttle proportional to the error, so if the error ever reached zero, the command would be zero as well, and with no extra throttle the car falls straight back to sixty. Say that again, because it is the crux. The only way this controller can command throttle is to be wrong. Ask it to hold a hundred and it must run below a hundred, permanently, to have any error left to work with. So the error cannot reach zero. It stops where the throttle it produces is exactly the throttle the car needs. Solve for that point and you get this. The error left over is the whole forty kilometre gap, divided by one plus the gain. Which tells you exactly what raising the gain buys. Turn it up to three, and the leftover error is ten. The car holds ninety, and it gets there quicker. Turn it up to nine and the error is down to four. But look what has happened to the shape. It arrives fast, sails past the setpoint, and takes a couple of swings to settle. And notice why that overshoot appeared at all. The engine's pull lags behind the command. By the time the car is on target there is still a large pull in the pipeline, and that surplus carries it straight past the line. Push on to fifteen and the ringing is worse again, while the offset has only crept from four down to two and a half. You can chase that gap as far as your patience for overshoot allows. You never reach zero. And now the hill, which is the same story told louder. Every one of these curves drops when the grade hits, and every one settles at a new, lower speed. With the gain at three the car held ninety on the flat. On the hill it holds eighty five. Same controller, same gain, twice the error. A bigger load needs a bigger throttle, and the only way this controller can command a bigger throttle is to be further from the setpoint. It is paid in error. So proportional control is fast, it is simple, and it is structurally incapable of finishing the job. To kill that last gap, the controller needs something proportional can never have. It needs a memory.
Proportional control reacts to the error right now, and right now is exactly what strands it. It has no way of knowing that this same small error has been sitting there, unfixed, for the last ten seconds. What it needs is a record. So here is the error from that last run, plotted on its own. It starts at forty, falls fast while the correction is big, flattens out at ten, and sits there until fifteen seconds. Then the hill makes the error jump and settle higher. One thing to keep straight: this is the error, not the speed. Zero on this axis means the car is exactly on target, and every kilometre of gap up here is a kilometre the controller has failed to close. Now, instead of reading the height of this curve, add it up. Every second that goes by with an error of ten piles ten more onto the total. That accumulation is the area underneath the curve, from the moment we switched on up to now. And watch what that pile does while the error is stuck. It grows, and it keeps growing, and nothing ever takes anything off it. A leftover error that proportional control is perfectly happy with turns into an accumulation that gets bigger without limit. So that is our second term. Add to the throttle a piece proportional to the accumulated error, the integral of e, with a gain of its own, K i. And now ask where this controller can possibly come to rest. Suppose it settles with any error at all left over, even half a kilometre an hour. Then the integral keeps climbing, so the throttle keeps climbing, so the speed keeps climbing. It cannot be at rest. The only value of the error it can hold still at is exactly zero. That is a strong claim, so let me put it plainly. A controller with an integral term, on a plant like this one, drives the steady error to zero. Not small. Not nearly. Zero. So let's drive it. Grey is proportional alone, stuck at ninety. Green is the same proportional gain with the integral added, K i equal to one. It takes its time, because it has to build the pile up before the extra throttle amounts to anything. But it arrives. And it arrives exactly on the line, with no offset at all, which proportional control could not do at any gain. Then the hill hits. The speed sags, so the error comes back, so the pile starts growing again, and the controller walks the car back up to a hundred and holds it there. That is the integral's whole job. It refuses to accept a leftover error, because a leftover error is a pile that never stops growing. But that memory costs you. Turn K i up to two and a half, and by the time the speed reaches the setpoint the accumulator has already commanded a large throttle. Being on target does not empty the pile. It only stops adding to it. So the car sails past, and the error has to go negative, the car has to spend time above the setpoint, before that accumulation comes back down. That is the overshoot and the ringing you are looking at. Taken to the extreme it has a name: integrator windup. If the throttle saturates, or the car is held back for a while, the accumulator keeps piling up error it cannot act on, and the plant spends the next several seconds paying it back. So proportional gets us moving, integral finishes the job, and between them they have bought us an oscillation. The third term is the cure for that.
The trouble with that last run is that the controller only discovered it was going too fast after it had already gone too fast. Everything it knows is about the present and about the past. Nobody in there is looking ahead. But there is a look ahead sitting right in the data. Here is the error again, this time from an aggressive proportional plus integral tuning, the kind that rings. And at any instant we can ask a different question about it. Not how big is the error, but how fast is it changing. That is the slope of this curve, and here it is at two seconds in. The error is still about twelve kilometres an hour, and it is falling at roughly twelve kilometres an hour each second. At that rate the car reaches the target in about one second. And that number is a prediction. It is the controller's only honest statement about the future, and it costs nothing to compute, because it is right there in the measurement it already has. It is also the warning that proportional and integral cannot hear. Proportional sees a large error and pushes. The integral sees a large pile and pushes. Neither one knows that the correction already in the pipeline is more than enough. So give the controller a third term. A piece proportional to the rate of change of the error, with a gain of its own, K d. And read the sign carefully, because the sign is the whole idea. When the error is falling, d e by d t is negative, so this term subtracts throttle. The faster you are closing in, the harder it eases off. It is a brake that comes on only when you are approaching quickly. Now watch it along the whole run. Every time the car swings toward the line, the slope is steep and the term leans against the motion. Every time it swings away, the sign flips and the term leans the other way. And once the swinging stops, the curve goes flat and the slope is zero. That last part matters. At steady state, derivative action is silent. It cannot fix an offset and it cannot cause one. It only ever speaks while things are changing. So here is that same aggressive tuning, with it and without it. Red is proportional plus integral, the run we just took apart. Green is the same two gains with derivative added, K d equal to three. The two leave the line at the same speed, because early on the error is huge and the proportional term dominates both of them. But the peak overshoot falls from about seventeen kilometres an hour to about ten, the ringing shrinks, and the green curve is within one kilometre an hour of the setpoint by about six seconds. Same car, same integral, one extra term. And the hill gets the same treatment, a shallow dip instead of a plunge, because the moment the speed starts to fall the derivative term sees the slope and pushes back, before the error has had time to grow. So there is the whole controller. Three terms, one error signal. The present, the past, and the projected future, added up into a single number and sent to the throttle. One caution before we tune it. That derivative is the slope of a measured signal, and real measurements are noisy. Differentiate noise and you amplify it, which is why practical controllers filter the derivative, or take it from the measurement rather than from the error.
So. Three gains, and one system that has to rise quickly, settle cleanly, and hold. Tuning is nothing more than deciding how much of each of the three answers you want. Proportional is the present. It reacts to the error you have right now, it is your main lever on how quickly you respond, and on its own it always leaves a gap. Integral is the past. It accumulates what proportional keeps leaving behind, and it is the only one of the three that can drive the steady error to zero. It buys you accuracy, and it costs you overshoot. Derivative is the future. It reads the slope and leans against fast change. It buys you damping, and it costs you sensitivity to noise. Here is the tuning we ended on. K p six, K i three, K d three. Quick off the line, one small overshoot, settled inside about six seconds, and it takes the hill in its stride. And watch which term is doing the work along that curve. In the first second it is nearly all proportional. Through the middle of the rise, derivative is holding it back. And after the hill, the last few kilometres an hour are closed by the integral, slowly, with the other two almost silent. Now let's break it, one gain at a time, and watch the same curve move. Triple the proportional gain and the response gets snappy and impatient. It hits harder, it swings wider, and it argues with itself for a good deal longer before it settles. Put that back, and turn the integral up instead. The accumulator gets ahead of the plant, so the overshoot grows, and there is a long slow ring while the pile is paid back down. And too much derivative goes the other way. It is so eager to lean against any change at all that it drags its feet, and the rise slows right down. In a real plant it would also be turning every twitch of sensor noise into a twitch of the throttle. The three broken tunings make the trade explicit. Too much proportional gain buys speed with overshoot and fast ringing. Too much integral gain buys persistence with overshoot and slow ringing. Too much derivative gain buys resistance to change with a slower rise and greater noise sensitivity. Which is also why tuning rules exist. Ziegler and Nichols, relay tuning, and the rest of them are recipes for finding a decent corner of that trade without walking the whole space by hand. So that is P I D. One error signal, read three ways. What it is, what it has been, and where it is going. Multiply each reading by a gain, add them up, and send the sum to the actuator. It is a strikingly small idea, and it runs a startling fraction of the machines around you. The ovens, the drones, the disk drives, the cruise control in the car you came here in. Three terms. One error.
Loading discussion…