Debugging Stateflow® Charts

Opening the Stateflow® Debugger

To open the Stateflow® Debugging window, follow these steps:

  1. In the Stateflow Editor, select Tools > Debug.

    The Stateflow Debugging window opens.

Animating Stateflow® Charts

During simulation, you can animate Stateflow charts in a Simulink® model to provide visual verification that your chart behaves as expected. Animation highlights objects in Stateflow charts as they execute. You can animate charts during simulation in two contexts:

Animating Stateflow® Charts in Normal Mode

During simulation in normal mode on a host machine, you can animate states and transitions in a Stateflow chart. Follow these steps:

  1. Open the Stateflow chart you want to animate.

  2. In the Stateflow Editor, select Tools > Debug to open the Stateflow Debugging window.

  3. In the Animation section of the Debugging window, select Enabled.

  4. Control the speed of animation by entering a value in the Delay field, as follows:

  5. Start simulation.

    The Stateflow chart highlights states and transitions as they execute during simulation.

Animating Stateflow® Charts in External Mode

You can animate Stateflow charts in external mode — the mode in which Real-Time Workshop® code generation software establishes a communications link between a Simulink model and code executing on a target system (see External Mode in the Real-Time Workshop User's Guide). In external mode, you can animate states in Stateflow charts, and view test point signals in a floating scope or signal viewer.

Animating States During Simulation in External Mode.   To animate states in Stateflow charts in external mode, follow these steps:

  1. Load the Stateflow chart you want to animate to the target machine.

  2. In the Stateflow Editor, select Tools > Debug to open the Stateflow Debugging window.

  3. In the Animation section of the Debugging window, select Enabled.

  4. In the Stateflow Editor, select Simulation > Configuration Parameters.

  5. In the left Select pane, select Real-Time Workshop > Interface.

  6. In the Data exchange section of the right pane, select External mode from the drop-down menu in the Interface field and click OK.

  7. In the Simulink model that contains the chart, select Tools > External Mode Control Panel.

  8. In the External Mode Control Panel dialog box, click the Signals & Triggering button.

  9. In the External Signal & Triggering dialog box, set these parameters:

    In:Select:
    Signal selection paneStateflow chart block you want to animate
    Trigger paneArm when connecting to target check box
    Trigger panenormal from drop-down menu in Mode field

  10. Build the model to generate an executable file.

  11. Start the target in the background by entering the following command at the MATLAB prompt:

    !model_name.exe -w &

    For example, if the name of your model is my_control_sys, enter this command:

    !my_control_sys.exe -w &

  12. In the Simulink model editor, select Simulation > External, and then select Simulation > Connect to Target.

  13. Start simulation.

    The Stateflow chart will highlight states as they execute.

Viewing Test Point Data in Floating Scopes and Signal Viewers.   When you simulate Stateflow charts in external mode, you can view test point data in floating scopes and signal viewers. In Stateflow charts, you can designate local data and states to be test points.

To view test point data during simulation in external mode, follow these steps:

  1. Open the Model Explorer and for each data you want to view, follow these steps:

    1. In the left Model Hierarchy pane, select the state or local data of interest.

    2. In the right Dialog pane, select the Test point check box.

  2. From a floating scope or signal viewer, click the signal selection button:

    The Signal Selector dialog box opens.

  3. In the Signal Selector Model hierarchy pane, select the Stateflow chart.

  4. In the Signal Selector Contents pane, list Testpointed signals only and check the test point signals you want to view.

  5. Simulate the model in external mode as described in Animating States During Simulation in External Mode.

    The scope or viewer displays the values of the test point signals as the simulation runs.

Setting Breakpoints for Debugging

A breakpoint indicates a point at which the Stateflow Debugging window halts execution of a simulating Stateflow chart. At this time, you can inspect Stateflow data and the MATLAB workspace and examine the status of a simulating Stateflow chart.

The Stateflow Debugging window supports global and local breakpoints. Global breakpoints halt execution on any occurrence of the specific type of breakpoint. Local breakpoints halt execution on a specific object.

Setting Global Breakpoints

Use the Breakpoint controls in the Stateflow Debugging window to specify global breakpoints. When a global breakpoint is encountered during simulation, execution stops and the Debugger takes control. Select any or all of these breakpoints:

These breakpoints can be changed during run-time and are immediately enforced. When you save a Stateflow chart, the breakpoint settings are saved with it.

Global breakpoints can be changed during run-time and are immediately enforced. When you save the Stateflow chart, all the Stateflow Debugging window settings (including breakpoints) are saved, so that the next time you open the model, the breakpoints are as you left them.

Setting Local Breakpoints

You can set breakpoints for specific state actions, transitions, function calls, and event broadcasts in a Stateflow chart.

  1. Open the properties dialog of the object for which you want to set a breakpoint, as follows:

    1. Right-click the object from one of these sources:

      ObjectRight-Click In:
      Stateflow ChartModel Explorer
      State

      Transition

       
      Function

      Event 

    2. From the resulting pop-up menu, select Properties.

      A dialog box appears for setting the properties of the object.

  2. In the properties dialog box, select from the following breakpoints options:

    For:Select:
    States

    State During — Stop execution before performing the state during actions.

    State Entry — Stop execution before performing the state entry actions.

    State Exit — Stop execution before performing the state exit actions.

    Transitions

    When Tested — Stop execution before testing the transition to see if it is a valid path.

    When Valid — Stop execution after the transition tests valid, but before taking the transition.

    Functions

    Function Call — Stop execution before calling the function.

    Events

    Start of Broadcast — Stop execution before broadcasting the event.

    End of Broadcast — Stop execution after a Stateflow object reads the event.

Disabling All Breakpoints

To disable all breakpoints in the Debugger window, select the check box Disable all.

Clearing All Breakpoints

There is no button or check box in the Debugger window to clear breakpoints. To find and clear all breakpoints without disabling them, you must use a set of Stateflow API commands as shown below. (See the Stateflow API documentation for more information.)

% get a handle for the root object
rootObject = find(sfroot,'-isa','Stateflow.Machine','Name',model); 
 
% find all states, transitions, data, and charts
stateObjects = rootObject.find('-isa','Stateflow.State'); 
transitionObjects = rootObject.find('-isa','Stateflow.Transition'); 
dataObjects = rootObject.find('-isa','Stateflow.Data'); 
chartObjects = rootObject.find('-isa','Stateflow.Chart'); 

% for all states, clear their breakpoints 
for i = 1:size(stateObjects,1) 
stateObjects(i).Debug.Breakpoints.OnEntry = 0; 
stateObjects(i).Debug.Breakpoints.OnDuring = 0; 
stateObjects(i).Debug.Breakpoints.OnExit = 0; 
stateObjects(i).Machine.Debug.BreakOn.ChartEntry = 0; 
stateObjects(i).Machine.Debug.BreakOn.EventBroadcast = 0; 
stateObjects(i).Machine.Debug.BreakOn.StateEntry = 0; 
end 

% for all transitions, clear their breakpoints
for i = 1:size(transitionObjects,1) 
transitionObjects(i).Debug.Breakpoints.WhenTested = 0; 
transitionObjects(i).Debug.Breakpoints.WhenValid = 0; 
end 

% for all data, clear their breakpoints
for i = 1:size(dataObjects,1)
dataObjects(i).Debug.Watch = 0; 
end 

% for all charts, clear their breakpoints
for i = 1:size(chartObjects,1)
chartObjects(i).Debug.Breakpoints.OnEntry = 0; 
end

The first command returns a handle to the machine object that represents the top level of the Stateflow hierarchy. The next four commands use the API method find to specify the type of object to find. For example, the command

stateObjects = rootObject.find(‘-isa','Stateflow.State')

searches through the rootObject and returns an array listing of all state objects in your model. (See Finding Objects and Properties in the Stateflow API documentation.)

You can also define the properties of Stateflow objects. For example, you can clear all breakpoints in your model by setting those property values to zero for all states, transitions, data, and charts as shown in the code.

Setting Error Checking in the Debugging Window

The options in the Error checking options section of the Stateflow Debugging window insert generated code in the simulation target to provide breakpoints to catch different types of errors that might occur during simulation. Select any or all of the following error checking options:

To include the supporting code designated for these debugging options in the simulation application, select the Enable debugging/animation check box in the Simulation Target dialog. This option is described in Configuring the Simulation Target for the Main Model.

Starting Simulation in the Debugging Window

To debug the Stateflow charts in a model, you start simulation in the Debugging window with these steps:

  1. Select the Start button.

    A debugging simulation session starts. When a breakpoint that you set is encountered, the Stateflow Debugging window takes on the following appearance:

At the breakpoint, the following status items appear in the upper portion of the Debugger window:

During simulation, the Stateflow chart is marked read-only. The appearance of the Stateflow Editor toolbar and menus changes so that object creation is not possible. When the Stateflow Editor is in this read-only mode, its condition is referred to as iced.

Controlling the Execution Rate in the Debugging Window

Once you start simulation as described in Starting Simulation in the Debugging Window, and a breakpoint is reached, you can control the rate of execution of Stateflow charts to execute step-by-step or continuously until another breakpoint is reached. Use the following buttons in the Stateflow Debugging window to control the rate of execution:

Setting the Output Display Pane

During simulation, the Debugging window monitors a variety of execution indicators in its output display in the bottom pane of the Debugging window. You select the contents of this display with the following pull-downs located just above the display, which are enabled only after a breakpoint is reached during simulation.

Once you make a selection, the pull-down menu corresponding to the current display is highlighted. Once you select an output display button, that type of output is displayed until you choose a different display type. You can clear the display by selecting Clear Display from the File menu of the Stateflow Debugging window.

  


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