State Machines - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

State Machines

Description:

State Machines – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 31
Provided by: Public6
Category:

less

Transcript and Presenter's Notes

Title: State Machines


1
State Machines
2
What are they?
  • Sometimes called a Finite State Machine
  • Depicted as a state diagram
  • Its a tool for specifying a systems behavior
  • Used in many disciplines
  • Language parsing
  • Software design
  • Systems design
  • Hardware design
  • Giving directions and other every day life
    things

3
Example
  • Consider a subway turnstile system
  • The gate is either locked or unlocked
  • If locked, the patron inserts a coin to unlock it
  • If unlocked, the patron passes through
  • This wordy definition is somewhat awkward
    although it does get the message across

4
A Better Description Through Symbols
  • Subway turnstile as a state machine

Actions (outputs)
Insert coin
/Unlock gate
Locked
Unlocked
States
Pass thru
/Lock gate
Transitions
Triggers (events)
5
Example
  • Subway turnstile as a state machine
  • There 2 states that the gate can be in
  • Unlocked
  • Locked
  • These are represented as circles in the diagram
  • There are 2 events that can change the state of
    the system
  • Insert coin
  • Pass thru
  • These are represented as state-to-state lines
    with text designators (before the / character)
  • There are 2 outputs/actions from the system
  • Lock gate
  • Unlock gate
  • These are represented as state-to-state lines
    with text designators (after the / character)

6
Symbology (notation)
  • The start state

Insert coin/Unlock gate
Locked
Unlocked
Pass thru/Lock gate
7
Symbology (notation)
  • System is always in exactly one state
  • The solid circle marks the start state
  • When the system starts up this is the first state
    visited
  • The link out of the start state has no event
    associated with it
  • There are no links into the start state
  • This is called an unconditional transition and
    always happens

8
Symbology (notation)
  • The final (end) state

Insert coin/Unlock gate
Locked
Unlocked
Close down
Close down
Pass thru/Lock gate
9
Symbology (notation)
  • The system may have zero or more final states
  • The solid circle with the ring around it
  • Anytime the system transitions into one of these
    states, this diagram is completed
  • The system may shut down
  • The system may transition to another state
    machine (up in the hierarchy more later)

10
Better than words?
  • In this case the wordy version wasnt too bad
  • But what if we need to add more, unforeseen stuff?

11
Example
  • Lets add some more options
  • If the gate is locked and someone passes through,
    set off an alarm
  • If the gate us unlocked and someone inserts
    money, thank them for the donation
  • The wordiness just doubled

12
Example
  • But the picture still isnt overly complicated

close
Coin/unlock
locked
unlocked
Coin/thank you
Pass/sound alarm
Pass/lock
close
13
Additions
  • Changing things midstream isnt even that hard
  • Instead of sounding an alarm on a dead-beat
    perhaps we might want to do more

14
Example
  • Addition of states/transitions/actions

violation
close
reset
Coin/unlock
locked
unlocked
Pass/sound alarm
Coin/thank you
Pass/lock
close
15
Additions
  • We could go on and on, but we wont

16
Implementation
  • The software implementation is basically a switch
    statement in Java (or the equivalent if/else
    if/else)
  • switch (state)
  • case locked
  • case unlocked
  • case violation

17
Implementation
  • The implementation in hardware is through
    sequential circuits with flip-flops (memory
    elements) and gates

18
And so on
  • There are a lot more features related to state
    machines
  • They can be Hierarchical
  • One super state can contain many sub states

19
Hierarchical
Enters state C at sub-state B
A
B
Enters state C at start state
C
20
Parallel
  • Two state machines may run in parallel,
    interacting with one another

21
Parallel
A
C
event_1/action_1
B
action_1/action_2
D
22
And so on
  • They can contain conditional connectors
  • Can put if statements on the transitions

23
Conditional connectors
A
B
event
case x
case y
C
24
And so on
  • Guarded transitions
  • Like a conditional transition but with just one
    case

25
Guarded Transitions
A
B
event case x
26
And so on
  • States can have
  • Entry actions
  • These are things that occur as soon as the state
    is entered
  • Transition actions
  • These are things that occur on the transition
    to/from the state
  • Exit actions
  • These are things that occur as a state is being
    exited

27
Summary
  • They can be as simple or as complicated as
    necessary.
  • But, if they get too complicated they are not
    serving their stated purpose and should be broken
    up into multiple state machines

28
Assignment The wordy version
  • Two buttons (call them B1 and B2)
  • Three LEDs,1 yellow, 1 red, 1 green (call them Y,
    R, and G)
  • System specification
  • Start in home position
  • LEDs off
  • Press B1 one time
  • Press B2 one time flash Y slow
  • Press B2 two times in succession flash Y medium
  • Press B2 three times in succession flash Y fast
  • Press B1 return to home position
  • Press B1 two times in succession
  • Press B2 one time flash R slow
  • Press B2 two times in succession flash R medium
  • Press B2 three times in succession flash R fast
  • Press B1 return to home position
  • Press B1 three times in succession
  • Press B2 one time flash G slow
  • Press B2 two times in succession flash G medium
  • Press B2 three times in succession flash G fast

29
Notes
  • one time, two times, three times means
    button presses in rapid succession
  • So for example
  • , B1 , B2-B2
    , , B1,
    all LEDs off
  • , B1-B1-B1 , B2
    , , B2
    changes G continues to flash at slow speed,
    B1,

30
Deliverables
  • A state-machine diagram depicting the operation
    of the system
  • Source code
  • A working demonstration on the Basic Stamp
    development board
Write a Comment
User Comments (0)
About PowerShow.com