MATLAB Examples

Generate Tunable Initial Condition Structure for Bus Signal

When you use a MATLAB® structure to specify initialization values for the signal elements in a bus, you can create a tunable global structure in the generated code.

If you set Configuration Parameters > Optimization > Signals and Parameters > Default parameter behavior to Tunable, the initial condition appears as a tunable substructure of the global parameters structure.

Whether you set Default parameter behavior to Tunable or Inlined, you can specify the initial condition by using a tunable Simulink.Parameter object whose value is a structure. If you apply a storage class other than Auto to the parameter object, the structure is tunable in the generated code.

To generate efficient code by avoiding data type mismatches between the structure and the bus signal, use either:

  • Typed expressions to specify the values of the structure fields. Match the data type of each field with the data type of the corresponding signal element.
  • A Simulink.Bus object to control the data types of the structure fields and the signal elements.

For basic information about using structures to initialize bus signals, and to decide how to control field data types, see docid:simulink_ug.bsff8zh.

Contents

Generate Tunable Initial Condition Structure

This example shows how to use a tunable structure parameter to initialize a virtual bus signal.

Open the example model rtwdemo_tunable_init_struct and configure it to show the generated names of blocks.

In the model, select View > Model Data.

On the Inports/Outports tab, the Data Types column shows that each Inport block in the model uses a different output data type.

Open the Bus Creator block dialog box. The block output is a virtual bus.

In the Configuration Parameters dialog box, open the Optimization > Signals and Parameters pane. The configuration parameter Default parameter behavior is set to Tunable. By default, block parameters, including initial conditions, appear in the generated code as tunable fields of the global parameters structure.

In the Model Data Editor, inspect the States tab.

For the Unit Delay block, set Initial Value to a structure that specifies an initial condition for each of the three signal elements. To generate efficient code, match the data types of the structure fields with the data types of the corresponding signal elements. For example, set Initial Value to the expression struct('thermocpl',15.23,'magFlow',uint32(79),'posSwitch',false).

set_param('rtwdemo_tunable_init_struct/Unit Delay','InitialCondition',...
'struct(''thermocpl'',15.23,''magFlow'',uint32(79),''posSwitch'',false)')

Generate code from the example model.

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

In the code generation report, view the file rtwdemo_tunable_init_struct_types.h. The code defines a structure type whose fields use the data types that you specified in the struct expression.

#ifndef DEFINED_TYPEDEF_FOR_struct_mqGi1jsItE0G7cf1bNqMu_
#define DEFINED_TYPEDEF_FOR_struct_mqGi1jsItE0G7cf1bNqMu_

typedef struct {
  real_T thermocpl;
  uint32_T magFlow;
  boolean_T posSwitch;
} struct_mqGi1jsItE0G7cf1bNqMu;

View the file rtwdemo_tunable_init_struct.h. The struct type definition of the global parameters structure contains a substructure, UnitDelay_InitialCondition, which represents the Initial condition parameter of the Unit Delay block.

struct P_rtwdemo_tunable_init_struct_T_ {
  struct_mqGi1jsItE0G7cf1bNqMu UnitDelay_InitialCondition;/* Mask Parameter: UnitDelay_InitialCondition

View the file rtwdemo_tunable_init_struct_data.c. This source file allocates memory for the global parameters structure. The substructure UnitDelay_InitialCondition appears.

/* Block parameters (auto storage) */
P_rtwdemo_tunable_init_struct_T rtwdemo_tunable_init_struct_P = {
  /* Mask Parameter: UnitDelay_InitialCondition
   * Referenced by:
   *   '<Root>/Unit Delay'
   *   '<Root>/Unit Delay'
   *   '<Root>/Unit Delay'
   */
  {
    15.23,
    79U,
    0
  },

View the file rtwdemo_tunable_init_struct.c. The model initialization function uses the fields of the substructure to initialize the block states.

  /* InitializeConditions for UnitDelay: '<Root>/Unit Delay' */
  rtwdemo_tunable_init_struct_DW.UnitDelay_1_DSTATE =
    rtwdemo_tunable_init_struct_P.UnitDelay_InitialCondition.thermocpl;
  rtwdemo_tunable_init_struct_DW.UnitDelay_2_DSTATE =
    rtwdemo_tunable_init_struct_P.UnitDelay_InitialCondition.magFlow;
  rtwdemo_tunable_init_struct_DW.UnitDelay_3_DSTATE =
    rtwdemo_tunable_init_struct_P.UnitDelay_InitialCondition.posSwitch;

Use Bus Object to Specify Data Types

If you create a bus object, you can use it to specify the data type of the bus signal and the tunable initial condition structure. Before code generation, the Simulink.Parameter object casts the values of the structure fields to the data types of the signal elements. For basic information about creating bus objects and using them in models, see docid:simulink_ug.f15-109955.

Open the example model rtwdemo_init_struct_busobj and configure it to show the generated names of blocks.

In the base workspace, double-click the Simulink.Bus object ComponentData. The object defines three signal elements: thermocpl, magFlow, and posSwitch. The elements each use a different data type.

In the model, open the Model Data Editor (View > Model Data). The Inports/Outports tab shows that for the Inport block DataIn, the output data type (Data Type column) is set to Bus: ComponentData.

At the command prompt, create the structure parameter initStruct. You can specify the field values by using untyped expressions. To improve readability, specify the field posSwitch with a Boolean value.

initStruct = struct(...
    'thermocpl',15.23,...
    'magFlow',79,...
    'posSwitch',false ...
    );
initStruct = Simulink.Parameter(initStruct);

In the Model Data Editor, inspect the Parameters tab.

In the model, click the Unit Delay block. The Model Data Editor highlights the row that corresponds to the Initial condition parameter of the block.

In the Model Data Editor, set the parameter value (Value column) to initStruct.

Click the Show/refresh additional information button. The parameter object, initStruct, appears in the data table as a row.

Use the Data Type column to set the data type of initStruct to Bus: ComponentData.

initStruct.DataType = 'Bus: ComponentData';

Set the Change View drop-down list to Code.

Use the Storage Class column to apply the storage class ExportedGlobal to initStruct.

initStruct.StorageClass = 'ExportedGlobal';

Generate code from the example model.

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

In the code generation report, view the file rtwdemo_init_struct_busobj_types.h. The code creates a structure type ComponentData whose fields use the data types in the bus object.

#ifndef DEFINED_TYPEDEF_FOR_ComponentData_
#define DEFINED_TYPEDEF_FOR_ComponentData_

typedef struct {
  real_T thermocpl;
  uint32_T magFlow;
  boolean_T posSwitch;
} ComponentData;

View the file rtwdemo_init_struct_busobj.c. The code creates a global variable to represent the tunable parameter object initStruct.

/* Exported block parameters */
ComponentData initStruct = {
  15.23,
  79U,
  0
} ;                                    /* Variable: initStruct

The model initialization function uses the structure fields to initialize the block states.

  /* InitializeConditions for UnitDelay: '<Root>/Unit Delay' */
  rtwdemo_init_struct_busobj_DW.UnitDelay_1_DSTATE = initStruct.thermocpl;
  rtwdemo_init_struct_busobj_DW.UnitDelay_2_DSTATE = initStruct.magFlow;
  rtwdemo_init_struct_busobj_DW.UnitDelay_3_DSTATE = initStruct.posSwitch;

To change the data type of any of the three signal elements, specify the new type in the bus object. The signal element in the model uses the new type. Before simulation and code generation, the parameter object initStruct casts the corresponding structure field to the new type.