PID function block for process control in PLC Programming - Time & Space Complexity
We want to understand how the time to run a PID function block changes as the process runs longer or with more data.
Specifically, we ask: How does the work inside the PID block grow with input size?
Analyze the time complexity of the following code snippet.
FUNCTION_BLOCK PID
VAR_INPUT
SetPoint : REAL;
ProcessValue : REAL;
Kp : REAL;
Ki : REAL;
Kd : REAL;
END_VAR
VAR
Integral : REAL := 0;
PreviousError : REAL := 0;
END_VAR
METHOD Run : REAL
VAR
Error : REAL;
Derivative : REAL;
END_VAR
Error := SetPoint - ProcessValue;
Integral := Integral + Error;
Derivative := Error - PreviousError;
PreviousError := Error;
Run := Kp * Error + Ki * Integral + Kd * Derivative;
END_METHOD
END_FUNCTION_BLOCK
This code calculates the control output using the PID formula each time it runs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The PID calculation steps inside the Run method.
- How many times: Each time the Run method is called, it performs a fixed number of calculations.
The number of calculations stays the same no matter how many times the PID runs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 calculations each run, total 50 |
| 100 | About 5 calculations each run, total 500 |
| 1000 | About 5 calculations each run, total 5000 |
Pattern observation: The work grows linearly with the number of times the method runs, but each run is constant work.
Time Complexity: O(1)
This means each PID calculation takes the same amount of time no matter what.
[X] Wrong: "The PID calculation time grows as the process runs longer because it stores past errors."
[OK] Correct: The PID here only keeps the sum of errors and the last error, not all past errors, so the calculation steps stay the same.
Understanding that PID calculations run in constant time helps you explain how control systems stay efficient even when running continuously.
"What if the PID stored all past errors in a list and summed them each time? How would the time complexity change?"
Practice
PID function block in process control?Solution
Step 1: Understand the role of PID blocks
PID blocks automatically adjust outputs to keep a process variable near a setpoint.Step 2: Compare options to this role
Only To keep a process variable close to a desired target value describes this automatic control function; others describe unrelated tasks.Final Answer:
To keep a process variable close to a desired target value -> Option AQuick Check:
PID controls process variable = keep near target [OK]
- Thinking PID stores data instead of controlling
- Confusing PID with display or manual control
- Assuming PID only measures values
pid1 in a PLC program?Solution
Step 1: Recall PLC function block declaration syntax
In PLC languages like Structured Text, function blocks are declared withname : Type;.Step 2: Match options to this syntax
pid1 : PID; matches the correct syntax; others use incorrect assignment or C-like syntax.Final Answer:
pid1 : PID; -> Option AQuick Check:
Function block declaration uses colon and semicolon [OK]
- Using equal sign instead of colon
- Using C or Java style declarations
- Using arrow operator incorrectly
pid1.Kp := 2.0; pid1.Ki := 0.5; pid1.Kd := 0.1; pid1.SetPoint := 100; pid1.ProcessVariable := 90; pid1(); Output := pid1.ControlOutput;
What is the expected behavior of
Output after this code runs once?Solution
Step 1: Analyze PID inputs and error
The error is SetPoint (100) minus ProcessVariable (90) = 10, positive error.Step 2: Understand PID output direction
Positive error means output should increase to raise the process variable, so output is positive.Final Answer:
Output will be a positive value to increase the process variable -> Option CQuick Check:
Positive error -> positive output to correct [OK]
- Assuming output is zero on first run
- Confusing output direction with error sign
- Thinking output equals setpoint directly
pid1.Kp := 1.5; pid1.Ki := 0.3; pid1.Kd := 0.05; pid1.SetPoint := 50; pid1.ProcessVariable := 55; Output := pid1.ControlOutput; pid1();
Solution
Step 1: Check order of function call and output read
The PID block must be called (executed) before readingControlOutputto update output.Step 2: Identify the mistake in code order
Here,Outputis assigned beforepid1()is called, so output is stale or invalid.Final Answer:
Calling the PID block functionpid1()after reading output -> Option DQuick Check:
Call PID block before reading output [OK]
- Reading output before calling PID block
- Confusing parameter order
- Ignoring function block declaration
Solution
Step 1: Understand overshoot and slow rise causes
High Kp causes overshoot; low Kd means less damping; slow rise means Kp might be too low.Step 2: Choose tuning to reduce overshoot and improve speed
Decreasing Kp reduces overshoot; increasing Kd adds damping to reduce oscillations and overshoot.Final Answer:
Decrease Kp and increase Kd -> Option BQuick Check:
Lower Kp + higher Kd = less overshoot, better control [OK]
- Increasing Kp causes more overshoot
- Ignoring derivative gain effect
- Changing Ki without addressing overshoot
Ready to prove it?
Pass the test and this pattern moves unknown
