Sei sulla pagina 1di 10

M-062A / M-063P: Mathematics of Control: Finite State Machines ESP Splash!

2001
Jeremy Nimmer (with dues to Felix Klock) November 17-18, 2001
Mathematics of Control: Finite State Machines
Catalog Description
We will learn the basics of Finite State Machines (FSMs), a simple but powerful technique for describing how a
system can process inputs and outputs. FSMs can describe systems as simple as a trac light, or as complex as a
computer. We will learn a little bit of the theory, but will focus mostly on interesting applications. A nal project
will be designing a brain for RoboAnt, a computer-simulated robotic ant which can explore mazes.
Jeremy Nimmer jwnimmer@mit.edu
http://web.mit.edu/mistere/www/splash01/
Welcome
Welcome to the class! To get your brains turned on, were going to try a warm-up exercise. It relates to the class,
but you wont see how until towards the end. Consider the following scenario...
The last thing you remember is being at school... but that must have been a while ago. You woke up
in a pitch dark room, with nothing except the clothes on your back. You feel ne, but have no idea
where you are. You are lying on a hard, concrete oor. Maybe you have been kidnapped? Where are
you? How are you going to get home? What do you do?
Please volunteer your ideas! If you suggest some action, I play the part of a narrator and tell you what happens.
(The results are dierent every time, so I wont write up a solution here).
1 What is a Finite State Machine?
Finite State Machines (FSMs) are abstractions that can be useful in modeling certain kinds of processes. A FSM
is a way to describe what happens in a process over time.
A Finite State Machine is made up of essentially two parts: a set of states (some of which are special start
states) and a set of transitions between those states. They have a convenient graphical representation where we
draw circles to symbolize the states and arrows joining the circles to represent the transitions.
First lets do an example.
1.1 Trac Light
start
'` __
Green
// '` __
Yellow
// '` __
Red
hh
Above is an FSM for a trac light. From the picture, we can deduce that all trac lights start o showing
red, and can then transition to a green light, and can nally display a yellow light before returning to the red
state. Notice that this diagram implies that it is impossible to go directly from green to red; you must go through
the yellow state rst. Why is this a good property for a trac light to have?
2 Mathematics of Control: Finite State Machines
1.2 Background and Basics
The main reason that FSMs are useful is that they are simple: simple for people to understand, simple to reason
about, simple to simulate or build, etc. Because they are so simple, they are not always the best way to model a
problem. However, they are not a bad way to get some ideas down, even if its only a stepping stone to the nal
product. Youll nd that engineers will often use a simple model which is close enough to reality, since simplicity
is such an important consideration.
As above, the two parts of an FSM are the set of states (circles), and the set of transitions (arrows). The
key property of a FSM is that all of the information about the process is captured in the current state. The prior
history of the process is ignored; all possible directions are dened solely by the current state and FSM itself. If
the trac light FSM is in the Red state, we dont know if it just turned on and is just starting, if it had just
turned from yellow a minute ago, or if this is the 10th or 100th time it has turned red. The only thing we know
about the world is that the light is now red.
Some FSMs have absorption states; they represent the end of the process. Absorption states are commonly
drawn with two nested circles rather than a single one.
You can also think about FSMs, is as a really simple (read: stupid) brain. We will see examples later on.
1.3 Counting to Five
Here is a FSM for how we might describe the process of counting to ve.
start

1
//
2
//
3
//
4
// \
5
Another way to think of absorption states is as a state whose only exiting transition loops back to itself.
start

1
//
2
//
3
//
4
//
5

If more than one arrow emerges from a particular circle, that means that either transition could occur; it is a
choice which can be made. When looking at how the process could develop, you need to account for all possibilities.
1.4 Getting up in the morning
__
Sleeping
(start)

__
Hitting
Snooze
Button
??












__
Alarm
Buzzing
oo // __
Turning
O Alarm
// __ __
Getting
Up
Notice the two edges leaving the Alarm Buzzing state; that tells us that each time my alarm goes o in the
morning, I might choose to turn my alarm o and get up, or I might hit the snooze button and go back to sleep.
Is it possible for a person following this FSM to leave the house with their alarm on?
Mathematics of Control: Finite State Machines 3
Whats wrong with this picture?
__
Sleeping
(start)

__
Hitting
Snooze
Button
??












__
Alarm
Buzzing
oo // __
Turning
O Alarm
__ ?
?
?
?
?
?
?
?
?
?
?
?
// __ __
Getting
Up
What is wrong with this machine?
Is it possible for a person following this FSM to leave the house with their alarm on?
Are these diagrams really accurate? Are there other things that could wake me up in the morning? For
example, I often wake up to the sound of my phone ringing; and then after I nish talking to the caller, I rarely
am able to go back to sleep.
__
Sleeping
(start)

// __
Answering
Phone

__
Hitting
Snooze
Button
??












__
Alarm
Buzzing
oo // __
Turning
O Alarm
// __ __
Getting
Up
Question Is it possible for a person following this FSM to leave the house with their alarm on?
2 Adding Inputs
Now, these FSMs are actually a bit too simple for our purposes. Right now, all they model is something that
changes state over time; this can be useful if you want to reason about any possible behavior the process might
exhibit, such as what states are reachable or what transitions could occur. But if we want to model a computation
or controllable process, then we need to add a little bit more information to our diagrams.
For example, in the state machine above, it is entirely conceivable that I would jump out of bed and pick up
my phones handset even if it wasnt ringing. Thats not terribly realistic.
One of the most useful extensions is to add the concept of input to the Finite State Machine diagrams.
Before, we said that if two transitions left a state, they were both considered legal possibilities. But you can
also model FSMs where the machine receives input from its environment, and changes its state according to the
input it receives. We notate this by labelling each arrow with the input that the machine needs to receive for the
transition to be legal. A * on an arrow means any input (in other words, this transition is always legal).
Also, the notation X means not X or X is not happening.
4 Mathematics of Control: Finite State Machines
__
Sleeping
(start)
alarm buzzes

phone rings
// __
Answering
Phone

__
Hitting
Snooze
Button

??












__
Alarm
Buzzing

oo

// __
Turning
O Alarm

// __ __
Getting
Up
Notice that most of the above machines have been non-deterministic. That is, they could take one of many
transitions when exiting a state. For instance, if the alarm is buzzing, I could either turn o the alarm, or hit the
snooze button, and the FSM doesnt tell you anything about which of those I will do.
Non-deterministic machines can be useful in modeling the real world, where chaos is rampant and nothing is
under anyones control. But when you are building your own system, it is often more useful to ensure that the
machine is deterministic. Its easy to make a machine deterministic: just make sure that at most one transition
can be legal at a time for every state.
Question Is this machine deterministic?
__
Sleeping
(start)
alarm triggers

phone rings
// __
Answering
Phone

__
Hitting
Snooze
Button

??












__
Alarm
Buzzing sleepy
oo
sleepy
// __
Turning
O Alarm

// __ __
Getting
Up
Question Can you think of other additions to this model?
2.1 Soda Machine
Imagine that you need to help construct a new soda machine for your school. Youve been put in charge of the
change counter. The machine takes quarters (Q), dimes (D), and nickels (N) as input, and there is also a selection
of drop soda buttons that the person presses to choose their soda (Coke (C) and Pepsi (P) are the only kinds
available for purchase) after at least twenty-ve cents has been deposited. Use Drop Coke (DC) and Drop
Pepsi (DP) as the outputs.
Exercise: Draw an FSM to control this soda machine.
You dont need to worry about returning excess change.
Mathematics of Control: Finite State Machines 5
You can represent dropping the soda (the output) by labelling the transitions that a particular soda should
be dropped on, like C/DP means this transition is legal when the person hits the Coke button, and
when taking this transition, drop a Pepsi.
1
Make sure that you dont forget to let more than one person buy a soda from your machine!
3 State Tables
Our circle-and-arrow diagrams are very succinct, but they arent always the best way to describe an FSM. Notably,
most computers cant interpret human pictures directly.
A dierent way of describing deterministic nite state machines is with a table. The rows of the table correspond
to arrows in the picture; each transition gets its own row. The left side of the table is for the current state and
the values of the inputs. The right side of the table is for the next state and the values of the outputs (if any).
In this table, 1 is on or yes, 0 is o or no, and is I dont care. It models an FSM much like
the Getting Up example FSMs, except that the Alarm Buzzing State has been removed.
Current State Sleepy Buzz Ring Next State
Sleeping 0 0 Sleeping
Sleeping 0 1 Answering Phone
Sleeping 1 1 Hitting Snooze
Sleeping 0 1 Turning O Alarm
Answering Phone Getting Up
Hitting Snooze Sleeping
Turning O Alarm Getting Up
Getting Up Getting Up
4 Roboant
Now, were going to have a bit of fun. At the beginning of class, we talked about what strategy you should take
to get out of an unknown environment in pitch black darkness. Were going to do something similar, except this
time were helping an ant get out of a maze.
Our ant has ve actions: turn left (TL), turn right (TR), go forward (F), mark the local area (M), and erase
marks in the area around the ant, if any (E). These are its outputs. The ant also has a left feeler and right feeler,
and a way to sense if it has left its mark there previously; these are its inputs (L, R, and S, respectively).
Well have a little extra time next Tuesday, so if you come up with an FSM over the weekend that you want
to show us, bring it in. The program that we use will be on our website, so you can play with the ant directly at
home.
Here are some tables which you can use to write out your ant FSMs. Fill in the state columns with short,
descriptive state names such as lost or wall. Fill in the input columns (L, R, S) with 0, 1, or -. Fill in the
output columns (TL, TR, F, M, E) with 0 or 1.
1
How does this representation of output dier from that of the Trac Light or the Getting Up machines?
6 Mathematics of Control: Finite State Machines
Current State L R S Next State TL TR F M E
Current State L R S Next State TL TR F M E
Mathematics of Control: Finite State Machines 7
5 Digital FSMs
If you are also taking Mathematics of Truth: Boolean and Digital Logic, you may read this section later and learn
how the two topics tie together.
5.1 State Encoding
Recall the trac light FSM from the preceding pages.
start
'` __
Green
// '` __
Yellow
// '` __
Red
hh
Lets choose an encoding from state to bits. Well call Green 00, Yellow 01, and Red 11.
State Bits
S
1
S
0
Green 0 0
Yellow 0 1
Red 1 1
Now we can think about what values S
1
and S
0
would have over time. We might draw a table and list the
values over time:
Time State S
1
S
0
1 Red 1 1
2 Green 0 0
3 Yellow 0 1
4 Red 1 1
5 Green 0 0
6 Yellow 0 1
. . .
We could build this FSM if, using digital logic gates, we had a way to remember what values S
1
and S
0
were
holding, and a way to move to the next state periodically. The D-register is a commonly-used component for
storing the values of state variables, and a clock signal provides a way to periodically transition to the next state.
5.2 Registers and Clocks
The logic gates shown in Lecture 3 were all combinatorial logic. That is, their behavior could be described by
boolean expressions, like X = AC. There is another category of digital logic, called sequential logic. Sequential
logic uses feedback loops and other techniques to add state to the system. With combinatorial logic, if you
knew all of the input values, you certainly knew the output value. With sequential logic, you also have to know
something about the past inputs, which is often reected in internal state.
A common component used to store state is a D-register, also called a D-ip op. You can think about the
behavior of a D-register as if it were a camera. You place a signal (boolean variable) at the input, and then push
the button; the D-register takes a snapshot of the value it was given, and then places that signal on the output.
A D-register is drawn like so:
8 Mathematics of Control: Finite State Machines
Input N S Output
Clock >
In this picture, N is the input (Next state) and S is the output (current State)
2
. Every time a tick is seen on
the clock, the value at N is copied to S. It is also common to draw two or more D-registers as one, if they share
the same clock:
Input N
1
S
1
Output
Input N
0
S
0
Output
Clock >
Always keep in mind that S is an output: it puts a signal (bit) on the wire; N is an input: it reads a signal
(bit) from the wire.
5.3 Building an FSM
To build an FSM, just follow four simple steps:
Draw a diagram of your FSM, as shown in Lecture 2. Be sure to label all of your transitions (arrows) with
the inputs which determine them.
Choose an encoding for each of your states, as shown on the previous page.
Draw a truth table for your FSM, using the current state, inputs, next state, and any outputs.
Translate your table into an ecient conguration of logic gates. Small table can be converted by hand, but
if your table is large, you probably want to use a Karnaugh map.
Lets build the trac light FSM in digital logic. We already have the rst and second steps done (from the
previous page). Now we draw the truth table:
S
1
S
0
N
1
N
0
0 0 0 1
0 1 1 1
1 1 0 0
The columns on the left side are the inputs: in this case, just the current state. The columns on the right are
the outputs: in this case, just the next state. Recall that 00 is green, 01 is yellow, and 11 is red, so the
table says that (row 1) when you are in the green state, your next state should be yellow, (row 2) when you are in
the yellow state, your next state should be red, and (row 3) when you are in the red state, your next state should
be green.
Exercise: Translate Truth Table Into Boolean Expressions The next step is to nd (minimal) expressions
for the outputs in terms of the input. See if you can come up with an expression for N
1
using S
1
and S
0
. Then,
do the same for N
0
.
2
In the real world, the input (N in our case) is called D, and the output (S in our case) is called Q. This is where the name D-register
comes from. We use S and N to make the notes easier to read, since we will always use D-registers with S for state, and N for next
state.
Mathematics of Control: Finite State Machines 9
FSMs With Inputs Lets expand the trac light FSM so that at nighttime, it goes into a ashing red mode,
and remains that way throughout the evening. Below is the FSM diagram for this situation. We will use D as a
input which is 1 during the daytime, and 0 during the nighttime.
start
'` __
Green

// '` __
Yellow

// '` __
Red
D
hh
D
// '` __
Flash
D
gg
D
ee
Exercise: Build a New Stoplight Given the FSM diagram above, ll in this table with the appropriate values
for the outputs (N
1
and N
0
). Then, ll in the Karnaugh maps and use them to determine boolean expressions for
both N
1
and N
0
in terms of the inputs. The solution is at the end of the notes.
FSM Table N
1
Karnaugh Map N
0
Karnaugh Map
S
1
S
0
D N
1
N
0
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
S
1
S
0
D 00 01 11 10
0
1
S
1
S
0
D 00 01 11 10
0
1
Exercise: Aibo Image that you are going to design the control system for a new Sony Aibo robot dog. Create
a FSM diagram to model the behavior and use the steps given earlier in this section to design a digital system
which will implement this behavior. You might use states such as sleepy, recharging, running, or barking,
and inputs such as low battery, daytime, has been kicked, cat nearby, etc. Feel free to share your designs
with us if youd like to talk about them.
10 Mathematics of Control: Finite State Machines
6 Solutions
Exercise: Translate Truth Table Into Boolean Expressions You could use a Karnaugh map here, but
with just two inputs, its probably not worth it. By inspecting the table, we see that N
1
is only 1 when S
1
is 0
and S
0
is 1. Therefore, one answer for N
1
is N
1
= (S
1
) S
0
, also written as N
1
= S
1
S
0
. You could also have
said N
1
= S
1
S
0
, since we dont care what happens when the state is 10. Finally, N
0
can be written simply
as N
0
= S
0
.
Exercise: Build a New Stoplight The nal answers are: N
1
= ((S
1
) S
0
) (S
1
(S
0
)) ((D) S
1
S
0
)
and N
0
= (S
1
) (D S
1
(S
0
)). Written in the more concise style, these are N
1
= S
1
S
0
+S
1
S
0
+DS
1
S
0
and
N
0
= S
1
+DS
1
S
0
. You could also combine the rst two terms of N
1
to get N
1
= (S
1
S
0
) +DS
1
S
0
.
FSM Table N
1
Karnaugh Map N
0
Karnaugh Map
S
1
S
0
D N
1
N
0
0 0 0 0 1
0 0 1 0 1
0 1 0 1 1
0 1 1 1 1
1 0 0 0 0
1 0 1 1 0
1 1 0 1 1
1 1 1 1 0
S
1
S
0
D 00 01 11 10
0 0 1 1 1
1 0 1 0 1
S
1
S
0
D 00 01 11 10
0 1 1 0 0
1 1 1 0 1

Potrebbero piacerti anche