Stateflow | ![]() ![]() |
Create the Model and Stateflow Diagram
To learn how to debug some typical run-time errors, create the following Simulink model and Stateflow diagram:
Using the graphics editor Add menu, add the Switch
Input from Simulink event and the Shift
Input from Simulink data object.
The Stateflow diagram has two states at the highest level in the hierarchy, Power_off
and Power_on
. By default Power_off
is active. The event Switch
toggles the system between the Power_off
and Power_on
states. Switch
is defined as an Input from Simulink event. Power_on
has three substates, First
, Second
, and Third
. By default, when Power_on
becomes active, First
also becomes active. Shift
is defined as an Input from Simulink data object. When Shift
equals 1, the system transitions from First
to Second
, Second
to Third
, Third
to First
, and then the pattern repeats.
In the Simulink model, there is an event input and a data input. A Sine wave block is used to generate a repeating input event that corresponds with the Stateflow event Switch
. The Step block is used to generate a repeating pattern of 1 and 0 that corresponds with the Stateflow data object Shift
. Ideally, the Switch
event occurs in a frequency that allows at least one cycle through First
, Second
, and Third
.
Define the sfun Target
Choose Open Simulation Target from the Tools menu of the graphics editor. Ensure that Enable Debugging/Animation is enabled (checked). Click Close to apply the changes and close the dialog box.
Invoke the Debugger and Choose Debugging Options
Choose Debug from the Tools menu of the graphics editor. Click the Chart entry option under the Break Controls border. When the simulation begins, execution halts on entry into the chart. Click Enabled under the Animation border to turn animation on.
Start the Simulation
Click the Start button to start the simulation. Informational messages are displayed in the MATLAB Command Window. The graphics editor toolbar and menus change appearance to indicate a read-only interface. The Stateflow diagram is parsed, the code is generated, and the target is built. Because you specified a breakpoint on chart entry, the execution stops at that point and the Debugger display status indicates the following.
Stopped: Just after entering during function of Chart debug__power
Executing: sf_debug_ex_debug_power
Current Event: Input event Switch
Debug the Simulation Execution
At this point, you can single-step through the simulation and see whether the behavior is what you expect. Click the Step button and watch the Stateflow diagram animation and the Debugger status area to see the sequence of execution.
Single-stepping shows that the desired behavior is not occurring. The transitions from Power_on.First
to Power_on.Second
, etc., are not occurring because the transition from Power_on
to Power_off
takes priority. The output display of code coverage also confirms this observation.
Resolve Run-Time Error and Repeat
Choose Stop from the Simulation menu of the graphics editor. The Stateflow diagram is now writeable. The generation of event Switch
is driving the simulation and the simulation time is passing too quickly for the input data object Shift
to have an effect. The model might need to be completely rethought.
In the meantime, there is a test that verifies the conclusion. Modify the transition from Power_on
to Power_off
to include a condition. The transition is not to be taken until simulation time is greater than 10.0. Make this modification and click the Start button to start the simulation again. Repeat the debugging single-stepping and observe the behavior.
Debugged Stateflow Diagram
This is the corrected Stateflow diagram with the condition added to the transition from Power_on
to Power_off
.
![]() | Debugging Run-Time Errors Example | Debugging State Inconsistencies | ![]() |