MATLAB: Does simulating the model containing a compiled S-Function result in unexpected behavior in Simulink 7.3 (R2009a)

acceleratorsimulink

I created an S-Function using S-Function Builder to read the COM port and my simulation runs correctly. However, if I compile this S-Function into a new S-Function using RTW, the simulation crashes.

Best Answer

The crash occurs in this case, because a necessary value stored in PWork was not initialized in the compiled S-Function. The initialization was performed in the mdlStart function of the original S-Function. However, mdlStart for this subsystem is never executed in the new S-Function, as a result of how the child S-Function is incorporated when generating the new S-Function from the parent model.
An S-Function may be either an inlined or a noninlined S-Function. The following explains the differences in using an inlined or noninlined child S-Function when generating code from a parent model:
a) Inlined S-Function:
In this case, the code for the parent model is combined with code for the child block into a single MEX-file. This MEX-file defines the behavior of both the parent model and the child block. However, the code used for the child block is NOT the same source code that was used to make the MEX-file for the child S-Function. Instead, this code is produced by the child block's TLC file. An S-function TLC file governs how RTW emits code for the S-function block.
This does not work in this case, because the TLC file did not define code to initialize the pointer used by the childname_Outputs_wrapper function.
b) Noninlined S-Function
In this case, code generation results in a MEX-file for the parent model. However, the child block's functionality is still defined by the MEX-file of the child S-Function and is called from the parent's MEX-file.
This works for your model, because the child S-Function's MEX-file performs the necessary initialization of the PWork data.
When using the rtwsfcn target, the presence or absence of a child.tlc file determines whether or not the child will become an inlined S-Function.
The following documents describe these two types of S-Functions and some of the tradeoffs involved in selecting either inlined or noninlined S-functions.
To clarify, if the new S-Function is generated with the child as a noninlined S-Function, the simulation will call the mdlInitializeSizes, mdlInitializeSampleTimes, mdlStart, mdlOutputs, etc. functions inside the child.mexw32 file. However, this MEX-file is not used if the child is incorporated as a noninlined S-Function. Instead, the child.tlc file is used to determine the code that is executed when each of these functions are called.
You can resolve this issue using either of following three options:
1) Use model referencing instead of generating an S-Function in order to incorporate parent.mdl in a larger simulation. This is the recommended way to reuse models in multiple simulations. You can read more about model referencing in the following documents:
2) If you would like to generate an S-Function from parent.mdl, the quickest method is to remove or rename the child.tlc file and then generate code. The child S-Function will be incorporated as a noninlined S-Function.
A benefit of using noninlined S-functions is that you do not have to write TLC code, however, non-inlined S-Functions are not suitable for embedded code.
If you remove the TLC file, you will notice that the following additional code
SimStruct *rts = ssGetSFunction(S, 0);
sfcnStart(rts);
if (ssGetErrorStatus(rts) != NULL)
return;
is generated within the mdlStart function defined in parent_sf.c. This code ensures that the mdlStart functions of all the S-Function blocks in your model will be executed. In your case, this will find and call the mdlStart function within the child.mexw32 file.
3) If you would like to use the child S-Function as in inlined S-Function, you will have to modify the child.tlc file to include all the custom functionality that you would like to execute in mdlStart and mdlOutputs. In addition, inlined S-functions do not allocate any Simstruct structures, therefore you cannot use work vectors with inlined S-Functions. Instead of storing the address to the communications port in PWork, you will have to create a global variable to store this. You will also have to modify childname_Outputs_wrapper to access this global variable instead of the PWork inside the SimStruct.