Block State Storage and Interfacing

Introduction

For certain block types, the Real-Time Workshop® product lets you control how block states in your model are stored and represented in the generated code. Using the State properties tab of a block dialog box, you can

Block State Storage

The discussion of block state storage in this section applies to the following block types:

These block types require persistent memory to store values representing the state of the block between consecutive time intervals. By default, such values are stored in a data type work vector. This vector is usually referred to as the DWork vector. It is represented in generated code as model_DWork, a global data structure. For more information on the DWork vector, see the Target Language Compiler documentation.

If you want to interface a block state to your hand-written code, you can specify that the state is to be stored in a location other than the DWork vector. You do this by assigning a storage class to the block state.

You can also define a symbolic name, to be used in code generation, for a block state.

Block State Storage Classes

The storage class property of a block state specifies how the Real-Time Workshop product declares and stores the state in a variable. Storage class options for block states are similar to those for signals. The available storage classes are

Default Storage Class

Auto is the default storage class. Auto is the appropriate storage class for states that you do not need to interface to external code. States with Auto storage class are stored as members of the Dwork vector.

You can assign a symbolic name to states with Auto storage class. If you do not supply a name, the Real-Time Workshop product generates one, as described in Symbolic Names for Block States.

Explicitly Assigned Storage Classes

Block states with storage classes other than Auto are stored in unstructured global variables, independent of the Dwork vector. These storage classes are appropriate for states that you want to interface to external code. The following storage classes are available for states:

The table in Summary of Signal Storage Class Options gives examples of variable declarations and the code generated for block states with each type of storage class.

The next section explains how to use the State Properties dialog box to assign storage classes to block states.

Using the State Properties Dialog Box to Interface States to External Code

The State Properties tab of the relevant blocks' parameter dialog boxes lets you interface a block's state to external code by assigning the state a storage class other than Auto (that is, ExportedGlobal, ImportedExtern, or ImportedExternPointer).

Set the storage class as follows:

  1. In your block diagram, double-click the desired block. This opens the block dialog box containing two or more tabs, one of which is State properties. Alternatively, you can right-click the block and select Block properties from the context menu.

  2. Click the State Properties tab. The State Properties dialog appears.

  3. Enter a name for the variable to be used to store block state in the State name field.

    The State name field turns yellow to indicate that you changed it.

  4. Click Apply to register the variable name.

    The first two fields beneath the State name, State name must resolve to Simulink signal object and RTW storage class, become enabled.

  5. If the state is to be stored in a Simulink® signal object in the base or model workspace, select State name must resolve to Simulink signal object.

    If you choose this option, you cannot declare a storage class for the state in the block, and the fields below becomes disabled.

  6. Select the desired storage class (ExportedGlobal, ImportedExtern, or ImportedExternPointer) from the RTW storage class menu.

  7. Optional: For storage classes other than Auto, you can enter a storage type qualifier such as const or volatile in the RTW storage type qualifier field. The Real-Time Workshop product does not check this string for errors; whatever you enter is included in the variable declaration.

  8. Click OK or Apply and close the dialog box.

Symbolic Names for Block States

To determine the variable or field name generated for a block's state, you can either

Default Block State Naming Convention

If you do not define a symbolic name for a block state, the Real-Time Workshop product uses the following default naming convention:

BlockType#_DSTATE

where

For example, consider the model shown in the next figure.

Model with Two Discrete Filter Block States

Examine the code generated for the states of the two Discrete Filter blocks. Assume that:

The states of the two Discrete Filter blocks are stored in DWork vectors, initialized as shown in the code below:

/* data type work */
disc_filt_states_M->Work.dwork = ((void *) 
&disc_filt_states_DWork);
    (void)memset((char_T *) &disc_filt_states_DWork, 0,
     sizeof(D_Work_disc_filt_states));
    {
      int_T i;
      real_T *dwork_ptr = (real_T *) 
&disc_filt_states_DWork.DiscFilt_DSTATE;

      for (i = 0; i < 2; i++) {
        dwork_ptr[i] = 0.0;
      }
    }

User-Defined Block State Names

Using the State Properties dialog box, you can define your own symbolic name for a block state. To do this,

  1. In your block diagram, double-click the desired block. This opens the block dialog box, containing two or more tabs, one of which is State properties.

    Alternatively, you can right-click the block and select Block properties from the context menu.

  2. Click the State properties tab.

  3. Enter the symbolic name in the State name field of the State Properties dialog box. For example, enter the state name Top_filter.

  4. Click Apply. The dialog box now looks like this:

  5. Click OK or Cancel to dismiss the block dialog box.

The following state initialization code was generated from the example model shown in figure ???, under the following conditions:

Top_filter is placed in the Dwork vector.

/* data type work */
    disc_filt_states_M->Work.dwork = ((void *)
&disc_filt_states_DWork);
    (void)memset((char_T *) &disc_filt_states_DWork, 0,
    sizeof(D_Work_disc_filt_states));
    disc_filt_states_DWork.Top_filter = 0.0;

    /* exported global states */
    Lower_filter = 0.0;

Block States and Simulink® Signal Objects

If you are not familiar with Simulink data objects and signal objects, you should read Simulink® Data Objects and Code Generation before reading this section.

You can associate a block state with a signal object, and control code generation for the block state through the signal object. To do this,

  1. Instantiate the desired signal object, and set its RTWInfo.StorageClass property as you require.

  2. Open the State Properties dialog box for the block whose state you want to associate with the signal object.

  3. Enter the name of the signal object in the State name field.

  4. Select State name must resolve to Simulink signal object.

    The Simulink product disables the RTW storage class and RTW storage type qualifier options in the State Properties dialog box, because the signal object specifies these settings.

  5. Click Apply and close the dialog box.

Summary of State Storage Class Options

Here is a simple model, unit_delay.mdl, which contains a Unit Delay block:

The following table shows, for each state storage class option, the variable declaration and initialization code generated for a Unit Delay block state. The block state has the user-defined state name udx.

Storage Class

Declaration

Initialization Code

Auto

In model.h

typedef struct 
D_Work_unit_delay_tag 
{
  real_T udx;
 }
D_Work_unit_delay;
unit_delay_DWork.udx = 0.0;

Exported Global

In model.c or model.cpp

real_T udx;

In model.h

extern real_T udx; 

In model.c or model.cpp

udx = 0.0;

Imported Extern

In model_private.h

extern real_T udx; 

In model.c or model.cpp

udx = 
  unit_delay_P.UnitDelay_X0; 

Imported Extern Pointer

In model_private.h

extern real_T *udx; 

In model.c or model.cpp

(*udx) = 
  unit_delay_P.UnitDelay_X0; 

  


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