Joachim Schlosser: Development and Verification of fast C/C++ Simulation Models for the Star12 Microcontroller
Although there is a whole bunch of different blocks, there will always be the need for an engineer to use own, proprietary blocks that are not present in the library of Simulink. So there exists the capability to define blocks, using S-functions. These are computer language descriptions of Simulink blocks. An S-function can be written in MATLAB, C, C++, Ada, or Fortran. We will use C/C++ for our purpose. For usage in models, a block named S-function is dragged from the library into the model. Its dialog box takes the name of the S-function, which has to be also the name of the file, the S-function is compiled in. Additionally, there is a second field, taking the parameters that should be passed to the S-function.
As all other blocks, S-functions process inputs u and states x to receive an output y. It is up to the S-function to perform this task by reacting to the callbacks Simulink invokes. These will be discussed below.
|
|
This diagram can be expressed using equations that show the mathematical relationship between the inputs, states and outputs, taken from [Mat01, p. 20].
| y | = fo(t,x,u) | (output) | ||||
| ẋc | = fd(t,x,u) | (derivative) | ||||
| ydk+1 | = fu(t,x,u) | (update) | ||||
| where x | = xc + xd |
A Simulink model is executed in stages [Mat01]. The first one is the initialization phase, where Simulink incorporates library blocks into the model. Widths, data types and sample times are propagated, parameters are processed and the block execution order is determined. The second phase is the simulation loop. A single pass through the loop is called a simulation step. During a simulation step, Simulink executes all blocks by invoking functions that calculate states, derivatives and outputs of a block.
In Figure 3.13 on page 234 you see the execution stages that all blocks run through. Two terms were not mentioned before: Locate zero crossings is a mechanism that allows solving the problem that occurs when an input signal crosses the zero line. Many functions change their behavior and output dramatically when getting an input zero. In numerical simulation, as it is used here, normally the zero line is passed with the same step size as the rest of the simulation. But this could lead to a mis-behavior in the output vector. Simulink faces this problem by decreasing the step size when being near zero, to allow the model producing correct results. Minor time step is the part of the loop where outputs can be re-calculated. Here the zero crossing detection is done, too.
The S-function has to provide a set of callbacks to allow Simulink directing the simulation. Subsequently there will be a list of the tasks, the callbacks perform:
There are three key concepts that are important for implementing S-functions:
The sample times are usually given in pairs of sample time and offset time. According to [Mat01], the valid sample time pairs are:
[CONTINUOUS_SAMPLE_TIME, 0.0]
[CONTINUOUS_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET] [<discrete_sample_time_period>, <offset>] [VARIABLE_SAMPLE_TIME, 0.0] |
with the following definitions:
CONTINUOUS_SAMPLE_TIME = 0.0
FIXED_IN_MINOR_STEP_OFFSET = 1.0 VARIABLE_SAMPLE_TIME = -2.0 |
The build process of C MEX S-functions is basically not complicated. The source file, in which the callbacks are implemented, has to follow certain rules. So it has to include a given header, define mandatory callbacks and include C source files in the footer. The source file is then compiled and linked with the Matlab/Simulink libraries to get a dynamically loadable library (DLL). This DLL has to have exactly the same name as the included S-function has. Implementation
For the implementation of the Simulink-Octopus interface several tasks had to be accomplished and parts to be written:
All tasks mentioned in this list will be discussed in the following sections on their own, although it is not always possible to explain one functionality without considering the other.