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:

aCoefficientsArray_a        : ARRAY[1..nFilterOrder+1] OF FLOAT;
aCoefficientsArray_b        : ARRAY[1..nFilterOrder+1] OF FLOAT;
aStDigitalFilterData        : ARRAY[1..nFilterOrder] OF
ST_CTRL_DIGITAL_FILTER_DATA;

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

aCoefficientsArray_b[1] := b1;
aCoefficientsArray_b[2] := b2;

aCoefficientsArray_b[n-1] := bn-1;
aCoefficientsArray_b[n]   := bn;

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

aCoefficientsArray_a[1] := xxx; (* not being evaluated *)
aCoefficientsArray_a[2] := a2;
...
aCoefficientsArray_a[n-1] := an-1;
aCoefficientsArray_a[n]   := an;

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

FB_CTRL_DIGITAL_FILTER 4: VAR_INPUT

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

Name

Type

Description

fIn

FLOAT

Input of the digital filter

fManValue

FLOAT

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

eMode

E_CTRL_MODE

Input that specifies the operation mode of the function block.

FB_CTRL_DIGITAL_FILTER 5: VAR_OUTPUT

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

Name

Type

Description

fOut

FLOAT

Output of the digital filter

eState

E_CTRL_STATE

State of the function block

bError

BOOL

Becomes TRUE, as soon as an error occurs.

eErrorId

E_CTRL_ERRORCODES

Supplies the error number when the output bError is set.

VAR_IN_OUT

VAR_IN_OUT
    stParams        : ST_CTRL_DIGITAL_FILTER_PARAMS;
END_VAR

Name

Type

Description

stParams

ST_CTRL_
DIGITAL_FILTER_PARAMS

Parameter structure of the function block

stParams consists of the following elements:

TYPE
ST_CTRL_DIGITAL_FILTER_PARAMS :
STRUCT
    tTaskCycleTime                 : TIME;
    tCtrlCycleTime                 : TIME := T#0ms;
    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

Name

Type

Description

tTaskCycleTime

TIME

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

TIME

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

USINT

Order of the digital filter [0... ]

pCoefficientsArray_a_ADR

POINTER TO FLOAT

Address of the array containing the a coefficients

nCoefficientsArray_a_SIZEOF

UINT

Size of the array containing the a coefficients, in bytes

pCoefficientsArray_b_ADR

POINTER TO FLOAT

Address of the array containing the b coefficients

nCoefficientsArray_b_SIZEOF

UINT

Size of the array containing the b coefficients, in bytes

pDigitalFilterData_ADR

POINTER TO ST_CTRL_
DIGITAL_
FILTER_DATA

Address of the data array

nDigitalFilterData_SIZEOF

UINT

Size of the data array in bytes

TYPE
ST_CTRL_DIGITAL_FILTER_DATA
STRUCT
 Internal structure. This must not be written to.
END_STRUCT
END_TYPE

Sample:

PROGRAM PRG_DIGITAL_FILTER_TEST
VAR
    fbDigitalFilter        : FB_CTRL_DIGITAL_FILTER;
    aCoefficientsArray_a    : ARRAY[1..3] OF FLOAT;
    aCoefficientsArray_b    : ARRAY[1..3] OF FLOAT;
    aStDigitalFilterData    : 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
    aCoefficientsArray_a[1] := 0.0; (* not used *)
    aCoefficientsArray_a[2] := 0.2;
    aCoefficientsArray_a[3] := 0.1;
    aCoefficientsArray_b[1] := 0.6;
    aCoefficientsArray_b[2] := 0.4;
    aCoefficientsArray_b[3] := 0.2;

    stParams.tTaskCycleTime := T#2ms;
    stParams.tCtrlCycleTime := T#2ms;
    stParams.nFilterOrder := 2;

    eMode := eCTRL_MODE_ACTIVE;
    bInit := FALSE;
END_IF

stParams.pCoefficientsArray_a_ADR := ADR(aCoefficientsArray_a);
stParams.nCoefficientsArray_a_SIZEOF := SIZEOF(aCoefficientsArray_a);
stParams.pCoefficientsArray_b_ADR := ADR(aCoefficientsArray_b);
stParams.nCoefficientsArray_b_SIZEOF := SIZEOF(aCoefficientsArray_b);
stParams.pDigitalFilterData_ADR := ADR(astDigitalFilterData);
stParams.nDigitalFilterData_SIZEOF := SIZEOF(aStDigitalFilterData);
fbDigitalFilter ( fIn := fIn,
                eMode := eMode,
                stParams := stParams,
                fOut => fOut,
                eErrorId => eErrorId,
                bError => bError);