Monitoring Test Points in Stateflow Charts

About Test Points in Stateflow Charts

A Stateflow test point is a signal that you can observe during simulation — for example, by using a Floating Scope block. You can designate the following Stateflow objects as test points:

You implicitly declare all states and local data as test points by selecting the Enable debugging/animation option for the Stateflow simulation target in the Configuration Parameters dialog box. If you do not select this option, you can specify individual local data or states as test points by setting their TestPoint property via the Stateflow API or in the Model Explorer (see Setting Test Points for Stateflow States and Local Data with the Model Explorer).

You can monitor individual Stateflow test points with a floating scope during model simulation. You can also log test point values into MATLAB workspace objects.

Setting Test Points for Stateflow States and Local Data with the Model Explorer

You can explicitly set individual states or local data as test points through the Model Explorer. Use the example you create in the following procedure to learn how to set individual test points for Stateflow states and data.

  1. Create this model.

    The model consists of a single Stateflow block named Chart1, which is triggered by a signal from a Sine Wave block through the input trigger event tic. In the Stateflow chart, the state A and its substate X are entered for the first tic event. State A and substate X stay active until 10 tic events have occurred, and then state B is entered. On the next event, state A and substate X are entered and the cycle continues.

    The data x is added to the state X. The entry and during actions for substate X increment x while X is active for 10 tic events. When state B is entered, x reinitializes to zero, and the cycle repeats.

  2. Save the model as myModel.mdl.

  3. Start the Model Explorer. In the Simulink model, select View > Model Explorer.

    The Model Explorer appears.

  4. In the Model Explorer, expand myModel.

  5. Expand Chart1, then select A.

  6. In the rightmost pane, State A, select the Test point check box. Click Apply.

    This action creates a test point for the state A.

    Alternatively, you can access a test point through the middle pane. By default, the Model Explorer displays event and data child objects in the Contents pane for the selected object in the Model Hierarchy pane. You can set the test point for the state A through this pane by selecting the parent of A. If the states do not appear in the middle pane, select the States/Functions/Boxes/Etc. check box in the View > List View Options for All Stateflow Objects.

  7. Repeat step 6 for state X. Click Apply.

  8. Select X again. Select the local data x in the Contents pane.

  9. In the rightmost pane for that data, select the Value Attributes tab and then select the Test point check box. Click Apply.

  10. Repeat step 6 for state B. Click Apply and save the model.

You can now log these test points. See Logging Data Values and State Activity for instructions on using the Signal Logging dialog box. See Logging Data Values Using the Command Line API for instructions on logging signals at the MATLAB command line.

Logging Data Values and State Activity

During simulation, you can log values for data and state activity into Simulink objects. After simulation, you can access these objects in the MATLAB workspace and use them to report and plot the values.

You can use the following procedure to learn how to access logged Stateflow data and state activity. This procedure uses the model, myModel, from the preceding topic, Setting Test Points for Stateflow States and Local Data with the Model Explorer.

  1. If myModel is not already open, at the MATLAB prompt, type

    >> myModel
    

    The model appears.

  2. In the Simulink model window, right-click the Stateflow block and select Log Chart Signals.

    The Signal Logging dialog box appears, as shown.

  3. Select the check box next to A.

    This box is the state activity signal for state A. When A is active, its value is 1. When A is inactive, its value is 0.

    After checking A, notice these properties in the right pane of the Signal Logging dialog box:

    Signal Properties

    Description

    Signal Name

    Name of the highlighted state or data.

    Log signal data

    Checking this selects the highlighted signal in the Signals pane.

    Logging name

    Name of the signal logged. By default, this is set to the name of the selected/highlighted state or data. You can select Custom for this property to rename the selected/highlighted signal in the adjacent field to the right.

    Limit data points to last

    Select this property to enter the number of most recent sample values to log in the adjacent field to the right for the selected/highlighted signal.

    Decimation

    Select this property to enter the level of decimation for the signal values logged for the selected/highlighted signal.

  4. Select all the signals in the Signal pane and click OK to close the Signal Logging dialog box.

  5. Simulate the model.

    During simulation, the Simulink model data log object logsout is generated in the MATLAB workspace.

  6. After simulation, enter this string at the MATLAB prompt:

    >> logsout
    

    You see this result:

    logsout =
     
    Simulink.ModelDataLogs (myModel):
    Name                   Elements  Simulink Class
    
    Chart1                    4      StateflowDataLogs
    

    The display identifies logsout as a Simulink object of type ModelDataLogs. This type is the highest level logging object. The object Chart1 appears as the only contents of logsout and represents logged data for the Stateflow block. Chart1 is identified as a Simulink object of type StateflowDataLogs.

  7. At the MATLAB prompt, enter this string:

    >> logsout.Chart1
    

    You see this result:

    ans =
     
    Simulink.StateflowDataLogs (Chart1):
      Name                   Elements  Simulink Class
    
      ('A.X.x')                 1      Timeseries
      A                         1      Timeseries
      ('A.X')                   1      Timeseries
      B                         1      Timeseries
    

    The signals that you selected in the Signal Logging dialog box appear as Simulink objects of type Timeseries. Notice that the signals for the activity of state X and the value of data x appear as ('A.X') and ('A.X.x'), respectively. Because of the way that logged signals are stored, you must use dot notation to access logged data for Stateflow objects below chart level in the Stateflow chart.

  8. At the MATLAB prompt, enter this string:

    >> logsout.Chart1.('A.X.x')
    

    You see this result:

    ans = 
    
              Name: 'A.X.x'
         BlockName: 'StateflowChart/A.X.x'
         PortIndex: 1
        SignalName: 'A.X.x'
        ParentName: 'A.X.x'
          TimeInfo: [1x1 Simulink.TimeInfo]
              Time: [114x1 double]
              Data: [114x1 double]
    

    The logging object for the data x, ('A.X.x'), is actually a structure of logged data pertinent to x. The actual logged signal values for x are contained in the Data object, a vector of 114 values. For example, if you were to enter the MATLAB command logsout.Chart1.('A.X.x').Data, a long stream of data would appear. A better way to see the logged values of x is to use the plot method shown in the next step.

  9. At the MATLAB prompt, plot the values of x with this command:

    >> logsout.Chart1.('A.X.x').plot
    

    You see this result:

    The preceding plot exhibits the expected results for the value of x. It increments for 10 time steps before resetting to 0 when states X and A are exited and state B is entered in the Stateflow chart.

The preceding example shows some capabilities you have for reporting logged Stateflow data. Stateflow data conforms to the general rules for handling logging signals in Simulink models.

Logging Data Values Using the Command Line API

You can also specify which signals to log by using API commands at the MATLAB prompt. The following procedure uses the model, myModel, from the previous topic Setting Test Points for Stateflow States and Local Data with the Model Explorer.

  1. If myModel is not already open, at the MATLAB prompt, type:

    >> myModel
    

    The model appears.

  2. To define a Simulink object of type SigPropNode for the Stateflow chart, use this command:

    >> signal_properties = ...
    get_param('myModel/Chart1','AvailSigsInstanceProps')
    

    You see this result:

    signal_properties = 
    
    Simulink.SigPropNode
  3. To retrieve the contents of this object, use the API method get:

    >> signal_properties.get
    

    You see this result:

       Path: 'StateflowChart'
       Name: 'Chart1@StateflowChart'
       Type: 'Stateflow'
    Signals: [4x1 Simulink.SigProp]

    You can log four signals in the chart.

  4. To view the properties of the first signal, type:

    >> signal_properties.Signals(1).get
    

    This list appears:

             SigName: 'A.X.x'
           BlockPath: 'StateflowChart/A.X.x'
           PortIndex: 1
           LogSignal: 0
       UseCustomName: 0
             LogName: 'A.X.x'
     LimitDataPoints: 0
           MaxPoints: 5000
            Decimate: 0
          Decimation: 2
       LogFramesIndv: 0
            Children: [0x1 double]

    By default, LogName is identical to SigName. If you want to use another name for the logged signal, instead of SigName itself, change LogName to a different string:

    >> signal_properties.Signals(1).LogName = 'new_name';
    

    Then enable the custom string for this signal:

    >> signal_properties.Signals(1).UseCustomName = 1;
    
  5. The value of LogSignal is 0 if the signal is not logged, and it equals 1 if the signal is logged. In this case, the signal is not logged.

    To enable logging of the signal A.X.x, change the value of LogSignal:

    >> signal_properties.Signals(1).LogSignal = 1;
    
  6. Reset the chart parameters using the updated signal_properties object:

    >> set_param('myModel/Chart1','AvailSigsInstanceProps', ...
    signal_properties)
    

In the Simulink model window, right-click the Stateflow block and select Log Chart Signals. In the Signal Logging dialog box, you see that the box next to the signal A.X.x is now checked. Also, the custom string new_name appears in the Logging name field for that signal.

For more information on how you can use and manipulate logged data with MATLAB commands and scripts, see Logging Signals in the Simulink software documentation.

Using a Floating Scope to Monitor Data Values and State Activity

In the steps of this topic, you configure a Floating Scope block to monitor a data value and the activity of a state in this example model:

The model consists of a Floating Scope block and a Stateflow block. The chart for the Stateflow block starts by adding an increment of 0.02 for 10 samples to the data x1. For the next 10 samples, x1 increments by 0.2, and the cycle repeats.

  1. Double-click the Floating Scope block.

    A Floating Scope window appears, already scaled for this example.

  2. In the Floating Scope window, select the Signal Selection tool .

    The Signal Selector dialog box appears with a hierarchy of Simulink blocks for the model.

  3. In the Model hierarchy pane, select the Stateflow block whose signals you want to monitor and, in the List contents pane, select the data you want to monitor.

    In the preceding example, the block named Chart is selected in the Model hierarchy pane, and the data x1 and the activity of state A are selected in the Contents pane.

  4. Simulate the model.

    When you simulate the example model, you receive a signal trace for x1 and for the activity of state A, as shown.

    When state A is active, its activity signal value is 1, and when it is inactive, its signal value is 0. Because this value is very low or very high compared to some data, you might want to put it in a second Floating Scope block to compare it with other data.

  


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