[an error occurred while processing this directive]

Finite State Machines (FSMs)

A technique, particularly suited to controllers. (Originally hardware, but also good in software.)

A small Example

A door-pad controller (keypresses "a","b","c"), which opens on the password ``bcaa'', and lights up the "denied" lamp if not.

We use an internal clock "tick" input, to get us out of states open-door and denied, back to initial state.

Repreresentations of FSMs

Example: FSM for 3-disc CD controller:

(This next might be redundant for lecture, if students already got involved w/ the previous example and added lots of features/fixes.)

Attempt 1:
   
   state  |   S/S    EOD   SOD   
----------+--------------------
 idle     |  play    ??     ??
 play     |  idle   rotate  ??
 rotate   |  rotate  ??    play

The state "play" is wired to the disc-spin motor, and the state "rotate" is wired to the carousel-rotate motor.

Note that the "??" are inputs that shouldn't be generated if all goes well. (General policy: "fail-safe": on failure, do something safe. In this case, "idle" seems reasonable, to keep black smoke from billowing out the back of the CD player, or to keep, the laser from blinding kittens.)

Some observations on this FSM:

Let's remedy this last problem: Attempt 2:
   state  |   S/S     EOD      SOD   
----------+------------------------
 idle     |  play1     ??       ??
 play1    |  idle     rotate2   ??
 rotate2  |  rotate2   ??      play2
 play2    |  idle     rotate3   ??
 rotate3  |  rotate3   ??      play3
 play3    |  idle      idle     ??

"play1"…"play3" are all just hooked up to the disc-spin motor, and "rotate2", is hooked up to the carousel-rotate motor.

Okay this attempt is better, but still has problems.
  • What happens after listening to all discs, and then coming along and pressing Start/Stop? [Which disc is cued up? don't be mis-led by the state name!]
  • When we stop, (wait a day), and then start, which disc gets resumed? This might be the desired behavior, or might not be.

  • On your own:

    1. Fix the first problem above.
    2. Now, add a toggle button rpt/rpt-all/seq/shuffle. Note that this requires more state, to remember which we're in.
    3. In particular, nearly an entire copy of our original machine. This is called the "cross-product construction". (At a high-level, this would of course be an awful program. In general you might consider adding variables in addition to state, but only judiciously — else we could do everything with a big program and one state. Alternately, we could consider the "rpt" toggle as an input, which we ignore most of the time, and make transitions like "EOD & rpt". How to avoid ANDing inputs? Add an extra state (effectively computing AND)!

      Further example: Volleyball [Moved to homework] Recall that only the serving side can score.

      Brief comment about implementing in a program; OO: clearly states are objects; the transitions (edges) might also be objects.

      [an error occurred while processing this directive] [an error occurred while processing this directive]