LabView Design Pattern - State Machine

A design pattern is a high level architecture or structure by which a program can be implemented. Part of software engineering is to choose the optimal design patterns to meet your goals in implementing an application. There are many design patterns that you can follow in LabView, but one in particular almost always works for any application and provides a flexible framework by which to expand the scope of your program as it increases in complexity. This design pattern is the "State Machine". This pattern can be followed in other languages as well, and if you are already a software engineer, you are likely very familiar with it.

A state machine is essentially a while loop that runs continuously with a case structure that executes different code for a variety of "states". See the picture below:
The shift register keeps track of the state of the state machine. In this implementation, the state is a string. It begins at the state "Init". The shift register is initialized to the value "Init" for the first iteration of the while loop by wiring it from outside of the loop to the left portion of the shift register. Note that the "Init" case is shown. Anything in that case will execute only when the state machine is in the Init state. This will happen immediately on startup. The state is changed to "State 1" via the output tunnel of the case structure which is wired to the shift register to the right.

The next iteration will receive "State 1" from the shift register and execute the code in that case. This code modifies the state to "State 2".
Above is State 2. The state is modified back to State 1.

This program will start in the "Init" state, then begin alternating between the "State 1" and "State 2" states infinitely. You can imagine using this design pattern to produce more complex programs by adding additional states and logic to determine the next state. You could add any code you like to each individual state.

10 comments:

Anonymous said...

Nice lessons. Simple and clear to understand.
So usefull, i hope you will post new lessons.
Thanks for your time and knowledge writting them.

RZG.

Anonymous said...

I'm using this now at my company,

thanks

Anonymous said...

Nice site i admire your work

Anonymous said...

This state machine is great..although I took a while to make mine work:)

Continue the good work..

kanmani said...
This comment has been removed by the author.
kanmani said...
This comment has been removed by the author.
kanmani said...

im interested in doing labview text to speech synthesizer.can u help me with this

kanmani said...
This comment has been removed by the author.
kanmani said...
This comment has been removed by the author.
Anonymous said...

Have wired up the circuit as shown in the diagrams but get an error: "No case for some selector values" I can't see any typos. No idea why it won't run. Is there any information I might be missing?