Your own controller (Structured Text)

Write the controller yourself in IEC 61131-3 Structured Text, run it on the bench against any process and test, and export it as an FB for TIA Portal or any IEC 61131-3 PLC.

Theory

A controller is a program that runs every cycle. Every rung on the ladder is one: a PID is a few lines of arithmetic with a memory (the integral). Writing it out makes three things concrete:

  • State: what it remembers between cycles (here, anything in a VAR block).
  • The cycle: everything happens once per TS seconds, from the measurement it read to the output it writes.
  • The edges: limits, windup, manual mode, a bad input. That is where most of the code in a real controller goes.

What the code can use

  • Reads: SP and PV in engineering units; SP_PCT and PV_PCT in % of span (on a 2×2 process also PV2, SP2 and their %); TS, TIME, MANUAL and MAN_OUT.
  • Writes: OUT, in % (and OUT2 on a 2×2 process).
  • Statements: IF, FOR, WHILE, REPEAT, assignments.
  • Functions: the standard ones (ABS, SQRT, EXP, LN, MIN, MAX, LIMIT, SEL, TRUNC, the TO conversions), with named arguments like LIMIT(MN := 0.0, IN := u, MX := 100.0).

It starts as the process's PI, written out in full: change it, run it, compare it with the built-in controllers on the same tests and scorecard.

When it works, and when it breaks

Works
  • Trying a variation the ladder doesn't have: a nonlinear gain, a deadband, a different anti-windup, a sequence.

  • Checking your own PLC code against a process before it goes near the plant.

  • Learning: write the PID yourself and see that it matches the built-in one.

Breaks
  • No manual tracking: the switch to auto bumps.

  • Integrating into a limit (windup): handle it, as the starting PI does.

  • Code that depends on the cycle time without using TS: it changes behaviour when the OB changes.

  • An endless loop: here it is stopped after 100 000 steps and the valve held; in a PLC the cycle-time watchdog stops the CPU.

How to tune it

Your code, your numbers. The scorecard, the margins (where the code is a PID in disguise, the PID rung shows them) and the mismatch test are the same as for every other controller. Use them.

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

  • One FB, called every cycle, from a cyclic interrupt OB.
  • Every variable that must survive a cycle is static (VAR); temporaries can be VAR_TEMP in the PLC.
  • Bumpless manual: the starting PI shows the pattern: in manual, set the integral so the output equals the manual value.
  • Test the edges: a sensor freeze, the valve at a limit, manual and back. The bench's tests include them.

Siemens and elsewhere

Siemens
  • The export wraps your code as LL_Custom: the built-in names become VAR_INPUT / VAR_OUTPUT, your VAR its statics, and in SCL every local gets its # prefix. Paste it into an external source file and compile it into a block.
  • Call it from OB30 at the cycle you tested with; the call site computes the % inputs from your analog tags.
Elsewhere
  • The same code runs in CODESYS, TwinCAT and any IEC 61131-3 runtime (the ST export, without # prefixes).
  • PLCopen has standard motion and process function blocks; your own FB is how anything else gets done.

Interactive Controller Sandbox

Test and tune Your own controller (Structured Text) live on a process, benchmark against baseline PI, and inspect the resulting code.

Closed-Loop Response: Your own (ST) vs Baseline PI

Simulating…
Running test scenarios…
Block Diagram
SP rΣ+−Your ST codeLL_CustomCV uValve1/(τᵥs + 1), char.stemΣ++d: header pressureProcessQ = K_v f √ΔpPV yTransmittere^(−θs)/(τₘs + 1)Σ++noise nPV measured
Generated code
// Loop Lab export: Liquid flow loop, FIC-101
// TIA Portal SCL (S7-1200 / S7-1500). Generated code: review before use on a real plant.

FUNCTION_BLOCK "LL_Custom"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT
      SP : Real;
      PV : Real;
      SP_PCT : Real;
      PV_PCT : Real;
      PV2 : Real;
      PV2_PCT : Real;
      SP2 : Real;
      SP2_PCT : Real;
      TS : Real;
      TIME : Real;
      MANUAL : Bool;
      MAN_OUT : Real;
      MAN_OUT2 : Real;
   END_VAR
   VAR_OUTPUT
      OUT : Real;
      OUT2 : Real;
   END_VAR
   VAR
      KC : Real := 0.45;
      TI : Real := 1.8;
      I : Real;
      E : Real;
      U : Real;
   END_VAR

BEGIN
    // A PI controller, written out. Runs every TS s.
    // Inputs: SP, PV (m³/h), SP_PCT, PV_PCT (% of span), TS (s), MANUAL, MAN_OUT (%).
    // Output: OUT (%). Variables in VAR keep their values from one cycle to the next.

    #e := #SP_PCT - #PV_PCT;
    IF #MANUAL THEN
      // Track the manual output, so switching to auto is bumpless.
      #I := #MAN_OUT - #Kc * #e;
      #OUT := #MAN_OUT;
    ELSE
      #u := #Kc * #e + #I + #Kc * #TS / #Ti * #e;
      // Anti-windup: don't integrate further into a limit.
      IF NOT ((#u > 100.0 AND #e > 0.0) OR (#u < 0.0 AND #e < 0.0)) THEN
        #I := #I + #Kc * #TS / #Ti * #e;
      END_IF;
      #OUT := LIMIT(MN := 0.0, IN := #Kc * #e + #I, MX := 100.0);
    END_IF;
END_FUNCTION_BLOCK

// OB30 "Cyclic interrupt", cycle time 0.1 s (100 ms).
// "FT-101" is the scaled input (NORM_X/SCALE_X from the analog card), "FV-101" the output to scale back to 0-27648.
// Your own controller. TIME is a running time in s, if your code uses it (here a static you keep counting).
"RunTime" := "RunTime" + 0.1;
"FIC_101_DB"(SP := "SP", PV := "FT-101", SP_PCT := ("SP" - 0.0) / 60.0 * 100.0, PV_PCT := ("FT-101" - 0.0) / 60.0 * 100.0, TS := 0.1, TIME := "RunTime", MANUAL := "Manual", MAN_OUT := "ManualOut");
"FV-101" := "FIC_101_DB".OUT;