State Flow Tutorial

Embed Size (px)

Citation preview

  • 8/10/2019 State Flow Tutorial

    1/51

  • 8/10/2019 State Flow Tutorial

    2/51

    StateFlow

    HandsOn Tutorial

    HS/PDEEC20100304

    Jos Pinto [email protected]

  • 8/10/2019 State Flow Tutorial

    3/51

    Session Outline

    Simulink andStateflow

    NumericalSimulationofODEs

    InitialValueProblem(Handson)

    ODEs withresets(Handson)

    FiniteStateMachines

    FSMs inStateflow (Handson)

    DiscreteEventSystems

    DESs inStateflow (Handson) HybridAutomata

    HybridSystemsinStateflow (Handson)

  • 8/10/2019 State Flow Tutorial

    4/51

    Simulink and Stateflow

    1.

  • 8/10/2019 State Flow Tutorial

    5/51

    Simulink

    Canbeusedtomodelandsimulatedynamical

    systemsinacomprehensiveandgraphicalway

    Modelsaredescribedasblockdiagrams(boxeswith

    inputs/outputs)

  • 8/10/2019 State Flow Tutorial

    6/51

    Simulink (2)

    Modelsarecomposedhierarchicallyallowing:

    Modelingofcomplexsystemsinamodularandorganized

    way

    Differentdetailperspectivesoverthesamemodelat

    designandsimulationtime

  • 8/10/2019 State Flow Tutorial

    7/51

    Simulink and Stateflow

    Simulink includes several builtin

    block types (Model Library)

    Additionally,extensions tothislibrary can be created by users

    and companies (toolboxes)

    Stateflow is one such toolbox

    Stateflow canbeusedtomodel

    thebehaviour ofFSMs

  • 8/10/2019 State Flow Tutorial

    8/51

    Numerical Simulation of ODEs

    2.

  • 8/10/2019 State Flow Tutorial

    9/51

    The Initial Value Problem

    Example:

    ?)(

    2)0(

    1

    tx

    x

    xx

    =

    +=&

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    10/51

    ODENumerical SimulationEuler method

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    11/51

    ODESimulationRangeKutta method

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    12/51

    ODESimulation Variablestep methods

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    13/51

    Solving the IVPin Simulink

    Simulink has an inbuilt ODEsolver(Integrator)

    Different integration methods (including variable step)

    Example:

    2)0(1 =+= xxx&

    (Integrator toolbox accepts initial value)

  • 8/10/2019 State Flow Tutorial

    14/51

    ODEs with resets

    x1: speed

    x2: accelerationg: gravity force

    c: ball elasticity constant

  • 8/10/2019 State Flow Tutorial

    15/51

    Integrator Block in Simulink

    Integratorblockacceptsaresetport

    Wheneveraresetistriggered,itsnewvaluewillbe

    takenfromtheinitialvalue(x0)port

    Stateportholdspreviousvalueofx

    Statecan

    be

    used

    to

    determine

    ifthe

    integrator

    needstobereset(x)

  • 8/10/2019 State Flow Tutorial

    16/51

    ODEs with resetsin Simulink

  • 8/10/2019 State Flow Tutorial

    17/51

    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)

  • 8/10/2019 State Flow Tutorial

    18/51

  • 8/10/2019 State Flow Tutorial

    19/51

    Finite State Machines (FSMs)

    Modelofsystemswhosebehaviorcanbemodeled

    asasetofstatesandtransitionsbetweenstates.This

    systemsaresometimescalledreactivesystems.

    Finitenumberofstates

    SystemsmodeledbyFSMs:

    Patternrecognition

    ATMs

    Computationalprocesses

    Humanintelligence?

    t t

    t

  • 8/10/2019 State Flow Tutorial

    20/51

    Mathematical Model of FSMs

    AFSMis aquintuple(,S,s0,,F),where:

    is an inputalphabet (finite set of symbols)

    S

    is afinite,nonempty,set of states

    S0is the initial state,where S0 S

    is astatetransition function: :SxS

    F

    is afinite,(possibly empty)set of finalstates

    = []

    S = [green, yellow, red]

    S0 = green

    = [green/ yellow, yellow/ red, red/ green]F= [red]

  • 8/10/2019 State Flow Tutorial

    21/51

    Simulation of FSMsStateFlow

    Simulink block(toolbox)formodelingFiniteState

    Machines

    Stateflow chartsreceiveinputsfromSimulink and

    provideoutputs(signals,events)

    Simulationadvanceswithtime

    Hybridstatemachinemodelthatcombinesthe

    semanticsofMealyandMoorechartswiththe

    extendedStateflow chartsemantics.

  • 8/10/2019 State Flow Tutorial

    22/51

    StateFlow States

  • 8/10/2019 State Flow Tutorial

    23/51

    StateFlow Transitions

  • 8/10/2019 State Flow Tutorial

    24/51

    StateFlow Initial State

  • 8/10/2019 State Flow Tutorial

    25/51

    StateFlow Transition Conditions

  • 8/10/2019 State Flow Tutorial

    26/51

    StateFlow State Actions

    entry: Quando entra no estado, during: enquanto est no estado,exit: quando sai do estado

  • 8/10/2019 State Flow Tutorial

    27/51

    StateFlow Transition Actions

  • 8/10/2019 State Flow Tutorial

    28/51

    StateFlow FinalStates

  • 8/10/2019 State Flow Tutorial

    29/51

    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)

  • 8/10/2019 State Flow Tutorial

    30/51

    Simulink/Stateflow Handson

    Create astateflow chart that calculates the factorial

    of anumber (uint32),given asinput

  • 8/10/2019 State Flow Tutorial

    31/51

    Stateflow Debugger

    Usestep toexecutethe FSMstepbystep

    Usebrowse data tomonitorvariable changes

  • 8/10/2019 State Flow Tutorial

    32/51

    Discrete Event Systems

    4.

  • 8/10/2019 State Flow Tutorial

    33/51

    Discrete Event Systems

    Discreteandqualitativechanges

    Statetransitionscausedbyoccurrenceof

    asynchronousdiscreteevents

    Parallelexecutionofmultiplesystems

    Synchronizationbyeventpassing

  • 8/10/2019 State Flow Tutorial

    34/51

    DESExample

    S1

    S2

    S1

    S3

    S2

    evA?

    evA?

    evB? x10

    /evB

    !

    Synchronization through events

    Parallel execution

    receive?/broadcast!

  • 8/10/2019 State Flow Tutorial

    35/51

    Machine composition in Stateflow

    Grouped:Inner states arevisible

    Subcharted:Creates asubchart

    Default behavior:subchart is executed

  • 8/10/2019 State Flow Tutorial

    36/51

    Parallel composition in Stateflow

  • 8/10/2019 State Flow Tutorial

    37/51

    [even(x)]and[~even(x)]arenowvalidconditions

    {actions}arecalledconditionactionsandareevaluatedevenwhendestination(state)isnotvalid

    Composition:Graphical functions

  • 8/10/2019 State Flow Tutorial

    38/51

    Event passing in Stateflow

    UseModel Explorertoadd Event datatypes

    Events used both asconditions and actions

  • 8/10/2019 State Flow Tutorial

    39/51

    DESin Stateflow Handson

    Createastateflow chartthatreceives2inputs

    (uint32)andsumsthenumberof1digitsintheir

    binaryrepresentation Useeventpassingtocountallthe1digits

  • 8/10/2019 State Flow Tutorial

    40/51

    Hybrid Automata

    4.

  • 8/10/2019 State Flow Tutorial

    41/51

    Hybrid Automata

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    42/51

    Bouncing Ball rerevisited

    2=n

    q1

    }:00,{),,(

    }00,{),,(

    }:,:{),,(

    }{

    2221121

    121121

    221121

    1

    xcxxxqxxq

    qxxqxxq

    gxxxqxxqf

    qQ

    =

  • 8/10/2019 State Flow Tutorial

    43/51

    Simulation of Hybrid Automata

    [Hespanha, J. P. 05]

  • 8/10/2019 State Flow Tutorial

    44/51

    Bouncing Ball in StateFlow

  • 8/10/2019 State Flow Tutorial

    45/51

    Bouncing Ball in StateflowSIMPLER

    Go toFile>Chart properties

    Select Continuous update method

  • 8/10/2019 State Flow Tutorial

    46/51

    Bouncing Ball in StateflowSIMPLER

    In Model Exploreryou can now localvariables of

    type double ascontinuous variables (update

    method)

  • 8/10/2019 State Flow Tutorial

    47/51

    Bouncing Ball in StateflowSIMPLER

    Foreach continuous local

    variable x

    stateflow automatically

    definesx_dot

    which is derivative of x

  • 8/10/2019 State Flow Tutorial

    48/51

    Exercise 1:Thermostat Hybrid System

    1. Define the HS formally

    2. Simulate Hybrid System

    [Hespanha, J. P. 05]

    k b d

  • 8/10/2019 State Flow Tutorial

    49/51

    Exercise 2:Tank Hybrid System

    [Hespanha, J. P. 05]

    E i 3 Billi d H b id S

  • 8/10/2019 State Flow Tutorial

    50/51

    Exercise 3:Billiard Hybrid System

    Ballinsidea10x10box

    Ballstartsatunknownposition(x,y)andwith

    unknownvelocityvector(vx,vy) Ballbouncesinanyofthe4walls

    Constantenergydissipationfactorbetween0and110

  • 8/10/2019 State Flow Tutorial

    51/51

    END

    References:

    http://www.ece.ucsb.edu/~hespanha/ece229/ http://www.mathworks.com/access/helpdesk/help/pdf_doc/stateflow/sf_ug.pdf