Reduce Global Variables in Nonreusable Subsystem Functions - MATLAB & Simulink (original) (raw)

Main Content

Global variables can increase memory requirements and reduce execution speed. To reduce global RAM for a nonreusable subsystem, you can generate a function interface that passes data through arguments instead of global variables. The Subsystem block parameterFunction interface provides this option. To compare the outputs for theFunction interface options, generate a function for a subsystem with avoid-void interface, and then generate a function with arguments.

Generate void-void Function

By default, when you configure a Subsystem block as a nonreusable function, it generates a void-void interface.

  1. Open the example modelRollAxisAutopilot.
    openExample('RollAxisAutopilot');
  2. Right-click the subsystem RollAngleReference. From the list select Block Parameter (Subsystem).
  3. In the Block Parameters dialog box, confirm that the Treat as atomic unit check box is selected.
  4. Click the Code Generation tab and set the Code interface packaging parameter to Nonreusable function.
  5. The Function interface parameter is already set tovoid_void.
  6. Click Apply and OK.
  7. Repeat steps 2–6, for the other subsystems HeadingMode andBasicRollMode.
  8. Generate code and the static code metrics report forRollAxisAutopilot. This model is configured to generate a code generation report and to open the report automatically. For more information, see Generate Static Code Metrics Report for Simulink Model.

In the code generation report, in RollAxisAutopilot.c, the generated code for subsystem RollAngleReference contains avoid-void function definition:

static void RollAngleReference(void) { ... }

In the static code metrics report, navigate to Global Variables. With thevoid_void option, the number of bytes for global variables is47.

Next, generate the same function with the Allow arguments (Optimized) option to compare the results.

Generate Function with Arguments

To reduce global RAM, improve ROM usage and execution speed, generate a function that allows arguments:

  1. Open the Subsystem Block Parameter dialog box forRollAngleReference.
  2. Click the Code Generation tab. Set the parameter to Allow arguments (Optimized).
  3. Click Apply and OK.
  4. Repeat steps 2 and 3, for the other subsystems HeadingMode andBasicRollMode.
  5. Generate code and the static code metrics report forRollAxisAutopilot.

In the code generation report, in RollAxisAutopilot.c, the generated code for subsystem RollAngleReference now has arguments:

static real32_T RollAngleReference(real32_T rtu_Phi,... boolean_T rtu_AP_Eng,... real32_T rtu_Turn_Knob) { ... }

In the static code metrics report, navigate to Global Variables. With theAllow arguments option set, the total number of bytes for global variables is now 39 bytes.

In some cases, when generating optimized code, the code generator might not generate a function that has arguments. To generate a predictable function interface that has arguments, set to Allow arguments (Match graphical interface).

See Also

Topics