Real-Time Workshop    

Signal Objects

This section discusses how to use signal objects in code generation.

Configuring Signal Objects for Code Generation

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

Effect of Storage Classes on Code Generation for Signal Objects

The way in which Real-Time Workshop 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, Real-Time Workshop follows the Signal storage reuse, Buffer reuse, and Local block outputs 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 a test point or externally interfaceable signal storage declaration, 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.

Example of Signal Object Code Generation

The discussion and code examples in this section refers to the model shown in Figure 5-7.

Figure 5-7: Example Model With Signal Object

To configure a signal object, you must first create it and associate it with a labelled 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 example class SimulinkDemos.Signal, which is provided with Simulink. For the definition of SimulinkDemos.Signal, see the directory
    matlabroot/toolbox/simulink/simdemos/@SimulinkDemos.
  2. Instantiate a signal object from your subclass. The following example instantiates SinSig, a signal object of class SimulinkDemos.Signal.
  1. Make sure that the name of the signal object matches the label of the desired signal in your model. This ensures that Simulink can resolve the signal label to the correct object. For example, in the model shown in Figure 5-7, the signal label SinSig would resolve to the signal object SinSig.

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

Table 5-7 shows, for each setting of RTWInfo.StorageClass, the variable declaration and the code generated for Sine Wave output (SinSig) of the model shown in Figure 5-7.

Table 5-7: Signal Properties Options and Generated Code
Storage Class
Declaration
Code

Auto

(with storage optimizations on)

  • real_T rtb_SinSig;
    
  • rtb_SinSig = rtP.Sine_Wave_Amp * 
    sin(rtP.Sine_Wave_Freq * 
    rtmGetT(rtM_Signals_examp) + 
    rtP.Sine_Wave_Phase) + 
    rtP.Sine_Wave_Bias;
    

Simulink Global

  • typedef struct 
    BlockIO_tag {
      real_T SinSig; 
      real_T Gain1Sig;
    } BlockIO;
    .
    .
    BlockIO rtB;
    
    
  • rtb_SinSig = rtP.Sine_Wave_Amp * 
    sin(rtP.Sine_Wave_Freq * 
    rtmGetT(rtM_Signals_examp) + 
    rtP.Sine_Wave_Phase) + 
    rtP.Sine_Wave_Bias;
    

Exported Global

  • extern real_T SinSig;
    

  • rtb_SinSig = rtP.Sine_Wave_Amp * 
    sin(rtP.Sine_Wave_Freq * 
    rtmGetT(rtM_Signals_examp) + 
    rtP.Sine_Wave_Phase) + 
    rtP.Sine_Wave_Bias;
    

Imported Extern

  • extern real_T SinSig;
    

  • rtb_SinSig = rtP.Sine_Wave_Amp * 
    sin(rtP.Sine_Wave_Freq * 
    rtmGetT(rtM_Signals_examp) + 
    rtP.Sine_Wave_Phase) + 
    rtP.Sine_Wave_Bias;
    

Imported Extern Pointer

  • extern real_T *SinSig;
    

  • (*SinSig) = rtP.Sine_Wave_Amp * 
    sin(rtP.Sine_Wave_Freq * 
    rtmGetT(rtM_Signals_examp) + 
    rtP.Sine_Wave_Phase) + 
    rtP.Sine_Wave_Bias;
    


  Parameter Object Configuration Quick Reference Diagram Signal Object Configuration Quick Reference Diagram