C-Frameworks: Basic State Machine Implementation

How to Use State Tables for Implementing Finite State Machines

There are several implementations of finite state machines avaible, the only drawback most of them are suffering is that they are moore machines (under certain circumstances this means a large number of states) and changing the machines behaviour will change the whole code structure (this means low flexibility and the reprogramming may be a huge cause of defects).

I’d like to present an Implementation of a FSM, i’ve found during my student research project that is a mealy machine and does not require a reprogramming of the underlying structure.

I realized this implementation in an event-driven structure in LabVIEW (g) (file a comment if you’re interested in that implementation). However i recently needed a state machine implementation in C, and tested some implementations and remebered my old implementation of that matrix-style implementation.

State Transition Table

The basic idea of this implementation is that every state machine can be directly represented as a state transition table. I’ll be using a two-dimensional transition table (matrix), depending of current active state and possible events (see Table 1).

State 1 State 2, Action 2 State 3, Action 1 State 1, Action 4
State 2 State 1, Action 3 State 2, Action 1 State 2, Action 3
State 3 State 3, Action 1 State 1, Action 4 State 2, Action 2
Table 1: State Transition Table

e.g. the current state would be ‘State 2′ and the second event has been triggered, the machine will initiate ‘Action 1′ and stay in ‘State 2′.

This compact representation can easily be derived from any state diagramm. Suchlike tabular structures can be transferred into programming languages without difficulty. This is the main benefit of this representation.

C Code

States, events and actions are represented by enumerator typedefinitions. All the rest will be build upon them.

 2 * typedefs
 3 *******************************/
 5typedef enum {
 6  STATE_0,
 7  STATE_1,
 8  STATE_2
 9} state;
11typedef enum {
13   ACTION_1,
14   ACTION_2,
15   ACTION_3,
16   ACTION_4
17} action;
19typedef enum {
21   EVENT_1,
22   EVENT_2
23} event;
25typedef struct {
26   state nextState;
27   action actionToDo;
28} stateElement;

Based on these type definitions the state transition table can be developed as a 2D-Array of stateElement structs.

1stateElement stateMatrix[3][3] = {

these definitions and constants can be stored in the header file. Getting the machine running is realized via a basic function.

2 * prototypes
3 ***************************************/
4 action stateEval(event e);

This function determines what action will be occasioned depending on the triggered event and sets the new state of the machine. The mechanism behind this function is lightweight.

 1action stateEval(event e) 
 3    //determine the State-Matrix-Element in dependany of current state and triggered event
 4    stateElement stateEvaluation = stateMatrix[currentState][e];
 6    //do the transition to the next state (set requestet next state to current state)...
 7    currentState = stateEvaluation.nextState;
 8    //... and fire the proper action
 9    return stateEvaluation.actionToDo;

All that is done is indexing the stateMatrix-Array depending on current state (row) and occured event (column). All things additional needed will be a global constant currentState to store the machines state. This could be done in the main()-loop. Control of the state machine can be achieved via simple mechanism over the stateEval()-function.

1// this single line is the whole FSM-Call, easy as that!
2   actionToDo = stateEval(eventOccured);

This could be put in an event-triggered interrupt-routine, a while-loop, a for-loop over each triggered event, or whatever is reasonable. The eventOccured has to be computed by a special algorithm, for example an combination of more or less complex expressions. In the easiest imaginable way this would be an if-structure:

1if ( T > T_MAX ) 
3   eventOccured = overTemperatureEvent;

All these little parts combined lead to a quite simple approach for a state machine implementation in programming languages. The most convincing part is the state transition table, wich is the only thing –beneath the states und events type definition– that has to be changed, to change the whole state machine behaviour. It has a direct connection to state machine theory and the common state diagram representation. Thus making this approach tremendously customizable and understandable.

comments powered by Disqus