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