Girish Mahajan (Editor)

State machine (LabVIEW programming)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
State machine (LabVIEW programming)

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.

Contents

Introduction to state machines

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 soda machine that dispenses soda 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.

State Machines in LabVIEW

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. To help explain this feature we will use a simple soda machine program to help illustrate the technique.

Common Devices of a State Machine

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 then new value can be saved on the shift register to be used at the start of the loop next time. 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. if you change one of them you end up changing all of them. Type def. allows the program to be much more robust and making it much simpler to change if something new needs to be added.


In the soda 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 shown below 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 there is 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.


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 change case).

Once in the Insert change 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 money every time you hit them. This is the main case where most of the work is being done and where the soda machine is as real as possible, with out 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.


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 Money Inserted display.

The dispense case starts by first checking to make sure that the user has inserted enough money to buy a soda. There is a case 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 soda and then subtracts the amount of money it takes to purchase a soda 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.

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.


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 soda. After 5000 milliseconds (or 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

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.

References

State machine (LabVIEW programming) Wikipedia