Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop   

Signal Objects

This section discusses how to use signal objects in code generation. Signal objects can be used to represent both signal and state data, and behave similarly to parameter objects, described in Parameter Objects.

Configuring Signal Objects for Code Generation

In configuring signal objects for code generation, you use the following code generation options and signal object properties:

Other signal object properties (such as user-defined properties of classes derived from Simulink.Signal) do not affect code generation.

Effect of Storage Classes on Code Generation for Signal Objects

The way in which the Real-Time Workshop product uses storage classes to determine how signals are stored is the same with and without signal objects. However, if a signal's label resolves to a signal object, the object's RTWInfo.StorageClass property is used in place of the port configuration of the signal.

The default storage class is Auto. If the storage type is Auto, the Real-Time Workshop product follows the Signal storage reuse, Reuse block outputs, Enable local block outputs, Eliminate superfluous local variables (Expression folding), and Minimize data copies between local and global variables code generation options to determine whether signal objects are stored in reusable and/or local variables. Make sure that these options are set correctly for your application.

To generate a test point or signal storage declaration that can interface externally, use an explicit RTWInfo.StorageClass assignment. For example, setting the storage class to SimulinkGlobal, as in the following command, is equivalent to declaring a signal as a test point.

SinSig.RTWInfo.StorageClass = 'SimulinkGlobal';

Controlling Signal Object Code Generation By Using Typed Commands

The discussion and code examples in this section refer to the model shown in the next figure.

To configure a signal object, you must first create it and associate it with a labeled signal in your model. To do this,

  1. Define a subclass of Simulink.Signal. In this example, the signal object is an instance of the class Simulink.Signal, which is provided with the Simulink product.

  2. Instantiate a signal object from your subclass. The following example instantiates inSig, a signal object of class Simulink.Signal.

    inSig = Simulink.Signal
    inSig =
    Simulink.Signal
             RTWInfo: [1x1 Simulink.SignalRTWInfo]
         Description: ''
            DataType: 'auto'
                 Min: -Inf
                 Max: Inf
            DocUnits: ''
          Dimensions: -1
          Complexity: 'auto'
          SampleTime: -1
        SamplingMode: 'auto'
        InitialValue: ''

    Make sure that the name of the signal object matches the label of the desired signal in your model. This ensures that the Simulink engine can resolve the signal label to the correct object. For example, in the model shown in the above figure, the signal label inSig would resolve to the signal object inSig.

  3. You can require signals in a model to resolve to Simulink.Signal objects. To do this for the signal inSig, in the model window right-click the signal line labeled inSig and choose Signal Properties from the context menu. A Signal Properties dialog appears.

  4. In the Signal Properties dialog box that appears, select the check box labelled Signal name must resolve to Simulink signal object, and click OK or Apply.

  5. Set the object properties as required. You can do this by using the Simulink Model Explorer. Alternatively, you can assign properties by using MATLAB commands. For example, assign the signal object's storage class by setting the RTWInfo.StorageClass property as follows.

    inSig.RTWInfo.StorageClass = 'ExportedGlobal';
    

Controlling Signal Object Code Generation By Using Model Explorer

If you prefer, you can create signal objects and modify their attributes using Model Explorer. This lets you see and set attributes of a signal in a dialog box pane, and alleviates the need to remember and type field names. Do the following to instantiate inSig and set its attributes from Model Explorer:

  1. Choose Model Explorer from the View menu.

    Model Explorer opens or activates if it already was open.

  2. Select Base Workspace in the Model Hierarchy pane.

  3. Select Simulink Signal from the Add menu.

    A new signal named Sig appears in the Contents pane.

  4. To set the signal name in Model Explorer, click the word Sig in the Name column to select it, and rename it by typing inSig followed by Return in place of Sig.

  5. To set the inSig.RTWInfo.StorageClass in Model Explorer, click the Storage class menu and select ExportedGlobal, as shown in the next figure.

  6. Click Apply.

The following table shows, for each setting of RTWInfo.StorageClass, the variable declaration and the code generated for the inport signal (inSig) of the current model:

Storage Class

Declaration

Code

Auto (with storage optimizations on)

In model.h

typedef struct 
_ExternalInputs_signal_ objs_examp_tag 
{
  real_T inSig;
 }
ExternalInputs_signal_ objs_examp;
rtb_Gain1Sig =
  signal_objs_examp_U.inSig *
  signal_objs_examp_P.Gain_Gain;

Simulink Global

In model.h

typedef struct 
_ExternalInputs_signal_objs_examp_tag 
{
  real_T inSig;
 }
ExternalInputs_signal_objs_examp;
rtb_Gain1Sig = 
  signal_objs_examp_U.inSig * 
  signal_objs_examp_P.Gain_Gain;

ExportedGlobal

In model.c or model.cpp

real_T inSig;

In model.h

extern real_T inSig;
rtb_Gain1Sig = inSig * 
  signal_objs_examp_P.Gain_Gain;

ImportedExtern

In model_private.h

extern real_T inSig;
rtb_Gain1Sig = inSig * 
  signal_objs_examp_P.Gain_Gain;

ImportedExternPointer

In model_private.h

extern real_T *inSig;
rtb_Gain1Sig = (*inSig) * 
  signal_objs_examp_P.Gain_Gain;

  


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