Thursday, February 17, 2011

Using the Finite State Machine Design Pattern in LabVIEW to Implement the xkcd 90s Flowchart

There are a number of really great flowcharts from xkcd, I chose this one to implement in LabVIEW as an example of using the Finite State Machine Design Pattern:

Implementation Notes:
  • I am assuming an implied program stop after 'hammertime' and 'listen'. 
  • The 'Stop' after 'No' is a stop of the program.
  • The 'Stop' after 'Yes' is a sub string of the program output (surrounded by matching parenthesis in order to avoid tension).
  • I have added a decision after the data flow 'Yes'->'Stop' to select either 'MCH' (MC Hammer) or 'ICE' (Vanilla Ice) which then determines which string the program returns.
Implementation Steps:

First, create a new VI from template and select the Standard State Machine:

This gets you started with a template VI:

Then implement the flowchart as a Finite State Machine.  Note that the end VI is a bit hard to visualize based on the nature of the Case structure, here are some screen shots that show each of the cases followed by some screen shots of the front panel at different states.

First initialize and give the user a message that the test is starting.
 Then ask the user if it is the 90s.  If yes continue to 'State 2', if no go to state 'Stop'.
If it is the 90s the program gets to 'State 2'.  Now ask the user to choose between MC Hammer (MCH) and Vanilla Ice (ICE).  Then return the string associated with the user choice and advance to the 'Stop' state.
Stop the program.

Here are example screen shots of the program: