10. Simulating your custom controller in Simulink®¶
FORCESPRO provides Simulink® interfaces for easy simulation of your custom controllers
within existing Simulink® diagrams. Once the solver has been generated and compiled, the appropriate Simulink® block and the necessary code for the Simulink® block definition are available along with your FORCESPRO solver inside the interface
folder. Depending on your controller configuration you will have different input and output ports on your block. The port labels are self-explanatory. Just wire the ports of the FORCESPRO block to other blocks in your Simulink® diagram and run the simulation. Note that several instances of the FORCESPRO block can exist in the same Simulink® diagram.
The FORCESPRO solvers support two Simulink® interfaces:
The S-Function interface
The Coder interface
10.1. The S-Function interface¶
The S-function interface consists of:
a Simulink® block definition of a level 2 S-Function (in interface folder);
the C implementation of the S-Function (in interface folder) which defines how the FORCESPRO solver is used in the context of the Simulink® block.
The Simulink® block definition is used for the inclusion of the FORCESPRO Simulink® block in a Simulink® model. The C implementation is either built into a MEX interface to be used in the simulation of a Simulink® model or used in the build process of the code generation of a Simulink® model using the Simulink® Coder.
10.2. The Coder interface¶
The Coder interface consists of:
a MATLAB Simulink® block (built-in in Simulink) along with the MATLAB code (in interface folder);
a Buildable (coder.ExternalDependency) class (in interface folder);
a FORCESPRO Simulink® block creation script (in interface folder).
The MATLAB Simulink® block is used for the inclusion of the FORCESPRO Simulink® block in a Simulink® model. The MATLAB code contains the code that the MATLAB Simulink® block will run in order to use the FORCESPRO solver. The Buildable class is used as the intermediate interface between the MATLAB Simulink® block (with the defined MATLAB code) and the FORCESPRO solver. Additionally, it is used to configure the build process of the Simulink® model. The FORCESPRO Simulink® block creation script handles the generation of the ready-to-use FORCESPRO Simulink® block from the rest components of the interface as well as defining the necessary properties for the block.
10.3. S-Function vs Coder interface¶
The two interfaces share a lot of similarities as their common purpose is to be used to perform a Simulink® model simulation/execution and as such any of the two can be used. However, there are also certain differences that distinguish them and make each of them either more or less suitable for specific use cases.
In more detail:
The S-Function interface does not support in-lining of the generated code while the Coder interface does. As a result, the Coder interface can generate faster code compared to the S-Function interface.
The Coder interface can only support a Fixed-Step solver for the Simulink® model, while the S-Function interface can support both a Fixed-Step solver and a Variable-Step solver.
The S-Function interface requires custom configuration for it to be integrated into the build process of the Simulink® model code generation, while the Coder interface, other than the inclusion of the Simulink® block in the Simulink® model, handles all dependencies automatically.
The Coder interface requires the MATLAB Coder Toolbox for a Simulink® model simulation while the S-Function interface can work without other dependencies. For code generation of a Simulink® block, the Simulink® Coder Toolbox is already assumed to be available, therefore both interfaces’ requirements are fulfilled.