State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example •...

49
1 State Flow

Transcript of State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example •...

Page 1: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

1

State Flow

Page 2: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 3: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Example

3

Page 4: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Inside the Stateflow

4

Page 5: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Events/Data

Input event on_switch rising_edge

event off_switch falling_edge

data temp

Output data y

5

Page 6: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 7: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Stateflow

• Stateflow is an example of finite state

machine.

• A finite state machine reacts to events by

changing states.

7

Page 8: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Flow Diagram

• Stateflow also adds the feature of flow

diagram that provides decision points in

transition

8

Page 9: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Explain

• A junction has been added to the diagram

that checks to see if the outside

temperature is less than 30 degree.

9

Page 10: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Stateflow Objects

10

Page 11: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

State Hierarchy

• Stateflow supports the representation of

graphical object hierarchy.

11

Page 12: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 13: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 14: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Representing State Transition

14

Page 15: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

State Decomposition

• Exclusive (OR) state

• Parallel (AND) state

15

Page 16: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 17: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 18: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 19: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 20: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Example

20

Page 21: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 22: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Hierarchy Example

• Ride1.On

• Ride1.Off

• Ride2.On

• Ride2.Off 22

Page 23: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 24: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 25: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 26: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 27: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Example of Transition

27

Page 28: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Transition

• Junctions divide a transition segments

• Each segment is evaluated in the process

of determining the validity of full transition

28

Page 29: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 30: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Example

30

Page 31: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Connective Junction

31

Page 32: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

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

Page 33: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Transition to and from Exclusive states

33

Page 34: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Transitions to and from Junctions

34

Page 35: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Transitions to and from

Exclusive Superstates

35

Page 36: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Transitions to and from Substates

36

Page 37: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Self-Loop Transitions

37

Page 38: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Before Using an Inner

Transition

38

Page 39: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Inner Transition

• After using E_one inner transition

39

Page 40: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Using an Inner Transition to a

History Junction

40 History junction is used to remember the previous state

Page 41: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Default Transition

41

Page 42: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Default Transition to a Junction Example

42

Page 43: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Default transition with a Label

43

Page 44: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Connective Junction

• Provide more efficient generated code and

optimize memory use

44

Page 45: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Connection Junction- Self Loop

45

Page 46: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

For Loop Example

46

Page 47: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

STMF4 Example: Traffic Light

47

Page 48: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Example

48

Page 49: State Flowesl.ait.ac.th/.../UGMicroprocessor/stateflow_tutorial.pdf · 2016. 9. 9. · Example • The Stateflow has 2 states: Power_on and Power_off. • When the state machine is

Questions?

49