The PID Loop instruction (Proportional, Integral, Derivative loop) is provided by the S7-200 CPU to perform the PID calculation. The operation of the PID loop is determined by 9 parameters stored in a 36-byte loop table .

**The PID Algorithm**

In steady state operation, a PID controller regulates the value of the output so as to drive the error (**e**) to zero. A measure of the error is given by the difference between the loop setpoint (the desired operating point), and the loop input or process variable (the actual operating point). The principle of PID control is based upon the following equation that expresses the output, M(t), as a function of a proportional term, an integral term, and a differential term:

where:

is the loop output as a function of time | |

is the loop gain | |

e |
is the loop error (the difference between setpoint and process variable) |

is the initial value of the loop output |

In order to implement this control function in a digital computer, the continuous function must be quantized into periodic samples of the error value with subsequent calculation of the output. The corresponding equation that is the basis for the digital computer solution is:

where:

is the calculated value of the loop output at sample time n | |

is the loop gain | |

is the value of the loop error at sample time n | |

is the previous value of the loop error (at sample time n - 1) | |

is the proportional constant of the integral term | |

is the initial value of the loop output | |

is the proportional constant of the differential term |

From this equation, the integral term is shown to be a function of all the error terms from the first sample to the current sample. The differential term is a function of the current sample and the previous sample, while the proportional term is only a function of the current sample. In a digital computer it is not practical to store all samples of the error term, nor is it necessary.

Since the digital computer must calculate the output value each time the error is sampled beginning with the first sample, it is only necessary to store the previous value of the error and the previous value of the integral term. As a result of the repetitive nature of the digital computer solution, a simplification in the equation that must be solved at any sample time can be made. The simplified equation is:

where:

is the calculated value of the loop output at sample time n | |

is the loop gain | |

is the value of the loop error at sample time n | |

is the previous value of the loop error (at sample time n - 1) | |

is the proportional constant of the integral term | |

is the previous value of the integral term (at sample time n - 1) | |

is the proportional constant of the differential term |

The CPU uses a modified form of the above simplified equation when calculating the loop output value. This modified equation is:

where:

is the calculated value of the loop output at sample time n | |

is the value of the proportional term of the loop output at sample time n | |

is the value of the integral term of the loop output at sample time | |

is the value of the differential term of the loop output at sample time n |

**The Proportional Term**

The proportional term MP is the product of the gain , which controls the sensitivity of the output calculation, and the error (**e**), which is the difference between the setpoint (**SP**) and the process variable (**PV**) at a given sample time. The equation for the proportional term as solved by the CPU is:

where:

is the value of the proportional term of the loop output at sample time n | |

is the loop gain | |

is the value of the setpoint at sample time n | |

is the value of the process variable at sample time n |

**The Integral Term**

The integral term MI is proportional to the sum of the error (**e**) over time. The equation for the integral term as solved by the CPU is:

where:

is the value of the integral term of the loop output at sample time n | |

is the loop gain | |

is the loop sample time | |

is the integral time (also called the integral time or reset) | |

is the value of the setpoint at sample time n | |

is the value of the process variable at sample time n | |

is the value of the integral term at sample time n-1 (also called the integral sum or the bias) |

The integral sum or bias (**MX**) is the running sum of all previous values of the integral term. After each calculation of , the bias is updated with the value of which may be adjusted or clamped (see the section “Variables and Ranges” for details). The initial value of the bias is typically set to the output value just prior to the first loop output calculation. Several constants are also part of the integral term, the gain , the sample time , which is the cycle time at which the PID loop recalculates the output value, and the integral time or reset , which is a time used to control the influence of the integral term in the output calculation.

**The Differential Term**

The differential term MD is proportional to the change in the error. The equation for the differential term is:

To avoid step changes or bumps in the output due to derivative action on setpoint changes, this equation is modified to assume that the setpoint is a constant **=** . This results in the calculation of the change in the process variable instead of the change in the error as shown:

or just:

where:

is the value of the differential term of the loop output at sample time n | |

is the loop gain | |

is the loop sample time | |

is the differentiation period of the loop (also called the derivative time or rate) | |

is the value of the setpoint at sample time n | |

is the value of the setpoint at sample time n – 1 | |

is the value of the process variable at sample time n – 1 | |

is the value of the process variable at sample time n – 1 |

The process variable rather than the error must be saved for use in the next calculation of the differential term. At the time of the first sample, the value of is initialized to be equal to

**Selection of Loop Control**

In many control systems it may be necessary to employ only one or two methods of loop control. For example only proportional control or proportional and integral control may be required. The selection of the type of loop control desired is made by setting the value of the constant parameters.

If you do not want integral action (no “I” in the PID calculation), then a value of infinity should be specified for the integral time (reset). Even with no integral action, the value of the integral term may not be zero, due to the initial value of the integral sum MX.

If you do not want derivative action (no “D” in the PID calculation), then a value of 0.0 should be specified for the derivative time (rate).

If you do not want proportional action (no “P” in the PID calculation) and you want I or ID control, then a value of 0.0 should be specified for the gain. Since the loop gain is a factor in the equations for calculating the integral and differential terms, setting a value of 0.0 for the loop gain will result in a value of 1.0 being used for the loop gain in the calculation of the integral and differential terms.

**Converting and Normalizing the Loop Inputs**

A loop has two input variables, the setpoint and the process variable. The setpoint is generally a fixed value such as the speed setting on the cruise control in your automobile. The process variable is a value that is related to loop output and therefore measures the effect that the loop output has on the controlled system. In the example of the cruise control, the process variable would be a tachometer input that measures the rotational speed of the tires.

Both the setpoint and the process variable are real world values whose magnitude, range, and engineering units may be different. Before these real world values can be operated upon by the PID instruction, the values must be converted to normalized, floating-point representations.

The first step is to convert the real world value from a 16-bit integer value to a floating-point or real number value. The following instruction sequence is provided to show how to convert from an integer value to a real number.

XORDAC0, AC0//Clear the accumulator.

MOVWAIW0, AC0//Save the analog value in the accumulator.

LDW>=AC0, 0//If the analog value is positive,

JMP0//then convert to a real number.

NOT//Else,

ORD16#FFFF0000, AC0//sign extend the value in AC0.

LBL0

DTRAC0, AC0//Convert the 32-bit integer to a real number.

The next step is to convert the real number value representation of the real world value to a normalized value between 0.0 and 1.0. The following equation is used to normalize either the setpoint or process variable value:

where:

is the normalized, real number value representation of the real world value | |

is the un-normalized or raw, real number value representation of the real world value | |

is 0.0 for unipolar values and 0.5 for bipolar values | |

is the maximum possible value minus the minimum possible value = 32,000 for unipolar values (typical) = 64,000 for bipolar values (typical) |

The following instruction sequence shows how to normalize the bipolar value in AC0 (whose span is 64,000) as a continuation of the previous instruction sequence:

/R64000.0, AC0//Normalize the value in the accumulator

+R0.5, AC0//Offset the value to the range from 0.0 to 1.0

MOVRAC0, VD100//Store the normalized value in the loop TABLE

**Converting the Loop Output to a Scaled Integer Value**

The loop output is the control variable, such as the throttle setting in the example of the cruise control on the automobile. The loop output is a normalized, real number value between 0.0 and 1.0. Before the loop output can be used to drive an analog output, the loop output must be converted to a 16-bit, scaled integer value. This process is the reverse of converting the PV and SP to a normalized value. The first step is to convert the loop output to a scaled, real number value using the formula given below:

where:

is the scaled, real number value of the loop output | |

is the normalized, real number value of the loop output | |

is 0.0 for unipolar values and 0.5 for bipolar values | |

is the maximum possible value minus the minimum possible value = 32,000 for unipolar values (typical) = 64,000 for bipolar values (typical) |

The following instruction sequence shows how to scale the loop output:

MOVRVD108, AC0//Move the loop output to the accumulator.

-R0.5, AC0//Include this statement only if the value is

//bipolar.

*R64000.0, AC0//Scale the value in the accumulator.

Next, the scaled, real number value representing the loop output must be converted to a 16-bit integer. The following instruction sequence shows how to do this conversion:

ROUNDAC0AC0//Convert the real number to a 32-bit integer.

MOVWAC0, AQW0//Write the 16-bit integer value to the analog

//output.

**Forward- or Reverse-Acting Loops**

The loop is forward-acting if the gain is positive and reverse-acting if the gain is negative. (For I or ID control, where the gain value is 0.0, specifying positive values for integral and derivative time will result in a forward-acting loop, and specifying negative values will result in a reverse-acting loop.)

**Variables and Ranges**

The process variable and setpoint are inputs to the PID calculation. Therefore the loop table fields for these variables are read but not altered by the PID instruction.

The output value is generated by the PID calculation, so the output value field in the loop table is updated at the completion of each PID calculation. The output value is clamped between 0.0 and 1.0. The output value field can be used as an input by the user to specify an initial output value when making the transition from manual control to PID instruction (auto) control of the output (see discussion in the Modes section below).

If integral control is being used, then the bias value is updated by the PID calculation and the updated value is used as an input in the next PID calculation. When the calculated output value goes out of range (output would be less than 0.0 or greater than 1.0), the bias is adjusted according to the following formulas:

when the calculated output, **> 1.0**

or

when the calculated output, **< 0.0**

where:

is the value of the adjusted bias | |

is the value of the proportional term of the loop output at sample time n | |

is the value of the differential term of the loop output at sample time n | |

is the value of the loop output at sample time n |

By adjusting the bias as described, an improvement in system responsiveness is achieved once the calculated output comes back into the proper range. The calculated bias is also clamped between 0.0 and 1.0 and then is written to the bias field of the loop table at the completion of each PID calculation. The value stored in the loop table is used in the next PID calculation.

The bias value in the loop table can be modified by the user prior to execution of the PID instruction in order to address bias value problems in certain application situations. Care must be taken when manually adjusting the bias, and any bias value written into the loop table must** **be a real number between 0.0 and 1.0.

A comparison value of the process variable is maintained in the loop table for use in the derivative action part of the PID calculation. You should not modify this value

**Modes**

There is no built-in mode control for S7-200 PID loops. The PID calculation is performed only when power flows to the PID box. Therefore, “automatic” or “auto” mode exists when the PID calculation is performed cyclically. “Manual” mode exists when the PID calculation is not performed.

The PID instruction has a power-flow history bit, similar to a counter instruction. The instruction uses this history bit to detect a 0-to-1 power flow transition, which when detected will cause the instruction to perform a series of actions to provide a bumpless change from manual control to auto control. In order for change to auto mode control to be bumpless, the value of the output as set by the manual control must be supplied as an input to the PID instruction (written to the loop table entry for ) before switching to auto control. The PID instruction performs the following actions to values in the loop table to ensure a bumpless change from manual to auto control when a 0-to-1 power flow transition is detected:

•Sets setpoint = process variable

•Sets old process variable = process variable

•Sets bias (MX) = output value

The default state of the PID history bits is “set” and that state is established at CPU startup and on every STOP-to-RUN mode transition of the controller. If power flows to the PID box the first time that it is executed after entering RUN mode, then no power flow transition is detected and the bumpless mode change actions will not be performed.

**Alarm Checking and Special Operations**

The PID instruction is a simple but powerful instruction that performs the PID calculation. If other processing is required such as alarm checking or special calculations on loop variables, these must be implemented using the basic instructions supported by the CPU.

**Error Conditions**

When it is time to compile, the CPU will generate a compile error (range error) and the compilation will fail if the loop table start address or PID loop number operands specified in the instruction are out of range.

Certain loop table input values are not range checked by the PID instruction. You must take care to ensure that the process variable and setpoint (as well as the bias and previous process variable if used as inputs) are real numbers between 0.0 and 1.0.

If any error is encountered while performing the mathematical operations of the PID calculation, then SM1.1 (overflow or illegal value) will be set and execution of the PID instruction will be terminated. (Update of the output values in the loop table may be incomplete, so you should disregard these values and correct the input value causing the mathematical error before the next execution of the loop’s PID instruction.)

**See Also:**