Create a Hierarchy to Manage System Complexity
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.
To create a hierarchy of states, place one or more states inside 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.
Model a Media Player
This example models a media system consisting of an AM radio, an FM radio, and a CD player. During simulation, you control the media player by clicking buttons on the Media Player Helper user interface.
The media player is initially off. When you select one of the Radio Request buttons, the media player 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.
Implement Behavior by Using State Hierarchy
This example implements the media player incrementally by focusing on a single level of activity at a time. For example, these conditions are necessary for the CD player to enter fast-forward play mode:
You turn on the media player.
You turn on the CD player.
You start playing a disc.
You click the FF button.
The model uses a hierarchy of states to consider each one of these conditions individually. For instance, the top level of the hierarchy defines the operating modes as the media player turns on and off. The middle levels define the transitions between the different media player subcomponents, and between the stop and play modes of the CD player. The bottommost level of the hierarchy defines the response to the CD Request buttons when you meet all the other conditions for playing a disc.
In the Model Explorer, under the Mode Manager
chart, you can see the hierarchy of nested states that implement the behavior of the media player. To open the Model Explorer, in the Modeling tab, select Model Explorer.
At the top level of the hierarchy, the Mode Manager
chart has two states:
Eject
corresponds to the disc ejection mode, which interrupts all other media player functions.NormalOperation
corresponds to the normal operating mode for the media player.
The child states of NormalOperation
control the activity of the media player:
Standby
is active when the media player is off.ON
is active when the media player is on.
The child states of On
control the media player subcomponents:
CDMode
is active when the CD player subcomponent is on.AMMode
is active when the AM radio subcomponent is on.FMMode
is active when the FM radio subcomponent is on.
The child states of CDMode
control the activity of the CD player:
Stop
is active when the CD player is stopped.Play
is active when the CD player is playing a disc.
The child states of Play
control the play modes for the CD player:
Normal
is active during normal play mode.Rewind
is active during reverse play modeFastForward
is active during fast-forward play mode
This figure shows the complete layout of the states in the chart.
Explore the Example
The model in this example contains two other Stateflow charts:
App Interface
manages the interface with the UI and passes inputs to theMode Manager
andCD Player
charts.CD Player
receives the output from theApp Interface
andMode 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 app. To switch quickly between charts, use the tabs at the top of the Stateflow Editor.