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

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

PID Proportional Gain Tuning Simulation

PLC Function Block Diagram (FBD)

Goal -- WPM

Ready
Exercise Algorithm Area
1PROGRAM PID_P_Gain_Tuning
2
3VAR
4// Inputs
5Setpoint : REAL;
6ProcessVariable : REAL;
7InputSignal : REAL;
8OutputSignal : REAL;
9Kp : REAL;
10MinOutput : REAL := 0.0;
11MaxOutput : REAL := 100.0;
12MinInput : REAL := 0.0;
13MaxInput : REAL := 100.0;
14
15// Internal variables
16Error : REAL;
17ProportionalTerm : REAL;
18ClampedInput : REAL;
19
20END_VAR
21
22// Main PID loop
23IF Kp < 0.0 THEN
24// Invalid proportional gain, handle error or default
25Kp := 0.0; // Or some other error handling
26END_IF
27
28// Calculate error
29Error := Setpoint - ProcessVariable;
30
31// Calculate proportional term
32ProportionalTerm := Kp * Error;
33
34// Clamp input signal to physical limits
35ClampedInput := Clamp(InputSignal, MinInput, MaxInput);
36
37// Calculate raw output before saturation
38InputSignal := ProportionalTerm; // In a real PID, this would be a sum of P, I, D terms
39
40// Saturate output signal to physical limits
41OutputSignal := Clamp(InputSignal, MinOutput, MaxOutput);
42
43// Helper function to clamp a value within a range
44FUNCTION Clamp : REAL
45VAR_INPUT
46Value : REAL;
47MinVal : REAL;
48MaxVal : REAL;
49END_VAR
50VAR
51ClampedValue : REAL;
52END_VAR
53
54IF Value < MinVal THEN
55ClampedValue := MinVal;
56ELSIF Value > MaxVal THEN
57ClampedValue := MaxVal;
58ELSE
59ClampedValue := Value;
60END_IF
61
62Clamp := ClampedValue;
63
64END_FUNCTION
Algorithm description viewbox

PID Proportional Gain Tuning Simulation

Algorithm description:

This FBD program simulates the proportional component of a PID controller. It calculates the proportional term based on the error between the setpoint and the process variable, multiplied by the proportional gain (Kp). The input signal is clamped to physical limits before being used, and the final output signal is saturated to prevent exceeding maximum or minimum operational values. This is crucial for controlling systems like motor speed or temperature where physical constraints exist.

Algorithm explanation:

The algorithm calculates the proportional term of a PID controller, which is directly proportional to the current error. The `Clamp` helper function is used twice: first to ensure the input signal to the controller logic stays within its defined physical range, and second to ensure the final output signal does not exceed the actuator's limits. This prevents unrealistic control actions. The proportional term is calculated as `Kp * Error`. The `Kp` gain determines the responsiveness; a higher `Kp` leads to a faster response but can also cause instability. The algorithm's correctness relies on the accurate calculation of the error and the proper application of clamping to respect system boundaries. Edge cases include negative `Kp` values, which are handled by setting `Kp` to 0, and inputs/outputs that fall outside their respective min/max bounds.

Pseudocode:

PROGRAM PID_P_Gain_Tuning
  INPUTS: Setpoint, ProcessVariable, InputSignal, Kp, MinOutput, MaxOutput, MinInput, MaxInput
  OUTPUTS: OutputSignal

  IF Kp < 0.0 THEN
    Kp = 0.0
  END IF

  Error = Setpoint - ProcessVariable
  ProportionalTerm = Kp * Error

  // Use InputSignal as the raw value before clamping for this simplified example
  // In a full PID, this would be the sum of P, I, D terms
  RawControlSignal = ProportionalTerm

  ClampedRawSignal = Clamp(RawControlSignal, MinInput, MaxInput) // This clamping is illustrative, typically applied to the *output* of the P term if it were an input to further stages.

  OutputSignal = Clamp(RawControlSignal, MinOutput, MaxOutput) // Final output saturation

END PROGRAM

FUNCTION Clamp(Value, MinVal, MaxVal)
  IF Value < MinVal THEN
    RETURN MinVal
  ELSIF Value > MaxVal THEN
    RETURN MaxVal
  ELSE
    RETURN Value
  END IF
END FUNCTION