Self-tuning (adaptive) PI
A PI that keeps measuring the plant and retunes itself: recursive least squares fits a simple model to what the loop does, and a tuning rule turns that model into gains, every cycle. It follows a plant that changes, as long as the loop gives it something to learn from.
Theory
A self-tuning regulator (indirect adaptive control) does on line what an engineer does with a bump test:
- Estimate a model from the recent data: here , a first-order model with the dead time assumed.
- Design the controller from it, as if it were true (certainty equivalence): here SIMC, , , , .
Recursive least squares updates the estimate with each new sample, weighting old data down by a forgetting factor (it remembers about samples):
What can go wrong, and it is why adaptive control is rare on plant floors:
- No excitation. In a steady loop the data says nothing about the plant, but forgetting keeps discarding what it knew. The covariance grows, the estimate drifts, and the next upset meets strange gains ("bursting"). The guards here: learn only when the signals move by more than the noise, bound , and keep the gains within 5× of the start.
- Wrong structure. A first-order fit to a second-order plant is biased, and the bias depends on what the loop is doing.
- Closed-loop identification: the controller's own feedback correlates input and noise.
MRAC (model reference adaptive control) is the other family: adjust the gains directly so the loop follows a reference model (the MIT rule, Lyapunov designs). It is elegant on paper, but harder to trust.
When it works, and when it breaks
A plant whose gain or speed changes slowly and predictably: batch stages, wear, fouling, a catalyst ageing.
Loops that see enough movement (setpoint changes, regular upsets) to keep the estimate honest.
As an advisor: estimate and display, let a person accept the new tuning.
Long quiet periods with forgetting on: the estimate wanders and the loop detunes (or worse) on nothing.
Fast changes: it learns more slowly than the plant moves.
Unmodelled dynamics or dead time error: the fitted model is biased, and so are the gains.
Nobody on site can say what it is doing at 3 a.m.
How to tune it
- Start it from a sound tuning: it should be safe even if it never learns anything.
- Dead time: measure it; the estimator doesn't.
- Forgetting: remember a few of the plant's slowest time constants (λ = 0.995 at a 2 s cycle remembers about 7 minutes).
- Guards: the dead zone (noise level), gain bounds, and a freeze switch.
- Watch the estimate (gain, time constant) on the HMI for a few weeks before trusting it.
The tuning rules in the lab
Model-based, one tuning knob: the closed-loop time constant τc. τc = θ gives a fast, still robust loop (Ms ≈ 1.6); larger τc is smoother. The min() on Ti keeps lag-dominant and integrating processes from responding too slowly to load upsets. PID uses the second-order model: Td = τ2 in series form.
The process-industry favourite: choose λ, the closed-loop time constant, and the setpoint response is first-order with no overshoot. Ti = τ cancels the process pole, which makes load rejection slow on lag-dominant processes. λ = 1–3 × τ is typical for robustness.
Internal model control worked back into a PID (Rivera, Morari and Skogestad, 1986): the controller that makes the loop a first-order λ response, with the dead time replaced by its first-order Padé approximation. That approximation is what gives the derivative, and why Ti = τ + θ/2 rather than τ. Keep λ above about 0.8θ for the PID and 1.7θ for the PI, where the approximation stops holding. Like Lambda, it cancels the process lag, so load upsets on lag-dominant processes recover slowly. The IMC structure itself (the model running beside the plant) is the Smith predictor on the bench.
For integrating processes such as levels, from the ramp rate k′ (% of span per second, per % of output). λ is the arrest time: how long the level keeps moving after an inflow upset. For a step upset Δ (in % of output) the level swings about k′·Δ·λ/e before it turns round, so choose λ from the room the tank has. A long λ lets the tank absorb the upset and keeps the outflow smooth: averaging level control. P-only: Kc = 1/(k′λ), which leaves an offset that is harmless in a buffer.
The 1942 rules, aiming for a quarter-amplitude decay. Aggressive: Ms is often above 2, so the loop rings and is sensitive to model error. Worth knowing because so many loops in the field were tuned this way.
Like Ziegler–Nichols but corrected for dead time, so it copes better when θ/τ is large. Also aimed at quarter-amplitude decay, so also aggressive.
Derived by optimizing load rejection with a robustness limit (Ms ≈ 1.4) over a large test batch of processes. A safe modern default when you have no reason to choose a speed.
How to implement it
- Order each cycle: run the PID with the present gains, then update the estimator with this cycle's data and compute the next gains.
- Freeze learning in manual, on a bad measurement, and when the operator asks.
- Log every gain change: it is a controller changing its own tuning.
Siemens and elsewhere
- PID_Compact / PID_Temp have pretuning and fine tuning: identification runs on demand, not continuously. For a batch you can trigger fine tuning per batch stage, which is a gain schedule in practice.
- For continuous adaptation: the exported LL_SelfTune next to the PID, writing Gain and Ti into the PID's parameters each cycle.
- PCS 7 has an adaptive / model-based block set in its Advanced Process Library; most DCSs have some self-tuning option.
- Commercial self-tuners (in single-loop controllers and DCSs) mostly retune on demand or on detected changes, not every sample, with heavy guarding.
- Gain scheduling is usually preferred when the change is predictable: it is adaptive control with the learning done once, by the engineer.
Interactive Controller Sandbox
Test and tune Self-tuning (adaptive) PI live on a process, benchmark against baseline PI, and inspect the resulting code.
Interactive Gains (Primary Loop)
Drag sliders or nudge gains to observe the closed-loop transient responseClosed-Loop Response: Adaptive vs Baseline PI
Simulating…// Loop Lab export: Exothermic batch reactor, TIC-1001
// TIA Portal SCL (S7-1200 / S7-1500). Generated code: review before use on a real plant.
FUNCTION_BLOCK "LL_PID"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Setpoint : Real; // engineering units
Input : Real; // measurement, engineering units
ManualEnable : Bool; // TRUE = manual: Output follows ManualValue
ManualValue : Real; // %
Feedforward : Real; // %, added to the output (0 when unused)
HoldIntegral : Int; // +1 / -1: don't integrate up / down (a cascade slave is saturated)
ExtResetEnable : Bool; // override: this controller is not selected, so its integral follows ExtReset
ExtReset : Real; // %, the selected (valve) output, for external reset feedback
Cycle : Real := 2.0; // s, must match the cyclic interrupt the block is called from
END_VAR
VAR_OUTPUT
Output : Real; // %
AtLimit : Int; // +1 / -1 when the output is pinned high / low
END_VAR
VAR
Gain : Real := 0.88; // % output per % of span
Ti : Real := 252.0; // s, 0 = no integral action
Td : Real := 0.0; // s, 0 = no derivative action
PWeighting : Real := 1.0; // setpoint weight b on P
DWeighting : Real := 0.0; // setpoint weight c on D
TdFiltRatio : Real := 0.1; // derivative filter = TdFiltRatio * Td
InLow : Real := 0.0;
InHigh : Real := 150.0;
OutLow : Real := 0.0;
OutHigh : Real := 100.0;
Invert : Bool := FALSE;
Integral : Real; // integral term, in % output (so gain changes don't bump)
DTerm : Real;
LastED : Real;
END_VAR
VAR_TEMP
sp : Real;
pv : Real;
dir : Real;
e : Real;
eP : Real;
eD : Real;
p : Real;
tf : Real;
iNext : Real;
v : Real;
u : Real;
END_VAR
BEGIN
// Scale to % of span: the gains are then independent of engineering units.
#sp := (#Setpoint - #InLow) / (#InHigh - #InLow) * 100.0;
#pv := (#Input - #InLow) / (#InHigh - #InLow) * 100.0;
#dir := 1.0;
IF #Invert THEN
#dir := -1.0;
END_IF;
#e := #dir * (#sp - #pv);
#eP := #dir * (#PWeighting * #sp - #pv);
#eD := #dir * (#DWeighting * #sp - #pv);
#p := #Gain * #eP;
IF #ManualEnable THEN
// Track: preload the integral so switching to auto doesn't bump the output.
#u := LIMIT(MN := #OutLow, IN := #ManualValue, MX := #OutHigh);
#Integral := #u - #p - #Feedforward;
#DTerm := 0.0;
#LastED := #eD;
#Output := #u;
#AtLimit := 0;
RETURN;
END_IF;
// Derivative on c*SP - PV through a first-order filter (backward Euler).
IF #Td > 0.0 THEN
#tf := MAX(IN1 := #TdFiltRatio * #Td, IN2 := 1.0E-6);
#DTerm := #tf / (#tf + #Cycle) * #DTerm
+ #Gain * #Td / (#tf + #Cycle) * (#eD - #LastED);
ELSE
#DTerm := 0.0;
END_IF;
#LastED := #eD;
IF #ExtResetEnable AND #Ti > 0.0 THEN
// External reset feedback: the integral follows the selected output through a Ti lag,
// so this controller's output is the valve signal plus its own P and D.
#iNext := #Integral + #Cycle / MAX(IN1 := #Ti, IN2 := #Cycle) * (#ExtReset - #Integral);
ELSIF #Ti > 0.0 THEN
#iNext := #Integral + #Gain * #Cycle / #Ti * #e;
ELSE
#iNext := #Integral;
END_IF;
#v := #p + #iNext + #DTerm + #Feedforward;
#u := LIMIT(MN := #OutLow, IN := #v, MX := #OutHigh);
// A saturated slave asks its master to stop integrating in that direction.
IF #ExtResetEnable THEN
#Integral := #iNext;
ELSIF #Ti > 0.0 AND NOT ((#HoldIntegral > 0 AND #iNext > #Integral) OR (#HoldIntegral < 0 AND #iNext < #Integral)) THEN
// Conditional integration: stop integrating while the output is pinned and the error pushes it further.
IF NOT ((#v > #OutHigh AND #iNext > #Integral) OR (#v < #OutLow AND #iNext < #Integral)) THEN
#Integral := #iNext;
END_IF;
END_IF;
IF #v > #OutHigh THEN
#AtLimit := 1;
ELSIF #v < #OutLow THEN
#AtLimit := -1;
ELSE
#AtLimit := 0;
END_IF;
#Output := #u;
END_FUNCTION_BLOCK
FUNCTION_BLOCK "LL_SelfTune"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
Input : Real; // TT-1001, °C
PidOutput : Real; // the PID's output this cycle, %
Learn : Bool := TRUE; // FALSE freezes the estimate (e.g. in manual, or when the operator says so)
END_VAR
VAR_OUTPUT
Gain : Real; // % per % of span: write to the PID
Ti : Real; // s
ModelGain : Real; // the estimate: K (% per %)
ModelTau : Real; // s
END_VAR
VAR
Lambda : LReal := 0.995; // forgetting: remembers about 200 cycles
Tc : LReal := 166.0; // closed-loop time constant, s
Theta : LReal := 83.0; // dead time, s
Cycle : LReal := 2.0;
Noise : LReal := 0.01; // % of span: don't learn from movements smaller than this
Kc0 : LReal := 0.88; // the gains it may move within 5x of
Ti0 : LReal := 252.0;
Th : Array[0..2] of LReal := [0.9972850723, 0.01051764209, 0.0]; // a, b, c
P : Array[0..2, 0..2] of LReal := [0.01, 0.0, 0.0, 0.0, 0.01, 0.0, 0.0, 0.0, 0.01];
UHist : Array[0..42] of LReal;
YPrev : LReal;
Started : Bool;
END_VAR
VAR_TEMP
i : Int;
j : Int;
y : LReal;
phi : Array[0..2] of LReal;
pphi : Array[0..2] of LReal;
k : Array[0..2] of LReal;
den : LReal;
err : LReal;
K : LReal;
tau : LReal;
span : LReal;
kc : LReal;
ti : LReal;
END_VAR
BEGIN
#y := (#Input - 0.0) / 150.0 * 100.0;
IF NOT #Started THEN
FOR #i := 0 TO 42 DO
#UHist[#i] := #PidOutput;
END_FOR;
#Th[2] := #y - #Th[0] * #y - #Th[1] * #PidOutput;
#YPrev := #y;
#Gain := LREAL_TO_REAL(#Kc0);
#Ti := LREAL_TO_REAL(#Ti0);
#Started := TRUE;
END_IF;
// phi = [y(k-1), u(k-1-d), 1]: UHist[42] is the oldest output kept.
#phi[0] := #YPrev;
#phi[1] := #UHist[42];
#phi[2] := 1.0;
#err := #y - (#Th[0] * #phi[0] + #Th[1] * #phi[1] + #Th[2]);
// Learn only from real movement, and only from errors bigger than the noise.
IF #Learn AND (ABS(#UHist[0] - #UHist[1]) > 0.02 OR ABS(#y - #YPrev) > 3.0 * #Noise) AND ABS(#err) > #Noise THEN
#den := #Lambda;
FOR #i := 0 TO 2 DO
#pphi[#i] := 0.0;
FOR #j := 0 TO 2 DO
#pphi[#i] := #pphi[#i] + #P[#i, #j] * #phi[#j];
END_FOR;
#den := #den + #phi[#i] * #pphi[#i];
END_FOR;
FOR #i := 0 TO 2 DO
#k[#i] := #pphi[#i] / #den;
#Th[#i] := #Th[#i] + #k[#i] * #err;
END_FOR;
FOR #i := 0 TO 2 DO
FOR #j := 0 TO 2 DO
#P[#i, #j] := (#P[#i, #j] - #k[#i] * #pphi[#j]) / #Lambda;
END_FOR;
END_FOR;
// SIMC from the estimate (an integrator if a is about 1), kept within 5x of the start.
#span := #Tc + #Theta;
IF #Th[0] < 0.9995 AND #Th[0] > 0.0 THEN
#K := #Th[1] / (1.0 - #Th[0]);
#tau := -#Cycle / LN(#Th[0]);
#kc := #tau / (ABS(#K) * #span);
#ti := MIN(IN1 := #tau, IN2 := 4.0 * #span);
ELSE
#K := 0.0;
#tau := 0.0;
#kc := #Cycle / (ABS(#Th[1]) * #span);
#ti := 4.0 * #span;
END_IF;
IF #Th[1] > 0.0 THEN // only a model with the expected sign
#Gain := LREAL_TO_REAL(LIMIT(MN := #Kc0 / 5.0, IN := #kc, MX := #Kc0 * 5.0));
#Ti := LREAL_TO_REAL(LIMIT(MN := #Ti0 / 5.0, IN := #ti, MX := #Ti0 * 5.0));
END_IF;
#ModelGain := LREAL_TO_REAL(#K);
#ModelTau := LREAL_TO_REAL(#tau);
END_IF;
#YPrev := #y;
FOR #i := 42 TO 1 BY -1 DO
#UHist[#i] := #UHist[#i - 1];
END_FOR;
#UHist[0] := #PidOutput;
END_FUNCTION_BLOCK
// OB30 "Cyclic interrupt", cycle time 2.0 s (2000 ms).
// "TT-1001" is the scaled input (NORM_X/SCALE_X from the analog card), "TY-1001" the output to scale back to 0-27648.
// Self-tuning: run the PID with the latest gains, then let the estimator learn from this cycle and retune.
"TIC_1001_DB"(Setpoint := "SP", Input := "TT-1001", ManualEnable := "Manual", ManualValue := "ManualOut");
"TY-1001" := "TIC_1001_DB".Output;
"TIC_1001_Tune_DB"(Input := "TT-1001", PidOutput := "TIC_1001_DB".Output, Learn := NOT "Manual");
"TIC_1001_DB".Gain := "TIC_1001_Tune_DB".Gain;
"TIC_1001_DB".Ti := "TIC_1001_Tune_DB".Ti;
// Show "TIC_1001_Tune_DB".ModelGain and .ModelTau on the HMI, with a button to freeze learning (Learn := FALSE).