1.finite State Machines
1.finite State Machines
Systems
12/15/2024 [email protected] 1
Finite State Machines (FSM)
aka Finite State Automation (FSA)
Models of behaviors of a system or object, with a limited
number of defined conditions or modes, where modes
change with circumstance
4 main elements:
States: define behavior and may produce actions
State transitions: move from one state to another
Rules (or conditions): must be met to allow a state transition
Input events: externally or internally generated; may trigger
rules and lead to state transitions
An initial state and a current state
2
Representation of any FSM
Set of States
Set of Inputs
Set of Outputs
State Transition Function
Output Generation Function
Initial State
3
Two Types of FSMs
Differ in how outputs are produced
Moore Machine:
Outputs are independent of the inputs, i.e. outputs are
produced from within the state of the state machine
Mealy Machine:
Outputs can be determined by the present state alone, or
by the present state and the present inputs, i.e. outputs
are produced as the machine makes a transition from one
state to another
Any Moore machine can be turned into a Mealy
machine (and vice versa)
4
Moore Machine
The Moore State Machine
output remains the same as
long as the state machine
remains in that state. State 1 a,b
The output can be arbitrarily q,r
complex but must be the
same every time the Input condition that
machine enters that state. must exist in order
i,j
to execute these
transitions from
State 1
Output condition that State 2
results from being in
a particular present state
x,y
5
Mealy Machine
The Mealy State Machine generates
outputs based on:
The Present State, and
The Inputs to the M/c.
So, same state can generate many
a,b
different patterns of output signals, State 1 q,r
depending on the inputs.
Outputs are shown on transitions since
Input condition that
they are determined in the same way as i,j must exist in order
is the next state. x,y to execute these
transitions from
State 1
Output condition that
results from being in State 2
a particular present state
6
Safety Belt Control
•
We want to design a controller for
safety belt
If the user is seated and the belt is not
buckled within a set time, a buzzer will
sound until the belt is buckled event
driven
Inputs: seat sensor, timer, belt sensor
Output: buzzer, timer
States?
7
FSM for Event-driven Systems
no seat/-
no seat/ idle
buzzer off seat/timer on
while (TRUE) {
switch (state) {
case state1: …
}
}
Switch is repeatedly evaluated by while-loop
9
C Implementation
#define IDLE 0
#define SEATED 1
#define BELTED 2
#define BUZZER 3
switch (state) {
case IDLE: if (seat)
{ state = SEATED; timer_on = TRUE; }
break;
case SEATED: if (belt) state = BELTED;
else if (timer) state = BUZZER;
break;
…
}
10
Another Example
in1=1/x=a
A B r=0/out2=1
r=1/out1=0
in1=0/x=b
s=0/out1=0 C D
s=1/out1=1
11
C State Table
switch (state) {
case A: if (in1==1) { x = a; state = B; }
else { x = b; state = D; }
break;
case B: if (r==0) { out2 = 1; state = B; }
else { out1 = 0; state = C; }
break;
case C: if (s==0) { out1 = 0; state = C; }
else { out1 = 1; state = D; }
break;
12
More FSM Design
Examples
Elevator FSM 1
• The elevator can be at one of two floors: Ground or First.
• There is one button that controls the elevator, and it has two
values: Up or Down.
• Also, there are two lights in the elevator that indicate the current
floor: Red for Ground, and Green for First.
• At each time step, the controller checks the current floor and
current input, changes floors and lights in the obvious way.
Elevator FSM 1
Elevator FSM 2
• Lift service is between two floors only.
• Lift doors closed /opened automatically.
• Lift fan switch on/off automatically (used only when lift is in
use).
• If lift doors remain open due to malfunctioning of door etc. lift
will not start.
• The direction of motion of motor changed automatically
according to position of lift.
• No lift calling system.
Lift controller operation :
• If the lift is downstairs, • If the lift is upstairs,
• Close the doors • Close the doors
• Start the lift fan and check for • Start the lift fan and check for
closed door. closed door.
• Select the motor direction to • Select the motor direction to
upward downward
• Start motor and go up. • Start motor and go down.
• Check for top floor position • Check for ground floor position
• Switch off lift fan and motor • Switch off lift fan and motor
• Open the doors • Open the doors
Elevator FSM 3
• loop
if (req_floor = curr_floor) then
direction := idle;
elseif (req_floor < curr_floor) then
direction := down;
elseif (req_floor > curr_floor) then
direction := up;
end if;
end loop;
Vending Machine FSM 1
Traffic Light FSM
const struct state traffic_controller[4] = {
// 00 01 10 11
while (1)
{
LATC = traffic_controller[state].output;
//wait state
delay_s(traffic_controller[state].delay);
//read buttons
input = (uint8_t)(IO_RB5_PORT|(IO_RB6_PORT<<1u));
//set next state
state = traffic_controller[state].next[input];