What do you mean by "Inlining s-function"?
MATLAB: What do you mean by “Inlining s-function”
rtwsimulinksimulink coder
Related Solutions
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.
Dear Glen,
It seems Mathworks has changed the API access for CGType massively since R2017b. The new API to query the CGType.Member.Name is LibCGTypeMemberName(cgTypeIdx, memberIdx) found in the following file.
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc:849:%% Function: LibCGTypeMemberName ===========================================
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-850-%% Abstract:
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-851-%% Returns the name of a member of a struct CGType
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc-852-%%
MATLABROOT/rtw/c/tlc/lib/dtypelib.tlc:853:%function LibCGTypeMemberName(cgTypeIdx, memberIdx) void
I am not sure if you are still looking for a solution to this question. But, I believe it is one of the only questions relevant to the recent change in the CGType APIs that I found on the Google searches and yet unanswered.
I would suggest finding the rest of the changed APIs by grep-ing through the installed TLC files in R2018a.
Hope this helps!
Siraj
Related Question
- Is there a TLC function “LibBlockIWorkAddr” or “LibBlockRWorkAddr” in the Real-Time Workshop
- Does Simulink Accelerator not use all TLC methods even if I use the SS_OPTION_USE_TLC_WITH_ACCELERATOR option in Real-Time Workshop 7.4 (R2009b)
- Model Reference Target Update Error
- Do I get an “Attempt to call a non-function value” error when implementing the example TLC script in the Real-Time Workshop 6.5 (R2006b) documentation
Best Answer