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:

  1. Click on the subsystem to select it.
  2. Select Generate S-function from the Real-Time Workshop submenu of the Tools menu. This menu item is enabled when a subsystem is selected in the current model.

    Alternatively, you can choose Generate S-function from the Real-Time Workshop submenu of the subsystem block's context menu.

  3. The Generate S-function window is diplayed (see Figure 10-4). This window shows all variables (or data objects) that are referenced as block parameters in the subsystem, and lets you declare them as tunable.
  1. The upper pane of the window displays three columns:

    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

  1. If you have licensed and installed the Real-Time Workshop Embedded Coder, the Use Embedded Coder check box is available, as in Figure 10-4. Otherwise, it is grayed out. When Use Embedded Coder is selected, the build process generates a wrapper S-Function via the Real-Time Workshop Embedded Coder. See the Real-Time Workshop Embedded Coder documentation for further information.
  2. After selecting tunable parameters, click the Build button. This initiates code generation and compilation of the S-function, using the S-function target. The Create New Model option is automatically enabled.
  3. The build process displays status messages in the MATLAB command window. When the build completes, the tunable parameters window closes, and a new untitled model window opens.

  1. The model window contains an S-Function block, subsys_blk, where subsys is the name of the subsystem from which the block was generated.
  1. The generated S-function component, 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.

  1. Note that the untitled generated model does not persist, unless you save it via the File menu.
  2. Note that the generated S-Function block has inports and outports whose widths and sample times correspond to those of the original model.

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.


  Tunable Parameters in Generated S-Functions Restrictions