State feedback with an observer (pole placement, LQR, Kalman)

Every state of the plant fed back with its own gain, u = −K·x̂, the states estimated by an observer (or a Kalman filter) from the few that are measured. The gains come from where you want the closed-loop poles, or from weights on what matters (LQR). It can damp a swing or a resonance that no PID can see.

Theory

State space. Write the plant as first-order equations in all its states (actuator, positions, speeds, transmitter lags):

x˙=Ax+Bu,y=Cx\dot x = Ax + Bu,\qquad y = Cx

A PID sees one output. State feedback uses all of xx:

u=−Kxu = -Kx

and the closed loop x˙=(A−BK)x\dot x = (A - BK)x has the eigenvalues of A−BKA - BK. If the plant is controllable (the input reaches every state; the controllability matrix [B AB ⋯ An−1B][B\ AB\ \cdots\ A^{n-1}B] has full rank), KK can put them anywhere.

Pole placement. Choose the closed-loop poles, solve for KK (Ackermann's formula, or the better-conditioned Sylvester-equation method used here). In Loop Lab you choose a dominant pair (ωn,ζ)(\omega_n, \zeta). Lightly damped pairs (a swing, a resonance) keep their frequency but get damping ζ\zeta, unstable poles are mirrored, and poles already faster stay put. Moving a pole a long way costs gain, and gain costs noise and margin.

LQR. Instead of poles, say what matters: minimize

J=∫0∞(x⊤Qx+u⊤Ru)dtJ = \int_0^\infty \left(x^\top Q x + u^\top R u\right)dt

The answer is K=R−1B⊤XK = R^{-1}B^\top X with XX from the Riccati equation A⊤X+XA−XBR−1B⊤X+Q=0A^\top X + XA - XBR^{-1}B^\top X + Q = 0. Here QQ puts weights qyq_y on the PV error, q2q_2 on the secondary measurement and qiq_i on the integral, against 1 on the output. LQR on its own has guaranteed margins (gain margin ∞, phase margin ≥ 60°) with the states measured.

Observers. The states usually aren't all measured. Run a copy of the model in the controller and correct it with what is measured:

x^˙=Ax^+Bu+L(y−Cx^)\dot{\hat x} = A\hat x + Bu + L(y - C\hat x)

The estimation error obeys e˙=(A−LC)e\dot e = (A - LC)e: LL is designed like KK, on the dual system. That needs observability (the outputs see every state). A Kalman filter chooses LL from how noisy the measurement is against how much the plant gets pushed around.

The separation principle. The closed-loop poles are those of A−BKA - BK plus those of A−LCA - LC: regulator and observer can be designed separately. But margins don't separate. LQR plus a Kalman filter (LQG) has no guaranteed margins: Doyle's counterexample (1978) has a gain margin as thin as you like. Always check the margins of the whole loop, broken at the plant input.

No offset. u=−Kx^u = -K\hat x alone leaves an offset under any constant upset. Two cures:

  • An integral of the error as an extra state, fed back like the others. Simple, but it integrates during every transient and has to unwind afterwards: overshoot.
  • Estimate the disturbance in the observer (a constant dd where it really acts, d˙=0\dot d = 0) and cancel it in steady state. No windup, no overshoot from the integral; it needs the disturbance to be observable from what is measured.

When it works, and when it breaks

Works
  • Lightly damped modes the measurement doesn't show directly: a crane's swinging load, a flexible shaft, a long boom or arm.

  • Open-loop unstable plants (magnetic bearings, balancing), where the design has to be done from the model anyway.

  • Plants with several measured states, each telling the controller something the main PV can't.

  • Anywhere the model is good and the payoff is speed: positioning, motion, drive trains.

Breaks
  • Model wrong: the observer's estimate drifts away from the plant, and the controller acts on fiction. Check the estimate against a measurement.

  • Something unobservable or uncontrollable from the chosen sensors and actuator: no gain can fix it (a crane with only the load position can't tell wind from a trolley offset).

  • A disturbance the observer doesn't know about: it explains it with the wrong states (the crane chases the wind).

  • Observer or regulator poles too fast for the controller cycle, the actuator or the noise: the margins collapse even though the poles look fine.

  • The plant is nonlinear and moves away from the design point: the gains are for one linearization.

How to tune it

  1. Get the model right first. Bump tests, a frequency sweep for any resonance, the physical constants. The design is only as good as AA and BB.
  2. Choose what to measure so every state is observable, including any disturbance to be estimated.
  3. Regulator. Pole placement: start with ωn\omega_n around what a PI could do, ζ≈0.7\zeta \approx 0.7–0.8, and raise ωn\omega_n until the output effort or the margins say stop. LQR: start with qyq_y = 1 per % and raise it by decades; put weight q2q_2 on a swing you want damped.
  4. Observer. Two to three times faster than the regulator, or a Kalman filter with a sensible ratio of upsets to noise. Faster isn't better: it passes more noise and needs a better model.
  5. Offset: prefer estimating the disturbance where it acts; use an integral of the error if you can't model one.
  6. Check the whole loop: margins at the plant input, the tests with model mismatch, and the observer's estimate against a real reading.
The tuning rules in the lab
SIMC (Skogestad) (PI, PID)
Kc=τK(τc+θ),Ti=min⁡(τ, 4(τc+θ))K_c = \frac{\tau}{K(\tau_c + \theta)},\quad T_i = \min\big(\tau,\ 4(\tau_c + \theta)\big)

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.

Lambda (IMC) (PI)
Kc=τK(λ+θ),Ti=τK_c = \frac{\tau}{K(\lambda + \theta)},\quad T_i = \tau

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.

IMC-PID (Rivera–Morari–Skogestad) (PI, PID)
Kc=2τ+θ2Kλ,Ti=τ+θ2PID: Kc=2τ+θK(2λ+θ), Td=τθ2τ+θK_c = \frac{2\tau + \theta}{2K\lambda},\quad T_i = \tau + \frac{\theta}{2}\qquad\text{PID: } K_c = \frac{2\tau + \theta}{K(2\lambda + \theta)},\ T_d = \frac{\tau\theta}{2\tau + \theta}

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.

Lambda for integrating (averaging level) (P, PI)
Kc=2λ+θk′(λ+θ)2,Ti=2λ+θK_c = \frac{2\lambda + \theta}{k'(\lambda + \theta)^2},\quad T_i = 2\lambda + \theta

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.

Ziegler–Nichols (reaction curve) (P, PI, PID)
Kc=0.9 τKθ,Ti=3.33 θK_c = \frac{0.9\,\tau}{K\theta},\quad T_i = 3.33\,\theta

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.

Cohen–Coon (P, PI, PID)
Kc=τKθ(0.9+θ12τ),Ti=θ 30+3θ/τ9+20θ/τK_c = \frac{\tau}{K\theta}\Big(0.9 + \frac{\theta}{12\tau}\Big),\quad T_i = \theta\,\frac{30 + 3\theta/\tau}{9 + 20\theta/\tau}

Like Ziegler–Nichols but corrected for dead time, so it copes better when θ/τ is large. Also aimed at quarter-amplitude decay, so also aggressive.

AMIGO (Åström–Hägglund) (PI, PID)
Kc=0.15K+(0.35−θτ(θ+τ)2)τKθ,Ti=0.35θ+13θτ2τ2+12θτ+7θ2K_c = \frac{0.15}{K} + \Big(0.35 - \frac{\theta\tau}{(\theta+\tau)^2}\Big)\frac{\tau}{K\theta},\quad T_i = 0.35\theta + \frac{13\theta\tau^2}{\tau^2 + 12\theta\tau + 7\theta^2}

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

  • Discretize the whole controller for the PLC cycle (here: the continuous design, then a zero-order-hold discretization of the observer). Change the cycle and the matrices must be regenerated.
  • Feed the observer the output actually applied, after the limits. Then it doesn't wind up, and with the disturbance estimate, nothing does.
  • Precision. Observer matrices have entries a hair away from 1: use LReal (64-bit), not Real.
  • Deviation variables. Everything is relative to the design point (the output, measurements and setpoint there); store those values with the matrices.
  • Manual and bumpless: in manual, set the estimate to the steady state matching the measurement, and the integral or disturbance estimate so the output equals the manual value.
  • Watch the estimate. Trend the observer's estimate of the PV next to the real reading. When they part company, the model is wrong.

Siemens and elsewhere

Siemens
  • No standard block. PID_Compact, PID_Temp and PID_3Step are PIDs. State feedback is custom SCL: the exported LL_StateFb is one FB with the matrices as LReal arrays, about 2n22n^2 multiply-adds per cycle (a few µs on an S7-1500).
  • Cyclic interrupt (OB30–38) at the design cycle; for motion, an isochronous OB synchronized to PROFINET IRT.
  • Drives carry their own versions: speed-loop observers, active vibration damping, and anti-sway packages for cranes. Use those where they exist; they run faster than any PLC.
  • Generate the matrices offline (MATLAB, Python, or this page) and load them as DB start values; document the model and the design point next to them.
Elsewhere
  • IEC 61131-3: the same FB in Structured Text, arrays of LREAL; CODESYS and TwinCAT run it as is.
  • Motion controllers and drives (servo, crane, robot) are where state feedback and observers live in industry, mostly hidden behind parameters.
  • MPC is the next rung: the same model, but the controller plans ahead and respects limits explicitly instead of saturating.

Interactive Controller Sandbox

Test and tune State feedback with an observer (pole placement, LQR, Kalman) live on a process, benchmark against baseline PI, and inspect the resulting code.

Closed-Loop Response: State feedback vs Baseline PI

Simulating…
Running test scenarios…
Block Diagram
SP rN_rN_r·rΣ+−uSC-601actuatord: wind on the loadProcesstrolley + pendulumPV yZT-601ZT-602: sway angleKalman filterx̂′ = Ax̂ + Bu + L(y − Cx̂)estimate ŷx̂Kevery stateK·x̂d̂ · N_dd̂ part
Generated code
// Loop Lab export: Crane with a swinging load, ZIC-601
// TIA Portal SCL (S7-1200 / S7-1500). Generated code: review before use on a real plant.

FUNCTION_BLOCK "LL_StateFb"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   // x[0] SC-601 (actuator); x[1] Trolley position; x[2] Trolley speed; x[3] Sway angle; x[4] Sway rate; x[5] ZT-601 reading; x[6] ZT-602 reading; x[7] Estimated wind on the load.
   VAR_INPUT
      Setpoint : Real;        // m
      Input : Real;           // ZT-601, m
      Input2 : Real;          // ZT-602, °
      ManualEnable : Bool;
      ManualValue : Real;     // %
   END_VAR
   VAR_OUTPUT
      Output : Real;          // %
      Estimate : Real;        // the observer's estimate of Input, m
   END_VAR
   VAR
      // x <- Phi*x + Gam*w with w = [last output, measurements, setpoint] (deviations from the design point, %).
      Phi : Array[0..7, 0..7] of LReal := [0.3678794412, -8.265670645E-12, -3.89770044E-12, -2.683994318E-8, -6.220031852E-10, -1.583329098E-11, -3.497469953E-10, -1.856012067E-14,
         5.357462637E-5, 0.9570217622, 0.04556041614, 2.141259257, 0.04135105742, -0.155384477, 0.0640452338, 1.124191762E-6,
         0.001870181062, -0.04971919343, 0.8483181679, 22.27310982, 0.4682343262, -0.1769519113, 0.4431525175, 1.331620627E-5,
         -6.274173653E-6, -0.0001710751794, 0.0004319692105, 0.4643639323, 0.03946646, -0.0006037479469, -0.01269306757, 2.210709016E-6,
         -0.0002320056837, -0.002952116254, 0.01868922659, -3.709370815, 0.9216443211, -0.0104386421, -0.07123812226, 9.775800532E-5,
         2.317056273E-6, 0.3561128675, 0.009857707832, 1.617706454, 0.05482319032, 0.4711967717, -0.02610825346, 2.114802826E-6,
         -8.310752679E-5, -0.004492764779, 0.005330895565, 15.7770408, 0.6608707545, -0.01585139404, -0.1475983657, 2.71839329E-5,
         0.0003519168724, -1.704717539, -0.05173009757, -197.1921632, -4.042567396, -6.023804815, -3.737565081, 0.9998864504];
      Gam : Array[0..7, 0..3] of LReal := [0.6321205588, 9.639584649E-12, 1.634473765E-10, 0.0,
         2.016740638E-5, 0.07934508593, -0.02148603641, 0.0,
         0.001145921501, 0.09066844189, -0.167795245, 0.0,
         -2.425780862E-6, 0.0003099292505, 0.004398827407, 0.0,
         -0.0001428232203, 0.005356303341, 0.02690410993, 0.0,
         5.211247415E-7, 0.0690761443, 0.009514811056, 0.0,
         -2.510116566E-5, 0.008137663527, 0.1742637705, 0.0,
         7.95137824E-5, 3.091408942, 1.388481843, 0.0];
      // Output = U0 + Cout*x + Nr*r: the state feedback gains with their sign.
      Cout : Array[0..7] of LReal := [-0.1130352575, -25.0, -38.99436658, 811.3970323, -35.50518347, 0.0, 0.0, -1.649577219];
      Nx : Array[0..7] of LReal := [0.0, 0.4, 0.0, 0.0, 0.0, 0.4, 0.0, 0.0];
      Cpv : Array[0..7] of LReal := [0.0, 0.0, 0.0, 0.0, 0.0, 2.5, 0.0, 0.0];
      Nr : LReal := 9.999999999;
      U0 : LReal := 50.0;        // design point: output, %
      Y0 : LReal := 50.0;        // design point: measurement, % of span
      X : Array[0..7] of LReal;
      XNew : Array[0..7] of LReal;
      W : Array[0..3] of LReal;
      ULast : LReal;
      Started : Bool;
   END_VAR
   VAR_TEMP
      i : Int;
      j : Int;
      r : LReal;
      dy : LReal;
      u : LReal;
      acc : LReal;
   END_VAR

BEGIN
    #r := (#Setpoint - 0.0) / 40.0 * 100.0 - #Y0;
    #W[0] := #ULast - #U0;
    #W[1] := (#Input - 0.0) / 40.0 * 100.0 - 50.0;
    #W[2] := (#Input2 - -10.0) / 20.0 * 100.0 - 50.0;
    #W[3] := #r;
    IF #ManualEnable OR NOT #Started THEN
        // Track: the steady state that matches the measurement, the disturbance estimate set so the output equals ManualValue.
        #dy := (#Input - 0.0) / 40.0 * 100.0 - #Y0;
        FOR #i := 0 TO 7 DO
            #X[#i] := #Nx[#i] * #dy;
        END_FOR;
        #acc := #U0 + #Nr * #r;
        FOR #i := 0 TO 6 DO
            #acc := #acc + #Cout[#i] * #X[#i];
        END_FOR;
        #X[7] := (#ManualValue - #acc) / #Cout[7];
        #u := #ManualValue;
        #Started := TRUE;
    ELSE
        FOR #i := 0 TO 7 DO
            #acc := 0.0;
            FOR #j := 0 TO 7 DO
                #acc := #acc + #Phi[#i, #j] * #X[#j];
            END_FOR;
            FOR #j := 0 TO 3 DO
                #acc := #acc + #Gam[#i, #j] * #W[#j];
            END_FOR;
            #XNew[#i] := #acc;
        END_FOR;
        #u := #U0 + #Nr * #r;
        FOR #i := 0 TO 7 DO
            #u := #u + #Cout[#i] * #XNew[#i];
        END_FOR;
        FOR #i := 0 TO 7 DO
            #X[#i] := #XNew[#i];
        END_FOR;
    END_IF;
    #Output := LREAL_TO_REAL(LIMIT(MN := 0.0, IN := #u, MX := 100.0));
    #ULast := #Output;   // the observer needs what was really applied
    #acc := #Y0;
    FOR #i := 0 TO 7 DO
        #acc := #acc + #Cpv[#i] * #X[#i];
    END_FOR;
    #Estimate := LREAL_TO_REAL(0.0 + #acc / 100.0 * 40.0);
END_FUNCTION_BLOCK

// OB30 "Cyclic interrupt", cycle time 0.05 s (50 ms).
// "ZT-601" is the scaled input (NORM_X/SCALE_X from the analog card), "SC-601" the output to scale back to 0-27648.
// State feedback: one block holds the observer, the gains and the integral. Call it every cycle, in manual too (it tracks).
"ZIC_601_DB"(Setpoint := "SP", Input := "ZT-601", Input2 := "ZT-602", ManualEnable := "Manual", ManualValue := "ManualOut");
"SC-601" := "ZIC_601_DB".Output;
// "ZIC_601_DB".Estimate is the observer's idea of "ZT-601": trend it next to the real reading while commissioning.