Stateflow | ![]() ![]() |
Create the Model and Stateflow Diagram
Using the sample (see Example Stateflow Diagram) as a guide, create the 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.
Define the sfun Target
Choose Open Simulation Target from the Tools menu of the graphics editor. Ensure that the check box to Enable Debugging/Animation is enabled (checked). Click on the Close button 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 on the Chart entry option under the Break Controls border. When the simulation begins, it will break on the entry into the chart. Click on the Enabled radio button under the Animation border to turn animation on.
Start the Simulation
Click on the Go 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 have specified a breakpoint on chart entry, the execution stops at that point and the Debugger display status indicates
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 on 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 Runtime 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 may 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 on the Go button to start the simulation again. Repeat the debugging single stepping and observe the behavior.
Solution Stateflow Diagram
This is the corrected Stateflow diagram with the condition added to the transition from Power_on
to Power_off
.
![]() | Typical Scenario to Debug Runtime Errors | Debugging State Inconsistencies | ![]() |