Configure a Model Parameter as a Global Variable for Tuning During Run Time
By default, code generation optimizations eliminate storage for model parameters and most signals that do not participate in the entry-point function interface. To make parameters tunable and related signals accessible, identify them by configuring them explicitly.
In the BasicRollMode
subsystem of model
RollAxisAutopilot
, configure a PID control parameter to appear in the
code as a global variable whose value you can tune.
Open the
BasicRollMode
subsystem.Open the Model Data Editor. On the Modeling tab, click Model Data Editor.
In the Model Data Editor, select the Parameters tab.
In the filter field, type
IntGain
. The Model Data Editor shows a row that corresponds to the Gain parameter and a row that corresponds to a workspace variable.In the Source column, click
IntGain
. That Gain block appears highlighted in the model diagram.In the Value column, next to
intGain
, click the action button (button with three vertical dots) and select Explore.Convert the model workspace variable to a parameter object. In the Model Explorer, right-click
intGain
and selectConvert to parameter object
.In the Dialog pane, on the Code Generation tab, click Configure in Coder App.
In the Code Mappings editor, on the Parameters tab, change the Storage Class setting for
intGain
toModel default
, which indicates that the parameter object prevents code generation optimizations from eliminating storage for the variable. With this setting, the object uses the storage class specified in the Code Mappings editor as the data default for category Model parameters.Save the model and regenerate the code.
Minimize the code generation report window for exploration later in this tutorial.
In the Code view:
Search for
intGain
.In
RollAxisAutopilot.c
, place your cursor over theP
in the highlighted codeP.intGain
.To see the parameter object definition for
intGain
inRollAxisAutopilot_data.c
, click the definition code in the dialog box.
The code that you generate from the model stores the parameter object in memory. Because
you left the default storage class settings in the Code Mapping Editor for category
Model parameters set to Default
, the code
generator determines the storage format, for example, as fields of structures.
Next, use a test harness model and software-in-the-loop (SIL) simulation to compare results of model simulation and generated code.