Showing posts with label state machine. Show all posts
Showing posts with label state machine. Show all posts

Wednesday, August 18, 2010

GUI Button State Machine

This post will share a design pattern that can be used in a serious GUI based application where several events must occur based on button presses. While LabView does have an event handling structure, this state machine can contain states based on button presses, but can also be extended to contain other cases (states) which execute in sequence as specified.

For this post, rather than show the details of every menu, here is the finished block diagram:

This follows the state machine design pattern. The state is tracked with a string via a shift register passed to the case structure each iteration of the while loop. The output tunnel will contain the state chosen for the next iteration. In this example, 4 buttons have been placed on the front panel, and there is a state called "Idle" which should be called when nothing is taking place. In the idle state, the 4 boolean buttons are built into an array which is searched for a "True". Since the Search 1D array function (found under the array menu) will return -1 if no "True" elements are found in the array, the "Idle" state continues until a button press is found (indicated by a search return value >= 0). When this happens, a new state is indexed from the string array constant and passed out of the Idle state on the output tunnel. You can place whatever code you like in the various states for your button presses.

Exercise 7 - Simple State Machine

Create a VI using state machine architecture that simulates a simple test sequence. The VI will have an initial state, where it will display a pop-up message indicating that it is starting the test.  Then it will proceed to the next case and then to the final state where it will ask the user whether to start over or end the test.

Front Panel
Rather than start from scratch, we will use a VI template to create our state machine.

1.      From the initial LabVIEW screen click on New…, and choose Standard State Machine, which is located under the VI from Template » Frameworks » Design Patterns heading.
2.      Examine the template, and then save it in another directory before you begin working on it.


Block Diagram

1.      Right click on the enum constant labeled Next State and select Open Type Def.
2.      On the front panel of the StateMachinesStates.ctl Type Def VI, right click on the States enum control and choose Edit Items.
3.      Add two more states.  Call them “State 1” and “State 2”
1.      Close the State Machines.ctl Type Def Front panel and save the control with the default name when prompted.
2.      Right click on the Case Selector Label of the case structure and choose Duplicate case.  Do this one additional time so that there are four cases: Initialize, State 1, State 2, and Stop.
1.      Change the value connected to the Wait function to 2000.
1.      Right click on the shift register on the left side of the while loop and create an indicator.  Change it’s name to “Current State”.
2.      In the “Initialize”, Default case place a One Button Dialog function and wire a string constant into the Message input.  Type “Now beginning test…” into the string constant.
1.      Change the enum constant labled Next State to “State 1”.
2.      Change to the next state in the case structure (“State 1”) and change the enum constant labled Next State to “State 2”.
3.      Change to the next state (“State2”) and add the following code.
a.       Place a Select function and connect two enum constants
(Tip:  Copy the enum constants from one of the previous cases)
Place a Two Button Dialog and wire create the constants as illustrated below.
14.      Run the VI.
15.      Save and close the VI.

End of Exercise



Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers