Example Code

State machine using Queues

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Download All

Description

The State Machine is one of the fundamental architectures LabVIEW developers frequently use to build applications quickly.  State Machine architecture can be used to implement complex decision-making algorithms represented by state diagrams or flow charts.  More precisely, a state machine implements any algorithm described by a “Moore machine,” which performs a specific action for each state in the diagram. 

 

State Machines are used in applications where distinguishable states exist.  Each state can lead to one or multiple states, and can also end the process flow. However, the normal implementation of the state machine does not allow to buffer up several states. Say you click a button, and wants to execution State1, then State2 and finely go back to the Idle case. As this is already known as soon as you hit the button, you can take advantages of a Queue.

 

The following demonstrates a very simple example of how to combine a state machine with a queue.

StateMachine with Queues.png

When pressing the button called 'Run Process 1+2', we want two states to be executed. First we want to execute 'Process 1', and when that has finished we want to execute 'Process 2'. Finally, we want to go back to the idle state called 'Wait on Events'. This is simply achieved by adding these states to the queue, one at a time and thereby building up a buffer.

 

 

For more information on the State Machine:

Application Design Patterns: State Machines

 

More information about the Queued Message Handler:

The QMH differs from the 'State Machine using Queues'-design, since this design pattern is using two loops: one event loop, and one loop with the state machine. By making use of two parallel loops, we can have states that take a long time to execute, without making the user interface unresponsive.

 

Queued State Machine with User Input

 

Queued Message Handler Template Documentation

 

LabVIEW Queued State Machine Architecture

http://expressionflow.com/2007/10/01/labview-queued-state-machine-architecture/

Best Regards

Alex E. Munkhaus
Certified LabVIEW Developer (CLD)
System Engineer

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
A.E.M
Active Participant
Active Participant
on

Link for LabVIEW Queued State Machine Architecture has changed to:

https://decibel.ni.com/content/docs/DOC-32964

Best Regards

Alex E. Munkhaus
Certified LabVIEW Developer (CLD)
System Engineer
Brent_Gordon
Member
Member
on

Link for Queued Message Handler Template Documentation has changed to:

http://www.ni.com/tutorial/53391/en/
Contributors