ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Cascade Control: Outer Loop Setpoint Generation

PLC Function Block Diagram (FBD)

Goal -- WPM

Ready
Exercise Algorithm Area
1FUNCTION_BLOCK Cascade_Outer_Loop
2
3VAR_INPUT
4// Primary process variable (e.g., temperature in a jacketed reactor)
5PrimaryPV : REAL;
6// Setpoint for the outer loop (e.g., desired temperature)
7OuterSetpoint : REAL;
8// Gain for the outer loop's proportional term
9OuterKp : REAL := 1.0;
10// Integral time constant for the outer loop (in seconds)
11OuterTi : REAL := 60.0;
12// Output of the inner loop controller (e.g., flow rate to jacket)
13InnerLoopOutput : REAL;
14// Status of the inner loop controller (TRUE if Manual, FALSE if Auto)
15InnerLoopManualMode : BOOL;
16// Minimum output of the inner loop controller
17InnerLoopMinOutput : REAL := 0.0;
18// Maximum output of the inner loop controller
19InnerLoopMaxOutput : REAL := 100.0;
20// Time step for discrete simulation (in seconds)
21CycleTime : REAL := 0.1;
22END_VAR
23
24VAR_OUTPUT
25// Setpoint for the inner loop controller (e.g., desired flow rate)
26InnerSetpoint : REAL;
27END_VAR
28
29VAR
30// Outer loop error
31OuterError : REAL;
32// Outer loop proportional term
33OuterPropTerm : REAL;
34// Outer loop integral term
35OuterIntegralTerm : REAL;
36// Outer loop output before clamping (potential inner loop setpoint)
37OuterOutputRaw : REAL;
38// Previous outer loop error for integral calculation
39PrevOuterError : REAL;
40// Integral gain for the outer loop
41OuterKi : REAL;
42// Clamped outer loop output
43ClampedOuterOutput : REAL;
44// Flag to indicate if integral term should be reset or held
45IntegralActive : BOOL := TRUE;
46END_VAR
47
48// --- Initialization (typically done once at startup or when mode changes) ---
49// This section would typically be handled by a system state manager or on mode change.
50// For this example, we'll assume CycleTime is constant and valid.
51
52// Calculate integral gain from integral time constant
53IF OuterTi > 0.0 THEN
54OuterKi := OuterKp / OuterTi;
55ELSE
56OuterKi := 0.0; // Prevent division by zero
57END_IF
58
59// --- Outer Loop Control Logic ---
60
61// Calculate the error for the outer loop
62OuterError := OuterSetpoint - PrimaryPV;
63
64// Calculate the proportional term
65OuterPropTerm := OuterKp * OuterError;
66
67// Calculate the integral term using the trapezoidal rule for better accuracy
68// Only integrate if the outer loop is active and not subject to anti-windup conditions
69IF IntegralActive THEN
70OuterIntegralTerm := OuterIntegralTerm + (OuterError + PrevOuterError) / 2.0 * OuterKi * CycleTime;
71END_IF
72
73// Sum the proportional and integral terms to get the raw outer loop output
74OuterOutputRaw := OuterPropTerm + OuterIntegralTerm;
75
76// --- Anti-Windup and Output Clamping ---
77
78// Anti-windup: If the inner loop is in manual mode OR its output is saturated,
79// we should prevent the outer loop's integral term from accumulating further.
80// Also, we might want to hold the last valid output or reset the integral.
81IF InnerLoopManualMode OR InnerLoopOutput <= InnerLoopMinOutput OR InnerLoopOutput >= InnerLoopMaxOutput THEN
82IntegralActive := FALSE; // Stop integral accumulation
83// Option 1: Hold the last valid output
84// Option 2: Reset integral term (e.g., OuterIntegralTerm := 0.0;)
85// For this example, we'll hold the last valid output and stop integration.
86// The OuterOutputRaw calculation will still use the last OuterIntegralTerm.
87// A more sophisticated approach would involve calculating the output based on the *current* state.
88ELSE
89IntegralActive := TRUE; // Allow integral accumulation
90END_IF
91
92// Clamp the outer loop's raw output to the physical limits of the inner loop's setpoint capability.
93// This ensures the generated inner setpoint is valid.
94ClampedOuterOutput := Clamp(OuterOutputRaw, InnerLoopMinOutput, InnerLoopMaxOutput);
95
96// Assign the clamped output as the setpoint for the inner loop
97InnerSetpoint := ClampedOuterOutput;
98
99// Store the current error for the next integral calculation
100PrevOuterError := OuterError;
101
102// Helper function to clamp a value within a range (assumed to be available elsewhere or defined locally)
103FUNCTION Clamp : REAL
104VAR_INPUT
105Value : REAL;
106MinVal : REAL;
107MaxVal : REAL;
108END_VAR
109VAR
110ClampedValue : REAL;
111END_VAR
112
113IF Value < MinVal THEN
114ClampedValue := MinVal;
115ELSIF Value > MaxVal THEN
116ClampedValue := MaxVal;
117ELSE
118ClampedValue := Value;
119END_IF
120
121Clamp := ClampedValue;
122
123END_FUNCTION
Algorithm description viewbox

Cascade Control: Outer Loop Setpoint Generation

Algorithm description:

This Function Block implements the outer loop of a cascade control system, commonly used in process industries to improve control performance. The outer loop (e.g., controlling temperature) adjusts the setpoint of an inner loop (e.g., controlling flow rate) to indirectly manipulate the primary process variable. It typically uses a PI controller structure. A critical feature is its anti-windup mechanism: if the inner loop is in manual mode or its output is saturated, the outer loop's integral term is disabled to prevent it from accumulating excessively, which could lead to overshoot when the inner loop returns to automatic or becomes unsaturated. This ensures stable and effective control in complex scenarios.

Algorithm explanation:

This Function Block implements a PI controller for the outer loop of a cascade system. The core logic calculates the `OuterError` (`OuterSetpoint - PrimaryPV`), then computes `OuterPropTerm` (`OuterKp * OuterError`) and `OuterIntegralTerm` (`integral of OuterError over time`). The `OuterIntegralTerm` is updated using the trapezoidal rule for better accuracy in discrete systems, scaled by `OuterKi` and `CycleTime`. A key aspect is the anti-windup logic: `IntegralActive` is set to `FALSE` if `InnerLoopManualMode` is `TRUE` or if the `InnerLoopOutput` is at its `InnerLoopMinOutput` or `InnerLoopMaxOutput`. This prevents integral windup. When `IntegralActive` is `FALSE`, the integral term is not updated, preserving its last valid value. The final `InnerSetpoint` is the sum of the proportional and integral terms, clamped to the `[InnerLoopMinOutput, InnerLoopMaxOutput]` range to ensure it's a valid setpoint for the inner loop. The time complexity is O(1) due to fixed operations. Space complexity is O(1) for storing state variables.

Pseudocode:

FUNCTION_BLOCK Cascade_Outer_Loop
  INPUTS: PrimaryPV, OuterSetpoint, OuterKp, OuterTi, InnerLoopOutput, InnerLoopManualMode, InnerLoopMinOutput, InnerLoopMaxOutput, CycleTime
  OUTPUTS: InnerSetpoint

  // Calculate integral gain
  OuterKi = OuterKp / OuterTi (handle OuterTi = 0)

  // Calculate outer loop error
  OuterError = OuterSetpoint - PrimaryPV

  // Calculate proportional term
  OuterPropTerm = OuterKp * OuterError

  // Anti-windup check
  IF InnerLoopManualMode OR InnerLoopOutput <= InnerLoopMinOutput OR InnerLoopOutput >= InnerLoopMaxOutput THEN
    IntegralActive = FALSE
  ELSE
    IntegralActive = TRUE
  END IF

  // Update integral term (only if active)
  IF IntegralActive THEN
    OuterIntegralTerm = OuterIntegralTerm + (OuterError + PrevOuterError) / 2.0 * OuterKi * CycleTime
  END IF

  // Calculate raw outer loop output
  OuterOutputRaw = OuterPropTerm + OuterIntegralTerm

  // Clamp outer loop output to inner loop's setpoint limits
  InnerSetpoint = Clamp(OuterOutputRaw, InnerLoopMinOutput, InnerLoopMaxOutput)

  // Store current error for next cycle
  PrevOuterError = OuterError

END FUNCTION_BLOCK

FUNCTION Clamp(Value, MinVal, MaxVal)
  // ... implementation ...
END FUNCTION