You can edit almost every page by Creating an account. Otherwise, see the FAQ.

State machine (LabVIEW programming)

From EverybodyWiki Bios & Wiki


A state machine is a type of programming technique that is used in a majority of different programming languages. The state machine is especially important and flexible tool that is used in LabVIEW programming because the coding is very easy to maintain, document and reuse.[1]

Introduction to state machines[edit]

State machines revolve around 3 concepts: the state, the event, and the action. The state is the position or status that the program is at when it is working through the problem. For example, a state could be waiting for the user to do something, or running a calculation. States work to help break up the big picture and help to make everything run smoother. Developing these wisely will help to make the state machine run more efficiently. Events are occurrences that have specific meaning to the program itself. The example that we will be building is a vending machine that dispenses an item after the user has inserted the correct amount of money. An event for this program could be the money being inserted or the person hitting the start button. The action is how the program will react to the particular event that has occurred.[1]

State machines in LabVIEW[edit]

In LabVIEW, state machines are even more powerful. A state machine can help the program respond intelligently to different types of data that the user could put in.[1] To help explain this feature, we will use a simple vending machine program to help illustrate the technique.

Common devices of a state machine[edit]

A simple case structure located in a while loop using enumerated constants and shift registers

In LabVIEW state machines can be started by simply creating a while loop with a case structure located within the loop. The while loop can have a special constant wired to it, called an enumerated constant. An enumerated constant is a constant word or phrase is changed to a simple integer from 0 to n − 1, where n is the number of cases the user would like to have. Doing this makes programming a lot easier for the user to understand because instead of looking at number codes they are looking at a few words describing what is going on. A state machine also relies heavily on shift registers. A shift register is used to pass information around in a while loop. For example, a shift register will start with information that will be worked on in the loop. After the work is finished, a new value can be saved on the shift register to be used at the start of the loop next time.[2] Next all the programmer has to do is add tasks to each case individually. After the initial enumerated constant is created, it is often worth while to make it into a type definition (commonly referred to as type def.). Making the enumerated constant into a type def. allows the user to continue to copy the original and re-use it in the program, like a normal control except with a type def., changing one instance changes all of them. Type def. allows the program to be much more robust and makes it much simpler to change if something new needs to be added.

Simple vending-machine example[edit]

File:LabVIEW State Machine example (Start Case).png

In the vending-machine example we are using shift registers to pass around the total amount of money in the machine and the enumerated constants that control which case we are in. When we look at our start-case picture, we can see the shift registers on the left and right side and the enumerated constants in the middle. We can also see at the bottom of the picture a tab that is used to display each individual section is being written to as an indicator. By doing this, we remove the possibility of the user getting off of the loop and locking them into the case they are supposed to be in. When we are in the start stage of the program, we want to make sure that the user is right there with the program.

LabVIEW State Machine example (Start Block Diagram).png

This case holds the user on the start menu on the block diagram, shown below, until the user hits the start button. Once the start button is pressed, the program is then sent to the next case (called the "Insert money" case).

LabVIEW State Machine example (Insert Money Case).png

Once in the "Insert money" case, the user will once again be held here to continue adding money to the total they have had so far through the use of shift registers and buttons that add money every time you hit them. This is the main case, where most of the work is being done and where the vending machine is as real as possible, without adding actual coins, every other case has most of the work running in the background. Once the user has finished adding money, they press the "Finished inserting money" button to move on to the "Dispense" case.

LabVIEW State Machine example (Insert Change Block Diagram).png

The "Insert money case" also changes which tab is being displayed to the "Insert change" tab on the block diagram. Here the user can insert money and also see how much money was through the "Total Change Inserted" display.

LabVIEW State Machine example (Dispense Case).png

The "Dispense" case starts by first checking to make sure that the user has inserted enough money to buy an item. There is a second case structure in this case to handle the two options of either enough money, or not enough money. If there is enough money in the system, a message box will pop up saying thank you for purchasing the item, and then the amount of money it takes to purchase an item is subtracted from the total amount of money in the system. If there is not enough money in the system, the program outputs a new message saying that there is not enough money and sends out all of the money that was previously inserted before moving on to the "end" case.

LabVIEW State Machine example (Dispense Change Block Diagram).png

The block diagram for the "Dispense" case and the "end" case is the same picture. There is really nothing that the user really has to do besides pop up messages that the program will output.

LabVIEW State Machine example (End Case).png

The "end" case is a very simple case that works to simply delay the program to allow the user enough time to check that they have received their change and picked up their item. After 5000 milliseconds (5 seconds) the wait timer is used, up and the program continues back to the start page to wait for another user to come by to begin the process over again.

Benefits[edit]

The state machine is often used because it is easy to quickly start up and add or remove parts of the program. State machines work in a fashion as to also help keep the developer organized as they work.[3]

References[edit]

  1. 1.0 1.1 1.2 Bitter, Rick. LabVIEW Advanced Programming Techniques. Boca Raton: CRC Press LLC, 2001.
  2. LabVIEW Help Page from LabVIEW 2011.
  3. "Tutorial: State Machines", www.ni.com, 2014.


This article "State machine (LabVIEW programming)" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:State machine (LabVIEW programming). Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.