Main Content

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 rtwdemo_roll, configure a PID control parameter to appear in the code as a global variable whose value you can tune.

  1. Open the BasicRollMode subsystem.

  2. Open the Model Data Editor, On the Modeling tab, click Model Data Editor.

  3. In the Model Data Editor, select the Parameters tab.

  4. 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.

  5. In the Source column, click IntGain. That Gain block appears highlighted in the model diagram.

  6. In the Value column, next to intGain, click the action button (button with three vertical dots) and select Explore.

  7. Convert the model workspace variable to a parameter object. In the Model Explorer, right-click intGain and select Convert to parameter object.

  8. In the Dialog pane, on the Code Generation tab, click Configure in Coder App.

  9. In the Code Mappings editor, on the Parameters tab, change the Storage Class setting for intGain to Model 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.

  10. Save the model and regenerate the code.

    Minimize the code generation report window for exploration later in this tutorial.

  11. In the Code view:

    • Search for intGain.

    • In rtwdemo_roll.c, place your cursor over the ellipsis menu over the P in the highlighted code P.intGain. In the model editor, notice that the Code view highlights the block corresponding to the generated code.

    • To see the parameter object definition for intGain in rtwdemo_roll_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.