| Real-Time Workshop | ![]() |
Automated S-Function Generation
The Generate S-function feature automates the process of generating an S-function from a subsystem. In addition, the Generate S-function feature presents a display of parameters used within the subsystem, and lets you declare selected parameters tunable.
As an example, consider SourceSubsys, the subsystem illustrated in Figure 10-2. Our objective is to automatically extract SourceSubsys from the model and build an S-Function block from it, as in the previous example. In addition, we want to set the gain factor of the Gain block within SourceSubsys to the workspace variable K (as illustrated below) and declare K as a tunable parameter.
To auto-generate an S-function from SourceSubsys with tunable parameter K:
Alternatively, you can choose Generate S-function from the Real-Time Workshop submenu of the subsystem block's context menu.
K is declared tunable.
When you select a parameter in the upper pane, the lower pane shows all the blocks that reference the parameter, and the parent system of each such block.
Figure 10-4: The Generate S-Function Window
subsys_blk, where subsys is the name of the subsystem from which the block was generated.
subsys, is stored in the working directory. The generated source code for the S-function is written to a build directory, subsys_sfcn_rtw. Additionally a stub file, subsys_sf.c, is written to the working directory. This file simply contains an include directive that you can use to interface other C code to the generated code.
Note that if the Use Embedded Coder option was selected, the build directory is named subsys_ert_rtw.
untitled generated model does not persist, unless you save it via the File menu.
The following code fragment, from the mdlOutputs routine of the generated S-function code (in SourceSubsys_sf.c), illustrates how the tunable variable K is referenced via calls to the MEX API.
static void mdlOutputs(SimStruct *S, int_T tid) ... /* Expression for <Root>/Out1 incorporates: */ /* Gain Block: <S1>/Gain */ /* Sum Block: <S1>/Sum */ /* Inport Block: <Root>/offsets */ /* Outport Block: <Root>/Out1 */ ((real_T *)ssGetOutputPortSignal(S,0))[0] = ((*(real_T *)(mxGetData(K(S)))) * (rtb_Product + *(((real_T**)ssGetInputPortSignalPtrs(S, 2))[0]))); ((real_T *)ssGetOutputPortSignal(S,0))[1] = ((*(real_T *)(mxGetData(K(S)))) * (rtb_Product + *(((real_T**)ssGetInputPortSignalPtrs(S, 2))[1])));
| Note In automatic S-function generation, the Use Value for Tunable Parameters option is always set to its default value (off). |
| Tunable Parameters in Generated S-Functions | Restrictions | ![]() |