Chart Hierarchy(chart hierarchy)
Introduction
Using hierarchy allows you to group states with common properties or functionality and to avoid redundancy in your Stateflow chart. Consider the operation of a traffic light. Traffic lights typically have a normal operating mode—where the light cycles between red, yellow and green—and a fault mode, in which the light changes to blinking red. You previously modeled a system similar to
this . It contained a Normal state and a Fault state, with some rules for when to transition between the two states. Grouping of states with common property or functionality and avoiding redundancy in state flow diagrams. Consider the operation of a traffic light. Traffic lights typically have a normal mode of operation in which the light cycles through red, yellow, and green, while In failure mode, the light turns blinking red. You modeled a similar system before. It consisted of a normal state and a fault state, and some rules for when to transition between the two states.)
To implement a traffic light with fault detection, the behavior of the light in each mode needs to be defined. In the lesson on temporal logic, you already modeled a traffic light in normal operation. It had three states: Stop, PrepareToStop, and Go . Using hierarchy, these three states become substates of the Normal superstate.
(In order to implement a traffic light with fault detection, the behavior of the light in each mode needs to be defined. is modeled. It has three states: Stop, PrepareToStop, and Go. Using a hierarchy, these three states become substates of the Normal superstate.)
A blinking light requires two states: BlinkOn and BlinkOff. These can be created as substates of Fault.
(A blinking light requires two states: BlinkOn and BlinkOff. These can be created as substates of Fault.)
With this structure, the transitions between the Normal and Fault states occur regardless of which substrate is active. Note that the example above contains a default transitionat each level of hierarchy . This is required for all hierarchical Stateflow charts.
(With this structure, transitions between normal and faulted states occur regardless of which substate is active. Note that the above example is in the hierarchy Each level of the contains a default transition. This is required for all hierarchical state flow diagrams.)
Click Next below to implement this hierarchical traffic light.
(Click Next below to implement this hierarchical traffic light.)
Practice
The states for normal operation have been placed in the chart for you. To create hierarchy, a state must fully encompass one or more other states. (Use the left mouse button to click and drag the boundary of a state to resize it.)
( The normal operating states have been placed in the diagram for you. To create a hierarchy, a state must entirely contain one or more other states. (Use the left mouse button to click and drag the border of a state to resize it.))
Task1 (task 1)
Create a new state, Normal , and make it a superstate of Stop , PrepareToStop and Go . Be sure to fully encompass all states and transitions . Be sure to fully include all states and transitions.)
Add a default transition to the Normal state.
(Add a default transition to the Normal state.)
Task2 (task 2)
It is often useful to model hierarchy from the top down: add the general functionality first, then fill in the details .
Add a new state, Fault , to the model, at the same level of hierarchy as Normal .
( Add a new state, Fault , to the model, at the same level of hierarchy as Normal .)
Create transitions with the following conditions. (Create transitions with the following conditions.)
Normal → Fault [fault == 1]
Fault → Normal [fault == 0]
The Symbol fault has already been defined for you.
The symbol fault has already been defined for you.
Task3 (task 3)
During a fault, the yellow and green lamps are always off, irrespective of the state of the red lamp.
(During a fault, the yellow and green lamps are always off, irrespective of the state of the red lamp.)
Add the following state entry actions to Fault .
(Add the following state entry actions to Fault .)
yellow = 0;
green = 0;
Task4 (task 4)
Stateflow automatically adds a default transition to the first state added at a given level of hierarchy.
(Stateflow automatically adds a default transition to the first state added at a given level of hierarchy.)
Add a new state within the boundaries of the Fault state . It will be created as a sub state with a default transition .
Name this state BlinkOn . (Name this state BlinkOn .)
Task5 (task 5)
Add another substate , BlinkOff , inside the Fault state . Add the following state entry actions to control the lights .
BlinkOn save = 1;
BlinkOff save = 0;
Task6 (task 6)
Add transition in both directions between BlinkOn and BlinkOff . Use the temporal operator after to pause for 1 second in each before transitioning. Pause 1 second before conversion.)