SFunWrappedStaticLib

Use cases

Encapsulating own code within static libraries can be useful to

Description

The following example shows the configuration of the module generator when using Sfunctions that depend on statically linked libraries. For this type of code integration, a suitable library must be available for all TwinCAT platforms for which the module is to be created.
The following diagram illustrates the typical workflow when using third-party algorithms in a custom Simulink model:

SFunWrappedStaticLib 1:

In this example, the source code for the "algorithm development side" is available and can be compiled for all TwinCAT platforms. It shows how

Overview of project directory

TE1400Sample_SFunWrappedStaticLib contains all the files necessary to reproduce this example:

TctSmplSFunWrappedStaticLib.mdl

contains the model that references the SFunction.

TctSample_SFunLibWrapper.cpp

must be present on the target system. Contains the source code of the SFunction.

StaticLib.cpp

Simple example of source code of a static library.

BuildLibsAndSFunction.m

contains an M-script that creates the static library for all currently available TwinCAT platforms and creates the SFunction.

OpenLibProject.m

contains an M-script that defines the MATLAB build environment for Visual Studio, such as MATLAB Include and library directories. The static library is then opened in Microsoft Visual Studio 2010 with the predefined environment variables.

Subdirectory LibProject

contains the static library.

Subdirectory BuildScripts

contains several M-scripts for "BuildLibsAndSFunction.m" and "OpenLibProject.m".

_PrecompiledTcComModules

This subdirectory contains readily compiled TwinCAT modules that were generated from the enclosed Simulink models. They enable the integration of a module in TwinCAT to be tested, without having to generate the module first. They can be used in situations where a MATLAB license is not yet available, for example. A quick reference guide for module installation on the development PC is also enclosed.

Attention: To start the module on an x64 target system, the system must be switched to test mode!

_PreviousSimulinkVersions

The MDL files described above are stored in the file format of the current Simulink version. This subdirectory contains the models in the file format of elder Simulink versions

Create SFunction and corresponding statically linked libraries

Build requirements

It is recommended to have TwinCAT 3 installed on your system to build the binaries, but not required. Requirements are:

Windows Driver Kit

installed on your system and the environment variable WinDDK set to its path.

TwinCAT SDK

installed on your system and the environment variable TwinCatSdk set to its path.

For more information about this requirement, see the system requirements in the TwinCAT 3 documentation.

Creating static libraries manually

The static libraries can be created manually with Visual Studio. To do this, execute OpenLibProject.m. This prepares the required environment variables and opens the SFunction project in Visual Studio. Create a project for all platforms that should be supported. The output file for all platforms is a static library:

SFunWrappedStaticLib 2:

Build the static link libraries via build script

Alternatively to the manual build procedure, run BuildLibsAndSFunction.m. This prepares the build environment and invokes MSBUILD multiple times to build the lib files for each platform.. Afterwards, all the build output files are copied to the subfolder LibProject\TctSample_WrappedStaticLib\BuildOutput. The .LIB files, which are necessary to build the SFunction and the generated TwinCAT modules are additionally copied to LibProject\TctSample_WrappedStaticLib\LibPackage.

Deliver the static libraries

LibProject\TctSample_WrappedStaticLib\LibPackage is the folder which can be delivered to users, which want to use this library inside their own - TwinCAT Target compatible - SFunctions. Copy the content of this folder to the users system, more precisely to the folder %TwinCat3Dir%Functions\TE1400-TargetForMatlabSimulink\Libraries. This is also done by BuildLibsAndSFunction.m for the local system. The content of this folder should be:

Subfolders TwinCAT xx (xxx)

contain the static link libraries for different TwinCAT platforms. These are used when generating a TwinCAT module from an appropriate Simulink model.

Subfolders Win32 / Win64

contain the static link libraries for the different MATLAB platform architectures (32 and/or 64 Bit). These are used when generating a TwinCAT module from an appropriate Simulink model.
To build the libraries for this example for 32 and 64 Bit MATLAB BuildLibsAndSFunction.m has to be executed with both MATLAB variants.

Compile mex file code

Before the SFunction can be used inside the Simulink model, it has to be built, too. Of course this can be done manually, as in any other SFunction. However, the MEX compiler has to be advised to link the static library to the SFunction.
When executing BuildLibsAndSFunction.m, this is also done automatically. Afterwards the file "SFunStaticLib.mexw32" should be located inside your working folder.
To check if everything works, open "TctSmplSFunWrappedStaticLib.mdl" and start simulation. If the simulation starts without error messages, everything is prepared as needed.

Generating a TwinCAT module

Configuring a model

The general settings for generating a TwinCAT module must be set, e.g. a fixed-step solver must be configured, and the system target file "TwinCAT.tlc" must be selected under the "General" tab in the model coder settings. For further information on the general configuration of the TwinCAT module generator see Quickstart.
In addition, the code generator must know which static libraries have to be linked to the generated code and where to find them. Enter this information in the corresponding option fields of the Simulink coder, as shown in the figures below.

SFunWrappedStaticLib 3:

SFunWrappedStaticLib 4:

The Include folder should already have been created automatically by TwinCAT Target. The Libraries setting must contain the names of the static libraries to be linked.

Background information for these settings:

In this example (and in most other cases) there are several instances of these libraries in the specified folders. MSBUILD decides which version is linked to the module when the generated TwinCAT module binary files are linked.

How to use this example as a template

The following list provides a short overview of the easiest way, to create an own TwinCAT Target compatible SFunction dependency:

1. Copy the sample folder
2. Replace the MDL file by your own
3. Rename the VCXPROJ file to the desired name of your SFunction
4. Copy your source files to the directory where the VCXPROJ file is located
5. Adapt the scripts BuildLibsAndSFunction.m and OpenLibProject.m to the new project name
6. Open the VCXPROJ file using OpenLibProject.m
7. Remove the existing CPP files from the project
8. Add your own CPP files to the project
9. If necessary, add include directories, dependency directories and libraries to the compiler and linker settings
10. Build the project (for different platforms and/or configurations)
11. Close the VCXPROJ file
12. Run BuildLibsAndSFunction.m