FB_CTRL_LOG_MAT_FILE (only on a PC system)

FB_CTRL_LOG_MAT_FILE (only on a PC system) 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 2 s.

FB_CTRL_LOG_MAT_FILE (only on a PC system) 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. 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.

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 of the type ARRAY OF BYTES, which is larger than 1500 bytes.
The individual messages are temporarily stored in the external buffer and this buffer is written to the file as soon as possible. The fBufferUsage output indicates how full the buffer is. The function block is stopped and an error is output if the buffer overflows.

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

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

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

VAR_OUTPUT

VAR_OUTPUT
    eState      : E_CTRL_STATE;
    bFileOpen       : BOOL
    bFileClosed     : BOOL
    fBufferUsage    : FLOAT     (* Buffer fill level in percent [0 ... 100] *)
    nLoggedColumns  : DINT;
    bBusy       : BOOL
    eErrorId        : E_CTRL_ERRORCODES;
    bError      : BOOL;
END_VAR

eState : State of the function block.

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

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

fBufferUsage : Current fill level of the external buffer as a percentage.

nLoggedColumns : Number of columns written in the file.

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

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_LOG_MAT_FILE_PARAMS;
END_VAR

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

TYPE
ST_CTRL_LOG_MAT_FILE_PARAMS:
STRUCT
    tLogCycleTime    : TIME := T#0ms; (* controller cycle
time [TIME] *)
    tTaskCycleTime       : TIME := T#0ms; (* task cycle time
[TIME] *)
    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

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

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.

sFileName : Name and path of the log file, e.g.: d:\Logfile.mat.

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

sMatrixName : Name of the data array.

arRowDescription : Array of strings that contain the line headings.

bWriteTimeStamps : If this parameter is set to TRUE, the time stamp of the measurement is written into the first row of the data array.

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

pLogBuffer_ADR : Address of the external LogBuffer. To be recognized, the buffer's address must be unequal to 0.

nLogBuffer_SIZEOF: 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.

Note

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 );

(* creat test data *)
  fIn   := fIn + 0.01;
  fOut  := SIN(fIn);

(* copy test data to the Logger-Input *)
  LoggerData[ 1 ] := fIn;
  LoggerData[ 2 ] := fOut;
  LoggerData[ 3 ] := fOut * 2;

(* log only 1000 columns *)
  IF fbCtrlLogMatFile.nLoggedColumns >= 25
  THEN
    eMode := eCTRL_MODE_Passive;
  END_IF

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

(* get max buffer usage *)
  fMaxBufferUsage := MAX(fbCtrlLogMatFile.fBufferUsage, fMaxBufferUsage);
FB_CTRL_LOG_MAT_FILE (only on a PC system) 3:

Requirements

Development environment

Target system type

PLC libraries to be linked

TwinCAT v2.8

PC (i386)

TcControllerToolbox.lib