MATLAB Examples

Generate Tunable Initial Conditions

You can represent initial conditions for signals and states by creating tunable global variables in the generated code. These variables allow you to restart an application by using initial conditions that are stored in memory.

If you set Configuration Parameters > Optimization > Signals and Parameters > Default parameter behavior to Tunable, initial conditions appear as tunable fields of the global parameters structure.

Whether you set Default parameter behavior to Tunable or Inlined, you can use a tunable parameter to specify the InitialValue property of a signal object or the Initial condition parameter of a block. For basic information about tunable parameters, see docid:rtw_ug.f1023655.

This example shows how to use tunable parameters to specify initial conditions for signals and states.

Contents

Explore Example Model

Open the example model rtwdemo_sigobj_iv and configure it to show the generated names of blocks. The signal S2 uses a Simulink.Signal object in the base workspace.

Double-click the Simulink.Signal object S2 to view its properties. The Initial value property is set to aa2. The object uses the variable aa2 to specify an initial condition for the signal S2. The Storage class property is set to ExportedGlobal. To use a Simulink.Signal object to initialize a signal, the signal object must use a storage class other than Auto or SimulinkGlobal.

On the Optimization > Signals and Parameters pane in the Configuration Parameters dialog box, click Configure. The variable aa2 is a tunable parameter that uses the storage class ExportedGlobal.

In the model, open the Enabled Subsystem. In the Outport block dialog box, the parameter Output when disabled is set to reset. When the subsystem becomes disabled, the output signal S2 resets to the initial value aa2.

Open the Unit Delay block dialog box. On the State Attributes tab, the State name box is set to X1.

Open the Enable block dialog box. The parameter States when enabling is set to reset. When the subsystem transitions from a disabled state to an enabled state, it resets internal block states, such as X1, to their initial values.

In the base workspace, double-click the Simulink.Signal object X1 to view its properties. The Initial value property is set to aa1.

Double-click the Simulink.Parameter object aa1 to view its properties. The Storage class property is set to ExportedGlobal. You can generate tunable initial conditions for block states by using tunable parameters such as aa1 and Simulink.Signal objects such as X1.

Generate and Inspect Code

Generate code with the example model.

### Starting build procedure for model: rtwdemo_sigobj_iv
### Successful completion of build procedure for model: rtwdemo_sigobj_iv

In the code generation report, view the file rtwdemo_sigobj_iv.c. The code uses global variables to represent the block state X1 and the signal S2.

/* Exported block states */
real_T X1;                             /* '<S2>/Unit Delay' */

/* Exported block signals */
real_T S1;                             /* '<Root>/In1' */
real_T S3;                             /* '<Root>/Signal Conversion' */
real_T S2;                             /* '<S2>/Unit Delay' */

The code uses global variables to represent the tunable parameters aa1 and aa2.

/* Exported block parameters */
real_T aa1 = -2.5;                     /* Variable: aa1

real_T aa2 = -2.0;                     /* Variable: aa2

The model initialization function uses the tunable parameter aa1 to initialize the state X1. The function also uses the tunable parameter aa2 to initialize the signal S2.

  /* SystemInitialize for Enabled SubSystem: '<Root>/Enabled Subsystem (state X1 inside)' */
  /* InitializeConditions for UnitDelay: '<S2>/Unit Delay' */
  X1 = aa1;

  /* SystemInitialize for Outport: '<S2>/Out1' */
  S2 = aa2;

In the model step function, when the Enabled Subsystem transitions from a disabled state to an enabled state, the Unit Delay block state X1 resets to its initial value.

  if (rtb_PulseGenerator > 0) {
    if (!rtwdemo_sigobj_iv_DW.EnabledSubsystemstateX1inside_M) {
      /* InitializeConditions for UnitDelay: '<S2>/Unit Delay' */
      X1 = aa1;
      rtwdemo_sigobj_iv_DW.EnabledSubsystemstateX1inside_M = true;
    }

If the Enabled Subsystem becomes disabled during code execution, the algorithm uses the tunable initial condition aa2 to set the value of the signal S2.

  } else {

    if (rtwdemo_sigobj_iv_DW.EnabledSubsystemstateX1inside_M) {
      /* Disable for Outport: '<S2>/Out1' */
      S2 = aa2;
      rtwdemo_sigobj_iv_DW.EnabledSubsystemstateX1inside_M = false;
    }
  }