Using the ToFile block

The ToFile block from the Simulink default library can be used to log signals in a MAT file. From within a created TcCOM, this block can still be used from the TwinCAT runtime.

For file system access from the real-time, an additional TcCOM "TcExtendedFilewriter“ is created and linked to the TcCOM with the ToFile block (referred to as Simulink TcCOM below). The TcExtendedFilewriter then receives the data from the assigned TcCOM and writes it to a MAT file (mat4).

The settings in Simulink and TwinCAT are described step by step below, based on an example.

Configuration in the Simulink model

A model with a sine and a cosine source serves as a simple example. Each signal is to be logged with a ToFile block.

Using the ToFile block 1:

To enable code generation for the ToFile blocks, the format must be set to Array:

Using the ToFile block 2:

The model is now ready for code generation.

Configuration in TwinCAT

To write from the generated Simulink TcCOM, the TcCOM TcExtendedFilewriter installed with the TE1400 is required. It accepts data from the Simulink object and stores the data in the file system. The module can be found in the TcCOM browser under Beckhoff Automation -> Extended File Access -> TcExtendedFileWriter:

Using the ToFile block 3:

Initially, both TcCOMs are instantiated. Both objects can be linked to a joint task or separate task. In order to establish a link between the two objects, the ObjectID of the TcExtendedFilewriter instance is communicated to the Simulink TcCOM.

The ObjectID can be found under the Object tab.

Using the ToFile block 4:

The ObjectID is then inserted under the "Parameters (Init)" tab of the Simulink TcCOM for the parameter ExtendedFileAccessOID:

Using the ToFile block 5:

It is possible to link several Simulink TcCOMs with one TcExtendedFileWriter instance. Ensure that filename conflicts are avoided. Several TcExtendedFileWriter instances can be used in parallel. For example, each Simulink TcCOM with a ToFile block can use its own TcExtendedFileWriter instance.

Parameterization of the TcExtendedFileWriter instance

The behavior of the object can be adapted under the Parameter (init) tab of the TcExtendedFileWriter instance.

Using the ToFile block 6:

Timeout:

A timeout can be set

Working directory:

If a relative path is used in the ToFile block, e.g. /logData, the full path is resolved via the Working Directory parameter.

Number of Files:

It is possible to limit the number of files. If the parameter is 0, limitation is inactive.

Max File Size:

Once the specified file size (default: 1 MB) has been reached, the file is closed and a new file is opened, in order to ensure that the logged data can be accessed while the module is running.

Internal Buffer Size:

A buffer with the size InternalBufferSize is created on the TwinCAT side, from which the data is then written.

Segment Size:

With each write command of the TcExtendedFileWriter instance, a segment with the size SegmentSize is written from the internal buffer to the specified file. The maximum theoretically possible data rate for writing is composed of the SegmentSize and the cycle time of the TcExtendetFileWriter (the TcExtendedFileWriter instance does not have to have the same cycle time as the assigned Simulink TcCOM module). However, be aware that a write command may not yet be complete when the next cycle starts. If this is the case, the write command is suspended in this cycle. It is therefore a best case assessment.

The TwinCAT project can now be activated.

Once the specified file size (default: 1 MB) has been reached, the file is closed and a new file is opened, in order to ensure that the logged data can be accessed while the module is running (in the diagram: *_part1.mat and *_part2.mat are completed, while writing of *_part3.mat is still in progress):

Using the ToFile block 7:

The TcExtendedFilewriter object has a Pause input to prevent continuous writing. If the input is set to TRUE, the file currently in use for write access is closed, and all further incoming data is discarded. If the input is set to FALSE again, a new file for logging incoming data is opened.

The closed files can be opened as usual in MATLAB:

Using the ToFile block 8:

The plot shows the expected sine wave:

Using the ToFile block 9: