Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Using Signal Objects to Initialize Signals and Discrete States

You can use Simulink signal objects to initialize signals and discrete states with user-defined values for simulation and code generation. Data initialization increases application reliability and is a requirement of safety critical applications. Initializing signals for both simulation and code generation can expedite transitions between phases of Model-Based Design.

For details on simulation behavior, see Initialization Behavior Summary for Signal Objects in the Simulink documentation.

Specifying an Initial Value for a Signal Object

You can use signal objects that have a storage class other than 'auto' or 'SimulinkGlobal' to initialize

The initial value is the signal or state value before a simulation takes its first time step.

To specify an initial value, use the Model Explorer or MATLAB commands to do the following:

  1. Create the signal object.

    Model Explorer

    MATLAB Command

    S1=Simulink.Signal;

    The name of the signal object must be the same as the name of the signal that the object is initializing. Although not required, consider setting the Signal name must resolve to Simulink signal object option in the Signal Properties dialog box. This setting ensures consistency between signal objects in the MATLAB workspace and the signals that appear in your model.

    Consider using the Data Object Wizard to create signal objects. The Data Object Wizard searches a model for signals for which signal objects do not exist. You can then selectively create signal objects for multiple signals listed in the search results with a single operation. For more information about the Data Object Wizard, see Data Object Wizard in the Simulink documentation.

  2. Set the signal object's storage class to a value other than 'auto' or 'SimulinkGlobal'.

    Model Explorer

    MATLAB Command

    S1.RTWInfo.StorageClass='ExportedGlobal';
  3. Set the initial value. You can specify any MATLAB string expression that evaluates to a double numeric scalar value or array.

     Model ExplorerMATLAB Command
    Valid1.5
    [1 2 3]
    1+0.5
    foo = 1.5;
    s1.InitialValue = 'foo';
    Invaliduint(1)foo = '1.5';
    s1.InitialValue = 'foo';

    If necessary, the Simulink engine converts the initial value to ensure type, complexity, and dimension consistency with the corresponding block parameter value. If you specify an invalid value or expression, an error message appears when you update the model.

    Model Explorer

    MATLAB Command

    S1.InitialValue='0.5'

The following example shows a signal object specifying the initial output of an enabled subsystem.

Signal s is initialized to 4.5. Note that to avoid a consistency error, the initial value of the enabled subsystem's Outport block must be [ ] or 4.5.

Signal Object Initialization in Generated Code

The initialization behavior for code generation is the same as that for model simulation with the following exceptions:

For details on initialization behavior for different types of signals and discrete states, see Initialization Behavior Summary for Signal Objects in the Simulink documentation.

When you initialize Simulink signal objects in a model during code generation, the corresponding initialization statements are placed in model.c or model.cpp in the model's initialize code.

For example, consider the demo model rtwdemo_sigobj_iv.

If you create and initialize signal objects in the base workspace, the Real-Time Workshop product places initialization code for the signals in the file rtwdemo_sigobj_iv.c under the rtwdemo_sigobj_iv_initialize function, as shown below.

 /* Model initialize function */
    
     void rtwdemo_sigobj_iv_initialize(boolean_T firstTime)

     {
        .
        .
        .
 /* exported global signals */
 S3 = -3.0;

 S2 = -2.0;
         .
         .
         .
/* exported global states */
X1 = 0.0;
X2 = 0.0;

/* external inputs */
S1 = -4.5;
         .
         .
         .

The following code shows the initialization code for the enabled subsystem's Unit Delay block state X1 and output signal S2.

void MdlStart(void) {
          .
          .
          .
 /* InitializeConditions for UnitDelay: '<S2>/Unit Delay' */
  X1 = aa1;

 /* Start for enable system: '<Root>/Enabled Subsystem (state X1 inside)' */

  /* virtual outports code */

  /* (Virtual) Outport Block: '<S2>/Out1' */

  S2 = aa2;

}

Also note that for an enabled subsystem, such as the one shown in the preceding model, the initial value is also used as a reset value if the subsystem's Outport block parameter Output when disabled is set to reset. The following code from rtwdemo_sigobj_iv.c shows the assignment statement for S3 as it appears in the model output function rtwdeni_sigobj_iv_output.

/* Model output function */

static void rtwdemo_sigobj_iv_output(void)
{
         .
         .
         .
 /* Disable for enable system: '<Root>/Enabled Subsystem (state X1 inside)' */

    /* (Virtual) Outport Block: '<S2>/Out1' */

    S2 = aa2;

Tunable Initial Values

If you specify a tunable parameter in the initial value for a signal object, the parameter expression is preserved in the initialization code in model.c.

For example, if you configure parameter df to be tunable for model signal_iv and you initialize the signal object for discrete state X1 with the expression df*2, the following initialization code appears for signal object X1 in signal_iv.c.

void MdlInitialize(void) {

/* InitializeConditions for UnitDelay: '<Root>/Unit Delay X1=2' */
X1 = (tunable_param_P.df * 2.0);
}

For more information about the treatment of tunable parameters in generated code, see Parameter Considerations.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS