0% found this document useful (0 votes)
42 views25 pages

SC1c Mod 2.B App-Manual 684 BB

The document discusses modeling finite state machines (FSMs) using STATEFLOW, a graphical extension of SIMULINK. STATEFLOW allows modeling event-driven reactive systems using state transition diagrams or charts. A STATEFLOW chart can be embedded within a SIMULINK model to create a hybrid system that is both time continuous and event driven.

Uploaded by

scribsunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views25 pages

SC1c Mod 2.B App-Manual 684 BB

The document discusses modeling finite state machines (FSMs) using STATEFLOW, a graphical extension of SIMULINK. STATEFLOW allows modeling event-driven reactive systems using state transition diagrams or charts. A STATEFLOW chart can be embedded within a SIMULINK model to create a hybrid system that is both time continuous and event driven.

Uploaded by

scribsunil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -1 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.

B -2

App. Overview 2.B - 2


2.B.1 STATEFLOW: a graphical extension of SIMUNLIK
Appendix Mod. 2.B

MATLAB / SIMULINK - STATEFLOW


Consulting as Manual  Subject:
Intensive Training Modeling and Simulation of Finite States Machines (= FSM) inside of the
MATLAB-SIMULINK environment.

 STATEFLOW = Special tool to model ‘event-driven reactive systems’.


App. Overview 2.B - 2
2.B.1 STATEFLOW: a graphical extension of SIMUNLIK  The ‘modes’ – better called “states” – are finite  Finite States Machines
(= FSM); that means: a definite number of states describes the total system !
App. Elements of STATEFLOW 2.B - 4
2.B.2 Operation of STATEFLOW / SIMULINK
 Comparison with SIMULINK  EVENTS:
 SIMULINK systems are calculated with special integration steps
App. Structures and Hierarchy 2.B - 32 independent of events. -
2.B.3 Groupbuilding and extended functions Events can be regarded additionally of course.
Typical block: ‘Hit crossing’.
App. Action Language 2.B - 45  STATEFLOW systems are only event triggered; i.e. they are calculated
2.B.4 Elements of execution inside of STATEFLOW only in case of internal or external events.

 Comparison with SIMULINK  STATE:


 SIMULINK Systems consist of state variables; they define a state vector.
Such a state vector describes the dynamic system over time:
x(t) = [ x1 x2 …. ].

 STATEFLOW Systems know states too, but they are only ‘true’ (= active)
or ‘false’ (= inactive).

 Example: ‘Traffic lights’


There are only the states: RED, YELLOW and GREEN, whereas
each state is ‘active’ or ‘inactive’.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 1 2.B - 2
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -3 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -4

 Representation of FSM: ‘Chart’ = ‘State Transitions Diagram’ ( = ‘STD’);


 A graphical editor will help us to perform a chart. App. Elements of STATEFLOW 2.B - 4
So we have a form of graphical programming. 2.B.2 Operation of STATEFLOW / SIMULINK
 All conditions for state transitions and actions are specified as ‘Labels’
by a so called ‘Action Language’.
2.B.2.1 Introduction 2.B - 6
 A STATEFLOW chart can be embedded in a SIMULINK environment. Inputs  To invoke StateFlow
and outputs interact between both the systems. 2.B.2.2 Graphical Elements of a Chart 2.B - 9
 In total we get a ‘hybrid system’.  States
 ’hybrid’ = time continuous and event driven ! 2.B.2.3. Chart Properties and Trigger Methods 2.B - 19
 Handling of events
 Running a hybrid system, SIMULINK generates a SIMULINK-S-function. 2.B.2.4 Non graphical Elements 2.B - 24
This S-function is produced automatically:  Events, Variables, Inputs/Outputs from/to SL
1. Production of C-code (for S-function) in subdirectory ‘sfprj’.
2. Compiling of C-code and generation of a ‘mex-file’ (*.dll in Windows).

 STATEFLOW requires a C compiler.


MATLAB provides the compiler “Lcc”.
(For the first time a setup is necessary: >> mex –setup  )

 During a run of STATEFLOW diagrams all active states are ‘animated’


(= highlighted).

 There are more tools available for a deeper analysis:


 Parser (checking semantic)
 Debugger (error checking step by step)
 Explorer (manipulating events, variables, …).

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 3 2.B - 4
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -5 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -6

2.B.2.1 Introduction 2.B – 5  Commands to generate an empty Chart:


 To invoke StateFlow >> stateflow  >> sfnew 
or: >> sf  only MODEL
 In STATEFLOW the basis are ‘state-afflicted’ elements; (= with a memory !)
SIMULINK
This opens parallel the MODEL
 But there are also ‘stateless elements’ (= without a memory) available to per- STATEFLOW LIBRARY  sflib:
form flow diagrams.

 All graphical elements are embedded in a Chart directly. >> sflib  % only LIB

 All non-graphical elements (e.g. events, data, …) are stored in a Data Dictionary.

 STATEFLOW is object oriented. Double


The mother of all objects is the Chart itself (= parent). click !

 Elements like ‘states’, ‘events’, ‘variables’ are children of the Chart.


A child object of ‘mother5’ is called ‘child1’. If you want to address the
child object, you use the MATLAB syntax: ‘mother5.child1’. The icon ‘Chart’ can be dragged and dropped
to SIMULINK model.

 STATE MACHINE (FSM) A double click on the icon


‘Chart’ opens the graphical
 A FSM consists of at least one ‘Chart’ in one SIMULINK model. STATEFLOW editor:
Bear in mind, before simulation all Charts have to form a single S-function,
using the C compiler ! So a state-machine consists of one C-program only ! State Tool

History Junctions

Default Transition

Connective Junctions

Zoom

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 5 2.B - 6
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -7 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -8

 HELP: Detailed info:  Basic elements of a STATEFLOW- Chart:


>> sfhelp  Introduction to Stateflow
Gives you a quick start at learning the fundamentals of Default Transition (e)
creating Stateflow diagrams.
 How Stateflow Works State1 State (a)
H
Introduces the fundamental concepts that are the
foundation of Stateflow. History Junction (d)
 Stateflow Notation [Condition1]
Explains the graphical and nongraphical symbolism used to Label (only condition) (f)
create Stateflow diagrams. event1[Condition2]
event1 Connective Junction (c)
 Printing: State3 Label (event + condition)
State2
The content of a Chart is printed out:
a) via menu: File / Print Current View Transition (b)
/ to File
/ to Clipboard K12_1_BasicElements.fig
States (a)
b) by command: >> sfprint 

 Saving:
A ‘Chart’ is saved as a *.mdl-file  SIMULINK-file !

>> sfsave  The actual SIMULINK-file, including Chart,


is saved !  Extented graphical elements of a STATEFLOW- Chart:

 Basic Elements:  g) Hierarchy


A Chart consists of seven basic elements: See App. 2.B.3
 a) States (rounded rectangles) via “State Tool”  h) Composition of objects
 b) State Transitions (arcs) via Left Mouse
 c) Connective Junctions (circles) via “Con. Junc. Tool”
 d) History Junctions (H-circles) via “Hist. Junc. Tool”
 e) Default Transition (dot arc) via “Def. Junc. Tool”
 f) Labels at States and Transitions via Text Editor
e.g. event1
entry: action1
exit: action2
event2[condition2]
event3[condition3]{action3}

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 7 2.B - 8
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -9 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -10

2.B.2.2 Graphical Elements of a Chart 2.B -9


 States  Every state is named at very beginning automatically with: “?”
We always have to give the state a new and unique name (compulsory).
a) STATES:  The label of a state can be followed by some optional actions.
A typical action is the change of the value of a variable.
 A state describes a mode of an event-driven system. The activity or inac-
tivity of the states dynamically changes based on events and conditions. Examples of a state
 There are two types of states: untitled/Chart
inclusive actions
State1/ State1/
Exclusive States (= OR-states) Parallel States (= AND-states) entry: action1; en: action1; Right: shortcut !
during: action2; du: action2;
* have solid borders * have dashed borders. exit: action3; ex: action3;
* modes are mutually exclusive * always active at the same time on event1: action4 on event1: action4
* selected by “Decomposition” * selected by “Decomposition” Pri nted 26-Jul- 2004 22:06:24

“exclusive” “parallel”
(Right Mouse  superstate) Beside the state name “State1” you find four different actions, starting
with the keywords: entry, during, exit and on event.

K12_1a_STATES/Chart
 Below an example: LEGOROBOT
S1 SuperState
P1 1

P2 2
FORW ARD
S2
entry:
after(T_TURN, SysClock)
after(T_TURN,SysClock) dir_motor_left=1;
dir_motor_right=1;

touch_sensor_right
TURN_RIGHT
TURN_LEFT entry:
P3 3 entry: dir_motor_left=1;
S3 dir_motor_left=2; dir_motor_right=2;
dir_motor_right=1; exit:
exit: lamp=0;
lamp=0;
touch_sensor_left

LEFT RIGHT after(T_BACK,SysClock)


after(T_BACK,SysClock)
Chart with 'Exclusive States' Chart with 'Parallel States'
BACkW ARD
entry:
dir_motor_left=2;
dir_motor_right=2;
The sequence of execution is numbered lamp=255;
K12_1c_ROBOT.fig
(upper right)

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 9 2.B - 10
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -11 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -12

Entry Action: During Action:  Remark:

 This action is executed if we come  This action is executed as long If we need the state activity outside of the Chart – that means in SIMULINK -
into this state via a valid transi- as no transition off the state is va- we select the button ‘Output State Activity’ in the menu ‘Properties’ (Right
tion. lid. Mouse Click on ‘State1’):

 If we end the line with a comma,  The frequency of execution of a


(instead of a semicolon) the re- During Action depends on “Trig- Effects:
sult of the action is displayed in ger Method”. (Details later).  In Data Dict. a new vari-
MATLAB-Command-Window. able ‚State1’ is defined.
 Syntax: see  Entry Action
 An outport at the Chart
 An Entry-Action can be placed
is showed for use in
in the 1st line after ‘StateName’,
SIMULINK
without the ‘entry’ key, but a ‘/’
 Outport = 1  State
at the end of ‘StateName/’.
active
Exit Action: On-Event Action: Outport = 0  State
 This action is executed if a valid  This action is executed if the state inactive
transition leads to another state. is active and the named event oc-
 Syntax: see  Entry Action curs.

State 1

S cope
K12_1_Label1/Chart What is displayed on Chart
screen, if we define the
K12_1_Label1.mdl
condition as:
State1/ [cSF == 0]
en: aSF = 3
[cSF == 3] ?
cSF = 0
ex: bSF = aSF*aSF
State2/ Here: integration step ‘1 sec’
en: aSF = 6

K12_1_Label1/Chart
Pri nted 30-Jul- 2004 20:02:34

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 11 2.B - 12
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -13 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -14

b) TRANSITIONS and LABELS:  Example:

 Transitions exist between two States or between States and Junctions. K12_1_Transition1/Chart

 A transition is a graphical object that links one object to another. One end S1
of a transition is attached to a source object and the other end to a destina- Event_S2 [ a ==4] {b = 5} / c = 6;
tion object.

 The source is where the transition begins and the destination is where the
transition ends.
S2
 A transition label describes the circumstances under which the system
moves from one state to another.
K12_1_Transition1/Chart
 It is always the occurrence of some events that causes a transition to take
place.

 The evaluation of transitions happens according to following pattern:


 Source has to be active.  Only if ‘Event_S2’ occurs, the transition above will be valid.
 All transitions going out from the object are checked:
valid or invalid ?  “a==4” has to be fulfilled additionally to get a valid transition
 The valid transition is executed.
(Bear in mind: the labels should allow only one transition to be  “b = 5” is only assigned, if ‘a == 4’ is true. This happens before
valid [OR-States]) the transition is really performed

 A transition can be provided with Labels. Such a Label describes the con-  “c = 6” is assigned, if ‘Event_S2’ occurs and ‘a == 4’ is true.
dition the transition is valid ! This happens after the transition is valid and performed.
A Label can consist of: an Event
a Condition
a Condition Action
a Transition Action
Syntax:
Event [Condition]{ConditionAction}/TransitionAction

The Transition is valid if the Event occurs a n d the Condition is true.


The ConditionAction is executed b e f o r e the transition is really per-
formed; the Condition has to be true.
The TransistionAction is only performed as a definite destination is
found and Event and Condition are both true.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 13 2.B - 14
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -15 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -16

c) Connective Junctions:
Default transition  A connective junction is a point of decision.
State1  We can construct: - for-loops
H

Decision point: - do-while loops


if … then …. - if-then structures
[Condition1] elseif ….  Connective junctions allow the construct ‘flow diagram’ ( = a FSM
end without states).
end
event1 event1[Condition2]  Basically a transition through a junction is executed only if the total path
from source to destination is valid.
see below:
State2 State3 Connective Junctions  Are more then one path valid, no preference is calculated if the transitions
are equivalent. We have to look for only one valid path in that case.

K12_1_BasicElements.fig
Transistion of one source to two destinations:
Chart_12_junctA/Chart

S2
 In STATEFLOW exist three different types of transitions:
1. ‘Standard Transition’
Connection between Source and Destination Objects.
A Standard Transitions is performed, if the total path is E_one
valid. (See examples above). S1

2. ‘Default Transitions’
We get this transition from menu ‘Default Transition Tool’ of
E_two S3
Graphical Editor. – Default Transitions don’t have a source.
If no Label (incl. a Condition) is used (like in the Chart above),
this transition is always analyzed but is only performed as long Chart_12_junctA.mdl
as no other state is active. This situation is given at very be- Pri nted 01-Aug-2004 20:44:42

ginning; so the FSM starts with ‘State1’.


 At start State S1 is active.
3. ‘Inner Transitions’
 The first transition (from S1 till junction) is executed for each
They go from a Superstate to a Substate. – They don’t leave
event (no labeling !).
the Superstate  App. 2.B.3
 If this event was neither ‘E_one’ nor ‘E_two’ pathing is stopped; the
Chart remains in S1.
 If this event was ‘E_One’ so we end in State S2.
 If this event was ‘E_Two’ so we end in State S3.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 15 2.B - 16
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -17 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -18

Transistion of two sources to one destination: FOR-Loop:


Chart_12_junctB/Chart Chart_12_junctD/Chart

E_alpha
S3 S1
S1

E_one {i =0}

E_S1
[i<10]{i++;k = 2*k}
E_beta
S2
S2
E_S2

Chart_12_junctB.mdl

Chart_12_junctB.mdl Pri nted 01-Aug-2004 21:21:56

Pri nted 01-Aug-2004 21:00:36

 If event ‘E_one’ occurs the transition from S1 to S2 is valid.


 At start State S1 (via event E_alpha) or State S2 (via event E_beta) Assignment: i = 0.
is active.  The transition [i<10]{i++: k = 2*k} has a condition and preceeds
 If S1 is active and event E_S1 occurs, the total transition from a transition without a condition.
S1 to S3 is valid.  This transition is executed more then ones, till condition [i<10] is
 If S2 is active and event E_S2 occurs, the total transition from anymore valid.
S2 to S3 is valid.
 If S1 is active and event E_S2 occurs, the total transition from If-Then-structure:
S1 to S3 is not valid. Chart remains in S1. Chart_12_junctE/Chart

 Etc. Flow diagram:


Chart_12_junctC/Chart
[a>b] if a>b
Self Loop Transistion: if a>c
[a>c]{action1} action1;
S1 else
 For event ‘E_two’ the action2;
transition S1 to S2 is {action2} end
valid. else
 For event ‘E_one’ the action3;
E_two z = z+1;
transition S1 to S1 is {action3; z++} end;
valid (self loop). E_one S2
This is favourably for Chart_12_junctE.mdl
counters or activating Printed 01-Aug-2004 21:35:44

events.
Chart_12_junctB.mdl

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 17 2.B - 18
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -19 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -20

2.B.2.3. Chart Properties and Trigger Methods 2.B - 19  Inherited / Triggered Update Method:
 Handling of events  An explicit external trigger signal (from SIMULINK) is necessary.
 This external signal is defined in Data Dictionary as an event;
 A STATEFLOW Chart is executed only by arising of events. (additionally to define: rising, falling or either edge of trigger signal).
(Bear in mind: FSM = event driven !)
 Events can be generated:  internal Chart Menu  Add  Event  Input from Simulink  Rising Edge
 implicit (e.g. by activating a state)
 external
 At beginning of a simulation the Chart is inactive (= calm). To start
the FSM we need at least one external event, produced mostly by a Default
Transition. This event wakes up the Chart.
 The rules activating a Chart by an external event can be designed individually
for each Chart. This is called: Individual Chart Update Method !
 We differentiate between three methods activating a Chart:
 Inherited Method
 Discrete Method
 Continuous Method
 This three methods are chosen from menu: File/Chart Properties:
Special case: Function Call:
Inherited is the Stan-
dard-Update-Method Normally SIMULINK controls the execution of all subsystems;
a Chart is a SIMULINK subsystem too.

If we use the trigger method ‘Function Call’ then STATEFLOW


itself controls the execution of the Chart.

We use this method e.g., if the trigger source is another


STATEFLOW Chart.
Using this button, That means:
we initialize the Chart A Chart is activated by another Chart without any control
independent of an of SIMULINK.
Update Method.
(All Default Transit-
ions are checked)

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 19 2.B - 20
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -21 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -22

 Example 1: Triggered Update  Rising Edge  Example 2: Triggered Update  Either Edge

ScopeData
2
ScopeData
2

1.5

1.5 Step Step1


Step 1

y
S cope
S cope 1

y
State1
State1 0.5

0.5
O ut 300 0
O ut 0 0 2 4
sec
6 8 10

0
0 2 4 6 8 10 Chart Display
Chart Display sec

Step at t = 1 sec "0" to "1"


Step at t = 1 sec "0" to "1" Chart_12b.mdl
Chart_12a.mdl Step1 at t = 2 sec "0" to "1"
Step Size: 1 sec
Step Size: 1 sec

Chart_12a/Chart MATLAB screen:


Chart_12b/Chart MATLAB screen:
aSF =
State1/ 3 aSF = 3
en: aSF = 3 cSF = State1/ cSF = 1
cSF = 1
dConst 1 en: aSF = 3 dConst = 100
[cSF == 1] cSF = 1
ex: bSF = dConst*aSF dConst = dConst bSF = 300
[cSF == 1]
100 ex: bSF = dConst*aSF aSF = 6
State2/
en: aSF = 6 >> State2/ Out = 300
Chart_12a/Chart Out = bSF
en: aSF = 6
Chart_12b/Chart Out = bSF
Pri nted 01-Aug-2004 15:55:21
>>
Pri nted 01-Aug-2004 16:19:25

Comments:
Comments:
o Chart is activated by a step signal  Trigger signal from
SIMULINK (rising edge at t = 1 sec): o Chart is activated by a step signal  Trigger signal from ‘step’
 State1 becomes ‘active’, because of the Default Transition. SIMULINK (rising edge at t = 1 sec):
 State1 becomes ‘active’, because of the Default Transition.
o The Chart remains in State1, because no second trigger takes place.
The Scope – showing State1 activity - remains always at “1”, beginning o At t = 2 sec a second Trigger Signal (Step1) takes place, here as with
at 1 sec. a falling edge (negative sign):
The Chart moves from State1 to State2 !.
o Variable bSF is not assigned ! We don’t leave State 1 !

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 21 2.B - 22
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -23 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -24

 Inherited Update Method:  Contiuous Update Method:


 An implicit trigger signal from SIMULINK controls the Chart . SIMULINK generates an implicit trigger signal of each integration step.
 These implicit triggers are the sampling time points of the SIMULINK That includes extra time points too.
model.
 The sampling rate is bequeathed by an input signal coming from
SIMULINK. 2.B.2.4 Non graphical Elements 2.B -24
 Events, Variables, Inputs/Outputs from/to SL
 Example:  Non graphical elements are:
 Events
 Variables
 Inputs from SIMULINK
State1  Outputs to SIMULINK
Scope
In
Out  Before use all these non graphical elements have to be declared in the
Sine Wave
Data Dictionary.
Chart
Scope1  The content of the Data Dictionary is regarded by the menu Tools/Explore.
Chart_12c.mdl

 Example: Chart_13a

a) SIMULINK-Modell
Here the sampling time of the
SINE – referred to ODE45 – is
used. This time is variable because
of the ‘variable-step-integration’.
(here 0.2 sec). 15
State1 Scope
State2
In State3
 Sampled Update Method: State4
Out 0.6
 An implicit trigger signal of 150
Chart Display
SIMULINK is generated.
It has exactly the provided
rate. Chart_13a_NonGraph.mdl Integration step: 1 sec

Here Chart_12c.mdl:
Sampling time: 2 sec
(provided in menu
’Properties’)  “In” is set to 15 or 150 by a manual switch.
 “State1” till “State4” show the active states over time in a scope.
 “Out” is a result of the Chart, transmitted to SIMULINK.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 23 2.B - 24
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -25 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -26

b) Chart e) Tool/ Explore (open: “State1” = child of Chart)

Chart_13a_NonGraph/Chart

State3/
en: aSF = 3
Out = bSF*aSF
State1/
en: aSF = 1
cSF = 10 [cSF==0]
dConst E_one
bSF = In*aSF/dConst
ex: cSF = 0
State2/ State4/
en: aSF = 2 en: aSF = 4
Out = bSF*aSF Out = bSF*aSF
[bSF >1]{E_one} c) Scope
ScopeData
Chart_13a_NonGraph/Chart 1
Pri nted 04-Aug-2004 20:48:31 y1 0.5
0 f) Results
10 2 4 6 8 10
y2

0
-1
In = 15 In = 150
10 2 4 6 8 10
y3

0.5
0
aSF = 1 aSF = 1
10 2 4 6 8 10
y4

0.5
cSF = 10 cSF = 10
0
0 2 4 6 8 10
sec
dConst = 100 dConst = 100
d) Tool/ Explore (open: “Chart” = parent)
bSF = 0.1500 bSF = 1.5000

cSF = 0 E_one
cSF = 0
aSF = 3
aSF = 2
Out = 0.4500
Out = 3
aSF = 4

Out = 0.6000

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 25 2.B - 26
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -27 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -28

c) Chart
 Example: Chart_13b

a) SIMULINK-Modell
Chart_13b_NonGraph/Chart

State3/
en: aSF = 3
Signal 1 Out = bSF*aSF
State1/
Signal Builder en: aSF = 1
cSF = 10 [cSF==0]
dConst E_one
15 Scope
bSF = In*aSF/dConst
State1 ex: cSF = 0
State2 State2/ State4/
In State3 en: aSF = 2 en: aSF = 4
Out = bSF*aSF Out = bSF*aSF
State4
[bSF >1]{E_one}
d) Scope
Out 0
150 ScopeData
Chart Chart_13b_NonGraph/Chart 1
Display

y1
Pri nted 04-Aug-2004 21:18:21
0.5
0
10 2 4 6 8 10
Chart_13b_NonGraph.mdl Integration step: 1 sec

y2
0
-1
10 2 4 6 8 10

y3
0.5

 “In” is set to 15 or 150 by a manual switch. 0


10 2 4 6 8 10

 “State1” till “State4” show the active states over time in a scope.

y4
0

 “Out” is a result of the Chart, transmitted to SIMULINK. -1


0 2 4 6 8 10


sec
“Signal1” is an triggered input from SIMULINK to the Chart.
The Chart is adjusted to ‘Tiggered’, so the Chart steps state
by state unter control of this triggered input signal. e) Tool/ Explore (open: “Chart” = parent)

b) Signal-1:

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 27 2.B - 28
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -29 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -30

f) Results  Property-Dialogue-Windows (Explorer):

In = 15 In = 150

aSF = 1 aSF = 1

cSF = 10 cSF = 10

dConst = 100 dConst = 100

bSF = 0.1500 bSF = 1.5000

cSF = 0 E_one
cSF = 0
aSF = 3
aSF = 2
Out = 0.4500
Out = 3
Local Variables: See above ‘Data: bSF’
 Scope/ select: ‘Local’
 Additionally: Type, Fixed Point, Limit Range,
etc.
 Initialize from: ‘data dictionary’ or ‘work-
 Declaration of variables or events: space’
Way 1: In graphical Editor of STATEFLOW variables and inputs/outputs Local Events: See above ‘Event: event’
(of/to SIMULINK) are added to the Chart by menu: Add | Data.  Events without a connection to SIMULINK are
local (= Scope: ‘Local’)
Equivalent we add on events to the Chart by menu: Add | Event.
Input Events from  Scope/ select: ‘Input from Simulink
Way 2: In STATEFLOW Explorer we choose the menu Add | Event or
SIMULINK: (Chart gets a triggered input. If more than
Add | Data.
one triggered input: vectorised input; ex-
In both cases a standard name is assigned (“event”, “data”). We ample: ‘Chart_13b’).
rename simply these names.  Trigger edge: Rising, Falling, Either, Function
Using the shortcut-menu (right mouse/Properties) you can Call.
change all relevant features (see below). (N.B: an edge requires a pass through zero;
correct edges: ‘0  1’, ‘1  0’, ‘-1  1’;
incorrect edge: ‘1  2’.
Function Call: instantaneous operation; no
delay to next integration step.)

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 29 2.B - 30
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -31 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -32

Output Events to  To activate other Charts or triggered SIMU-


SIMULINK: LINK subsystems
App. Structures and Hierarchy 2.B - 32
(An additional output appears at the Chart,
2.B.3  Groupbuilding and extended functions
named with the event).
See below: “ event23”.

Input Data from  Scope: ‘Input from SIMULINK’. 2.B.3.1 Superstates 2.B - 34
SIMULINK: See below: “Sine_Input”.  Simplify a chart
2.B.3.2 Boxes 2.B - 41
Output Data to  Scope: ‘Output to SIMULINK’.  To organize the chart
SIMULINK: See below: “Scope_Output”. 2.B.3.3 Subcharts 2.B - 42
 Alternative to Superstates
Constants:  Special type of a local variable. 2.B.3.4 Graphical Functions 2.B - 44
They can’t be changed by the Action Language  flow diagrams
Temporary Variables:  Temporary Variables exist only, if the parent
object is active.
Good example: Counter variables.

 Example:

Pulse
Generator
Scope_Output

Sine_Input Scope

Sine Wave event23

Chart

In1 Out1

Triggered Scope1
Subsystem

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 31 2.B - 32
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -33 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -34

2.B.3.1 Superstates 2.B – 34


OVERVIEW:
 Simplify a chart
 In App. 2.B.2 we generated all graphical objects (states, transitions, labels) in
the upper-most hierarchy level of the Chart.  A superstate is a parent object for some other states (= children).
 A superstate is active, if the superstate is the target of a transition or if
 STATEFLOW allows you different ways to group these objects: one of its children objects is active.
 to get a better survey on the Chart
 to get new functionalities (e.g. parallel actions)  A stand alone superstate is never active; at least one of its children objects
has been active.
 A superstate acts as a normal state inside the environment (labeling, transi-
tiones, …)
 The functionality of superstates depends on the type:
 Type-1: Exclusive (OR - states)
(to break down the Chart in different substates)
 Type-2: Parallel (AND – states)
(parallel course of actions)

 To generate a superstate:
 Generate a normal state in STATEFLOW editor.
 Increase this state (by dragging / left mouse).
 Insert new states into this increased state. The first one
becomes a superstate.
(By double clicking the grouping becomes visible).

 The alignment (Exclusive or Parallel) is changed by marking


the superstate and the selection: Decomposition.

Decomposition / Exclusive: Decomposition / Parallel:


untitled/Chart untitled/Chart

? ?
? ? 1

? ?
2

Pri nted 08-Aug-2004 15:03:40

Printed 08-Aug-2004 15:04:55

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 33 2.B - 34
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -35 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -36

a) Example: EXCLUSIVE-States:  Generating the events (SIMULINK environment):


 Following automaton exists of two superstates: S1 and S2.
 Both superstates have inside two substates each, called On and Off. 1

Constant
on_off
0

Constant1
Chart_21 a_Superstate/Chart

off_on

S1
S2
switch_12
On
on_of f
switch_1 2 On
on_off Chart

of f_ on Of f of f_ on
Of f
switch_21
switch_2 1

 Event ‘on_off’ occurs:


Pri nted 08-Aug-2004 16:08:07

 “S1.On” = inactive  “S1.Off” = active.

 The states are controlled by 4 events, coming from SIMULINK:  Event ‘on_off’ occurs again:
 “S1.Off” = inactive  “S1.On” = active.
 Event ‘switch_12’ occurs:
 “S1” = inactive  “S2” = active
(in this connection it is irrelevant, whether ‘S1.On’ or ‘S2.Off’
is active; in any case S2 will be active).
 Inside S2: “S2.Off” = active, because of Default Transistion.
 Event ‘on_off’ occurs again:
 “S2.Off” = inactive  “S2.On” = active.
 Event ‘on_off’ occurs:
 “S2.On” = inactive  “S2.Off” = active.
 Event ‘switch_21’ occurs:
 “S2” = inactive  “S1” = active
 Inside S1: “S1.On” = active, because of Default Transistion.
 Starting the automaton, superstate S1 will be active because of the Default
Transition.
 Now STATEFLOW decides, which internal state of S1 has to become  Different states have not to model with a large number of single
active: Because of the Default Transition: S1.On; this state is active states, if we use a hierarchical structure:
if we enter S1.  Excellent survey.
 Simplified Chart.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 35 2.B - 36
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -37 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -38

b) Example: EXCLUSIVE-States with HISTORY Junctions: d) Example: EXCLUSIVE-States with Inner Transistions:
 The example is exactly the same as before. Only in superstate S2 we  Problem:
installed a History Junction: H 1. An automaton has three states S1, S2 and S3. We go to one of these,
depending on the condition [cond]:
 The History Junction provokes, coming back to S2 to start with the
IF (cond == 1) THEN S1,
last active state in S2.
IF (cond == 2) THEN S2,
 The Default Transition is used only once, if S2 was not yet active. IF (cond == 0) THEN S3.
2. Initializing the automaton starts with state S3.
3. After the event ‘update’ all conditions have to be rechecked in order to
History change into a new state.
Chart_21 b_Sup erstatehistory/Chart
junction
S1
 Solution A:
S2
switch_12 H Chart_21d_Innertransition1/Chart
On
on_off On
on_off

off_on Off
off_on Off
switch_21

Pri nted 08-Aug-2004 16:47:10

[cond==1]
[cond==2]

S1 S2 S3
c) Example: EXCLUSIVE-States with Supertransistions:

Chart_21 c_Superstate2 /Chart

S1
S2
update
On
on_off On
on_off

Pri nted 08-Aug-2004 17:54:25


off_on switch_12
Off
off_on Off

switch_21

Pri nted 08-Aug-2004 17:00:09


Default Transition:
S3 is selected, because a transition without a label has higher priority.

At event „switch_21“ we go directly


from „S2.Off“ to „S1“ !

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 37 2.B - 38
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -39 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -40

 SIMULINK environment: e) Example: PARALLEL-States:


 Parallel states are quasi parallel in the same hierarchy level.
0  The execution of states depends on the placement (upper/left to lower/right,
see internal enumeration).
update
1  The ignition of a passenger car has two states: On and Off.
 Only if ignition is ON, we can switch on the Lights and/or the
0
Air Condition.
cond
1  The Lights can be switched On or Off.
2 Chart  The Air Condition has the states Off, Stage1 or Stage2
condition

 Solution with parallel states: Chart_21e_Parallel.mdl

 Solution B: more elegant by an inner transition


Chart_21d_Innertransition2/Chart

Super_S
update

[cond==1]
[cond==2]

S1 S2 S3  SIMULINK environment:
1
Constant
ignition
0
Constant1
Pri nted 08-Aug-2004 18:10:11

lights

A superstate is active, if a child-object is active. - So the inner stage

switch
transition ‘update’ could be always valid, if Super_S is active.
0 Chart
stufe0
Say, we are in state S3 and the event ‘update’ occurs. We leave S3 and 1
via ‘update’ we come back to the decision point. stufe1
2
stufe2

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 39 2.B - 40
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -41 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -42

2.B.3.2 Boxes 2.B - 41 2.B.3.3 Subcharts 2.B - 42


 To organize the chart  Alternative to Superstates

 Boxes are visual units to organize a Chart.  Inside of a chart you can create more other charts  Subcharts
 They are not real elements in a FSM.  These subcharts have the same properties as the supercharts, only the visual ar-
rangement is different.
 A lot of STATEFLOW elements can be assembled in a box.
 To establish a Subchart make first a Superstate around all the objects you need.
 The effect of boxes is only the turn of execution. A box has a higher
Mark this Superstate and change it by the Shortcut menu Make Contents / Sub-
priority than all other parallel superstates. This superstates have to placed be-
charted to a Subchart.
low or right in the chart.

 Below we change Superstate Z2 into a Subchart:


 To establish a box:

1. Produce first a superstate for ‘State2’ and ‘State3’.


2. Take Shortcut menu Type/Box to create the Box

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 41 2.B - 42
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -43 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -44

 The Subchart Z2 s now a Child of main Chart. It opens by a double click. 2.B.3.4 Graphical Functions 2.B - 44
 flow diagrams
 Like in Superstates you can make Transitions into Subcharts (Supertransitions):
 A graphical function is a flow diagram without states.
 The graphical function can be called by all actions (e,g. Transitionsaction) and
you can use the results.
 Fundamentally a graphical function is only apparent inside the mother object and
all other child objects.
 To establish a graphical function you start with the icon ‘function’ = f( ).
 In the label of the function you have to note the prototype.
Syntax:
y = f1(a1, a2 a3)

The return value y and the arguments have to be defined in the Data Dictionary.
 Example:
You need the product of three parameters:

 The essential advantage of Subcharts in comparison to Superstates is the effect,  When you invoke the graphical function the formal parameter have to be replaced
that the content is masked in Subcharts. By minimizing the Subchart icon you can by numbers or by other defined variables; here m,n,k.
economise the place on your screen.
The variables m,n,k have to be defined in the Data Dictionary already !

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 43 2.B - 44
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -45 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -46

2.B.4.1 Keywords 2.B - 46


App. Action Language 2.B - 45  Special meanings of commands
2.B.4
during The following action is active during the state is active
du
entry The following action is executed when entering the state
2.B.4.1 Keywords 2.B - 46 en
 Special meanings of commands exit The following action is executed when leaving the state
2.B.4.2 Numerical Operators 2.B - 46 ex
 Not valid for matrices in (state_name) Condition is true, if the state state_name is active
2.B.4.3 Logical Operators 2.B - 46
 comparison of two operands on event_name The following actions are executed, if the event
2.B.4.4 Unary Operators 2.B - 47 event_name occurs.
 see language C
2.B.4.5 Calling MATLAB-functions 2.B - 47
 ml operator
2.B.4.6 Variables and Events in Action Language 2.B - 48 2.B.4.2 Numerical Operators 2.B - 46
 Broadcasting  Not valid for matrices
2.B.4.7 Temporary logical oprerators 2.B - 49
 repetitive events a+b Addition of two operands (not matrices)
a-b Subtraction of two operands (not matrices)
a*b Multiplication of two operands (not matrices)
a/b Division of two operands (not matrices)

2.B.4.3 Logical Operators 2.B - 46


 comparison of two operands

a==b Comparison of equality of two operands


a~= b Comparison of inequality of two operands
a != b
a >= b Greater than or equal
dto. <, >, <=
a && b Logical AND
a&b bitwise AND
a || b Logical OR
a|b bitwise OR

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 45 2.B - 46
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -47 Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -48

2.B.4.4 Unary Operators 2.B - 47 2.B.4.6 Variables and Events in Action Language 2.B - 48
 see language C  Broadcasting

~a NOT a (bitwise)  Variables or events have to be in the same hierarchy. Otherwise STATEFLOW
!a Logical NOT cannot find the variables or events.
a++ Increment by one  If you want to use a variable from an other mother object, you have to apply the
a-- Decrement by one full path:
a = expression Simple assignment A local variable x inside of State S1.Open is found by: S1.Open.x
a += expression a = a + expression
dto: -=, *=, /=  Event broadcasting:
In Action Language you can start an event immediately. The event and his scope
has to be defined in the Data Dictionary.

2.B.4.5 Calling MATLAB-functions 2.B - 47 o Undirected Broadcast:


 ml operator all acceptors of the chart see this event.

 In Action Language assignments we can use MATLAB functions. The result is o Directed Broadcast:
assigned to a variable in the Data Dictionary. only a special state will see your event

 Example: In a Condition Action the variable a needs the value of sin(x)  Example 1: Undirected Broadcast  Synchronization of parallel states

 Using variables from the MATLAB Workspace, use the ‘ml’-operator:


ml.x = ml.a + ml.b + ml.c

 Using variables from the STATEFLOW dictionary, use the ‘ml’-operator and C  If event e1 occurs, we go from S1.A1 to S1.A2.
syntax:
 Entering S1.A2 we start the undirected event e2 by the ‘entry statement’; so
[Cond == 1]{ a = ml(‘sin(%g)’,d1)}
we go from S2.A1 to S2.A2. - e1 and e2 are parallel now.

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW
2.B - 47 2.B - 48
Prof. H. Kull FSM - STATEFLOW App. 2.B 2.B -49

 Example 2: Directed broadcast

 Event e2 is now apparent only in S2 !

2.B.4.7 Temporary logical oprerators 2.B - 49


 repetitive events

after(n,E) Becomes true, if event E occurs min. n-times

before(n,E) Becomes true, if event E occurs max. n-times

at(n,E) Becomes true, if event E occurs once

every(n,E) Becomes true, if event E occurs exactly after n-times

Example: du: on after(5,E); action_during;

The action ‘action_during’ is exexcuted, if event E occurred at least


5-times

SC1c_Mod_2.B_App-Manual_684b.doc Appendix 2.B MATLAB - STATEFLOW


2.B - 49

You might also like