Notes on the documentation

This description is only intended for the use of trained specialists in control and automation engineering who are familiar with the applicable national standards.
It is essential that the following notes and explanations are followed when installing and commissioning these components.

The responsible staff must ensure that the application or use of the products described satisfy all the requirements for safety, including all the relevant laws, regulations, guidelines and standards.

Disclaimer

The documentation has been prepared with care. The products described are, however, constantly under development.
For that reason the documentation is not in every case checked for consistency with performance data, standards or other characteristics.
In the event that it contains technical or editorial errors, we retain the right to make alterations at any time and without warning.
No claims for the modification of products that have already been supplied may be made on the basis of the data, diagrams and descriptions in this documentation.

Trademarks

Beckhoff®, TwinCAT®, EtherCAT®, Safety over EtherCAT®, TwinSAFE® and XFC® are registered trademarks of and licensed by Beckhoff Automation GmbH.
Other designations used in this publication may be trademarks whose use by third parties for their own purposes could violate the rights of the owners.

Patent Pending

The TwinCAT Technology is covered, including but not limited to the following patent applications and patents:
EP0851348, US6167425 with corresponding applications or registrations in various other countries.

Copyright

© Beckhoff Automation GmbH & Co. KG, Germany.
The reproduction, distribution and utilization of this document as well as the communication of its contents to others without express authorization are prohibited.
Offenders will be held liable for the payment of damages. All rights reserved in the event of the grant of a patent, utility model or design.


TwinCAT PLC Library PID

NOTE:

This Library is not supported anymore.

Please use instead the Library: TwinCAT PLC Controller Toolbox.

Introduction

This library contains function blocks which permit the implementation of PID controller functionality, the generation of pulse-width modulated signals and the generation of 2 and 3-point characteristic curves in TwinCAT PLC. In addition to the actual PID regulation task, a number of technical precautions have been taken, such as Anti-Reset-Windup and limitations on manipulated variables, which are indispensable for the practical operation of a real process. The status of the controller is indicated by flags. An analogue control value and, in the case of the 3-point PID controller, two additional digital outputs for a positive and a negative set signal are available as outputs.


Concept

The heart of the controller consists of an real PID controller (PID-T1). Any controller can, according to the purpose of the regulation, take effect in either a positive or a negative sense. Thus a controller for the level to which a container is filled can be implemented as an inlet controller or as an outlet controller.

It is possible to limit the control values, just as must be the case in a real process, or to switch off the digital controller outputs when the controlled device reaches a positive or a negative limit position. In case this magnitude restriction is triggered by the controlled device reaching a limiting position, ARW (Anti-Reset Windup) measures have been introduced to the controller’s integral component. These ARW measures should ensure that a regulation error, which might remain constant, does not cause the controller’s I component to go on and on integrating. Otherwise, when the sign of the error signal changes again, there would be a correspondingly long time before the manipulated variable would change to the opposite direction.

Jerk-free start-up of the controller is also made possible using a special initialisation routine (Sync flag), which ensures steady alterations in the manipulated variable.

The close integration of the PID controller block and the (oscilloscope) trace function, which is also a component of the TwinCAT PLC software, should particularly be emphasised. Through the intelligent interplay between controller and value measurement, the controller can be brought into operation and optimised according to desired criteria in the shortest possible time, and without the necessity of auxiliary hardware for data collection and display, since even internal controller magnitudes such as the P, I and D components can be displayed.

Another fundamental advantage of the controller scheme is that the specific controller values are maintained, without error, in their own physical units, rather than being normalised to specific numeric ranges, as is otherwise usual.


Basic Principles

The mathematical equations for a PID controller are summarised briefly below.

Continuous PID Controller

Real continuous PID controller in product form (pole-zero form):

Real continuous PID controller in sum form:

with:

Transfer function (step response) of the real PID controller in the sum form with the trapezoidal rule:

Discrete Time PID Controller

Discrete-time real PID controller in product form (pole-zero form) with the trapezoidal rule:

Discrete-time real PID controller in sum form with the trapezoidal rule:


Overview

The following table provides a summary of the function blocks in the TcPlcCtrl.lib library.

Function Block | Description
CtrlStandardPID | Standard PID controller
Ctrl3PointPID | 3-point PID controller
PwmOutput | Block for the generation of a pulse-width modulated signal
Ctrl2PointWithHysteresis | Block for the generation of a general 2-point characteristic curve
Ctrl3PointWithHysteresis | Block for the generation of a general 3-point characteristic curve


CtrlStandardPID

PID Controller Block Diagram

Transfer function

Function Block

Interface

VAR_INPUT
bInit : BOOL; (* initialization flag for first cycle *)
bSync : BOOL; (* synchronization for bumpless initialisation *)
(* controller input (deviation) *)
fCtrlInput : REAL; (* controller input (deviation=error) *)
fMinOutput : REAL; (* controller minimum output (min. limit) *)
fMaxOutput : REAL; (* controller maximum output (max. limit) *)
fSyncValue : REAL; (* syncronization value for bumpless init. *)
(* controller values *)
fCtrlCycleTime : REAL; (* controller cycle time in seconds [s] *)
fKp : REAL; (* proportional gain Kp (P) *)
fTn : REAL; (* integral gain Tn (I) [s] *)
fTv : REAL; (* derivative gain Tv (D-T1) [s] *)
fTd : REAL; (* derivative damping time Td (D-T1) [s] *)
fInnerWindow : REAL; (* inner window for reduced I-part (dE-window) *)
fOuterWindow : REAL; (* outer window for disabling I-part (dE-window) *)
END_VAR

VAR_OUTPUT
nErrorStatus : DINT; (* controller error output (0: no error; >0:error) *)
fCtrlOutput : REAL; (* controller output command *)
bMinLimit : BOOL := FALSE;(* minimum limiting active ? [TRUE/FALSE] -> ARW *)
bMaxLimit : BOOL := FALSE;(* maximum limiting active ? [TRUE/FALSE] -> ARW *)
bARWActive : BOOL := FALSE;(* ARW actice ? [TRUE/FALSE] -> freeze I-part *)
END_VAR

Inputs

Name | Unit | Value range | Description
fCtrlInput |  | ± ¥ | Controller input magnitude ?(error difference e(t) )
fMinOutput |  | ± ¥ | Minimum output limit (minimum bound for manipulated variable)
fMaxOutput |  | ± ¥ | Maximum output limit (maximum bound for manipulated variable)
fCtrlCycleTime | s | > 0 | Controller cycle time for discrete-time implementation
fKp |  | ³ 0 | Proportional amplification Kp (P component)
fTn | s | ³ 0 | Integral-action time Tn (I component)
fTv | s | ³ 0 | Derivative action time Tv (D T1 component)
fTd | s | ³ 0 | Damping time Td (D T1 component)
fInnerWindow |  | ³ 0 | Internal window for controller input magnitude (error difference e(t) ), in which the I component is taken linearly to zero.
fOuterWindow |  | ³ 0 | External window for controller input magnitude (error difference e(t) ), in which the I component is switched off (frozen), if the controller input magnitude is outside this window. If this property is not desired, the parameter must be given the value 0.0.
bInit | 1 | [TRUE,FALSE] | Initialisation flag, which must be active (TRUE) for precisely the first cycle in which the controller is called.
bSync | 1 | [TRUE,FALSE] | Synchronisation flag for jerk-free start-up of the controller. It can be used together with the Initialisation flag, and may only be active (TRUE) for exactly one cycle. The jerk-free start-up synchronisation is only possible if the controller has an I and a D component, otherwise it has no effect.
fSyncValue |  | ± ¥ | This is the controller output value used for the synchronisation (last set magnitude, used for exactly one cycle as the set magnitude).

Outputs

Name | Unit | Value range | Description
fCtrlOutput |  | ± ¥ | Controller output value (manipulated variable y(t) )
nErrorStatus |  | ³ 0 | Error status/error code for the controller. A zero value indicates no error, and a value greater than zero indicates a special error code (see table).
bARWActive |  | [TRUE,FALSE] | ARW (anti-reset windup) active/inactive. The ARW measures have the effect that, for instance, in the case of manipulated variable limitation being active, the I component in one direction is blocked, whereas the I component can at any time change in the opposite direction. This has a positive effect on the controller’s dynamic behaviour.
bMinLimit | 1 | [TRUE,FALSE] | Minimum output magnitude limitation active/passive (minimum set magnitude limitation).
bMaxLimit | 1 | [TRUE,FALSE] | Maximum output magnitude limitation active/passive (maximum set magnitude limitation).

Error Codes

Name | Description
nERR_NOERROR | No error.
nERR_NOINIT | Missing controller initialisation. The controller must be initialised once using the initialisation flag. This flag must be active for exactly one PLC cycle when the controller is first called up. This bit must be reset after the first cycle. Re-initialisation will again be necessary if controller parameters are to be altered.
nERR_INVALIDPARAM | Invalid controller parameters. One or more controller parameters have an invalid value. The proportional amplification Kp, the integral-action time Tn, the derivative action time Tv and the damping time Td must be greater than or equal to zero.
nERR_INVALIDCYCLETIME | Invalid cycle time / sampling time. The controller cycle must be greater than 0.0 s and less than 60.0 s. The physical unit for the cycle time is seconds.


Ctrl3PointPID

3-point PID controller block diagram

Function Block

Interface

VAR_INPUT
bInit : BOOL; (* initialization flag for first cycle *)
bSync : BOOL; (* synchronization for bumpless initialisation *)
(* controller input (deviation) *)
fCtrlInput : REAL; (* controller input (deviation=error) *)
bLimitSwitchMinus : BOOL; (* NEW: digital limit switch minus (for minimum) *)
bLimitSwitchPlus : BOOL; (* NEW: digital limit switch plus (for maximum) *)
fSyncValue : REAL; (* syncronization value for bumpless init. *)
(* controller values *)
fCtrlCycleTime : REAL; (* controller cycle time in seconds [s] *)
fKp : REAL; (* proportional gain Kp (P) *)
fTn : REAL; (* integral gain Tn (I) [s] *)
fTv : REAL; (* derivative gain Tv (D-T1) [s] *)
fTd : REAL; (* derivative damping time Td (D-T1) [s] *)
fDeadBand : REAL; (* NEW: dead band/neutral zone for zero output *)
fInnerWindow : REAL; (* inner window for reduced I-part (dE-window) *)
fOuterWindow : REAL; (* outer window for disabling I-part (dE-window) *)
END_VAR

VAR_OUTPUT
(* main outputs *)
nErrorStatus : DINT; (* controller error output (0: no error; >0:error) *)
fCtrlOutput : REAL; (* controller output command *)
bCtrlOutputMinus : BOOL := FALSE; (* NEW: digital controller output command minus *)
bCtrlOutputPlus : BOOL := FALSE; (* NEW: digital controller output command plus *)
(* auxiliary outputs *)
bMinLimit : BOOL := FALSE; (* minimum limiting active ? [TRUE/FALSE] -> ARW *)
bMaxLimit : BOOL := FALSE; (* maximum limiting active ? [TRUE/FALSE] -> ARW *)
bARWActive : BOOL := FALSE; (* ARW actice ? [TRUE/FALSE] -> freeze I-part *)
END_VAR

Inputs

Name | Unit | Value range | Description
fCtrlInput |  | ± ¥ | Controller input magnitude ?(error difference e(t) )
bLimitSwitchMinus | 1 | [TRUE,FALSE] | Limit switch minus for limit position of controlled device (minimum control value)
bLimitSwitchPlus | 1 | [TRUE,FALSE] | Limit switch plus for limit position of controlled device (maximum control value)
fCtrlCycleTime | s | > 0 | Controller cycle time for discrete-time implementation
fKp |  | ³ 0 | Proportional amplification Kp (P component)
fTn | s | ³ 0 | Integral-action time Tn (I component)
fTv | s | ³ 0 | Derivative action time Tv (D T1 component)
fTd | s | ³ 0 | Damping time Td (D T1 component)
fDeadBand |  | ³ 0 | A dead band associated with the digital control value output to prevent continuous switching ("fluttering") between positive and negative control value within a specified range. This parameter should at least be given a value larger than 1.0*e-10 in order to permit the controlled device to rest idle.
fInnerWindow |  | ³ 0 | Internal window for controller input magnitude (error difference e(t) ), in which the I component is taken linearly to zero.
fOuterWindow |  | ³ 0 | External window for controller input magnitude (error difference e(t) ), in which the I component is switched off (frozen), if the controller input magnitude is outside this window. If this property is not desired, the parameter must be given the value 0.0.
bInit | 1 | [TRUE,FALSE] | Initialisation flag, which must be active (TRUE) for precisely the first cycle in which the controller is called.
bSync | 1 | [TRUE,FALSE] | Synchronisation flag for jerk-free start-up of the controller. It can be used together with the Initialisation flag, and may only be active (TRUE) for exactly one cycle. The jerk-free start-up synchronisation is only possible if the controller has an I and a D component, otherwise it has no effect.
fSyncValue |  | ± ¥ | This is the controller output value used for the synchronisation (last set magnitude, used for exactly one cycle as the set magnitude).

Outputs

Name | Unit | Value range | Description
fCtrlOutput |  | ± ¥ | Controller output value (manipulated variable y(t) )
bCtrlOutputMinus | 1 | [TRUE,FALSE] | Negative digital controller output value (negative control value yminus(t))
bCtrlOutputPlus | 1 | [TRUE,FALSE] | Positive digital controller output value (positive control value yplus(t))
nErrorStatus |  | ³ 0 | Error status/error code for the controller. A zero value indicates no error, and a value greater than zero indicates a special error code (see table).
bARWActive |  | [TRUE,FALSE] | ARW (anti-reset windup) active/inactive. The ARW measures have the effect that, for instance, in the case of manipulated variable limitation being active, the I component in one direction is blocked, whereas the I component can at any time change in the opposite direction. This has a positive effect on the controller’s dynamic behaviour.
bMinLimit | 1 | [TRUE,FALSE] | Minimum output magnitude limitation active/passive (minimum set magnitude limitation).
bMaxLimit | 1 | [TRUE,FALSE] | Maximum output magnitude limitation active/passive (maximum set magnitude limitation).

Error Codes

Name | Description
nERR_NOERROR | No error.
nERR_NOINIT | Missing controller initialisation. The controller must be initialised once using the initialisation flag. This flag must be active for exactly one PLC cycle when the controller is first called up. This bit must be reset after the first cycle. Re-initialisation will again be necessary if controller parameters are to be altered.
nERR_INVALIDPARAM | Invalid controller parameters. One or more controller parameters have an invalid value. The proportional amplification Kp, the integral-action time Tn, the derivative action time Tv and the damping time Td must be greater than or equal to zero. It is also necessary that the 'DeadBand' parameter, which specifies the dead band for the digital control value, is greater than or equal to zero. For reasons of plausibility the two parameters 'InnerWindow' and 'OuterWindow' must be selected in such a way that size of the 'InnerWindow' parameter is always smaller than or equal to the 'OuterWindow' parameter.
nERR_INVALIDCYCLETIME | Invalid cycle time / sampling time. The controller cycle must be greater than 0.0 s and less than 60.0 s. The physical unit for the cycle time is seconds.


PwmOutput

The PLC block "PwmOutput" is available for the generation of a pulse width modulated signal from an analogue signal (REAL). The PwmOutput block normally follows a controller, and generates a digital output magnitude (BOOL) from the scaled analogue set magnitude (REAL). "fCtrlCycleTime" refers to the sampling time of the controller which is followed by the PwmOutput block. The time "fPlcCycleTime" is the cycle time of the task which calls the PwmOutput block.

Function Block

Interface

VAR_INPUT
bInit : BOOL; (* initialization flag for first cycle *)
fPwmInput : REAL; (* controller output = PMW input [0.0 ... 100.0] *)
fCtrlCycleTime : REAL; (* controller cycle time in seconds [s] *)
fPlcCycleTime : REAL; (* PLC/PWM cycle time in seconds [s] *)
END_VAR

VAR_OUTPUT
nErrorStatus : DINT; (* controller error output (0: no error; >0:error) *)
bPwmOutBit : BOOL; (* PWM output bit *)
END_VAR

Inputs

Name | Unit | Value range | Description
bInit | 1 | [TRUE,FALSE] | Initialisation flag, which must be active (TRUE) for precisely the first cycle in which the controller is called.
fPwmInput |  | [0.0 - 100.0] | Scaled analogue input signal. The input signal must be scaled to the range 0.0 -100.0.
fCtrlCycleTime | s | >= fPlcCycleTime | Controller cycle time for discrete-time implementation. This parameter can be used to adjust subsidiary controller cycles. If the subsequent PWM block is used, it is usual to set an integral multiple of the ”fPlcCycleTime”. Without the PWM block, the value set must be identical to the ”fPlcCycleTime”.
fPlcCycleTime | s | > 0 | PLC/Pwm cycle time for discrete time implementation. This time must correspond to the set task interval time in s.

Outputs

Name | Unit | Value range | Description
nErrorStatus |  | ³ 0 | Error status / error code for the block. A zero value indicates no error, and a value greater than zero indicates a special error code (see table).
bPwmOutBit |  | [TRUE,FALSE] | Output value of the PWM block, as a bit.
nPwmOutDirection |  | -1,0,+1 | Shows the sign of the variable fPwmInput when the cycle starts.

Error Codes

Name | Description
nERR_NOERROR | No error.
nERR_NOINIT | Missing block initialisation. The PwmOutput block must be initialised once with the aid of an initialisation flag. This flag must be active for exactly one PLC cycle when the controller is first called up. This bit must be reset after the first cycle.
nERR_INVALIDPARAM | Invalid parameter.
nERR_INVALIDCYCLETIME | Invalid cycle time / sampling time. The controller cycle must be greater than 0.0 s and less than 60.0 s. The physical unit for the cycle time is seconds.

Example

The upper curve in the diagram below shows the input of a PID controller block, and the lower curve shows the corresponding controller output. The output of the PwmOutput block can be seen right at the bottom.


Ctrl2PointWithHysteresis

The present PLC function block "Ctrl2PointWithHysteresis" creates a general 2-point curve with hysteresis for TwinCAT PLC. This 2-point curve is described in such a general way that specially selected parameters yield a wide range of special cases (see diagram).

Overview

The following diagram provides a graphic representation of how the selection of the parameters gives rise to particular curves.

Function Block

Interface

VAR_INPUT
bInit : BOOL; (* initialization flag for first cycle *)
(* controller input (deviation) *)
fCtrlInput : REAL; (* controller input (deviation=error) *)
(* controller values *)
fParamXOnOff : REAL; (* parameter x for threshold ON/OFF *)
fParamYGain : REAL; (* parameter y-gain *)
fParamYOffset : REAL; (* parameter y-offset *)
END_VAR

VAR_OUTPUT
nErrorStatus : DINT; (* controller error output (0: no error; >0:error) *)
fCtrlOutput : REAL; (* controller output command *)
END_VAR

Inputs

Name | Unit | Value range | Description
bInit | 1 | [TRUE,FALSE] | Initialisation flag, which must be active (TRUE) for precisely the first cycle in which the controller is called.
fCtrlInput |  | ± ¥ | The block's analogue input signal.
fParamXOnOff |  | ³ 0 | Hysteresis response threshold. There will be no hysteresis if the input is 0.0.
fParamYGain |  | ³ 0 | Amplification of the output magnitude. If this input is 0.0 the output magnitude will always retain the same value regardless of the input magnitude. The polarity, however, will change according to the value of the offset and the hysteresis.
fParamYOffset |  | ³ 0 | Offset of the output magnitude against the input magnitude.

Outputs

Name | Unit | Value range | Description
nErrorStatus |  | ³ 0 | Error status / error code for the block. A zero value indicates no error, and a value greater than zero indicates a special error code (see table).
fCtrlOutput |  | ± ¥ | The block's output magnitude.

Error Codes

Name | Description
nERR_NOERROR | No error.
nERR_NOINIT | Missing block initialisation. The block must be initialised once using an initialisation flag. This flag must be active for exactly one PLC cycle when the controller is first called up. This bit must be reset after the first cycle.
nERR_INVALIDPARAM_HYSTERESIS | Invalid parameter. The value of the hysteresis parameter "fParamXOnOff" must be greater than or equal to zero.
nERR_INVALIDPARAM_SCALING | Invalid parameter. The values for the amplification "fParamYGain" and for the offset "fParamYOffset" must be greater than or equal to zero.


Ctrl3PointWithHysteresis

The present PLC function block "Ctrl3PointWithHysteresis" creates a general 3-point curve with hysteresis for TwinCAT PLC. This 3-point curve is described in such a general way that specially selected parameters yield a wide range of special cases (see diagram).

Overview

The following diagram provides a graphic representation of how the selection of the parameters gives rise to particular curves.

Function Block

Interface

VAR_INPUT
bInit : BOOL; (* initialization flag for first cycle *)
(* controller input (deviation) *)
fCtrlInput : REAL; (* controller input (deviation=error) *)
(* controller values *)
fParamXOff : REAL; (* x-parameter for threshold OFF *)
fParamXOn : REAL; (* x-parameter for threshold ON *)
fParamYGain : REAL; (* y-parameter gain *)
fParamYOffset : REAL; (* y-parameter offset *)
END_VAR

VAR_OUTPUT
nErrorStatus : DINT; (* controller error output (0: no error; >0:error) *)
fCtrlOutput : REAL; (* controller output command *)
END_VAR

Inputs

Name | Unit | Value range | Description
bInit | 1 | [TRUE,FALSE] | Initialisation flag, which must be active (TRUE) for precisely the first cycle in which the controller is called.
fCtrlInput |  | ± ¥ | The block's analogue input signal.
fParamXOff |  | >= 0 and <= fParamXOn | Hysteresis switch-off threshold. The hysteresis switch-off threshold is symmetrical about the axis of the output magnitude. If this threshold is reached the output magnitude becomes zero until the switch-on threshold is reached.
fParamXOn |  | >= 0 and >= fParamXOff | Hysteresis switch-on threshold. The hysteresis switch-on threshold is symmetrical about the axis of the output magnitude. If this threshold is reached the output magnitude adopts a value not equal to zero, determined by the offset and amplification parameters.
fParamYGain |  | ³ 0 | Amplification of the output magnitude. If this input is 0.0 the output magnitude will always retain the same value regardless of the input magnitude. The polarity, however, will change according to the value of the offset and the hysteresis.
fParamYOffset |  | ³ 0 | Offset of the output magnitude against the input magnitude.

Outputs

Name | Unit | Value range | Description
nErrorStatus |  | ³ 0 | Error status / error code for the block. A zero value indicates no error, and a value greater than zero indicates a special error code (see table).
fCtrlOutput |  | ± ¥ | The block's output magnitude.

Error Codes

Name | Description
nERR_NOERROR | No error.
nERR_NOINIT | Missing block initialisation. The block must be initialised once using an initialisation flag. This flag must be active for exactly one PLC cycle when the controller is first called up. This bit must be reset after the first cycle.
nERR_INVALIDPARAM_HYSTERESIS | Invalid parameter. The values for "fParamXOn" and "fParamXOff" must be greater than or equal to zero, and "fParamXOff must be smaller than or equal to "fParamXOn".
nERR_INVALIDPARAM_SCALING | Invalid parameter. The values for the amplification "fParamYGain" and for the offset "fParamYOffset" must be greater than or equal to zero.


Sample

The sample illustrates linking the library to a PLC program capable of running on the PC:

Sample PC program

This sample illustrates linking the library to a PLC program capable of running on the BCxxxx:

Sample BCxxxx program