A box as an equation: lag plus dead time
In the previous post of this series the heated box was a model I used without justifying it. That is a bit of a cheat, because every tuning rule you will ever read starts with “assume the process is” followed by three letters: $K$, $T$ and $L$.
This post earns the model. Where the three letters come from, why something this crude works for a box full of warm air, and how to measure them with nothing more than a step in heater power and a log file.
A box, a heater and a leak
Start with the simplest box there is: everything inside is at one temperature $\theta$, a heater puts in $P$ watts, and the walls leak heat to a room at $\theta_a$. Two numbers describe it. $C$ is the heat capacity in joules per kelvin, the energy it takes to warm the contents by one degree. $R$ is the thermal resistance of the walls in kelvin per watt, the temperature difference it takes to push one watt through them. Energy in minus energy out is energy stored:
$$ C \frac{d\theta}{dt} = P - \frac{\theta - \theta_a}{R} $$
A controller does not think in watts, it thinks in percent of full power, so write $P = P_{max} u / 100$ with $u$ between 0 and 100. Measure the temperature as the rise above the room, $y = \theta - \theta_a$, multiply everything by $R$, and you get
$$ T \frac{dy}{dt} + y = K u, \quad T = R C, \quad K = \frac{R P_{max}}{100} $$
This is a first-order lag, $K / (T s + 1)$ as a transfer function. $K$ is the gain in degrees per percent: leave the heater at 1 percent forever and the box settles $K$ degrees above the room, at the point where the walls lose exactly what the heater gives. $T$ is the time constant in seconds: heat stored per degree, divided by heat leaked per degree per second. After a step in $u$ the box covers $1 - 1/e$, or 63.2 percent, of the remaining distance every $T$ seconds.
Two things here are worth a second look. Better insulation (a bigger $R$) makes the box hotter and slower by the same factor. And the ratio $K / T = P_{max} / (100 C)$ does not contain $R$ at all: the slope right after you switch the heater on depends only on the heater and on what it has to warm up. The walls only decide where the curve bends over.
Three masses in a row
Now the bad news: no real box has a single temperature. The heater element is a lump of metal that has to warm up before it gives anything to the air. The air has to warm up before the sensor notices. The sensor is a small lump of its own. Each one stores heat and passes it on, so each one is its own little energy balance, and the heat reaches the sensor only after going through all of them.
For a chain like this the linear algebra is kind to us. The state matrix is tridiagonal, and if you scale each temperature by the square root of its heat capacity it becomes symmetric and negative definite. So the poles are real and negative, there are no zeros between the first mass and the last, and from heater to sensor you have a product of lags. The masses load each other, so the three time constants are not simply one $RC$ per mass, but they are three positive real numbers.
One lag starts moving the moment you step the heater, and its steepest slope is at $t = 0$. Three lags start as $t^3$: flat, then reluctantly up. From the outside, a box that sits flat for a while and then rises like a single lag looks just like a box where nothing happens for $L$ seconds and then a single lag starts. That is the first-order-plus-dead-time model, FOPDT for short:
$$ \frac{K}{(\tau_1 s + 1)(\tau_2 s + 1)(\tau_3 s + 1)} \approx \frac{K e^{-L s}}{T s + 1} $$
The honest reason this works fits in one line. For a small time constant, $1 / (\tau s + 1) \approx e^{-\tau s}$, because both are $1 - \tau s$ to first order, and delays in series simply add. Push it to the limit and it becomes exact: $n$ equal lags of $T/n$ each give $(1 + sT/n)^{-n} \to e^{-sT}$. So the dead time of a box does not need anything that actually travels. It is the sum of everything fast that you decided not to model.
Here is the model on its own, for a 10 percent step in heater power:
Move one slider at a time and watch what stays fixed. Then drag L up until it is as long as T.
$K$ only stretches the picture vertically and $L$ only slides it to the right. $T$ is the one that changes the shape. The tangent drawn where the response starts hits the final value exactly $T$ seconds later, which is also the moment the curve has covered 63.2 percent of the way. Those two facts are the two classical ways of reading the numbers off a plot.
In code the model is a delay line in front of a lag:
// one sample of K e^(-Ls) / (Ts + 1), called every DT seconds
float fopdt_step(float u) {
static float line[DELAY_STEPS], y; // DELAY_STEPS = L / DT
static int head;
float delayed = line[head]; // what the heater did L seconds ago
line[head] = u;
head = (head + 1) % DELAY_STEPS;
// exact, not Euler, as long as the input is constant over one sample
y += (K * delayed - y) * (1.0f - expf(-DT / T));
return y;
}
Reading the numbers off a step test
The experiment is as simple as it gets. Hold the heater at a constant value until the temperature stops moving, step it by $\Delta u$, and log the temperature until it stops again. The gain is the total rise divided by $\Delta u$. $T$ and $L$ come from the shape of the curve.
The oldest recipe is from Optimum settings for automatic controllers (Ziegler and Nichols, 1942), the paper with the first PID tuning rules. As Åström and Murray describe it, they reduced the whole step response to two numbers: the intercepts of its steepest tangent with the two axes. Find the steepest point of the curve and draw the tangent through it. Where it crosses the starting level is $L$, and its slope gives $T$, as in the picture above.
This is lovely with a ruler and a paper chart, and fragile with real data, for two reasons. It needs the derivative of a measured signal, and taking differences amplifies noise: two samples one second apart, each with noise $\sigma$, give a slope error of $\sigma \sqrt{2}$ per second, which for a slow box is easily more than the slope you are looking for. And everything hangs on a single point of the curve.
The more robust recipe uses two levels instead of one slope. An FOPDT response reaches 28.3 percent of its rise at $L + T/3$ and 63.2 percent at $L + T$, so two crossing times $t_{28}$ and $t_{63}$ are enough: $T = 1.5 (t_{63} - t_{28})$ and $L = t_{63} - T$. No derivative anywhere.
// first time the samples reach `level`, interpolated between two samples
function crossing(ys, dt, level) {
for (var i = 1; i < ys.length; i++) {
if (ys[i] < level) continue;
return dt * (i - 1 + (level - ys[i - 1]) / (ys[i] - ys[i - 1]));
}
}
var t28 = crossing(ys, dt, 0.283 * rise), t63 = crossing(ys, dt, 0.632 * rise);
var T = 1.5 * (t63 - t28), L = t63 - T, K = rise / du;
A note on who to thank. Marlin’s textbook presents the tangent as “Method I” and this as “Method II”, prefers the second because of the slope problem, and credits the choice of points to C. Smith’s 1972 book Digital Computer Process Control. You will also see the method credited to Sundaresan and Krishnaswamy (1978). That is the same idea with different points, 35.3 and 85.3 percent, and different constants.
Now back to the three masses. Here is a plant that is three pure lags with no dead time anywhere, and the two-point fit laid over it.
Press "One mass", then bring the heater and sensor sliders up from zero and watch L.
With one mass the fit is exact and $L$ is zero. Every mass you add creates dead time out of nothing: with 20, 60 and 8 seconds the fit says $L = 23.9$ s and $T = 67.6$ s, although nothing in the plant is delayed. With three equal masses $L$ is more than half of $T$. The fit is wrong at the very start, where it has a corner and the real curve is smooth, and close everywhere else.
And here is the same plant measured through a noisy sensor, one sample per second, fitted by both methods again and again:
Set the smoothing to 1 and watch the coral fits jump. Then raise it slowly and watch what happens to the tangent's T.
With 1 percent noise and no smoothing the tangent fit is useless: the steepest slope it finds is a noise spike, $T$ comes out near 40 seconds and $L$ lands anywhere. The two-point fit on the very same samples stays within a couple of seconds of $L = 24$, $T = 67$. A moving average calms the tangent down, but now its answer depends on the filter: $T$ is about 76 seconds with 5 samples, about 97 with 15, and 107 with no noise at all. That last number is the other problem. On an S-shaped curve the steepest tangent reaches the final value late, so the tangent’s $T$ is too long even with perfect data.
The ratio that tells you how hard it is
Of the three numbers, $K$ is just a scale factor. The one that tells you how hard the loop is to control is the ratio $L/T$.
A lag alone is easy. Its phase lag never exceeds 90 degrees, so on paper you can turn the proportional gain up as far as you like and the loop stays stable. Dead time is the opposite: it leaves the amplitude alone at every frequency and adds a phase lag of $\omega L$ that grows without bound. The loop starts to oscillate at the frequency where the total reaches 180 degrees, $\arctan(\omega T) + \omega L = \pi$. When $L$ is small compared to $T$ the lag has already spent its 90 degrees, so $\omega \approx \pi / (2L)$, the gain of the lag there is about $K / (\omega T)$, and the largest proportional gain before the loop oscillates forever is
$$ k_u \approx \frac{\pi}{2} \cdot \frac{T}{K L} $$
The loop gain you are allowed, $K k_u$, depends on $T/L$ and nothing else. The Ziegler and Nichols rule for a proportional controller says the same thing in their own terms: the gain is $1/a$, where $a$ is the intercept of the steepest tangent with the vertical axis, and for this model $a = K L / T$.
There is a time-domain way to see it too. The controller cannot see the result of anything it does for $L$ seconds, so the closed loop cannot be much faster than $L$, while the box on its own moves at the pace of $T$. A small $L/T$ means feedback can make the box many times faster than it naturally is. An $L/T$ near 1 means feedback buys you very little and you mostly wait. Åström and Murray use the bounded version $L / (L + T)$, call it the normalized time delay, and name the two ends “lag dominated” and “delay dominated”. They also note that derivative action does little for delay dominated processes.
The half rule
If you already have the higher-order model, from physics or from a better fit, you do not need a step test to reduce it. Simple analytic rules for model reduction and PID controller tuning (Skogestad, 2003) gives a rule you can do in your head:
the largest neglected (denominator) time constant (lag) is distributed evenly to the effective delay and the smallest retained time constant.
For three lags sorted from slowest to fastest that means
$$ T = \tau_1 + \frac{\tau_2}{2}, \quad L = \frac{\tau_2}{2} + \tau_3 + \frac{h}{2} $$
where $h$ is the sampling period of a digital controller. I like that last term: the paper counts half a sample as dead time, which is a good reminder that your own loop rate is part of the process. Why half of $\tau_2$ and not all of it? Turning a whole lag into delay is pessimistic, because a delay hurts control more than a lag of the same size, and the paper says so in as many words.
Tick “Half rule” in the three-mass widget (it uses $h = 0$). For 20, 60 and 8 seconds it gives $L = 18$ and $T = 70$, close to the two-point fit. For three equal masses it gives $L = T$, while the two-point fit says $L/T = 0.54$. That is not a contradiction. The two-point fit is trying to match the step response. The half rule is trying to produce a model that the tuning rules in the same paper can safely use, so it leans towards more dead time.
What I will do with the real box
All of this is heading towards BroodBoard, my egg incubator controller. The board is a prototype that is being manufactured as I write this, so I have no measured step response to show you yet. The desktop simulator I wrote for it models the box as a single lag plus noise. The plan is to run exactly the step test described here on the real box and fit the model.
I will let the box settle with the heater at a fixed percentage, step it, log until it is flat again, and take $t_{28}$ and $t_{63}$. No ruler needed :).
Further reading
- J. G. Ziegler and N. B. Nichols, Optimum settings for automatic controllers, Transactions of the ASME 64, 1942.
- S. Skogestad, Simple analytic rules for model reduction and PID controller tuning, Journal of Process Control 13, 2003. The author keeps a copy on his page.
- K. R. Sundaresan and P. R. Krishnaswamy, Estimation of time delay time constant parameters in time, frequency, and Laplace domains, The Canadian Journal of Chemical Engineering 56, 1978.
- K. J. Åström and R. M. Murray, Feedback Systems: An Introduction for Scientists and Engineers, second edition, free online. The PID chapter has the step response method and the normalized time delay.
- T. Marlin, Process Control: Designing Processes and Control Systems for Dynamic Performance, second edition, free online. Chapter 6 is about fitting models to step tests.