Real-Time Workshop User's Guide    

Creating an S-Function Block from a Subsystem

This section demonstrates how to extract a subsystem from a model and generate a reusable S-function component from it.

Figure 10-1 illustrates SourceModel, a simple model that inputs signals to a subsystem. Figure 10-2 illustrates the subsystem, SourceSubsys. The signals, which have different widths and sample times, are:

Figure 10-1: SourceModel

Figure 10-2: SourceSubsys

Our objective is to extract SourceSubsys from the model and build an S-Function block from it, using the S-function target. We want the S-Function block to perform identically to the subsystem from which it was generated.

Note that in this model, SourceSubsys inherits sample times and signal widths from its input signals. If an S-Function block is built from SourceSubsys, without explicitly setting input widths and sample times, the new block will inherit its sample times and signal widths from the model in which it is placed.

In this example, however, we want the S-Function block to retain the properties of SourceSubsys as it exists in SourceModel. Before building the subsystem as a separate S-function component, the inport sample times and widths must be set explicitly. In addition, the solver parameters of the S-function component must be the same as those of the original model. This ensures that the generated S-function component will operate identically to the original subsystem.

To build SourceSubsys as an S-function component:

  1. Create a new model and copy/paste SourceSubsys into the empty window.
  2. Set the signal widths and sample times of inports inside SourceSubsys such that they match those of the signals in the original model. Inport 1, Filter, has a width of 1 and a a sample time of 1. Inport 2, Xferfcn, has a width of 1 and a sample time of 0.5. Inport 3, offsets, has a width of 2 and an inherited sample time of 1.
  3. The generated S-Function block should have three inports and one outport. Connect inports and an outport to SourceSubsys, as shown below.

    Note that the correct signal widths and sample times propagate to these ports.

  1. Set the solver type, mode, and other solver parameters such that they are identical to those of the source model.
  2. Save the new model.
  3. Open the Simulation Parameters dialog and click the Real-Time-Workshop tab. On the Real-Time-Workshop page, select Target configuration from the Category menu.
  4. Click the Browse button to open the System Target Browser. Select the S-function target in the System Target Browser, and click OK. The Real-Time-Workshop page parameters should appear as below.

  1. Select RTW S-function code generation options from the Category menu. Make sure that Create New Model is selected.

    When this option is selected, the build process creates a new model after it builds the S-function component. The new model contains an S-Function block, linked to the S-function component.

  1. Click Apply if necessary and select Target configuration from the Category menu.
  2. Click Build.
  3. Real-Time Workshop builds the S-function component in the working directory. After the build, a new model window displays.

  1. You can now copy the Real-Time Workshop S-Function block from the new model and use it in other models or in a library. Figure 10-3 shows the S-Function block plugged in to the original model. Given identical input signals, the S-Function block will perform identically to the original subsystem.

Figure 10-3: Generated S-Function Plugged into SourceModel

Note that the speed at which the S-Function block executes is typically faster than the original model. This difference in speed is more pronounced for larger and more complicated models. By using generated S-functions, you can increase the efficiency of your modeling process.


 Intellectual Property Protection Tunable Parameters in Generated S-Functions