Labview Project Examples
Figure 1. LabVIEW start up screen with Help menu | In order to provide a time / voltage output to show the quality of supposedly steady data LabVIEW must behave as an oscilloscope which repeatedly collects voltage / time data and displays it on a screen. It must also check if the user wants to log the data each time it collects some and do some manipulation of the data. There are several distinct processes to be performed which should be treated separately
LabVIEW comes with several examples of its functionality and one of these will almost certainly be ready to use, after a little manipulation, for any purpose required. Choosing one of these to collect data is step 1. Figure 1 shows the start up screen after LabVIEW loads. This is for version 7.1 but all versions will be similar. Clicking the arrow next to the Help... button brings up the menu shown which contains the Find Examples... link. click on this. |
Figure 2 shows the Example Finder window which has various ways of looking for examples. Usually browsing by task - left side of the window as shown - is the best method. Since the intention is to collect data from a DAQ card (hardware) the Hardware Input and Output folder has been chosen followed by DAQmx, Analog Measurements and voltage. DAXmx is the method LabVIEW communicates with the hardware. It is a low level language which sits between LabVIEW and the hardware. There are various means of taking analogue measurements of which voltage is the most basic. All the others assume some kind of signal conditioning is present or will apply it from within them. The bumber of examples displayed can be narrowed by specifying the hardware to use in the bottom of the left side of the window. The example highlighted is the one to use. It collects a finite amount of data using the cards onboard clock to set the rate and then stops. Once the program is called it does everything required itself and doesn't need further user input or an external timing signal as a trigger. The panel on the right shows what the example is designed to do and double clicking on the example will open it. | Figure 2. Example Finder window |
Figure 3. Front panel of example vi | A vi (virtual instrument) comprises two parts - a front panel and a block diagram. The front panel is what the user sees when working with the vi and is similar to the front panel of a physical instrument with controls and readouts on it. Figure 3a shows the front panel for the example chosen. Along the top are the usual title bar and menu bar with a set of control icons below them. Below that is the virtual instrument's panel. On the left are five input boxes. The top one, physical channel, tells the program which channel to read data from the DAQ card on. DAQ cards typically have 8 differential A/D input channels numbered from 0 - 7. Any one can be read individually or, if the vi is configured for it, several can be read at the same time. The next two input boxes allow the user to set the gain of the DAQ card. DAQ cards typically have a resolution of 16 bits and by default read in the range -10V to +10V. This gives them a voltage resolution of 20/2^16 = 0.3mV which is OK unless you need to measure voltages lower than that. If your voltage changes are smaller set the maximum and minimum to be just outside the limits you expect to measure - 0 - 1V say. The A/D reference voltage will be set to 1V and the voltage resolution will be 20 times better. The timing parameters set how many samples to collect each time the channel is read and how fast they are collected. Dividing the number by the rate gives the time it will take to collect each set of data. Care has to be taken in setting these depending on the application. On the right of the panel is a readout where the data collected will be displayed. with the numbers shown 1000 samples will be taken which will take 0.1s to perform. The time scale on the chart will show the 0.1s interval. Figure 3b shows the block diagram for the vi. This is the main part of the program and takes input from the front panel and sends output to it. By convention the general order of processes will be left to right b ut that isn't strictly true in all cases. Each of the squares represents a block of code that can be thought of as a subroutine and are called sub vi's. The rectangles wired to them are parameters that are passed to them. The first of the six sub vi's has the minimum and maximum values and the physical channel wired to it. These are directly connected to the controls on the front panel and pass the values typed into the front panel to the AI voltage sub vi. The fourth sub vi has a timeout wired to it and measurement. The time out is a constant and is not displayed on the front panel although it could be if it were changed to a control. The measurement is the chart. |
Many vi's are 'polymorphic' which means they can have their function altered to suit different requirements. All the sub vi's in figure 3b with a label underneath them are polymorphic. clicking on the down arrow on the right of the label brings up the menus to select the function of the vi. Figure 4 shows the expanded menus for sub vi 4 which deals with the output of data. Analog is required, as opposed to digital or others, but there are choices within this type that can be made. A type using several channels reading multiple samples at a time and outputting a 1D waveform is chosen. A description of the different types of signal and output is given here. | Figure 4. Polymorphic vi |
Labview Visa Serial Read Example
Figure 5. Right click on front panel icon Figure 6. Connector pane | This vi is now capable of performing the first two of the five distinct processes the finished vi is required to perform. If we make it into a sub routing we can call it from a higher level vi which will deal with some, or all, of the other three. A subrouting in LabVIEW is called a sub vi. Right click on the icon on the front panel - not the block diagram. The menu of figure 5 will appear which has the 'Edit icon...' and 'Show Connector' options. Editing the icon is a useful exercise dealt with later. For the moment the connector is required so select that option. A white square with dividing lines will be shown. Each of the rectangles formed by the dividing lines represent a point that a terminal can be made to wire commands indicators to when the vi is being used as a sub vi. The link between a particular rectangle and an item on the front panel is made by clicking the mouse pointer, which should show the wiring tool, on the terminal required to use and then on the item on the front panel it is to be associated with. The middle of the right hand rectangles has been associated with the indicator in figure 6 as indicated by the rectangle around the newly selected indicator item. Repeated associations for all items that may be required to be wired to the sub vi. When finished click the mouse on a blank part of the grey background on the front panel and right click the icon so the 'show icon' command can be clicked. It is possible to make a terminal compulsory to wire but the default of optional is usually OK. If a terminal isn't wired in a sub vi the value of the control in the sub vi when it is loaded is used as the default so choose these carefully. It is conventional to put inputs (controls) on the left side of the connector and outputs (indicators etc.) on the right but this isn't strictly necessary. |
Normally when a vi is saved and reloaded all the controls revert back to the state they were in when they were put on the front panel - usually 0. To change the value type the value required into the control, click outside the box to enter it and right click in the box to bring up the context menu. Now hover over 'Data operations' on the context menu and then click 'Make current value default' in the next one that appears. Save the vi to make the change permanent. |
The default icon in an example or a vi created from scratch won't be very instructive if the vi is to be used as a sub vi so it can be edited. Right click on the icon in either the front panel or block diagram and choose edit icon. The basic editing tools shown in figure 7 will appear. Clear the space and draw and write a suitable icon in it. Figure 7 shows the required icon in the B&W section with the 16 and 256 colours still in default. work in one of the three modes and when done click the others and press copy from the one you actually worked with. all three wil then show the same icon. Save the vi. | Figure 7. Icon editing palette |
Figure 8. Functions and Controls palettes | To construct the higher level vi to execute the other required functions will require manually placing items on the block diagram and front panel. for this the Functions and Controls palettes are required - figure 8. Right clicking in a blank area of the block diagram will bring up a temporary form of the Functions palette - figure 8a. It's temporary as once it has been used it will vanish. It can be kept on the screen by clicking the drawing pin at the top left. The Controls palette, figure 8b, works in a similar manner on the front panel. Each icon on the palettes represents a collection of similar structures that can be applied to either the front panel or block diagram. The isolated icon, bottom left on the Functions palette, is the one that allows the user to select a sub vi created by themselves. This isn't the default manner LabVIEW shows either palette in figure 8 but the functionality is the same. See LabVIEW's help to get a fuller description of the palettes. |
Create a blank vi by clicking 'New VI' from the file menu in an open vi or choosing 'blank vi' after clicking the arrow next to the 'new...' button in the start up screen (figure 1). Go to the block diagram, bring up the functions menu and click the icon circled in red in figure 8a. It allows a vi to be used as a sub vi. If the ensuing dialogue box isn't at the right location navigate to where the vi previously made has been saved. Open it and click the mouse in the empty block diagram. The icon created for the sub vi will appear in the diagram as in figure 9. | Figure 9. Sub vi in a higher level vi |
Figure 10. Adding controls to the sub vi | The sub vi can have data passed to it by wiring controls and constants to the terminals created in the connector pane. As previously mentioned if a terminal isn't wired the value of the control in the actual vi will be used. To create a connection select the wiring tool from the tools palette and hover over the input to connect. It's position will blink and a message will appear saying what it is. Right click on it and choose 'create' and either control, constant or indicator as appropriate. Strangely indicators can be connected to inputs and controls to outputs but an error will ensure as it shouldn't be allowed to happen. In this case all the inputs are connected to controls and will appear on the front panel. The output is the 'Measurement' and will appear on the front panel as a chart. The output is also connected to a function which calculates the mean and standard deviation from an array of data. The outputs from this are wired as well. There is also a while loop around all of the code which ensures it will all be executed repeatedly until the stop button is pressed on the front panel. While loops and other structures are explained by clicking here. At this stage the vi will repeat until told to stop and does some manipulation of the data. The while loop will repeat as quickly as the set rate and number of samples to collect dictates, with a tiny lag due to checking the stop button hasn't been pressed and calculating the mean and sd. |
Figure 11. Completing the higher vi | Figure 11 shows the rest of the vi added. A case structure is placed in the while loop. Case structures can have as many windows as there are choices to be made but only one of them will be executed at a time. There are two choices in this one - log the collected data or not. If the data is not to be logged the false case is executed each time the while loop repeats. This is empty except the wire from the down arrow on the left of the while loop to the up arrow on the right passes straight through without a +1 function. It's only there as any data exiting the case structure must have something wired to it in each frame and the false frame actually does nothing. The arrows on the frame of the while loop are linked and represent a shift register. At the end of the execution of one instance of the code inside the while loop the value in the right hand part of the register is passed to the one on the left and enters the while loop the next time it executes the code. The 0 wired to the left side of the register from outside the loop is an initial value when the loop runs for the first time. This shift register is being used to increment a count each time the log button is pressed. The last value is carried round the shift register until the button is pressed again. |
When the log data button is pressed the true part of the case runs. The frame inside the case is a sequence which allows the code inside it to run in a particular order. LabVIEW executes code as soon as it has all the necessary data at the input to a particular function which is usually OK. On occasion the code needs to be run in a particular order and a sequence structure ensures it does. It can have many frames but this only has two 0 and 1. Unlike a case all frames in a sequence are executed every time it is called. This one saves data to a file and then, in the second frame, reads the file and plots a point on a graph. Other features are a file outside the while loop with a control to name it. Anything outside the loop is executed only once before the loop starts, unless it requires data from the loop to run. This instance of writing a file overwrites one of the same name, if it exists. This effectively creates an empty file. The one inside the sequence adds data to the empty file. The front panel of the final vi is shown in figure 12. The controls of the sub vi created are duplicated on the left and are joined by the file path control, a control to enter a value called torque, and the mean (labelled voltage) and standard deviation indicators. The graph labelled calibration data is the one hidden in the second frame of the sequence structure while the one labelled voltage data shows the data from the sub vi. The log data button is added below the graphs with the stop button far enough from it not to be pressed by accident. | Figure 12. Front panel of higher vi |