Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Creating Subsystems

Introduction

The Real-Time Workshop product allows you to control how code is generated for any nonvirtual subsystem. The categories of nonvirtual subsystems are:

See Systems and Subsystems in the Simulink documentation, and run the sl_subsys_semantics demo for more information on nonvirtual subsystems and atomic subsystems.

You can control the code generated from nonvirtual subsystems as follows:

Generating Code and Executables from Subsystems

The Real-Time Workshop software can generate code and build an executable from any subsystem within a model. The code generation and build process uses the code generation and build parameters of the root model.

To generate code and build an executable from a subsystem,

  1. Set up the desired code generation and build parameters in the Configuration Parameters dialog box, just as you would for code generation from a model.

  2. Select the desired subsystem block.

  3. Right-click the subsystem block and select Build Subsystem from the Real-Time Workshop submenu of the subsystem block's context menu.

    Alternatively, you can select Build Subsystem from the Real-Time Workshop submenu of the Tools menu. This menu item is enabled when a subsystem is selected in the current model.

      Note   If the model is operating in external mode when you select Build Subsystem, the Real-Time Workshop build process automatically turns off external mode for the duration of the build, then restores external mode upon its completion.

  4. The Build Subsystem window opens. This window displays a list of the subsystem parameters. The upper pane displays the name, class, and storage class of each variable (or data object) that is referenced as a block parameter in the subsystem. 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.

    The StorageClass column contains a popup menu for each row. The menu lets you set the storage class of any parameter or inline the parameter. To inline a parameter, select the Inline option from the menu. To declare a parameter to be tunable, set the storage class to any value other than Inline.

    In the previous figure, the parameter K2 is inlined, while the other parameters are tunable and have various storage classes.

    See Parameter Considerations and Simulink Data Object Considerations for more information on tunable and inlined parameters and storage classes.

  5. After selecting tunable parameters, click the Build button. This initiates the code generation and build process.

  6. The build process displays status messages in the MATLAB Command Window. When the build completes, the generated executable is in your working directory. The name of the generated executable is subsystem.exe (on PC platforms) or subsystem (on The Open Group UNIX® platforms), where subsystem is the name of the source subsystem block.

    The generated code is in a build subdirectory, named subsystem_target_rtw, where subsystem is the name of the source subsystem block and target is the name of the target configuration.

When you generate code for a subsystem, you can generate an S-function by selecting Tools > Real-Time Workshop > Generate S-function, or you can use a right-click subsystem build. See Automated S-Function Generation and Generating S-Function Wrappers for more details.

Real-Time Workshop Subsystem Build Limitations

The following limitations apply to building subsystems using the Real-Time Workshop software:

Nonvirtual Subsystem Code Generation Options

For any nonvirtual subsystem, you can choose the following code generation options from the Real-Time Workshop system code menu in the subsystem Block parameters dialog box:

The following sections discuss these options further.

Auto Option

The Auto option is the default, and is generally appropriate. Auto causes the Real-Time Workshop code generator to inline the subsystem when there is only one instance of it in the model. When multiple instances of a subsystem exist, the Auto option results in a single copy of the function whenever possible (as a reusable function). Otherwise, the result is as though you selected Inline (except for function call subsystems with multiple callers, which is handled as if you specified Function). Choose Inline to always inline subsystem code, or Function when you specifically want to generate a separate function without arguments for each instance, optionally in a separate file.

To use the Auto option,

  1. Select the subsystem block. Then select Subsystem Parameters from the Simulink model editor Edit menu. The Block Parameters dialog box opens, as shown in the next figure.

    Alternatively, you can open the Block Parameters dialog box by

    • Shift-double-clicking the subsystem block

    • Right-clicking the subsystem block and selecting Block parameters from the menu

  2. If the subsystem is virtual, select Treat as atomic unit as shown in the next figure. This makes the subsystem nonvirtual, and the Real-Time Workshop system code option becomes enabled.

    If the system is already nonvirtual, the Real-Time Workshop system code option is already enabled.

  3. Select Auto from the Real-Time Workshop system code menu as shown in the figure below.

  4. Click Apply and close the dialog box.

    The border of the subsystem thickens, indicating that it is nonvirtual.

Auto Optimization for Special Cases.   Rather than reverting to Inline, the Auto option can optimize code in special situations in which identical subsystems contain other identical subsystems, by both reusing and inlining generated code. Suppose a model, such as the one shown in Reuse of Identical Nested Subsystems with the Auto Option, contains identical subsystems A1 and A2. A1 contains subsystem B1, and A2 contains subsystem B2, which are themselves identical. In such cases, the Auto option causes one function to be generated which is called for both A1 and A2, and this function contains one piece of inlined code to execute B1 and B2, ensuring that the resulting code will run as efficiently as possible.

Reuse of Identical Nested Subsystems with the Auto Option

Inline Option

As noted above, you can choose to inline subsystem code when the subsystem is nonvirtual (virtual subsystems are always inlined).

Exceptions to Inlining.   There are certain cases in which the Real-Time Workshop code generator does not inline a nonvirtual subsystem, even though the Inline option is selected. These cases are

To generate inlined subsystem code,

  1. Select the subsystem block. Then select Subsystem Parameters from the Simulink model editor Edit menu. The Block Parameters dialog box opens.

    Alternatively, you can open the Block Parameters dialog box by

    • Shift-double-clicking the subsystem block

    • Right-clicking the subsystem block and selecting Block parameters from the menu

  2. If the subsystem is virtual, select Treat as atomic unit as shown in the next figure. This makes the subsystem atomic, and the Real-Time Workshop system code menu becomes enabled.

    If the system is already nonvirtual, the Real-Time Workshop system code menu is already enabled.

  3. Select Inline from the Real-Time Workshop system code menu as shown in the figure below.

  4. Click Apply and close the dialog box.

When you generate code from your model, the Real-Time Workshop code generator writes inline code within model.c or model.cpp (or in its parent system's source file) to perform subsystem computations. You can identify this code by system/block identification tags, such as the following.

/* Atomic SubSystem Block: <Root>/AtomicSubsys1 */

See Tracing Generated Code for more information on system/block identification tags.

Function Option

Choosing the Function or Reusable function option lets you direct the Real-Time Workshop code generator to generate a separate function and optionally a separate file for the subsystem. When you select the Function option, two additional options are enabled:

The figure below shows the Block Parameters dialog box with the Function option selected, with Real-Time Workshop file name options set to User specified, and with a name specified for the generated file.

Subsystem Function Code Generation Option with User-Specified File Name

Real-Time Workshop Function Name Options Menu.   This menu offers the following choices, but the resulting identifiers are also affected by which General code appearance options are in effect for the model:

Real-Time Workshop File Name Options Menu.   This menu offers the following choices:

To generate both a separate subsystem function and a separate file,

  1. Select the subsystem block. Then select Subsystem Parameters from the Simulink model editor Edit menu, to open the Block Parameters dialog box.

    Alternatively, you can open the Block Parameters dialog box by

    • Shift-double-clicking the subsystem block

    • Right-clicking the subsystem block and selecting Block parameters from the menu.

  2. If the subsystem is virtual, select Treat as atomic unit. The Real-Time Workshop system code menu becomes enabled.

    If the system is already nonvirtual, the Real-Time Workshop system code menu is already enabled.

  3. Select Function from the Real-Time Workshop system code menu as shown in Subsystem Function Code Generation Option with User-Specified File Name.

  4. Set the function name, using the Real-Time Workshop function name options described in Real-Time Workshop Function Name Options Menu.

  5. Set the file name, using any Real-Time Workshop file name option other than Auto (options are described in Real-Time Workshop File Name Options Menu).

    Subsystem Function Code Generation Option with User-Specified File Name shows the use of the User Specified file name option.

  6. Click Apply and close the dialog box.

Modularity of Subsystem Code

Code generated from nonvirtual subsystems, when written to separate files, is not completely independent of the generating model. For example, subsystem code may reference global data structures of the model. Each subsystem code file contains appropriate include directives and comments explaining the dependencies. The Real-Time Workshop software checks for cyclic file dependencies and warns about them at build time. For descriptions of how generated code is packaged, see Generated Source Files and File Dependencies.


[1] VxWorks is a registered trademark of Wind River® Systems, Inc.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS