asys
asys

Reputation: 731

Linear Interpolation in TwinCAT

I'm trying to use FB_CTRL_LIN_INTERPOLATION, which exists in Controller Toolbox in TwinCAT3 as described in this link.

Unfortunately, the examples on the Infosys site are not practical; for example, there is no data type FLOAT on twincat3 at all; it can replace with REAL or LREAL in the next revision of the site!

By the way, here is my first attempt to implement the example.

The deceleration part is:

VAR
    linInt:FB_CTRL_LIN_INTERPOLATION;
    stParams       : ST_CTRL_LIN_INTERPOLATION_PARAMS;
    fIn           : LREAL;
    fManValue     : LREAL;
    bExtrapolate  : BOOL;
    eMode         : E_CTRL_MODE;
    
    fOut                        : LREAL;
    bInIsGreaterThanMaxElement  : BOOL;
    bInIsLessThanMinElement     : BOOL;
    eState                      : E_CTRL_STATE;
    eErrorId                    : E_CTRL_ERRORCODES;
    bError                      : BOOL;
    
    Data: ARRAY[1..6, 1..2] OF INT := [10, 15, 21, 22, 30, 40, 7, 10, 9, 2, 3, 6];
END_VAR

and the body part is :

stParams.pDataTable_ADR:=ADR(Data);
stParams.tCtrlCycleTime:=T#4S;
stParams.tTaskCycleTime:=T#2S;
stParams.nDataTable_SIZEOF:=SIZEOF(Data);
stParams.nDataTable_NumberOfRows:=6;


linInt(fIn:=fIn,
    fManValue:=fManValue,
    bExtrapolate:=bExtrapolate,
    eMode:=emode,
    fOut=>fOut,
    bInIsGreaterThanMaxElement=>bInIsGreaterThanMaxElement,
    bInIsGreaterThanMaxElement=>bInIsLessThanMinElement,
    eState=>eState,
    eErrorId=>eErrorId,
    bError=>bError,
    stParams:=stParams);

I do not have any idea what is the meaning of Size of the n x 2 array for the Description of nDataTable_SIZEOF so I used SIZEOF(Data) command to assign a value to it. exactly I now wonder what the 24 is!

enter image description here

as shown in the following figure, there is an error with the table description.

any help would be appreciated if someone clears it to me what is the meaning of stparams with detail here and how I can handle this example completely.

enter image description here

Upvotes: 0

Views: 565

Answers (2)

Guiorgy
Guiorgy

Reputation: 1734

I have no experience with TwinCAT, however implementing a linear interpolator shouldn't be too difficult.

Here's an example Function Block.

Assuming you have a data array, you can interpolate using the following formulae:

// find the interval of points x falls into. let `i` be the left point of that interval.
y := (x - data[i, 1]) / (data[i + 1, 1] - data[i, 1]) * (data[i + 1, 2] - data[i, 2]) + data[i, 2];

Upvotes: 0

Hopperpop
Hopperpop

Reputation: 31

Your data table needs to be "ARRAY[x..y, 1..2] OF FLOAT", not INT.

The FLOAT datatype is defined as an alias inside the library as a LREAL or REAL. See: InfoSys

Upvotes: 1

Related Questions