Finite State Machines CS 447 Wireless Embedded Systems
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 1
Discrete Systems Operates in sequence of discrete steps Have discrete dynamics E.g., system that counts cars entering / leaving parking garage 2
Discrete Systems Parking garage counter Each entry / departure is a discrete event Occurs at some instant in time, not over time 3
Discrete Systems Parking garage counter model 4
Discrete Systems ArrivalDetector actor Outputs present when car arrival detected DepartureDetector actor Outputs present when car departure detected 5
Discrete Systems Counter actor has two inputs: u & d u: R -> { absent, present } At time t, u(t) either present or absent Known as a pure signal (carries no value, has all info needed) 6
Discrete Systems Counter actor has two inputs: u & d d: R -> { absent, present } At time t, d(t) either present or absent Known as a pure signal (carries no value, has all info needed) 7
Discrete Systems Counter actor: Event present on up input port => increments count, outputs Event present on down port => decrements count, outputs 8
Discrete Systems Counter actor: All other times, i.e., u(t), d(t) absent Count not adjusted Count not outputted 9
Discrete Systems Counter actor output port count c: R -> {absent } Z At time t, c(t) is either absent or an integer 10
Discrete Systems Discrete dynamics can be described as sequence of steps or reactions Each is assumed to be instantaneous Counter is event-triggered Input present events cause Counter to react up present => increment then output count down present => decrement then output count 11
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 12
Finite State Machines Have notion of state System s condition at some particular point in time State Machine Models a discrete dynamic system Each reaction maps valuations of inputs to outputs Mapping depends on current state Finite state machine - # states if finite 13
Finite State Machines Few states, draw FSM using graph notation Nodes: states Edges: transitions Start state indicated with dangling edge 14
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 15
Transitions Transitions between states governed by discrete dynamics of state machine Map input valuations to output valuations Can go to another state Or stay in same state 16
Transitions E.g., transition from State1 to State2 Labeled guard / action Guard: determines whether transition is taken on a reaction Action: output produced by each reaction 17
Transitions Guard Determines whether transition will be taken on a reaction Predicate (Boolean-value expression) evaluates to true or false When true, transition taken (aka transition enabled) 18
Transitions Action Defines what output(s) are produced by reaction E.g., assignment of values (or absent) to output port No action => implicitly absent Typically defined as name := value 19
FSM for Garage Counter 20
FSM for Garage Counter E.g., up ^ ~down Outputs 1 Moves to state 1 (up present, down absent) ( count := 1 ; count var implicit..) 21
FSM for HVAC System w/ Thermostat Two states: { heating, cooling } 22
FSM for HVAC System w/ Thermostat Setpoint is 20 C Heater on: allow temp to rise to 22 Heater off: allow temp to drop to 18 This is called hysteresis Avoids chattering Heater turning on/off rapidly (when temperature close to setpoint) 23
FSM for HVAC System w/ Thermostat System could be event triggered when temperature provided Or time-triggered updates at regular intervals 24
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 25
Timing Nothing constrains when a state machine reacts Talk about this later If input absent and no guard evaluates to true, Machine will stutter In other words: If inputs & outputs absent, FSM does not change state (implicit) 26
Timing In Garage FSM, what happens if counter = 0 and car leaves? It will stay in state 0 (implicit, no transition with ~up ^ down) FSM models should define all possible behaviors! 27
Timing Default transition Sometimes useful to be explicit E.g., in state 0, no transition means stay in state 0 (implicit) Could use a default transition Dashed line Guard is true Not always necessary Enabled if no other transition taken 28
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 29
Update Functions Graphical notation for FSM defines specific mathematical model for dynamics of state machine Mathematical notation helpful for larger state machines FSM is five-tuple: < states, inputs, outputs, updates, initial > states: finite set of states inputs: set of input valuations outputs: set of output valuations updates: transitions - (state, input) => (state, output) initial: starting state 30
Update Functions Updates (aka transitions) Maps (state + input) to (state + output) Encodes all transitions, guards, and outputs for FSM 31
Update Functions FSM reacts in sequence s: N -> states s is function that gives FSM state at each n N s(0) is initial state N is number of reactions (natural number) x: y: N -> inputs N -> outputs Input and output valuations at each reaction x(0) initial input valuation y(0) initial output valuation 32
Update Functions Dynamics of state machine (S(n+1), y(n)) = update( s(n), x(n) ) English: current state s(n) and input x(n) yields next state s(n+1) and output y(n) 33
Update Functions Formal FSM model for garage counter states: { 0, 1, 2, M } inputs: up, down, {present, absent} outputs: count, { 0, 1, 2,, M } initial state: 0 transitions: 34
Outline Discrete systems Finite State Machines Transitions Timing Update functions Determinacy and Receptiveness 35
Determinacy and Receptiveness State machines have important properties Determinacy Receptiveness 36
Determinacy and Receptiveness Determinacy State machine is deterministic if, for each state, there is at most one transition enabled by each input value Guards leaving a state must NOT overlap Overlap = non-deterministic Determinate: given same inputs, FSM will always product same outputs NOTE: not ALL determinate state machines are deterministic Why? Give an example? 37
Determinacy and Receptiveness Receptiveness State machine is receptive if, for each state, there is at least one transition possible on each input symbol Receptiveness ensures that state machine is always ready to react to any input Won t get stuck in any state Default transitions can help.. 38
Determinacy and Receptiveness FSM is both determinate and receptive: For every state, there is exactly one transition possible on each input value. This is helpful when designing embedded systems.. 39
Why non-determinism can be problematic flying ejectbuttonpressed / ejectbuttonpressed / do nothing eject pilot 40