Title: Designing User Interfaces Spring 1999
1SE 746-NT Embedded Software Systems
Development Robert Oshana Lecture
14a For more information, please
contact NTU Tape Orders NTU Media
Services (970) 495-6455
oshana_at_airmail.net
tapeorders_at_ntu.edu
2Finite state machines
3Finite State Machines(FSMs)
- Widely used specification technique
- Used to specify system behavior in response to
events - Both output and next state can be determined
solely on the basis of understanding the current
state and the event that caused the transition - H/W engineers have been using FSM for years
4(No Transcript)
5(No Transcript)
6(No Transcript)
7(No Transcript)
8Same stimulus event
Different response
9State Machines
- Models how a system responds differently to
events over time
10State Machines
- Models how a system responds differently to
events over time
button switch
11States
OFF
ON
12States
Press
OFF
ON
Press
13Finite State Machines
- State
- the memory of previous events
- Events
- inputs to a system
- Actions
- output in some form
- mechanical, electrical or software event
14Finite State Machines
- State
- the memory of previous events
- Events
- inputs to a system
- Actions
- output in some form
- mechanical, electrical or software event
15Coke Machine
- Events
- coin insertion
- Actions
- coke delivery
- coins returned
- State
- the memory of how much money has been deposited
16FSM Diagram
Event-1
State 1
State 2
Action-k
17FSM
Event-1
State 1
State 2
Action-k
Event-1
Not all state transitions have actions associated
with them
18ScenarioQuarters Only, 75 cents
- Customer
- enter quarter
- Customer
- enter quarter
- Customer
- enter quarter
- Coke Machine
- deliver coke
19Enumerate Events Actions
EventsE1 Deposit 25 cents Actions A1 Deliver
coke
20Define States
Start State
E1
A
B
E1
E1/A1
C
EventsE1 Deposit 25 cents Actions A1 Deliver
coke
States A No money B 25 cents entered C 50
cents entered
21State Transition Table
22Coke Machine Scenario 2Coin Return
- Customer
- enter quarter
- Customer
- enter quarter
- Customer
- press coin return (CR)
- Coke Machine
- return coins
23Enumerate Events Actions
EventsE1 Deposit 25 centsE2 Coin Return
(CR) Actions A1 Deliver coke A2 Return coins
24Define States
Start State
E1
A
B
E1
E1/A1
E2/A2
C
EventsE1 Deposit 25 centsE2 Coin Return
(CR) Actions A1 Deliver coke A2 Return coins
States A No money B 25 cents entered C 50
cents entered
25Define States
E2/A2
Start State
E1
A
B
E1
E1/A1
E2/A2
C
EventsE1 Deposit 25 centsE2 Coin Return
(CR) Actions A1 Deliver coke A2 Return coins
States A No money B 25 cents entered C 50
cents entered
26Transition Table
- State E1 E2
- A B
- B C A/coin return
- C A/coke A/coin return
27Transition Table
No BlanksAllowed !!
- State E1 E2
- A B
- B C A/coin return
- C A/coke A/coin return
28Transition Table
- State E1 E2
- A B A
- B C A/coin return
- C A/coke A/coin return
29Telephone System FSMLocal 4-digit exchange
Events d digit dialed h hang up Actions c1 con
nect to caller
30Telephone System FSMLocal 4-digit exchange
d
d
d
d/c1
A
B
C
D
E
Events d digit dialed h hang up Actions c1 con
nect to caller
StatesA start B 1 digit dialed C 2 digits
dialed D 3 digits dialed E call in progress
31Telephone System FSMLocal 4-digit exchange
h
d
d
d
d/c1
A
B
C
D
E
Events d digit dialed h hang up Actions c1 con
nect to caller
StatesA start B 1 digit dialed C 2 digits
dialed D 3 digits dialed E call in progress
32Telephone System FSMLocal 4-digit exchange
h
d
d
d
d/c1
A
B
C
D
E
Events d digit dialed h hang up Actions c1 con
nect to caller
StatesA start B 1 digit dialed C 2 digits
dialed D 3 digits dialed E call in progress
33From the article Embedded State Machine
Implementation
- Martin Gomez
- Embedded systems programming
34Embedded state machines
- Many embedded software applications are natural
candidates for mechanization as a state machine - Program that must sequence a series of actions,
or handle inputs differently depending on what
mode its in - Soft key interfaces to comm protocols
35State machines
- An algorithm that can be in one of a small number
of states - Mealey
- Output a function of present state and output
- Moore
- Function only of state
36Example state machine
input char / /no output
input char / /output and signal
start
find 2nd /
find 1st /
done
input char lt gt/ /no output
37State machine approach
- Learn what the user wants
- Sketch the state transition diagram
- Code the skeleton of the state machine, without
filling in the details of the transition actions - Make sure the transitions work properly
- Flesh out the transition details
- Test
38Another example
- Program that controls the retraction and
extension of an airplanes landing gear - Hardware
- Nose gear
- Left main gear
- Right main gear
- Hydraulically actuated
39What does the user want?
- dont retract the landing gear if the airplane
is on the ground - Is it ok to retract the landing gear the instant
the airplane leaves the ground? - Use a white board or sequence enumeration!
- ltevent that caused transitiongt/ltoutput as a
result of the transitiongt
40First implementation only does what the user
asked for
gear lever up not on ground / pump on, dir
up
start
Gear down
Raising gear
gear lever down / pump on, dir down
41Some things to keep in mind
- Computers are very fast compared to mechanical
hardware you may have to wait - Mechanical engineer may not know as much about
computers! - What happens if parts break?
42A better implementation
pilots lever rising edge squat_switch
AIR/start timer, lights RED
timer 2 sec / pump on, dir UP
Waiting For takeoff
start
Raising gear
Gear down
squat_switch GND or pilots lever down / restart
timer
pilots lever down / dir DOWN
All 3 down switches made/ pump OFF, lights GREEN
pilots lever up / dir UP
Gear up
Lowering gear
pilots lever down / lights RED, pump on, dir
DOWN
All 3 down switches made/ pump off, extinguish
lights
43Implementation
typedef enum GEAR DOWN 0, WTG_FOR_TKOFF,
RAISINGGEAR, GEAR_UP, LOWERING_GEAR
State_type / this table contains a pointer to
the function to call in each state / void (
state_table )( ) GearDown, WtgForTakeoff,
RaisingGear, GearUp, LoweringGear State_Type
curr_state main( ) InitializLdgGearSM( )
/ the heart of the state machine is this
one loop. The function corresponding to the
current state is called once per iteration
/ while( 1 ) state_tablecurr_state
( ) DecrementTimer( ) / do other
functions, not related to this state machine /
44Implementation
void InitializeLdgGearSM( ) curr_state
GEAR_DOWN timer 0.0 / stop all
the hardware, turn off the lights, etc / void
GearDown( ) / raise the gear upon
command, but not if the airplane is on the ground
/ if ((gear_lever UP)
(prev_gear_lever DOWN) (squat_switch
UP) timer 2.0
curr_state WTG_FOR_TKOFF
prev_gear_lever gear_lever / store for edge
detection / void RaisingGear( ) / once
all 3 legs are up, go to the GEAR_UP state /
if ((nosegear_is_up MADE) (leftgear_is_up
MADE) (rtgear_is_up MADE))
curr_state GEAR_UP
45Implementation
/ if the pilot changes his mind, start
lowering the gear / if (gear_lever DOWN)
curr_state LOWERING_GEAR
void GearUp( ) / if the pilot moves
the lever to DOWN, lower the gear / if
(gear_lever DOWN) curr_state
LOWERING_GEAR void WtgForTakeoff( )
/ once weve been airborne for 2 seconds, start
raising the gear / if (timer lt 0.0)
curr_state RAISING_GEAR / if we
touch own again, or if the pilot changes his
mind, start over /
46Implementation
if ((squat_switch DOWN)
(gear_lever DOWN))
timer 2.0 curr_state
GEAR_DOWN / dont want to
require that pilot toggle the lever again this
was just a bounce /
prev_gear_lever DOWN Void
LoweringGear( ) if (gear_lever UP)
curr_state RAISING_GEAR if
((nosegear_is_down MADE) (leftgear_is_down
MADE) (rtgear_is_down MADE))
curr_state GEAR_DOWN
47RaisingGear() function
void RaisingGear( ) / once all 3 legs
are up, go to the GEAR_UP state / if
((nosegear_is_up MADE) (leftgear_is_up
MADE) (rtgear_is_up MADE))
pump_motor OFF gear_lights
EXTINGUISH curr_state GEAR_UP
/ if the pilot changes his mind,
start lowering the gear / if (gear_lever
DOWN) pump_direction DOWN
curr_state GEAR_LOWERING
48Testing
- Test plan almost writes itself
- Avoid hidden states
- Number of transitions is measure of systems
complexity - Use print statement to output current state
- Be careful when connecting to actual hardware
49Quote
- By relieving the brain of all unnecessary work,
a good notation sets it free to concentrate on
more advanced problems, and, in effect, increases
the mental power of the race - Alfred North Whitehead (English mathematician)
50Complexity
- Simplicity is a basic tenet of all good
engineering - Can we build skyscrapers with Lego blocks
- Were asked to build increasingly complex systems
- Modern jet has over 6 million parts
51Essential acceleration/deceleration mechanism
cruising
too fast
too slow
speed ok
speed ok
acceleration
deceleration
52Adding an off state
off
off
off
off
on
cruising
too fast
too slow
speed ok
speed ok
acceleration
deceleration
53Dealing withadditionalrequirements
diagnosing
off
ok
off
off
off
off
on
suspended
suspend
resume
cruising
too fast
too slow
speed ok
suspend
speed ok
acceleration
deceleration
54SE 746-NT Embedded Software Systems
Development Robert Oshana End of
Lecture For more information, please
contact NTU Tape Orders NTU Media
Services (970) 495-6455
oshana_at_airmail.net
tapeorders_at_ntu.edu