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.
- Open the example model
RollAxisAutopilot
.
openExample('RollAxisAutopilot'); - Right-click the subsystem
RollAngleReference
. From the list selectBlock Parameter (Subsystem)
. - In the Block Parameters dialog box, confirm that the Treat as atomic unit check box is selected.
- Click the Code Generation tab and set the Code interface packaging parameter to
Nonreusable function
. - The Function interface parameter is already set to
void_void
. - Click Apply and OK.
- Repeat steps 2–6, for the other subsystems
HeadingMode
andBasicRollMode
. - Generate code and the static code metrics report for
RollAxisAutopilot
. 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:
- Open the Subsystem Block Parameter dialog box for
RollAngleReference
. - Click the Code Generation tab. Set the parameter to
Allow arguments (Optimized)
. - Click Apply and OK.
- Repeat steps 2 and 3, for the other subsystems
HeadingMode
andBasicRollMode
. - Generate code and the static code metrics report for
RollAxisAutopilot
.
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)
.