FB_CTRL_GET_TASK_CYCLETIME (only on a PC system)

FB_CTRL_GET_TASK_CYCLETIME (only on a PC system) 1:

This function block allows the task cycle time of a program to be determined with a resolution of 1 ms.

FB_CTRL_GET_TASK_CYCLETIME (only on a PC system) 2:

The TaskCycleTime can only be correctly determined if the program does not contain any active breakpoints.

The task cycle time is only determined once. No further measurements are taken if either of the bCycleTimeValid or bError outputs is TRUE.

This function block should not be used if cycle times of less than 1 ms, or that are not a multiple of 1 ms, are in use.

VAR_INPUT

VAR_INPUT
    eMode: E_CTRL_MODE;
END_VAR

eMode : Input that specifies the block's operating mode.

VAR_OUTPUT

VAR_OUTPUT
    tTaskCycleTime      : TIME;             (* resolution: 1ms *)
    bCycleTimeValid     : BOOL;
    eState      : E_CTRL_STATE;
    eErrorId        : E_CTRL_ERRORCODES;
    bError      : BOOL;
END_VAR

tTaskCycleTime : This output indicates the current task cycle time, with a resolution of 1 ms.

bCycleTimeValid : The time contained in the tTaskCycleTime output is valid when this output is TRUE.

eState : State of the function block.

eErrorId : Supplies the error number when the bError output is set.

bError : Becomes TRUE, as soon as an error occurs.

Sample:

PROGRAM PRG_GET_TASK_CYCLETIME_TEST
VAR
    tTaskCycleTime          : TIME;
    bCycleTimeValid         : BOOL;
    eState              : E_CTRL_STATE;
    eErrorId            : E_CTRL_ERRORCODES;
    bError              : BOOL;
    fbCTRL_GET_TASK_CYCLETIME   : FB_CTRL_GET_TASK_CYCLETIME;
    (* control loop *)
    bInit         : BOOL := TRUE;
    fSetpointValue    : FLOAT := 45.0;
    fActualValue      : FLOAT;
    fbCTRL_PI     : FB_CTRL_PI;
    stCTRL_PI_Params  : ST_CTRL_PI_PARAMS;
    fbCTRL_PT1    : FB_CTRL_PT1;
    stCTRL_PT1_Params : ST_CTRL_PT1_PARAMS;
END_VAR
(* call fb to get the task cycle time *)
fbCTRL_GET_TASK_CYCLETIME( eMode       :=
eCTRL_MODE_ACTIVE,
               tTaskCycleTime  =>
tTaskCycleTime,
               bCycleTimeValid =>
bCycleTimeValid,
               eState      =>
eCTRL_MODE_ACTIVE,
               eErrorId    => eErrorId,
               bError      => bError
              );
(* call control loop if the cycle time is valid *)
IF fbCTRL_GET_TASK_CYCLETIME.bCycleTimeValid
THEN
    IF bInit
    THEN
    stCTRL_PT1_Params.tTaskCycleTime :=
fbCTRL_GET_TASK_CYCLETIME.tTaskCycleTime;
    stCTRL_PT1_Params.tCtrlCycleTime := T#100ms;
    stCTRL_PT1_Params.fKp        := 1.0;
    stCTRL_PT1_Params.tT1        := T#10s;
    stCTRL_PI_Params.tTaskCycleTime  :=
fbCTRL_GET_TASK_CYCLETIME.tTaskCycleTime;
    stCTRL_PI_Params.tCtrlCycleTime  := T#100ms;
    stCTRL_PI_Params.fKp         := 0.5;
    stCTRL_PI_Params.tTn         := T#5s;
    stCTRL_PI_Params.fOutMaxLimit    := 100.0;
    stCTRL_PI_Params.fOutMinLimit    := 0.0;
    bInit := FALSE;
    END_IF
    (* call controller *)
    fbCTRL_PI( fActualValue   := fbCTRL_PT1.fOut,
           fSetpointValue := fSetpointValue,
           eMode      := eCTRL_MODE_ACTIVE,
           stParams       := stCTRL_PI_Params
          );
    (* call PT1 *)
    fbCTRL_PT1( fIn      := fbCTRL_PI.fOut,
        eMode    := eCTRL_MODE_ACTIVE,
        stParams := stCTRL_PT1_Params,
        fOut     => fActualValue
          );
END_IF

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8

PC (i386)

TcControllerToolbox.lib