Analyzing and Debugging Logic with Stateflow
A fuel control system is a critical component of an engine. Ensuring the engine operates reliably requires a controller that is tolerant to sensor failures. Designing such a controller involves keeping track of multiple sensor states and determining the fuel mode based on which or how many sensors have failed. Stateflow® has graphical programming features that will help you model interconnected and complex logic in a clear and concise manner. Its visual tools provide a powerful way to break down sequences of decisions and explore the behavior of your system in detail. The Symbols pane shows which variables are present in the chart and their value at the current execution stage. Clicking a state or transition will highlight the relevant symbols for the object. Clicking on an individual symbol will in turn show where it is used in the chart. This can help you identify connections that are otherwise not explicit. The value shown in the Symbols pane flashes when its value changes from one time step to the next, making it easy to keep track of these changes—even in a system with many symbols. Breakpoints can be added to the states and transitions to debug unexpected chart execution. Once a breakpoint is hit, you can use the Step Forward and Step Over buttons to advance the simulation manually and inspect the logic of the system in detail.
Stateflow provides powerful tools for modelling, analyzing, and debugging complex logic. This video will show you how to use its visual programming features to break down sequences of decisions and explore the behavior of your system in detail.
But first, an overview of the system.
This chart models the logic for fault detection and handling of a fuel rate control system and can be divided into three parts. The four parallel subcharts at the top keep track of the state of each individual sensor; the superstate in the middle counts how many of the sensors have failed; and the superstate at the bottom determines the fuel mode of the system based on which sensors have failed. The complexity in this chart comes from the interdependence of these parallel states and their respective child states.
This dashboard can be used to control the fail state for each of the sensors, as well as the engine speed. Interacting with the panel during simulation is a great way to explore the logic of the system.
To access this model, follow the first link below this video or execute this command in the MATLAB Command Window.
The chart starts in a low emissions mode with no failures. Increasing the engine speed activates the transition to the ‘Overspeed’ state. In this state, fuel injection is disabled because the engine is above the maximum allowable speed.
The exit condition here is more complex than the entry condition, as it involves multiple chart symbols. To better understand the logic, pause the simulation.
Open the symbols pane to see what variables are involved. Stateflow will highlight the relevant ones and show their current value. Clicking on an individual symbol will in turn show where it is used in the chart. This can help you identify connections that are otherwise not explicit, such as the connection with the ‘Speed’ subchart. It also tells you that failing any other sensor will not affect this transition.
Now, reduce the engine speed and click on Step Forward to advance the simulation by a single time step. This shows the chart proceeding to this junction and taking the first transition, because there is only one failed sensor.
At the end of the step, the value of the ‘fuel_mode’ symbol flashes to indicate it has just changed, in this case from ‘DISABLED’ to ‘LOW’. With this, it is simple to monitor how the different symbols are affected during simulation.
Stepping forward once again transitions the ‘Running’ state from ‘Low_Emissions’ to ‘Rich_Mixture’, because of the failed EGO sensor. It took the chart two steps to get here because of this history junction, which makes a superstate resolve to its most recent child state. Was this the behavior you expected?
You can add breakpoints to transitions and states to debug unexpected chart execution. Stop the simulation and click on the ‘Add Breakpoint’ button on the Toolstrip to add one to the ‘Running’ state.
To understand how the simulation proceeds in detail, use the Step Over button. Stateflow will highlight the state in green, because it is currently being processed, and will also indicate that it is being entered.
This level of granularity will help you break down complex sequences of decisions when analyzing the logic of the chart.
Stepping over once more shows that the ‘Running’ state resolves into the ‘Low_Emissions’ state without taking a transition. Stepping over twice more shows the same happening to the ‘Normal’ child state. With this, the inner workings of a history junction become much clearer.
Is this a desirable behavior? Would you also use a history junction here or would you rather add a transition straight into ‘Single_Failure’?
With the powerful analysis and debugging tools of Stateflow, it is easy to try out different control logic designs and determine what is best for your system.
This fuel rate control system shows how Stateflow can be used to analyze complex and interconnected logic. For more information on modelling and understanding logic with Stateflow, visit the ‘Get Started’ page linked below.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本Japanese (日本語)
- 한국Korean (한국어)