1 Software Design Patterns First Sate Machine - Application Design a state machine, is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A particular FSM is defined by a list of its states, and the triggering condition for each transition. The behavior of state machines can be observed in many devices in modern society which perform a predetermined sequence of actions depending on a sequence of events with which they are presented. Simple examples are vending machines which dispense products when the proper combination of coins are deposited, elevators which drop riders off at upper floors before going down, traffic lights which change sequence when cars are waiting, and combination locks which require the input of combination numbers in the proper order. Finite-state machines can model a large number of problems, among which are electronic design automation, communication protocol design, and other engineering applications. In biology and artificial intelligence research, state machines or hierarchies of state machines have been used to describe neurological systems and in linguistics—to describe the grammars of natural languages. References: 1- Finite State Machine- Wikipedia, http://en.wikipedia.org/wiki/Finite-state_machine, Visited on Feb 28, 2013 2- Katz- University of California, Berkley, http://wwwinst.eecs.berkeley.edu/~cs150/sp00/classnotes/katz-ch9-mod.pdf, Visited on Feb 28, 2013 Read more on state machines. In this project, we will build a state machine that will demonstrate front panel control of events. 2 A- Menu VI Build menu system for a sample application Front Panel 1open a new VI 2place a cluster control 3place Boolean button (OK button) (just one for now) 4Set the button’s mechanical action to Latch When Released (right click and change) – however, do it from properties so the preview is seen before making the choice. 5Copy and paste to create the remaining buttons The user basically clicks a button in the cluster to trigger an application in the state machine 6- make sure the order of the elements in the cluster is as shown. Block Diagram 1- place a while loop on the block diagram to enclose the cluster and create a shift register 2- place a numeric constant to the left of the loop and set its value to -1. Connect the constant to the shift register – this is to initialize the state machine to start at the default state. 3- Place a case structure in the while loop. The case structure creates the states for the state machine. Wire as shown. Add 4 more cases to the case structure. 4- Go back to the default case and do the next steps. 3 5- Place a wait until so the loop executes 10 times a second. 6- Place the cluster to array function to convert the cluster of Boolean buttons into an array of Boolean data types. This way the Boolean object at cluster order 0 becomes the Boolean element at array index 0 and so on. 7- Place the search 1D array function and a True constant on the block diagram 8- Wire the True constant to the element input of the search 1D array . Also wire the cluster to array to the array input of the search 1D array. This allows the function to search for a T value in the Boolean array that cluster to array returns. A true value of any element implies that the corresponding button was clicked. The function returns a value of -1 if no button is pressed. 9- Connect a False constant to conditional terminal of the while loop. 4 10- Select the 0 from the case structure and put the one button dialog function on the block diagram – right click it and select Create Constant to create a string constant. Enter the exact string Login State. Wire a -1 to the case structure tunnel and a False constant to the conditional terminal. Next to the -1, place the comment: Next State: -1 (No Event) 11- Repeat step 10 for cases 1, 2, 3. the following string constants in order are:: Acquire Data State, Analyze Data State, View File State, and for case number 4 do the following: wire a -1 to the case structure tunnel and place the comment Next State: -1 (No Event) also place the comment Stop the VI. Also wire a True constant to the conditional terminal. A summary is shown: 5 6 Create a subfolder and name it Application Save the VI as Menu in the application folder. Run the VI 7 B- Acquire Data VI Goal: Build a VI that acquires, analyzes, and presents data while using error handling techniques. Acquire Data VI 1- close all LabVIEW VIs 2- open a new VI and build the following front panel 3- make sure you adjust the scales for the waveform graphs in properties>> Display Format and precision to be as shown on the graph 4- build the block diagram shown (NOTE: the sampling info is added on the block diagram next) The sampling info is added as : create>>control at the sampling info of the “sine waveform.vi” 8 The front panel should look like this run it and watch the time and frequency plots. 9 Save the VI as: Acquire Data in the application folder. C- Enhance Acquire Data VI The goal here is to modify a VI to use tab control along with proper user interface design techniques. 1- Open the Acquire Data VI 2- Place a horizontal smooth box decoration on the front panel. Place the three dials, the sampling information cluster, and the stop button on the decoration 3- Place a tab control (located in the containers palette) on the front panel. Select the time waveform graph and place it on the first page of the tab control 4- Name the two pages of the tab control Time Domain and Power Spectrum, respectively. 5- Click the Power Spectrum page and add the power spectrum graph to the page. The following is what you should have so far on the block diagram. 10 6- Create an icon for the VI 7- Create the connector pane and select the pattern with two inputs and one output. Connect the output to the Time Waveform graph (do not connect anything to inputs) 8- Save the VI in the application folder as: Enhanced Acquire Data 9- Open the block diagram and modify it as shown: And the other case: 11 Note, using the case structure enhances the performance. This is the case since the Power Spectrum operations are only performed when called for. 10 – display the front panel and run the V. Adjust the controls to change the time and frequency waveforms. 11 Make sure tht each of the inputs has a valid nonzero values then >>operate>>make current values default 12- Save the vi (after having shrunk the window around the needed items in the block diagram. 12 D- State Machine with Enhanced Acquire Data The goal is to add the Enhanced acquire data VI to the state machine application of Part A. Read instructions carefully 1- Open the menu.vi in your application folder 2- Open the block diagram 3- Display case 1 of the Case structure and delete the One button Dialog function. Add the Enhanced Acquire Data.VI that you created in part C. 4- Add a shift register to the border of the while loop. Connect the Time waveform output of the Enhanced Acquire Data.VI to the right side of the shift register. 5- Initialize the new shift register you created a. Right click the left side of the shift register and select create>>control to create an empty Time Waveform control on the front panel. b. Hide the waveform control by right clicking the terminal and selecting Hide Control. This will prevent confusion to users on the front panel. 6- Modify the VI properties of the Enhanced Acquire Data VI so that it appears like a dialog box when it is called. a. Double click the Enhanced Acquire Data VI to open its front panel b. Select File>>VI Properties>>Window Appearance>>then select Dialog Option c. Click OK then save and close the Enhanced Acquire Data VI 7- Now, recall, if one case in the Case structure passes data out of the case, then all other cases in the case structure must also send out data. Finish wiring the VI so that the data passes through all other cases unchanged. Make sure that the data passes through other cases correctly (see below) 13 8- Save as State Machine With Enhanced Acquire Data.VI in the application folder 9- Display the front panel and run the VI 14 E- Analyze & Present Data VI Objective: Create a VI in which you use graph cursors to select a subset of data for analysis. 1- Open the Analyze & Present Data VI found on my web site (on the course’s main page) Save, extract the files and move the three VIs to your application folder. 2- Use the two cursors in the plot window to select a subset of data to analyze. A cursor can move freely or be locked to the plot. You control this action using the Lock control button at the right side of the cursor display. Use the cursors that are locked to the plot. When the user clicks the Analyze Select Button, the VI reads the location of each cursor and uses this information to find the DC, RMS, Frequency, and amplitude values of the subset data. 3- Block Diagram: 4- Open and complete the following block diagram as follows: (many of these steps are already done – but go over them to make sure you understand) 5- Right click the data terminal and select Create>>Property Node to create the data property node. Resize the property node to four terminals. Right click the node and select the Active Cursor and Cursor>>Cursor Index properties from the property menu. Right click each Active Cursor property and select Change to Write from the short cut menu. Note, the property node executes from top to bottom. The property node selects each cursor individually and returns the index of each cursor. 6- Place the Max&Min function. This function helps determine the beginning and the ending index of the cursor locations. 7- Place the Extract Portion of Signal Express VI, located in Signal Manipulation palette, on the block diagram. 8- Complete the following steps to configure the dialog window for the vi as shown: 15 9- Place the Amplitude and Level measurements Express VI, located in Signal Analysis palette. 10- Configure the dialog window as shown: 16 11- Place the Tone Measurements Express VI and configure the VI as shown: 17 12- Place the Feedback Node, located on Functions>> Structures palette, on the block diagram. Feedback node transfers values from one loop iteration to the next (like shift registers) 13- Wire the cluster constant on the left of the while loop to the Feedback node initialize terminal. 14- Place the bundle by name and expand to show 5 inputs. Complete wiring 15- Finish the VI so that the data passes from the Feedback Node unchanged through other cases. Make sure the other cases in the structure pass data correctly. As shown: 16- Save the VI as: Analyze & Present Data.VI in the application folder. 17- Display the front panel and run the VI Move the cursors along the graph to select a subset of data to anakyze and click Analyze Selected Subset button. The results will appear at the right in the Analysis Results. When finished, click Return 18- Close the VI G- State Machine with Analyze and Present Data VI To add Analyze & Present Data VI you created above to the State Machine Application 1- Open the State Machine with Enhanced Acquire Data VI you created earlier in the application. The Front panel is already done. 2- Open the block diagram and Display Case 2 of the Case Structure and delete the One Button Dialog function. Use Select VI to navigate the Analyze & Present Data VI 3- Wire the waveform containing the collected data to the Data input 18 4- Save the file as: State Machine with Analyze & Present Data.vi in the application folder 5- Display the front panel and run the vi. Make sure you can acquire data and analyze the selected date 6- Stop and close H- Analyze and Present Data with Type Definition (TypeDef) VI In the application you are developing, you want to build around a program architecture that includes the ability to easily edit the code in the future. Because the Analyze & Present Data VI uses a cluster to store the analyzed data, the complexity of editing the code increases. In this practice you will convert the cluster constant and cluster into a strict type definition to allow you to easily add new data items to the cluster in the future without having to rewrite the code. Front Panel 1- Open the Analyze & Present Data VI (not the state machine …) you completed earlier. 2- Right click the border of the cluster in the Analysis Results as shown below and select Advanced>>Customize to open the cluster in the Control Editor. 19 34- 56- a. In the TypeDef. Status pull-down menu, select TypeDef. For the type definition b. Select File>>Save and save the type definition as Extracted Data.cti in the application folder c. Close the control editor. When prompted to replace the current control with the newly created control, choose YES. Block Diagram Display the block diagram and replace the cluster constant wired to the Feedback node Initializer terminal with the custom control as follows: Right Click the border of the cluster constant, select Replace>>All Functions>>Select a VI from the short cut menu, Select the Extracted Data.ctl When you modify the type definition custom cluster, all other clusters connected to it automatically update. Add a string indicator to the type definition custom cluster as follows: a. Right Click the cluster constant and select Open Type Def to open the Extracted Data.ctl 20 b. Add a string indicator to the cluster as shown and label it Operator c. Select File>>Save to save changes to the control d. Select File>>Apply Changes to update all controls and constants that are connected to the type definition e. Close the control editor 7- Notice now that a string indicator has been added to the cluster constant wired to the feedback node initializer terminal on the block diagram. 8- Display the Front Panel and notice that a string indicator has been added to the cluster indicator. 9- Display the block diagram and resize the bundle by name function to add an input for the operator string. Right click the input and select Create >>Control. The following block diagram should result. 21 10- Display the front panel, Add the Operator Control to a terminal on the connector pane as an input 11- Later on, information will be passed about the operator into this VI. 12- Save and close the VI 22 - Ensure completions of the exercise above Exercise 1: Verify Information VI Objective: Build a VI that demonstrates the Simple VI architecture Front Panel: 1- Open a new VI and build the following front panel as listed below: 2- Modify the user name and password string controls by right clicking each control and select: Limit to Single Line 3- Modify the password control: right click>>Select Password Display 4- Place a table control. Note, the table is a 2D array of strings. The first cell is element 0,0 a. Resize the table to contain two columns b. Right click>>Select Visible Items>> uncheck Label (to hide the label) c. Add free labels as shown for the two columns for the table 5- Enter the information as shown, save these values as default: right click the table>>Data Operations>>Make Current Values Default 23 Block Diagram: 6- Open the block diagram and build the following Notes: 1- To add the text, it is best to put it in-line as shown. Double click, add the text, then move it over the line. Use coloring tools to fill with no color. 2- Make sure the indexing is enabled at the while loop inputs 3- Indexing is disabled at the output to the operator Save the VI as: Verify Information.VI in the application folder 7- Display the front panel and enter some names and passwords in the table control 8- Enter corresponding name and password for the user name and password and RUN the VI. Make sure it runs correctly 9- On the front panel, from the menu bar >> Edit>>Make Current Values Default 10- Create an icon with two input and two output connector terminals a. Inputs are: User Name and Password (Top to bottom respectively) b. Outputs are: Operator and Access Granted (top to bottom) 11- Save the VI 24 Exercise 2: LOGIN VI Objective: To use local variables to initialize controls on the front panel. 1- Download the Login VI located on my web site under Lecture link into your application folder and open it. 2- Complete the block diagram as shown and explained below: Note: The local variables are enclosed in a single frame sequence structure and the empty string constant is wired to the border of the while loop. This ensures that both local variables are updated before the while loop starts. a- Create Login Name local variable. Set to Write. It is designed to reset the login name to an empty string b- Create Password local variable and set to write c- Connect an empty string constant as shown d- Create the Login Name Property Node by right clicking the Login Name terminal and selecting Create>>Property Node. Select the Key Focus property. Right click the property and select Change to Write. Wire a True Boolean to the Key Focus e- Place the Verify Information VI you created earlier today and connect as shown. f- Save the VI 25 3- Display the front panel and run the VI Notice, the login name and password reset to empty strings when you start. 4- Resize the front panel to include ONLY the necessary objects, use decorations to improve the appearance as shown 5- save the VI 6- Close the VI Exercise -3: State Machine with Login VI In this VI you add more functionality to the final application. Front Panel: 1- Open the State Machine with Analyze & Present Data VI you completed last time Modify the front panel to look as shown by placing a string indicator and a square LED. 26 Block Diagram: 2- Open the block diagram and modify case 0 by: a. Delete one button dialog function b. Place the login VI you completed earlier Complete the empty string connection, place the false constant to initialize the Access Granted LED to OFF Add, as shown, two additional shift registers since the VI needs Operator and Access Granted information in subsequent loop iterations. By using the shift registers, only case 0, in which the login vi runs, can change the operator and access values. 3- Write the operator and Access Granted straight through the other cases in the Case structure as shown. 27 28 4- Modify Case (-1) as shown 5- Save the VI as : State Machine with Login.vi in the application folder Display the front panel and run the VI to test it. Close the VI if all is well. The remaining part is logging data and loading the data. 29 i. The last completed VI is the State Machine with Login.vi. File I/O techniques Objectives Gain familiarity with file I/O Complete the State Machine application Exercise -1 Binary File Writer Objective: To build a VI that writes data to a binary file with a simple data formatting Build a VI that saves data to a binary file using simple formatting scheme in which the header for the file is 32-bit signed integer (I32) containing the number of data points in the file. The next Exercise (2) will read this data file. 1- Open a new VI and build the following front panel. 30 2- Build the following block diagram. Most functions are found under File I/O. The File Dialog is found under Express>>Input. Do not change anything in it, simply check the VIEW as icon. 3- Save As Binary File Writer.vi in the Application Folder. 4- Display the front panel and run the VI When the dialog window appears, navigate to the application folder. To save the file, enter the file name as data1.dat then ok 5- Close the VI Exercise 2: Binary File Reader Objective: To build a VI that reads the binary file created in previous exercise. 1- Open a new VI and place a waveform graph on the front panel (Do not place the array indicator yet) 31 2- Open and build the following block diagram Note, on the Read from Binary File vi, must make the connections to the left, then add a double constant (0) and THEN connect it to Data Type on that VI. 1- Edit>>Make Current Values default 2- save the VI as Binary File Reader 3- Run the VI, when prompted, navigate to the file you saved earlier and click on it. 4- You should obtain the same graph generated in the exercise above. 5- Save and close the VI 32 Exercise -3 Write to spreadsheet VI 1- on the front panel place an array with two string controls as shown 2- Build the block diagram shown --- Use Context help to gain familiarity Save the VI as Save to Spreadsheet.vi Run the VI, navigate to the application folder and save the file as test1.dat once the file is written: 1- open Excel, change the File type in the pull down menu to All files 2- open the file test1.dat 3- make sure Delimited radio button is checked, then click next 4- Tab is checked, then next 5- then click finish You should see two columns and 11 rows (including header) Exercise -4 Create a VI to read the spreadsheet file created in Exercise -3 – Demo to instructor. Exercise -5: View Analysis File VI Objective: To study a VI that reads data files created by the vi Save Data to File (already given to you to save time – you may investigate that vi at a later time.) 33 Download the compressed file: View Analysis and uncompressed it. Move the uncompressed files to the Application folder (should be two files). 1- Start with blank VI 2- Open the View Analysis File Unfinished VI found in Application folder 3- Place two definition controls that you created in previous exercises (See next instruction for process.) a. On the front panel, Controls>> Select a Control. Then navigate to ExtractedData.ctl to place the type definition controls on the front panel. b. Label the clusters as Initial Cluster and Data Cluster. c. Make sure the Initial Cluster is a Control and the Data Cluster is an Indicator. 4- Resize the Data cluster and reorganize the data items in the cluster similar to the following front panel 34 5- Resize the window to ONLY show the Data Cluster and the Return button. The Initial cluster does not need to be seen by the user, thus cover it with the Data Cluster 6- Complete the clock diagram as shown 35 Hint, when connecting the initial cluster to the open file function, right click the function and select Visible Items>>Terminals. Notice, the Dialog Type extends across the icon. You can wire the Initial Cluster at the top or bottom of the function. Though it is hard to see, the Initial Cluster is connected to the Record Type on the Open File vi 7- In the False case, right click Initial Cluster and select Create>> Constant. Place the constant in the True case and wire it to the output tunnel in the true case. 8- Save the VI as View Analysis File.VI in the Application Folder 9- Close the VI Now, let us complete the application A- Open the State Machine with Login VI B- Modify Case 3 as shown in the block diagram 36 Save the VI as Completed Application.VI in the Final1 folder. Display the front panel and run the vi Go through all of the choices. Demonstrate Successful Operation
© Copyright 2025 Paperzz