Gomez ESP2000
Gomez ESP2000
f e a t u r e
Embedded State
Machine
Implementation
Turning a state machine into a program can be pretty
straightforward if you follow the advice of a skilled
practitioner.
M
any embedded software applications are natural candi-
dates for mechanization as a state machine. A program
that must sequence a series of actions, or handle inputs
differently depending on what mode it’s in, is often best
implemented as a state machine.
This article describes a simple approach to implement-
ing a state machine for an embedded system. Over the last 15 years, I have
used this approach to design dozens of systems, including a softkey-based
user interface, several communications protocols, a silicon-wafer transport
mechanism, an unmanned air vehicle’s lost-uplink handler, and an orbital
mechanics simulator.
State machines
For purposes of this article, a state machine is defined as an algorithm that
can be in one of a small number of states. A state is a condition that causes
a prescribed relationship of inputs to outputs, and of inputs to next states.
A savvy reader will quickly note that the state machines described in this
article are Mealy machines. A Mealy machine is a state machine where the
outputs are a function of both present state and input, as opposed to a
Moore machine, in which the outputs are a function only of state.1 In both
cases, the next state is a function of both present state and input. Pressman
the software that controls it. The you been given a one-line problem • The mechanical engineer who’s
landing gear on this airplane consists description similar to this one: don’t describing what he wants probably
of a nose gear, a left main gear, and a retract the gear if the airplane is on doesn’t know as much about com-
right main gear. These are hydrauli- the ground. puters or algorithms as you do.
FIGURE 3 The result of finding out what the user really wants
Start Waiting
Gear Down for RaisingGear
Takeoff
curr_state = GEAR_UP;
};
void GearUp()
{
/* If the pilot moves the lever to DOWN, lower the gear.*/
if (gear_lever == DOWN)
{
curr_state = LOWERING_GEAR;
};
}
void WtgForTakeoff()
{
/* Once we ve been airborne for 2 sec., start raising the gear.*/
if (timer <= 0.0)
{
curr_state = RAISING_GEAR;
};
/* If we touch down again, or if the pilot changes his mind, start over.*/
if ((squat_switch == DOWN) || (gear_lever == DOWN))
{
timer = 2.0;
curr_state = GEAR_DOWN;
void LoweringGear()
{
if (gear_lever == UP)
{
curr_state = RAISING_GEAR;
};
3 are 3 are Squat Timer Pilot’s Pump Dir Timer Lights Next State
up down switch lever on/off
Gear down X X Air X Rising edge NC NC Start RED Waiting for takeoff
Waiting for X X X 2 sec X ON UP stop NC Raising gear
takeoff
Waiting for X X GND X Down NC X Restart NC Gear down
takeoff
Raising gear Made X X X X OFF NC NC OFF Gear up
Raising gear X X X X Down NC DN NC NC Lowering gear
Gear up X X X X Down ON DN X RED Lowering gear
Lowering gear X X X X Up NC UP NC NC Raising gear
Lowering gear X Made X X X OFF NC NC GREEN Gear down
Once the user’s requirements are fleshed out, I can crank out a state didn’t expect them to? Ideally, the out-
machine of this complexity in a couple of days. They almost always do put of your printfs would look a lot
what I want them to do. like the state transition table.
Finally, and this applies to all
embedded software and not just to
that based on state machines, be sus-
source-level debugger, or build an It is very handy to include print picious when you connect your soft-
“input poker” utility that lets you write statements that output the current ware to the actual hardware for the
the values of the inputs into your state, the value of the inputs, and the first time. It’s very easy to get the
application. value of the outputs each time through polarity wrong—“Oh, I thought a ‘1’
This requires a fair amount of the loop. This lets you easily observe meant raise the gear and a ‘0’ meant
patience and coffee, because even a what ought to be the Golden Rule of lower the gear.” On many occasions,
mid-size state machine can have 100 Software Testing: don’t just check that my hardware counterpart inserted a
different transitions. However, the it does what you want—also check that temporary “chicken switch” to protect
number of transitions is an excellent it doesn’t do what you don’t want. In his precious components until he was
measure of the system’s complexity. other words, are you getting only the sure my software wasn’t going to move
The complexity is driven by the user’s outputs that you expect? It’s easy to ver- things the wrong way.
requirements: the state machine ify that you get the outputs that you
makes it blindingly obvious how much expected, but what else is happening? Crank it
you have to test. With a less-organized Are there “glitch” state transitions, that Once the user’s requirements are
approach, the amount of testing is, states that are passed through inad- fleshed out, I can crank out a state
required might be equally large—you vertently, for only one cycle of the loop? machine of this complexity in a couple
just won’t know it. Are any outputs changing when you of days. They almost always do what I
want them to do. The hard part, of
course, is making sure that I understand
what the user wants, and ensuring that
the user knows what he wants—that
takes considerably longer! esp
References:
1. Hurst, S.L. The Logical Processing of
Digital Signals. New York: Crane,
Russak, 1978.
2. Pressman, Roger A. Software
Engineering: A Practitioner’s Approach,
3rd Edition. New York: McGraw-Hill,
1992.
3. Shumate, Kenneth C. and Marilyn M.
Keller. Software Specification and
Design: A Disciplined Approach for
Real-Time Systems. New York: John
Wiley & Sons, 1992.