Add structure to your model one subcomponent at a time by creating a hierarchy of nested states. You can then control multiple levels of complexity in your Stateflow® chart. For more information, see Model Finite State Machines.
To create a hierarchy of states, place one or more states within the boundaries of another state. The inner states are child states (or substates) of the outer state. The outer state is the parent (or superstate) of the inner states.
The contents of a parent state behave like a smaller chart. When a parent state becomes active, one of its child states also becomes active. When the parent state becomes inactive, all of its child states become inactive.
This example models a stereo system consisting of an AM radio, an FM radio, and a CD player. During simulation, you control the stereo system by clicking buttons on the Media Player Helper user interface.
The stereo is initially in standby mode (OFF). When you select one of the Radio Request buttons, the stereo turns on the corresponding subcomponent. If you select the CD player, you can click one of the CD Request buttons to choose Play, Rewind, Fast-Forward, or Stop. You can insert or eject a disc at any point during the simulation.
Initially, the complete implementation of this stereo system appears rather complicated. However, by focusing on a single level of activity at a time, you can design the overall system design incrementally. For example, these conditions are necessary for the CD player to enter Fast-Forward play mode:
You turn on the stereo.
You turn on the CD player.
You playing a disc.
You click the FF button in the UI.
You can construct a hierarchical model that considers each of these conditions one at a time. For instance, the outermost level can define the transitions between the stereo turning on and off. The middle levels define the transition between the different stereo subcomponents, and between the stop and play modes of the CD player. The bottommost level defines the response to the CD Request buttons when you meet all the other conditions for playing a disc.
To implement the behavior of the stereo system,
the hierarchy of nested states listed by the Model Explorer under the Media Player Mode
Manager chart. To open the Model Explorer, in the Modeling tab,
select Model Explorer.
This table lists the role of each state in the hierarchy.
|Top level (Media Player Mode Manager chart)||Normal operating mode for stereo system|
|Disc ejection mode (interrupts all other stereo functions)|
Stereo system activity (child states of
|Stereo system is in standby mode (OFF)|
|Stereo system is active (ON)|
Stereo subcomponents (child states of
|AM radio subcomponent is active|
|FM radio subcomponent is active|
|CD player subcomponent is active|
CD player activity (child states of
|CD player is stopped|
|CD player is playing disc|
Disc play modes (child states of
|Normal play mode|
|Reverse play mode|
|Fast-Forward play mode|
This figure shows the complete layout of the states in the chart.
You can simplify the overall appearance of a chart with a complex hierarchy by hiding
low-level details inside subcharts, which appear as opaque boxes. The use of subcharts
does not change the behavior of the chart. For instance, in
sf_cdplayer, the stereo subcomponents
CDMode are implemented as
subcharts. When you open the Media Player Mode Manager chart, you see only three levels
of the state hierarchy. To see the details inside one of the subcharts, double-click the subchart.
sf_cdplayer contains two other Stateflow charts:
User Request manages the interface with the UI and passes inputs to the Media Player Mode Manager and CD Player Behavior Model charts.
CD Player Behavior Model receives the output from the User Request and Media Player Mode Manager charts and mimics the mechanical behavior of the CD player.
During simulation, you can investigate how each chart responds to interactions with the Media Player Helper. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.