FB_CTRL_DIGITAL_FILTER

FB_CTRL_DIGITAL_FILTER 1:

This function block calculates a discrete transfer function with the structure described below. This structure allows either an FIR filter (Finite Impulse Response) or an IIR filter (Infinite Impulse Response) to be implemented. The transfer function here can be of any order, n.

The coefficients for the following transfer structure are stored in the parameter arrays:

FB_CTRL_DIGITAL_FILTER 2:
FB_CTRL_DIGITAL_FILTER 3:

The programmer must create the following arrays in the PLC if this function block is to be used:

ar_CoefficientsArray_a     :
ARRAY[1..nFilterOrder+1] OF FLOAT;
ar_CoefficientsArray_b     : ARRAY[1..nFilterOrder+1] OF
FLOAT;
ar_stDigitalFilterData     : ARRAY[1..nFilterOrder  ] OF
ST_CTRL_DIGITAL_FILTER_DATA;

The coefficients b1 to bn are stored in the array ar_CoefficientsArray_b. This must be organized as follows:

ar_CoefficientsArray_b[ 1 ]     := b1;
ar_CoefficientsArray_b[ 2 ]     := b2;
...
ar_CoefficientsArray_b[ n-1 ]   := bn-1;
ar_CoefficientsArray_b[ n ]     := bn;

The coefficients a1 to an are stored in the array ar_CoefficientsArray_a. This must be organized as follows:

ar_CoefficientsArray_a[ 1 ]    := xxx;    (* wird nicht ausgewertet
*)
ar_CoefficientsArray_a[ 2 ]    := a2;
...
ar_CoefficientsArray_a[ n-1 ]  := an-1;
ar_CoefficientsArray_a[ n ]    := an;

The internal data required by the function block is stored in the ar_stDigitalFilterData array. This data must never be modified from within the PLC program. This procedure is also illustrated in the sample program listed below.

VAR_INPUT

VAR_INPUT
    fIn         : FLOAT;
    fManValue       : FLOAT;
    eMode       : E_CTRL_MODE;
END_VAR

fIn : Input of the digital filter.

fManValue : Input whose value is present at the output in manual mode.

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

VAR_OUTPUT

VAR_OUTPUT
    fOut        : FLOAT;
    eState      : E_CTRL_STATE;
    bError      : BOOL;
    eErrorId        : E_CTRL_ERRORCODES;
END_VAR

fOut : Output of the digital filter.

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.

VAR_IN_OUT

VAR_IN_OUT
    stParams        : ST_CTRL_DIGITAL_FILTER_PARAMS;
END_VAR

stParams : Parameter structure of the function block. This consists of the following elements:

TYPE
ST_CTRL_DIGITAL_FILTER_PARAMS :
STRUCT
    tTaskCycleTime          : TIME;      (* task cycle
time       *)
    tCtrlCycleTime          : TIME := T#0ms; (* controller
cycle time *)
    nFilterOrder        : USINT;
    pCoefficientsArray_a_ADR    : POINTER TO FLOAT := 0;
    nCoefficientsArray_a_SIZEOF : UINT;
    pCoefficientsArray_b_ADR    : POINTER TO FLOAT := 0;
    nCoefficientsArray_b_SIZEOF : UINT;
    pDigitalFilterData_ADR      : POINTER TO
ST_CTRL_DIGITAL_FILTER_DATA;
    nDigitalFilterData_SIZEOF   : UINT;
END_STRUCT
END_TYPE

tTaskCycleTime : Cycle time with which the function block is called. If the function block is called in every cycle this corresponds to the task cycle time of the calling task.

tCtrlCycleTime : Cycle time with which the control loop is processed. This must be greater than or equal to the TaskCycleTime. The function block uses this input value to calculate internally whether the state and the output values have to be updated in the current cycle.

nFilterOrder : Order of the digital filter [0... ]

pCoefficientsArray_a_ADR : Address of the array containing the a coefficients.

nCoefficientsArray_a_SIZEOF : Size of the array containing the a coefficients, in bytes.

pCoefficientsArray_b_ADR : Address of the array containing the b coefficients.

nCoefficientsArray_b_SIZEOF : Size of the array containing the b coefficients, in bytes.

pDigitalFilterData_ADR : Address of the data array.

nDigitalFilterData_SIZEOF : Size of the data array in bytes.

TYPE
ST_CTRL_DIGITAL_FILTER_DATA
STRUCT
    Interne Struktur. Auf diese darf nicht schreibend
zugegriffen werden.
END_STRUCT
END_TYPE

Sample:

PROGRAM PRG_DIGITAL_FILTER_TEST
VAR
    fbDigitalFilter : FB_CTRL_DIGITAL_FILTER;
    ar_CoefficientsArray_a : ARRAY[1..3 ] OF FLOAT;
    ar_CoefficientsArray_b : ARRAY[1..3 ] OF FLOAT;
    ar_stDigitalFilterData : ARRAY[1..2 ] OF
ST_CTRL_DIGITAL_FILTER_DATA;
    eMode     : E_CTRL_MODE;
    stParams  : ST_CTRL_DIGITAL_FILTER_PARAMS;
    eErrorId  : E_CTRL_ERRORCODES;
    bError    : BOOL;
    fIn       : FLOAT := 0;
    fOut      : FLOAT;
    bInit     : BOOL := TRUE;
END_VAR
IF bInit
THEN
    (* set values in the local arrays *)
    ar_CoefficientsArray_a[1] := 0.0;    (* not used *)
    ar_CoefficientsArray_a[2] := 0.2;
    ar_CoefficientsArray_a[3] := 0.1;
    ar_CoefficientsArray_b[1] := 0.6;
    ar_CoefficientsArray_b[2] := 0.4;
    ar_CoefficientsArray_b[3] := 0.2;
    (* set values in the parameter struct *)
    stParams.tTaskCycleTime := T#2ms;
    stParams.tCtrlCycleTime := T#2ms;
    stParams.nFilterOrder   := 2;
    (* set the mode *)
    eMode := eCTRL_MODE_ACTIVE;
    bInit := FALSE;
END_IF
(* set addresses *)
stParams.pCoefficientsArray_a_ADR    := ADR(
ar_CoefficientsArray_a);
stParams.nCoefficientsArray_a_SIZEOF := SIZEOF(
ar_CoefficientsArray_a);
stParams.pCoefficientsArray_b_ADR    := ADR(
ar_CoefficientsArray_b);
stParams.nCoefficientsArray_b_SIZEOF := SIZEOF(
ar_CoefficientsArray_b);
stParams.pDigitalFilterData_ADR      := ADR( ar_stDigitalFilterData
);
stParams.nDigitalFilterData_SIZEOF   := SIZEOF(
ar_stDigitalFilterData );
fbDigitalFilter ( fIn       := fIn,
          eMode     := eMode,
          stParams  := stParams,
          fOut      => fOut,
          eErrorId  => eErrorId,
          bError    => bError);

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8

PC (i386)

TcControllerToolbox.lib

TwinCAT v2.9, build 947 onwards

BC

TcControllerToolbox.lb6

TwinCAT v2.9, build 956 onwards

BX

TcControllerToolbox.lbx