Products & Services Solutions Academia Support User Community Company

Learn more about Stateflow   

Using the Stateflow Debugger

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 a chart in your model to provide visual verification that your chart behaves as expected. Animation highlights objects in a chart as execution progresses.

You can animate a chart during simulation in one of 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 chart.

  1. Open the 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:

    • For the fastest animation, select a value of 0 seconds.

    • For the slowest animation, select a value of 1 second.

  5. Start simulation.

    The chart highlights states and transitions as they execute.

Animating Stateflow Charts in External Mode

You can animate a chart 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 Communicating With Code Executing on a Target System Using Simulink External Mode in the Real-Time Workshop User's Guide). In external mode, you can animate states in a chart, and view test point signals in a floating scope or signal viewer.

Animating States During Simulation in External Mode.   To animate states in a chart in external mode:

  1. Load the 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 editor, select Tools > External Mode Control Panel.

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

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

    In:Select:
    Signal selection paneChart 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 typing this 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 &

      Note   -w allows the target code to wait for the Simulink model connection.

  12. In the Model Editor, select Simulation > External, and then select Simulation > Connect to Target.

  13. Start simulation.

    The chart highlights states as they execute.

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

To view test point data during simulation in external mode:

  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 chart.

  4. In the Signal Selector Contents pane, list Testpointed signals only and select 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 Chart

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:

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, graphical functions, and truth table functions from the Stateflow Editor or the Model Explorer. You can set breakpoints for events only from the Model Explorer.

  1. Open the properties dialog box of the object for which you want to set a breakpoint, using one of the following tools:

    ToolAction
    Stateflow EditorRight-click a state, transition, graphical function, or truth table function in your chart, and select Properties.

     What if my chart objects are grouped?

    Model Explorer
    1. Show all Stateflow objects by selecting View > List View Options > All Stateflow Objects.

    2. Click the chart of interest in the Model Hierarchy pane.

    3. Click a state, transition, graphical function, truth table function, or event in the Contents pane.

    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.

    Graphical or truth table 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 Disable all check box.

Clearing All Breakpoints

To find and clear all breakpoints without disabling them, use the following Stateflow API commands. (For more information, see the Stateflow API documentation.)

% get a handle for the root object
rootObj = find(sfroot,'-isa','Stateflow.Machine','Name',model);

% find all states, transitions, data, events, and charts
stateObjects = rootObj.find('-isa','Stateflow.State');
transitionObjects =rootObj.find('-isa','Stateflow.Transition');
dataObjects = rootObj.find('-isa','Stateflow.Data');
eventObjects = rootObj.find('-isa', 'Stateflow.Event');
chartObjects = rootObj.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 events, clear their breakpoints
for i = 1:size(eventObjects,1)
eventObjects(i).Debug.Breakpoints.StartBroadcast = 0;
eventObjects(i).Debug.Breakpoints.EndBroadcast = 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 five commands use the API method find to specify the type of object to find. For example, the command

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

searches through the rootObj 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, events, and charts as shown in the code.

Options for 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 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 pane of the Configuration Parameters dialog box. This option is described in Speeding Up Simulation.

Starting Simulation in the Debugging Window

To debug the charts in a model, you start simulation in the Debugging window:

  1. Click the Start button.

    A debugging simulation session starts. When simulation reaches a breakpoint that you set, the Stateflow Debugging window appears as follows:

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

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

Options to Control Execution Rate in the Debugging Window

When the chart reaches a breakpoint, you can control the execution rate using single-step mode or continuous execution until the chart reaches another breakpoint. Use the following buttons in the Stateflow Debugging window to control the execution rate:

During single-step mode, the debugger does not zoom automatically to the chart object that is executing. Instead, the debugger opens the subviewer that contains that object. This behavior minimizes visual disruptions as you step through your analysis of a simulation.

Options to Control the Output Display Pane

During simulation, the Debugging window monitors several execution indicators in the output display in the bottom pane of the Debugging window. You select the contents of this display with the following pull-down menus, which are available only after chart execution reaches a breakpoint.

After you make a selection, the pull-down menu for the current display appears highlighted. When you select an output display button, that type of output appears until you choose a different display type. You can clear the display by selecting File > Clear Display in the Stateflow Debugging window.

  


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