General operating principle
The following sections provide a generation description of the function blocks in the TwinCAT controller toolbox.
Discretization
The continuous transfer functions of the transfer elements assembled in this library are transformed to discrete values using the trapezoidal rule (Tustin formula).
The Tustin formula:
Function block inputs
eMode
The operation mode of the majority of function blocks can be selected with this input. This makes it possible to select one of the following operation modes:
eCTRL_MODE_PASSIVE | The output or outputs of the function block are set to zero, but the internal states are retained. |
eCTRL_MODE_ACTIVE | The function block is executed in accordance with its description, and appropriate output values are calculated (normal operation). |
eCTRL_MODE_RESET | In this operation mode all internal states are reset and the error bit is cleared. |
eCTRL_MODE_MANUAL | The value of the input value |
stParams
The necessary parameters are passed to the function block with this structure. The variables tTaskCycleTime
and tCtrlCycleTime
are contained in all the parameter structures. These parameters function in the following way:
The parameter tTaskCycleTime
specifies the cycle time with which the function block is called. If the function block is called in every cycle this corresponds to the cycle time of the calling task. If it is only called in every second cycle, the time must be doubled accordingly. The parameter tCtrlCycleTime
indicates the control loop's sampling time. This time must be greater than or equal to the parameter tTaskCycleTime
. If the sampling time is set equal to tTaskCycleTime
then the function block is executed with every call. If the selected value is larger by a factor of 5, the function block is only processed in every fifth call. This makes it possible to implement slow control loops even in a fast task.
The parameters tTaskCycleTime
and tCtrlCycleTime
are of type TIME and therefore do not permit inputs of less than 1ms. In order to use the controller in a fast PLC task with a cycle time of less than 1ms, a global base time can be specified as reference for the specified cycle times. The use of the base time is explained in the following examples.
It is assumed that the function block is called in every task cycle.
Task: Configuration | Parameter: | Parameter: | Action |
T#10ms | T#10ms | T#10ms | The control loop is processed using a 10 ms sampling time. |
T#10ms | T#10ms | T#50ms | The control loop is processed using a 50 ms sampling time. |
T#100ms | T#100ms | T#100ms | The control loop is processed using a 100 ms sampling time. |
T#100ms | T#100ms | T#50ms | ERROR, execution is not possible! |
T#100ms | T#50ms | T#50ms | ERROR, although the function block has been executed, incorrect output values have been calculated! |
The outputs of the function blocks
eState
This output indicates the current internal state of the function block.
eCTRL_STATE_IDLE | The function block has successfully been reset, and is now waiting for selection of the operation mode. |
eCTRL_STATE_PASSIVE | The function block is in the passive state in which no calculations are carried out. |
eCTRL_STATE_ACTIVE | The function block is in the active state, which is the normal operating state. |
eCTRL_STATE_RESET | A reset request is being processed, but the reset has not yet been completed. |
eCTRL_STATE_MANUAL | The function block is in the manual state, and the output value can be manually specified at the appropriate input. |
eCTRL_STATE_... | If there are any other internal states, they are described together with the corresponding function blocks. |
eCTRL_STATE_ERROR | An error has occurred; the function block is not executed when in this state. See eErrorId for further information. |
bError
An error in the function block is indicated by a TRUE at this boolean output.
eErrorId
The error number is provided at this output if the bError
output is TRUE.
Using the global base time
In order to be able to use the function blocks of a PLC task with a cycle time of less than 1ms, it is possible to interpret the specified cycle times as ticks of a base time. In this special parameterization, the time unit of 1ms is interpreted as 1 tick. This approach is equivalent to setting a PLC cycle time of less than 1ms in the TwinCAT System Manager.
The switchover and declaration of the base time is done with the global structure stCtrl_GLOBAL_CycleTimeInterpretation
for all function blocks of the toolbox.
VAR_GLOBAL
stCtrl_GLOBAL_CycleTimeInterpretation :
ST_CTRL_CYCLE_TIME_INTERPRETATION;
END_VAR
TYPE ST_CTRL_CYCLE_TIME_INTERPRETATION :
STRUCT
bInterpretCycleTimeAsTicks : BOOL; (* e.g. 2ms -> 2ticks *)
fBaseTime : FLOAT; (* Base time in seconds, e.g. 200µs -> 200E-6s *)
END_STRUCT
END_TYPE
In order to interpret the specified cycle times as ticks, the variable bInterpretCycleTimeAsTicks
in the global structure stCtrl_GLOBAL_CycleTimeInterpretation
is set to TRUE. Within this structure, the base time unit has to be set in variable fBaseTime
.
By setting the flag bInterpretCycleTimeAsTicks
, the interpretation of the parameters with the names
- tTaskCycleTime
- tCtrlCycleTime
is changed. The interpretation and effect of all other parameters of type TIME remains unaffected.
Sample
The base time unit of the TwinCAT system is 200µs. The PLC task, and therefore the function blocks of the Toolbox, are called cyclically every 400µs.
Setting the global structure:
stCtrl_GLOBAL_CycleTimeInterpretation.bInterpretCycleTimeAsTicks := TRUE;
stCtrl_GLOBAL_CycleTimeInterpretation.fBaseTime := 200E-6;
Parameterization of a function block from the Toolbox:
stParams.tTaskCycleTime := T#2ms; (* 2*200µs=400µs *)
stParams.tCtrlCycleTime := T#4ms; (* 4*200µs=800µs *)
stParams. ...
The TaskCycleTime specified on the function blocks is "2•200E-6s = 400µs" and thus corresponds to the set PLC cycle time. The CtrlCycleTime is set to "800µs = 4•200E-6s" so that the control loop operates with a sampling time of 800µs, i.e. it is processed in every second PLC cycle.