FB_CTRL_LOG_MAT_FILE

FB_CTRL_LOG_MAT_FILE 1:

This function block allows a log file to be created in Matlab 5 (*.mat) format, in which a maximum of 10 magnitudes may be recorded.

Two variables are created in the file, a double array and a cell array. The recorded magnitudes are recorded, line-by-line, in the double array. The identifiers of the individual rows are stored in the cell array. The user can specify the name used for the double array in the function block's parameter structure. The name of the cell array is derived from the name of the double array by appending "_Info" to the variable name.

The input data is written at equal time intervals in the columns of the data array. A time stamp for the relevant entry, in s, can be stored in the first column. The time interval between the entries is specified in the tLogCycleTime parameter. If, for instance, "tLogCycleTime := T\#2s" is chosen, then an entry is made in the file every 2s.

FB_CTRL_LOG_MAT_FILE 2:

When the mode is set to eCTRL_MODE_ACTIVE the log file is opened and entries are written into the file. The file remains open until the function block's mode is set to eCTRL_MODE_PASSIVE.

It is essential that the file is closed by switching into eCTRL_MODE_PASSIVE before attempting to analyze the log file. If this is not done, it is possible that not all the entries will be written into the file, which will then not be consistent.

The function block makes it possible to work with or without an external buffer.

Operating without an external buffer:

The bBusy output is TRUE when the logging of a row has been started. The following data set will not be logged until the bBusy output is FALSE again. The fBufferUsage output indicates how full the internal buffer is.

Operating with an external buffer:

The user must create a buffer larger than 1500 bytes and with the type ARRAY OF BYTES.
The individual messages are temporarily stored in the external buffer, and this buffer is written into the file as quickly as possible. The fBufferUsage output indicates how full the buffer is. If a buffer overflow occurs, the function block is stopped and an error is output.

FB_CTRL_LOG_MAT_FILE 3:

The external buffer is used if a buffer address and a buffer size greater than zero are parameterized. In the absence of an external buffer, an internal buffer with the size of 1500 bytes is used.

FB_CTRL_LOG_MAT_FILE 4: VAR_INPUT

VAR_INPUT
    fLogData  : T_CTRL_LOGGER_DATA;
    eMode     : E_CTRL_MODE;
END_VAR
VAR_GLOBAL CONSTANT
    nCTRL_LOGGER_DATA_ARRAY_SIZE :UINT := 10;
END_VAR

TYPE
    T_CTRL_LOGGER_DATA :ARRAY [1..nCTRL_LOGGER_DATA_ARRAY_SIZE]
OF FLOAT;
END_TYPE

Name

Type

Description

fLogData

T_CTRL_
LOGGER_DATA

Array containing the values that are to be written into the log file.

eMode

E_CTRL_MODE

Input that specifies the operation mode of the function block.

FB_CTRL_LOG_MAT_FILE 5: VAR_OUTPUT

VAR_OUTPUT
    eState          : E_CTRL_STATE;
    bFileOpen       : BOOL
    bFileClosed     : BOOL
    fBufferUsage    : FLOAT    
    nLoggedColumns  : DINT;
    bBusy           : BOOL
    eErrorId        : E_CTRL_ERRORCODES;
    bError          : BOOL;
END_VAR

Name

Type

Description

eState

E_CTRL_STATE

State of the function block

bFileOpen

BOOL

A TRUE at this output indicates that the file has successfully been opened.

bFileClosed

BOOL

A TRUE at this output indicates that the file has successfully been closed.

fBufferUsage

FLOAT

Current fill level of the external buffer as a percentage

nLogged
Columns

DINT

Number of columns written in the file

bBusy

BOOL

A TRUE at this output indicates that logging a row is active.

eErrorId

E_CTRL_ERRORCODES

Supplies the error number when the output bError is set.

bError

BOOL

Becomes TRUE, as soon as an error occurs.

FB_CTRL_LOG_MAT_FILE 6: VAR_OUTPUT

VAR_IN_OUT
    stParams        : ST_CTRL_LOG_MAT_FILE_PARAMS;
END_VAR

Name

Type

Description

stParams

ST_CTRL_LOG_MAT_FILE_
PARAMS

Parameter structure of the logging function block

stParams consists of the following elements:

TYPE ST_CTRL_LOG_MAT_FILE_PARAMS:
STRUCT
    tLogCycleTime         : TIME := T#0ms;
    tTaskCycleTime        : TIME := T#0ms;
    sFileName             : STRING;
    nNumberOfRows         : INT(1..10);
    sMatrixName           : STRING;
    arRowDescription      : ARRAY [1..10] OF STRING(25);
    bWriteTimeStamps      : BOOL := TRUE;
    bWriteRowDescription  : BOOL := TRUE;
    pLogBuffer_ADR        : POINTER TO
     ST_CTRL_MULTIPLE_PWM_OUT_DATA;
    nLogBuffer_SIZEOF       : UDINT;
END_STRUCT
END_TYPE

Name

Type

Description

tLogCycleTime

TIME

Cycle time with which entries are written into the log file. This must be greater than or equal to the TaskCycleTime.

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.

sFileName

STRING

Name and path of the log file, for example: d:\Logfile.mat

nNumberOfRows

INT

Number of columns written into the file (maximum 10).

sMatrixName

STRING

Name of the data array

arRow
Description

ARRAY

Array of strings that contain the line headings.

bWriteTime
Stamps

BOOL

If this parameter is set to TRUE, the timestamp of the measurement is written into the first row of the data array.

bWriteRow
Description

BOOL

If this parameter is set to TRUE a cell array is created into which the descriptions of the rows are written.

pLogBuffer_ADR

POINTER TO ST_CTRL_
MULTIPLE_PWM_OUT_DATA

Address of the external LogBuffer. To detect a buffer, the address must not be 0.

nLogBuffer_
SIZEOF

UDINT

Size of the LogBuffer. The buffer must be an ARRAY OF BYTE with at least 1501 elements. The size of the buffer can be optimized with the aid of the fBufferUsage output.

Notice

Error during file handling

The parameter set can only be changed when the file is closed (bFileClosed = TRUE). Otherwise errors can occur during file handling.

Sample

PROGRAM PRG_LOG_MAT_FILE_TEST_BUFFERED
VAR
    eMode             : E_CTRL_MODE;
    stParams          : ST_CTRL_LOG_MAT_FILE_PARAMS;
    LoggerData        : T_CTRL_LOGGER_DATA;
    eErrorId          : E_CTRL_ERRORCODES;
    bError            : BOOL;
    fbCtrlLogMatFile  : FB_CTRL_LOG_MAT_FILE;
    LogBuffer         : ARRAY[0..2000] OF BYTE;
    bInit             : BOOL := TRUE;
    fIn               : LREAL;
    fOut              : LREAL;
    fMaxBufferUsage   : LREAL;
END_VAR

IF bInit THEN
    stCtrl_GLOBAL_CycleTimeInterpretation.bInterpretCycleTimeAsTicks := FALSE;
    stCtrl_GLOBAL_CycleTimeInterpretation.fBaseTime := 0;
    stParams.tLogCycleTime     := T#2ms;
    stParams.tTaskCycleTime         := T#2ms;
    stParams.nNumberOfRows     := 3;
    stParams.sFileName     := 'D:\test.mat';
    stParams.sMatrixName     := 'TwinCAT_ControllerToolbox_Log';
    stParams.arRowDescription[1] := 'Input';
    stParams.arRowDescription[2] := 'Output 1';
    stParams.arRowDescription[3] := 'Output 2';
    stParams.bWriteRowDescription := TRUE;
    stParams.bWriteTimeStamps     := TRUE;
    eMode := eCTRL_MODE_ACTIVE;
    bInit := FALSE;
END_IF

stParams.nLogBuffer_SIZEOF := SIZEOF( LogBuffer );
stParams.pLogBuffer_ADR := ADR( LogBuffer );

fIn := fIn + 0.01;
fOut := SIN(fIn);

LoggerData[1]:= fIn;
LoggerData[2]:= fOut;
LoggerData[3]:= fOut * 2;

IF fbCtrlLogMatFile.nLoggedColumns >= 25 THEN
    eMode := eCTRL_MODE_Passive;
END_IF

fbCtrlLogMatFile( fLogData := LoggerData,
             eMode := eMode,
             stParams :=stParams,
             eErrorId => eErrorId,
             bError => bError);

fMaxBufferUsage := MAX(fbCtrlLogMatFile.fBufferUsage, fMaxBufferUsage);
FB_CTRL_LOG_MAT_FILE 7: