Sei sulla pagina 1di 51

StateFlow

HandsOn Tutorial
HS/PDEEC20100304
Jos Pinto zepinto@fe.up.pt
Session Outline
Simulink andStateflow
NumericalSimulationofODEs
InitialValueProblem(Handson)
ODEs withresets(Handson)
FiniteStateMachines
FSMs inStateflow (Handson)
DiscreteEventSystems
DESs inStateflow (Handson)
HybridAutomata
HybridSystemsinStateflow (Handson)
Simulink and Stateflow
1.
Simulink
Canbeusedtomodelandsimulatedynamical
systemsinacomprehensiveandgraphicalway
Modelsaredescribedasblockdiagrams(boxeswith
inputs/outputs)
Simulink (2)
Modelsarecomposedhierarchicallyallowing:
Modelingofcomplexsystemsinamodularandorganized
way
Differentdetailperspectivesoverthesamemodelat
designandsimulationtime
Simulink and Stateflow
Simulink includes several builtin
block types (Model Library)
Additionally,extensions tothis
library can be created by users
and companies (toolboxes)
Stateflow is one such toolbox
Stateflow canbeusedtomodel
thebehaviour ofFSMs
Numerical Simulation of ODEs
2.
The Initial Value Problem
Example:
? ) (
2 ) 0 (
1
t x
x
x x
=
+ =
&
[Hespanha, J. P. 05]
ODENumerical Simulation Euler method
[Hespanha, J. P. 05]
ODESimulation RangeKutta method
[Hespanha, J. P. 05]
ODESimulation Variablestep methods
[Hespanha, J. P. 05]
Solving the IVPin Simulink
Simulink has an inbuilt ODEsolver(Integrator)
Different integration methods (including variable step)
Example:
2 ) 0 ( 1 = + = x x x
&
(Integrator toolbox accepts initial value)
ODEs with resets
x
1
: speed
x
2
: acceleration
g: gravity force
c: ball elasticity constant
Integrator Block in Simulink
Integratorblockacceptsaresetport
Wheneveraresetistriggered,itsnewvaluewillbe
takenfromtheinitialvalue(x
0
)port
Stateportholdspreviousvalueofx
Statecanbeusedtodetermineiftheintegrator
needstobereset(x

)
ODEs with resetsin Simulink
ODEs with resetsin Simulink (3)
Simulink ODEsolverdetects zerocrossing behaviour
When aresetis detected,the solvergoes back in
time todeterminewhere the resetoccurred
(zero-crossing detection disabled)
(with zero-crossing detection enabled)
Finite State Machines
3.
Finite State Machines (FSMs)
Modelofsystemswhosebehaviorcanbemodeled
asasetofstatesandtransitionsbetweenstates.This
systemsaresometimescalledreactivesystems.
Finitenumberofstates
SystemsmodeledbyFSMs:
Patternrecognition
ATMs
Computationalprocesses
Humanintelligence?
t t
t
Mathematical Model of FSMs

AFSMis aquintuple (,S,s


0
,,F),where:
is an inputalphabet (finite set of symbols)
Sis afinite,nonempty,set of states
S
0
is the initial state,where S
0
S
is astatetransition function::Sx S
Fis afinite,(possibly empty)set of finalstates
= []
S = [green, yellow, red]
S
0
= green
= [green/ yellow, yellow/ red, red/ green]
F = [red]
Simulation of FSMs StateFlow
Simulink block(toolbox)formodelingFiniteState
Machines
Stateflow chartsreceiveinputsfromSimulink and
provideoutputs(signals,events)
Simulationadvanceswithtime
Hybridstatemachinemodelthatcombinesthe
semanticsofMealyandMoorechartswiththe
extendedStateflow chartsemantics.
StateFlow States
StateFlow Transitions
StateFlow Initial State
StateFlow Transition Conditions
StateFlow State Actions
entry: Quando entra no estado, during: enquanto est no estado, exit: quando sai do estado
StateFlow Transition Actions
StateFlow FinalStates
Stateflow integration with Simulink
User definesvariables tobe used inside Stateflow
chart
Variable types areimportant!
Variables may be:
Inputsfrom Simulink
OutputstoSimulink
Local,Constant,
Todefinevariables useModel Explorer(CtrlR)
Simulink/Stateflow Handson
Create astateflow chart that calculates the factorial
of anumber (uint32),given asinput
Stateflow Debugger
Usestep toexecutethe FSMstepbystep
Usebrowse data tomonitorvariable changes
Discrete Event Systems
4.
Discrete Event Systems
Discreteandqualitativechanges
Statetransitionscausedbyoccurrenceof
asynchronousdiscreteevents
Parallelexecutionofmultiplesystems
Synchronizationbyeventpassing
DESExample
S1
S2
S1
S3
S2
evA?
evA?
evB?
x
<
=
1
0

/

e
v
A
!
x
>
1
0

/

e
v
B
!
Synchronization through events
Parallel execution
receive?/broadcast!
Machine composition in Stateflow
Grouped:Inner states arevisible
Subcharted:Creates asubchart
Default behavior:subchart is executed
Parallel composition in Stateflow
[even(x)]and[~even(x)]arenowvalidconditions
{actions}arecalledconditionactionsandare
evaluatedevenwhendestination(state)isnotvalid
Composition:Graphical functions
Event passing in Stateflow
UseModel Explorertoadd Event datatypes
Events used both asconditions and actions
DESin Stateflow Handson
Createastateflow chartthatreceives2inputs
(uint32)andsumsthenumberof1digitsintheir
binaryrepresentation
Useeventpassingtocountallthe1digits
Hybrid Automata
4.
Hybrid Automata
[Hespanha, J. P. 05]
Bouncing Ball rerevisited
2
=
n
q
1
} : 0 0 , { ) , , (
} 0 0 , { ) , , (
} : , : { ) , , (
} {
2 2 2 1 1 2 1
1 2 1 1 2 1
2 2 1 1 2 1
1
x c x x x q x x q
q x x q x x q
g x x x q x x q f
q Q
= < =
< =
= = =
=

& &
Simulation of Hybrid Automata
[Hespanha, J. P. 05]
Bouncing Ball in StateFlow
Bouncing Ball in Stateflow SIMPLER
Go toFile>Chart properties
Select Continuous update method
Bouncing Ball in Stateflow SIMPLER
In Model Exploreryou can now localvariables of
type double ascontinuous variables (update
method)
Bouncing Ball in Stateflow SIMPLER
Foreach continuous local
variable x
stateflow automatically
definesx_dot
which is derivative of x
Exercise 1:Thermostat Hybrid System
1. Define the HS formally
2. Simulate Hybrid System
[Hespanha, J. P. 05]
Exercise 2:Tank Hybrid System
[Hespanha, J. P. 05]
Exercise 3:Billiard Hybrid System
Ballinsidea10x10box
Ballstartsatunknownposition(x,y)andwith
unknownvelocityvector(v
x
,v
y
)
Ballbouncesinanyofthe4walls
Constantenergydissipationfactorbetween0and1
10
END
References:
http://www.ece.ucsb.edu/~hespanha/ece229/
http://www.mathworks.com/access/helpdesk/help/pdf_doc/stateflow/sf_ug.pdf

Potrebbero piacerti anche