State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example •...
Transcript of State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example •...
1
State Flow
What is StateFlow?
• Stateflow is a graphical design and
development tool for control and
supervisory logic used in conjunction with
Simulink.
• It provides clear, concise description of
complex system behavior using finite state
machine theory
2
Example
3
Inside the Stateflow
4
Events/Data
Input event on_switch rising_edge
event off_switch falling_edge
data temp
Output data y
5
Example
• The Stateflow has 2 states: Power_on and Power_off.
• When the state machine is first turned on, we will use the
default transition.
• The default transition will turn to the initial state in this
case Power_off state.
• When we change the manual switch from Off to On, the
model sends an event (on_switch), that makes the
change from Power_Off state to Power_on state
• When we change the manual switch from On to Off, the
model sends an even (off_switch), that makes the
change from Power_On state to Power_off state
6
Stateflow
• Stateflow is an example of finite state
machine.
• A finite state machine reacts to events by
changing states.
7
Flow Diagram
• Stateflow also adds the feature of flow
diagram that provides decision points in
transition
8
Explain
• A junction has been added to the diagram
that checks to see if the outside
temperature is less than 30 degree.
9
Stateflow Objects
10
State Hierarchy
• Stateflow supports the representation of
graphical object hierarchy.
11
Example
• The diagram is the parent of Car_done
• The state Car_done is the parent of
Car_made and Car_Shipped
• The state Car_made is the pareant of
Parts_assembled and Painted
12
Texture representation
• The texture representation of the hierarchy
of object is as follows:
/Car_Done
/Car_Done.Car_made
/Car_Done.Car_shipped
/Car_Done.Car_made.Parts_assembled
/Car_Done.Car_made.Painted
13
Representing State Transition
14
State Decomposition
• Exclusive (OR) state
• Parallel (AND) state
15
Exclusive (OR) State
16
• Exclusive OR decomposition is used to
describe system model that are mutually
exclusive.
• From the example, either state A or state
B can be active. If state A is active, either
state A1 or A2 can be active at any one
time
Parallel (AND) State
• The children of parallel (AND)
decomposition parents are parallel states.
• All states at the same level are always
active at the same time.
• In the following example, when state A is
active, A1 and A2 are both active at the
same time.
17
Example
• In the following example, when state A
becomes active, both states B and C
become active at the same time.
• When state C becomes active, either state
C1 or state C2 can be active.
18
State Label Notation
• The label for a state appears on the top
left corner of the state rectangle with the
following format:
name
entry: entry actions
during: during actions
bind: events, data
exit: exit actions
on event_name: on event_name actions 19
Example
20
State Name
• A state label starts with the name of the
state
• Valid state names consist of alphanumeric
characters and can include underscore(_)
• The use of hierarchy provides some
flexibility in the naming of states.
21
Hierarchy Example
• Ride1.On
• Ride1.Off
• Ride2.On
• Ride2.Off 22
State Actions
• Entry Action: preceded by the prefix entry.
In the previous example, we set on_count =
0 whenever state On becomes active
• During Action: preceded by the prefix
during. In the previous example, state On
has two during actions, light_on() and
on_count++. These action are executed
when state on is already active and any
event occurs
23
State Actions
• Exit Action: preceded by the prefix exit. In
the previous example, state Off has the
exit action light_off(). If state Off is active
and becomes inactive, this action is
executed.
• On Event action name: preceded by the
prefix on event_name. . In the previous
example, state On has an on
power_outage action. If state On is active
and the event the power_outage occurs,
the action handle_outage is executed.
24
State Actions
• Bind action: preceded by the prefix bind.
In previous example, the data on_count is
bound to state On. Other states can use
the data, but cannot change the value.
Only state On or a child of state On can
change the value of data on_count
25
Transition
• A transition is a curved line with an
arrowhead that link one object with another.
• The source state is where the transition
begins, and the destination is where the
transition ends.
26
Example of Transition
27
Transition
• Junctions divide a transition segments
• Each segment is evaluated in the process
of determining the validity of full transition
28
Transition Label Notation
• The label consists of an event, a condition,
a condition action, and a transition action
event [ condition]{condition_action}/transition_action
29
Example
30
Connective Junction
31
Transition Notation
• Event: causes the transition to be taken.
Specifying an event is optional
• Condition: is a Boolean expression to
specify that a transition occurs given that
the specified expression is true.
• Condition Action: follows the condition for
a transition. It is executed as soon as the
condition is evaluated as true.
• Transition Action: is executed, once the
transition is completed (reached).
32
Transition to and from Exclusive states
33
Transitions to and from Junctions
34
Transitions to and from
Exclusive Superstates
35
Transitions to and from Substates
36
Self-Loop Transitions
37
Before Using an Inner
Transition
38
Inner Transition
• After using E_one inner transition
39
Using an Inner Transition to a
History Junction
40 History junction is used to remember the previous state
Default Transition
41
Default Transition to a Junction Example
42
Default transition with a Label
43
Connective Junction
• Provide more efficient generated code and
optimize memory use
44
Connection Junction- Self Loop
45
For Loop Example
46
STMF4 Example: Traffic Light
47
Example
48
Questions?
49