Getting Started With Labview: Joseph Vignola, John Judge and Patrick O'Malley Spring 2010
Getting Started With Labview: Joseph Vignola, John Judge and Patrick O'Malley Spring 2010
Introduction
In ME 392 well use the PCs in the lab to acquire data from experiments, and,
in some cases, to control experiments as well. There are a variety of computer
codes that can allow computers to interface with experimental hardware; some
companies or laboratories use custom codes of their own, others use commercially available software. The software well be using is called LabVIEW, which
is one of the more common commercial packages. The goal of using LabVIEW
in ME 392 is to give you a little experience using LabVIEW itself (since it
is a commonly used program) but mainly to give you an idea of how to use
computers in general when performing laboratory work, so that you can be a
proficient experimentalist no matter what software you may find yourself using
in the future.
The textbook for the course is LabVIEW 2009 Student Edition (ISBN
0132141299) by R. H. Bishop. This book provides 700-some pages of introduction to using this particular software, and is available both as a book alone and
in a bundle that includes the student edition of the software.
Chapter 1
1.1
Two-window Programming
is a display that you design, and can look like the front panel of an electrical
instrument, the dashboard of a car, or the control panel of a video game. Once
youve finished creating a VI, you (or any other user of your VI) need only
interact with front panel to use the VI. Once you have made a VI it can also be
used as a subVI or module by other VIs.
When you first run the LabVIEW application, the Getting Started window, shown in Fig 1.1, appears. Click on Blank VI to open a new VI and
empty front panel and block diagram windows will appear, as shown in Fig.
1.2. If you open an existing VI (from the open panel of the Getting Started
window, or by finding and clicking on the icon for the existing VI using the
computers operating system) you will only seen the front panel. You can open
the block diagram using the Window pull down menu.
Figure 1.1: The Getting Started window that appears after LabVIEW is
started. This window can also be opened using the view pull-down menu.
1.1.1
Front Panel
LabVIEW has a vast collection of controls and indicators you can choose
from to build your virtual instruments. The front panel is where a user turns
components on or off, changes experimental parameters, and where the VI displays measured operating conditions and data. LabVIEW controls are pictures
of controls that you are familiar with like switches, knobs, buttons and dials.
By clicking or moving the mouse on these controls the user can change settings
while the program is running. Some controls are set by typing text or numbers
into a box. All controls have some feedback to show the user the state they
are in. It is possible for the programmer to set things up so that the system
prevents a control parameter from being set to an invalid setting (or controls
what happens when an invalid setting is attempted). For example, if you dont
want a user to set a certain voltage higher than 5, you can prevent that outright,
or you can create an error message, have the computer make an alert noise, etc.
Indicators provide information to the user and can take many forms. These
include flashing LED indicators, numerical or character displays, bar level indicators, etc. Indicators might look like familiar objects like mercury thermometers, speedometers, voltmeters or the screen of an oscilloscope.
1.1.2
Block Diagram
1.2
The front panel has seven pull down menus and eleven pushbuttons at the top
of the window, see Fig. 1.2a. The block diagram has the same seven pull down
menus as the front panel and fifteen pushbuttons (eleven that are also on the
front panel, and four more debugging buttons), see Fig. 1.2b. The functions
of the pushbuttons are common commands for executing the code or formatting
and arranging objects in the widow, and they are described in Section 1.5 of
Bishop and are summarized briefly in Table 1.1 on page 9. The pull-down menus
are not unlike menus in many other applications. They provide a wide array of
(a) Tools
(b) Controls
(c) Functions
Figure 1.3: Palettes for selecting cursor tools, front panel controls, and block
diagram functions.
options, and a brief description of each menu is given in Section 1.7 of Bishop.
Additionally, pop-up menus are available by right-clicking on items in the front
panel or the block diagram, and descriptions of the options available in the
pop-up menus are available in Section 1.6 of Bishop.
There are three palettes, described in Section 1.8 of Bishop, that contain
various tools and objects used to create a VI. The tools palette has different
options for the mouse cursor: each one allows you to do something different by
pointing and clicking the mouse. In general, you can leave the Automatic Tool
Selection button on, so that LabVIEW will change the cursor for you to be
appropriate for whatever item you point at on the screen. The controls palette
can be visible only when you are programming in the front panel window: it
contains nested menus of all the possible controls, indicators, structures, and
decorations that you can place in the front panel. The functions palette is
visible only when the block diagram window is selected, and contains nested
menus of functions, subroutines, programming structures, and constants that
can be used in the block diagram. For both controls and functions palettes,
there are several different View options available, which simply alter the way
you navigate through the nested menus.
10
1.3
Lets perform a basic task: well add two numbers. In the Functions palette,
select the Programming menu and then the Numeric submenu, and drag
the Add function into the block diagram. You need to send two numbers into
the Add function, as shown at in Figure 1.4. For now, you can create these
numbers either by right-clicking the edge of the Add triangle and selecting
Create > Constant, or you can find a numeric constant in the Functions palette
near the bottom of the Programming > Numeric menu.
You can change the number by clicking in the constant
box and typing a new number. When we execute the
code, we need to display the answer somewhere: create
an indicator that will show up in both the block diagram
and front panel. To do this, you can right-click the right
corner of the Add triangle and select Create > Indicator,
or you can switch to the front panel window and find
Figure 1.4: Adding
a numeric indicator icon in the Controls palette under
Modern > Numeric.
The program above adds two constants, but youd need to modify the code
by clicking in the block diagram to change those constants if you wanted to
add different numbers. If you replace the constants with controls, youll be able
to alter the numbers in the front panel, without needing to modify the block
diagram each time the program is run. To do this, right-click either or both of
the numeric constants and select Change to Control.
Figure 1.5 shows a simple block diagram and front panel for a VI that reads
a temperature and can display the result in Fahrenheit or Celsius. You can
do a search for the Digital Thermometer.vi program and open it to explore
how this VI works. The front panel contains one control (a switch to select F
or C) and one indicator (a gauge and numerical readout for the Temperature).
The same two objects, labeled Mode and Temperature, appear in the block
diagram, but they are connected to wires that carry information along as the
VI executes.
1.4
Data Types
11
Figure 1.5: front panel and block diagram windows for a simple VI.
12
1.5
Order of Execution
The sequence of events, is typically a primary concern in a VI. In most programming environments (such as Matlab, Fortran, C++, etc.) the order of execution
flows from the sequence of text commands in the code (with if statements and
for and while loops capable of changing the sequence of events). The sequence of events in LabVIEW is a bit different and is dictated by the block
diagram. A dataflow is established from icon to icon and an item is executed
when all its inputs are available, that is, when all incoming wires are carrying
the necessary information. This can leave some ambiguity in the order items
are executed, and even implies that certain operations might be executed simultaneously (which is not precisely true). Careful layout of the block diagram
can make it easier to ensure that items are executed in the desired order: think
of the block diagram like a flowchart, where the flow of information from one
icon to the next dictates the order in which the icons items are executed. The
sequence of events tends to run left to right across the block diagram (there is
no hard rule that the sequence of events must proceed from left to right, but
most icons have inputs on the left and outputs on the right).
To illustrate this idea, say we need a vi that has two indicator lights (call
them A and B) and we need Light B to go on two seconds after Light A. To do
this, we can drop two round LEDs (Controls > Modern > Boolean) onto our
front panel. The front panel should look like Figure 1.6.
On the block diagram, wire them with True constants (Programming > Boolean). We also need our
delay function (Programming > Timing > Wait (ms))
with an input of 2000. Now the block diagram should
look like Figure 1.7a. When this VI is run, you will
notice that both lights go on at the same time, followed by a two second delay. This illustrates the point
that even though the block diagram elements are laid
out left to right, they are not executed left to right.
To correct this problem, we need a Sequence, which acts (and looks) like a
filmstrip, executing frames sequentially. You can drop a sequence (Programming
> Structures > Flat Sequence) on the block diagram and then drag the true
constant and A indicator into it. To add a second frame, right-click on the
border and choose Add Frame After. Drag in the delay, and add another
frame for the B indicator. Your block diagram should now look like Figure
1.6b. To test it out, youll need to first go to the front panel and switch off the
indicators by clicking them (they were left in the on state after the last run)
and run the VI. You should now get the behavior we expected; light A goes on,
then a two second delay, then light B goes on.
13
1.6
Programming Structures
Many of the programming concepts you are already familiar with from MATLAB
or other text-based languages are also found in LabVIEW. You can place an
if, for, while loop or other programming structure in the block diagram by
right clicking on the background on the block diagram and selecting structures
in the functions palette and then whatever structure you need. Use the mouse
to drag the structure to where you need it to be on the block diagram and to
stretch the loop to the desired size.
1.6.1
Conditionals
if-else The simplest form of the conditional is the if-else statement. LabVIEW
implements this feature using a Case Structure. When dropped on the block
diagram the case structure is set up to accept a boolean input to the connector
on the outside of the rectangle. Clicking the right and left arrows on the top of
the rectangle will toggle between true and false cases, only one of which will be
executed depending on the value of the input. Figure 1.7 shows an example of
a boolean case structure.
switch The switch-type conditional also uses the case structure in LabVIEW,
but takes a different input. The case structure can accept not only boolean
values, but also strings and integers and error clusters. When you wire a nonboolean type to the input connector, you will be able to add additional cases to
your case structure. If you wired an integer, for example, you could have a case
for each possible integer if you needed. In general, however, there would only be
14
a few integers that you would be interested in matching and the rest you dont
care about. For non-boolean type inputs, there is a special designation you can
put into the case selector named Default which will match any possibilities
that you did not already specify. Note that for non-boolean type case structures,
you must specify at least one default case. An example of an integer input to a
case structure is shown in Figure 1.8.
switch x
case 0,
case 1,
case 2,
otherwise,
end
msgbox(st)
st
st
st
st
=
=
=
=
'x
'x
'x
'x
is
is
is
is
0';
1';
2';
not 0,1,2';
(a) Matlab
1.6.2
Looping
15
Condition-Controlled (While) Loops A while loop executes a sequence of steps repeated over and over while a condition is meet. The
icon
feeds out the number times the loop has been executed (it starts counting at
zero) The condition is defined in the lower right corner of the while loop using
this
. This icon represents the stop if true condition. The condition can
be changed by right clicking and selecting something else (select continue if
true and the icon will change to
). A simple while loop is shown in Figure
1.10.
while now < datenum(2010,4,1)
end
(a) Matlab
(b) LabVIEW
Figure 1.10: A simple while loop that will run until April 1, 2010
Shift registers Shift registers are used in for and while loops to hold a
value from one loop iteration to the next. This is useful if you need the result
from one iteration when performing the same operation the next time around.
For example, we can use a for loop with shift registers to calculate the factorial
16
of a number. The loop in Fig. 1.11 is run a number of times controlled by the
input number. On the first time through, the constant 1 is multiplied by the
because LabVIEW
iteration number (one must be added to the result of
starts counting iterations at zero). The second time through, however, the result
of the previous calculation (in this case 1) comes through the shift register
and is multiplied by the iteration number. The result of this is 2, which then
passes through the shift register to be used in the third iteration and so on.
if n = 0
0
1
if n = 1
F (n) =
F (n 1) + F (n 2)
if n 2
1.7
An Example VI
There are many example VIs you can look at that should be loaded on the lab
computers. You can find them on the open panel of the getting starting
window or browsing from the panel. These examples give the opportunity to see
how LabVIEW handles familiar programming structures like for and while
loops, if statements, and subroutines.
The VI shown in Fig. 1.12 shows how LabVIEW handles familiar programming structures like for and while loops, if statements, and subroutines.
Each of these can be seen in the Temperature System Demo which you can
open from the getting started window. Figure 1.12 shows the front panel of
this VI. The program is executed from the front panel by pressing the run button (
) on the tool bar. Once you have started the VI with the run button
you can start and stop the measurement process with the Acquisition toggle
switch. The program plots made-up temperature data (there is no real temperature sensor in the system) as a function of time. The temperature is measured,
or sampled, at even intervals of time set by the user by adjusting the slider next
to the system control box. If you play with this VI from the front panel for
a few minutes youll figure out what the program does.
1.7. AN EXAMPLE VI
17
Figure 1.12: Front panel and block diagram windows for Temperature System
Demo VI.
18
1.8
The next several pages list common elements in the block diagram. The first
table lists the representation on the block diagram of controls and indicators
that also appear in the front panel, which varies in color depending on the type
of data coming from the control or going to the indicator. The next table shows
a variety of common objects, including different wire types and programming
structures. The final table shows a few example programming constructs.
Figure 1.13: Data format of controls and indicators based on color in the block
diagram
1.9
Debugging Tools
Highlight execution Build the VI you see in Fig 1.14 and run it a few times.
Change parameters of the Express VI by double clicking the Express VI. Change
the function so that the number of samples is not automatic and set it to 90.
19
Text labels
20
21
22
1.10. SUBVIS
1.10
23
SubVIs
to making sub-vis. First, they can make your code more readable. For set of
code that performs a single action, such as the factorial example above, it is
convenient in the main program to see a single icon representing a factorial,
rather than the for loop. Secondly, sub-vis allow you to re-use code without
having to recreate it on the block diagram. Again using the factorial example,
if you needed to take the factorial several times in your program, it would be
cumbersome to reproduce the for loop every time. It is much easier to have the
factorial be represented by a single icon.
Lets take our factorial code and convert it into a sub-vi. The easiest way
to do this is to select the code (by clicking and dragging the rectangular box)
that you would like to make into a sub-vi. In this case, since we only have the
factorial right now, select the entire code, as in Figure 2.2a. Then, go to Edit >
Create SubVI... LabVIEW will automatically put the selected code into a new
vi (the sub-vi) and replace what you had with the icon for the sub-vi, as shown
in Figure 2.2b. Note what happened, the original input and output to the loop
have become inputs and outputs to the sub-vi. You can double-click the sub-vi
to show its front panel and block diagram. If you right click on its icon, and
select Show Connector... You can see that there are two blue rectangles ,
24
If you like, you can edit the default icon that is created by right-clicking on
the icon in the sub-vi and choosing Edit Icon... You will then need to save
the sub-vi. Once you have a saved sub-vi, you can drop a second instance on
the block diagram by right-clicking on the block diagram and selecting Select
a VI... or simply dragging its icon onto the main block diagram.
1.11
In some cases, it is simpler to perform mathematical operations using a textbased editor, rather than a graphical one. There are several ways to accomplish
this in LabVIEW.
Formula node
One of the structures shown in the tables above is the formula node, which can
be found in the function palette under Programming > Structures (or under
Mathematics > Scripts & Formulas). The code shown at right performs the
exact same function as the very first addition example we did, but in a more
traditional, text-based way. Building this is a little more cumbersome for simple
operations, since it requires right clicking the edges of the formula node to create
various inputs and outputs. If the math you want to do is not very simple,
however, the formula node may be a quicker way to accomplish it than wiring
together a large number of Add, Multiply, Subtract, etc. blocks.
25
(a) Palette
Figure 1.19: The path to find the Matlab script node is shown with an example.
The icon between the Matlab script node and the waveform graph bundles data
of different types into a specific kind of cluster referred to as waveform data.
1.12
Express VIs
Some of the more sophisticated commands in the controls and functions palettes
are called Express VIs. These are subroutines for performing certain tasks in
a streamlined way. The Express VIs are light blue in color and typically feature
one or many options that can be set either using pop-up dialog boxes or menus
accessible by double-clicking the Express VI icon. For example, the Formula
Express VI brings up a Configure Formula window that allows you to create
a formula in a little more streamlined way than the simple Formula node. Fig.
9 below shows this configuration window, as well as how the Express VI icon
26
5. Create a subVI that multiplexes four inputs into a single output. The
subVI should have four floating-point (DBL) numeric controls (In1 through
In4), one floating point numeric indicator (Out), and one unsigned
8-bit integer control (Select). If Select=1, then Out=In1; if Select=2
then Out=In2 and so on. You should make two different versions of this
subVI. In the first, use the Select function from the Programming >>
Comparison palette. In this case, make sure that the user entering a value
other than 1-4 in the Select control is either handled or not possible.
27
28
For the second version of this subVI, use an Enumerated integer control
(Controls >> Modern >> Ring and Enum >> Enum) as your Select
input and use a case structure to select the output. Make sure to test both
subVIs by placing the sub in a main VI an wiring all the inputs. Make
sure to create an icon for your subVI. (Adapted from P4.3 in Bishop)
29
30
Chapter 2
2.1
Arrays
Arrays are groups of data of the same type. A one-dimensional (1D) array
is a group of data arranged in a row or column (a vector). A two-dimensional
array is a collection of one-dimensional arrays of the same length arranged side
by side or one atop the others, essentially forming a matrix. Arrays can be
composed of integer, real, complex, boolean, string, waveform and cluster data
types (and perhaps still others). Indexing of arrays in LabVIEW starts from
zero: the first element in an array is referred to as the zeroth the second is
the first, etc., so that if there are eight elements in an array, the index for the
last one is element seven.
To make an array, you begin with an array shell and then add a single item
of the type the array will be filled with. On the front panel, you can use the
Controls palette, enter the Modern > Array, Matrix & Cluster menu and select
Array. This shell can hold any sort of control or indicator. You can create
an array constant by going to the block diagram, using the Functions palette,
and selecting Programming > Array > Array Constant, and then filling it with
whatever sort of constant you wish (numeric, boolean, string, etc.).
31
32
Figure 2.1: Auto-Indexed For loop. Note that if 5,3,6,1,8 were replaced with
0,1,2,3,4, this loop would behave the same as Figure 1.9a
Auto-Indexed Tunnels
Auto Indexed tunnels can be used on the output side of the loop as well. In
many cases, as with the factorial example, we only care about the value after
the loop is completed. In some cases, however, it is important to know what
the value was after each iteration of the loop. In these cases, an auto-indexed
tunnel on the output side assembles information from all iterations of a loop
into an array upon completion of the loop.
Make the VI shown in Fig. 3.2 to see how scalar data can be pulled from an
array and then put back into an array and displayed. Note that the
icon in
the while loop does not need to be wired: the array enters the structure through
an auto-indexing tunnel (it does this by default) so the loop will automatically
execute a number of times equal to the size of the array.
2.1.1
Array Functions
Array functions are found in the Functions palette of the block diagram, under
Programming > Array. Here are a few:
2.2. CLUSTERS
33
2.2
Clusters
A cluster is a group of data elements that are of different types. Imagine pulling
together a green binary wire, a thick orange wire representing a one dimensional
array of real numbers and two thin blue wires carrying integers, and wrapping
them all into a single cable. Perhaps the most apparent reason to use clusters
is that they simplify block diagrams by reducing clutter. A more important
reason to use clusters is that they can keep together things that should stay
together. For example if you are measuring the temperature in the lab (a real
number) once per minute, you may like to keep track of other thing at the same
time, such as whether the light or air conditioning is turned on (binary), the
number of people in the room (integer), or the names of the people in the room
(vector of strings). A cluster lets you group this information together naturally.
Different clusters can be bundled together to make bigger clusters.
To make a cluster control or indicator on the front panel, select Cluster
from the Controls palette > Modern > Arrays, Matrix & Cluster menu. Like
34
an array, the cluster is comprised of the cluster shell and the individual controls
or indicators placed in it. A shell for creating a cluster full of constants in
the block diagram can be found in the Functions palette > Programming >
Cluster & Variant menu. This menu also contains functions for combining wires
into clusters and extracting individual wires again, referred to as bundling and
unbundling the wires:
If a cluster has more than one wire of the same type, it can be useful to
refer to each element by the label of the control or constant that created it.
For example, in Fig. 3.3, the input cluster contains three controls, labeled
Name, Age, and Height on the front panel, and the second two are the same
type, double precision real numbers. If the standard Unbundle function is
used, distinguishing between the Age and Height wires is problematic, but the
Unbundle By Name function solves this problem.
Figure 2.3: A cluster on the front panel containing three controls (left) and
unbundling the three wires with (bottom right) and without (top right) referring
to the labels on the controls.
2.3
The waveform data type is a type of wire used to carry information about a
signal that is sampled at regular intervals in time (or some other independent
variable). It is similar to a cluster, but it is hardwired to carry three specific
components, named Y, t0, and dt, which store the data, start time, and sample
interval (si or t) respectively. The data Y is a 1D or 2D array of numerical
values. The sampled interval dt is a single numerical value. The start time t0
contains a time stamp object. On the block diagram, the wire for a waveform resembles a pink cluster wire except the color is orange/brown. You can
35
create a waveform with the Build Waveform function in the Functions palette
> Programming > Waveform menu. Many of the VIs and functions you use
to acquire or analyze waveforms accept and return waveform data by default.
When you wire waveform data to a waveform graph or chart, the graph or chart
automatically plots a waveform based on the data, start time, and delta t of the
waveform. When you wire an array of waveform data to a waveform graph or
chart, the graph or chart automatically plots all waveforms. (Plots and charts
can also accept other forms of input, see the section on plotting below).
2.4
Many Express VIs accept and/or return the dynamic data type, which is another
cluster-like form. In addition to the data associated with a signal, the dynamic
data type includes attributes that provide additional information about the
signal, such as the name of the signal and the date and time the data was
acquired. You can wire the dynamic data type to most indicators or inputs that
accepts numeric, waveform, or Boolean data, such as a graph, chart, or numeric
indicators. The additional attributes included in the dynamic data can help
specify how the signal appears on a graph or chart. For example, if you use the
DAQ Assistant Express VI to acquire a signal and plot that signal on a graph,
the name of the signal appears in the plot legend of the graph, and the x-scale
adjusts to display timing information associated with the signal in relative or
absolute time based on the attributes of the signal. If you use the Spectral
Measurements Express VI to perform an FFT analysis on the signal and plot
the resulting value on a graph, the x-scale automatically adjusts to plot the
signal in the frequency domain based on the attributes of the signal. Right-click
a dynamic data type output terminal of a VI or function on the block diagram
and select Create > Graph Indicator to display the data in a graph or select
Create > Numeric Indicator to display the data in a numeric indicator.
Since most other (non-Express) VIs do not accept this data type,
you must convert the dynamic data type to something else to use a built-in VI
or function to analyze or process the data. The function for this conversion
can be found in the Functions paletter > Express > Signal Manipulation menu,
and is called Convert from Dynamic Data (From DDT for short). It allows
you to convert the dynamic data type to a variety of things, including a single
waveform data type, an array of multiple waveforms, and 1D and 2D arrays of
36
numbers. There is a corresponding To DDT that lets you convert these other
data types to the dynamic data type for use by Express VIs.
2.5
Plotting Data
There are many different ways to visualize data with LabVIEW. We will start
with looking at three ways to create traditional two-dimensional plots (LabVIEW also has means to visualize three dimensional data that we will not cover
here). Each of these plotting functions can be added to the front panel from
the Modern > Controls menu in the Controls palette (see Fig. 3.6). Charts and
graphs are described in Chapter 7 of Bishop.
2.5.1
A waveform chart is a plot that moves along as new data is added like an oldfashioned strip chart recorder. Create the VI in Fig. 3.7 and then explore how
it works.
Right click on the plot in the front panel and select Properties. Find the
Plots tab and try changing then way the different curves appears. Make visible
(by right-clicking the plot and pointing the mouse to the Visible Items) the
37
Figure 2.7: The chart recorder adds data points one at a time and the plot
moves to the right. These plots represent the time history of a sequence of
discrete measurement (each made at an instant in time)
Plot Legend, Scale Legend, Graph Palette, etc., and explore what these
additional options can do.
2.5.2
When data is already in the form of an array, it is often best to use the waveform
graph to plot. The waveform graph assumes that you have collected data with
an evenly spaced sampling interval. The waveform graph accepts several data
types. If it receives a single array of values, it interprets the data as points on
the graph (y values), and uses x values corresponding to the index of the points
in the array (that is, the x axis will contain integers 0, 1, 2, 3, 4. . . ). The graph
can also be given a cluster containing an initial x value, a delta x, and an array
of y data. This same information could be delivered via the waveform data type
or the dynamic data type. Recall that, n addition to the data associated with
a signal, the dynamic data type includes attributes (such as the name of the
signal or the date and time the data was acquired) that specify how the signal
appears on the waveform graph. Figure 3.8 shows these four different inputs to
the Waveform Graph function.
The same waveform graph function can be used to display multiple curves,
and there are a variety of ways to accomplish this. If given a 2D array of
numerical values, each row of the array is interpreted as data for a single plot
(you can right-click the graph, and select Transpose Array from the shortcut
menu to handle each column of the array as a single plot instead). The waveform
graph also accepts a cluster containing an initial x value, a delta x value, and a
2D array of y data. It will also create multiple plots if given an array in which
each element is a cluster containing the data for a single plot (y data, initial x,
and delta x). Finally, it is possible for the dynamic data type to carry more
than one signal simultaneously. These different arrangements are shown in Fig.
3.9.
Create a VI that uses two copies of the Simulate Signal Express VI to create two signals carried by dynamic data type wires. Combine the two signals
38
Figure 2.8: Different input formats for the Waveform Graph function. Top to
bottom: (1) a 1D array, (2) a cluster containing a 1D array, initial x and x,
(3) a waveform, and (4) dynamic data.
into a single wire and connect it to a Waveform Graph indicator. Then, try
converting the dynamic data into an array of waveforms using the From DDT
Express VI
in the Functions palette > Express > Signal Manipulation menu. Plot the signals contained in the array with another Waveform
Graph indicator and observe how the results are different. Try to convert the
dynamic data to a 2D array of numbers as well, and observe how the graph is
different for this input form.
2.5.3
The xy graph indicator is used when the x data is not evenly spaced. For
each line in the plot, the input is a cluster of two arrays, and the result is a plot
of one versus the other.
39
40
Chapter 3
Data Acquisition
3.1
There are a variety of functions in LabVIEW for acquiring data from a data
acquisition card (DAQ) or sending a signal out to an experiment via the same
card. The most user-friendly method is to use Express VIs. The DAQ Assistant
is an express VI found from the Functions palette, under the Express > Input or
Express > Output menus. When you drag this express VI onto a block diagram,
a startup window (see Fig. 4.1) will appear. After you make selections related
to how you will use the DAQ, you will see the DAQ Assistant properties window
(see Fig. 4.2).
42
from measured voltage into some other units appropriate to whatever sensor is
being used), and triggering for the data acquisition to begin at specific times
relative to other events.
The two 1 Sample options under Acquisition Mode on the Task Triggering
tab cause the DAQ Assistant to acquire (or send) a single piece of data on
each channel every time the flow of the LabVIEW program reaches the DAQ
Assistant Express VI. Typically, the N Samples option is used instead, to send
or receive a specified number of data points at a specified rate. The number
of samples and the sampling rate are set in the fields just to the right, under
Clock Settings. When using the DAQ Assistant to send a signal out from the
computer to an experiment, an option can be checked to use waveform timing,
which means the number of samples and the rate at which they are sent will be
dictated by whatever signal is wired into the DAQ Assistant icon on the block
diagram. The number of samples and sampling rate can also be provided to the
DAQ Assistant express VI as inputs from elsewhere in your block diagram, and
if this is done, any incoming information overrides information entered in the
properties window. For example, Fig. 4.3 contains a simple VI that reads data
from the DAQ at a sampling rate that is specified by a wire on the block diagram
that connects to on numerical control on the front panel. The output data from
the DAQ Assistance express VI is wired into a Waveform Graph indicator to be
displayed.
The Input Setup tab to the right of the list of channels in the DAQ Assistant
properties window allows you to select the range used by the digitizer when acquiring data, as well as to scale the measured voltages to convert them into units
43
Figure 3.3: Usage of the DAQ Assistant to record data at a specified rate
more appropriate to whatever device the signal is being read from. Note that
this tab might be hidden if the Show Details button at the top of the channel
list has been pressed pressing Hide Details returns the tab to visibility. The
values entered for Min and Max under Signal Input Range dictate the range
used by the digitizer. A typical data acquisition card has several fixed ranges
that can be used: in this case the smallest range that completely encompasses
the Min and Max entered by the user will be the range used. If custom scaling
is employed, these values should be entered in the scaled units, not as voltages,
but keep in mind that the range selected for use may not exactly match the
range you enter.
For example, lets say youre acquiring data from a microphone that has a
sensitivity of 0.053 V/Pa. You can pull down the Custom Scaling menu and
select the option to create a new scale. In the window that appears, you could
name the scaled units Pa for Pascals, and set set the slope of the linear scaling
relationship to be 18.87, which is the reciprocal of 0.053. Once you close this
window and make sure your newly created scale is the one selected, the acquired
data will have units of Pascals instead of Volts. Now, to select a value to enter
for Min and Max, you should consider the highest and lowest sound pressure
levels you expect the experiment to involve. If you create a table like the one
shown below, you can determine what range the digitizer will actually use (and
thus at what pressure the digitized signal would be clipped).
Table 3.1: Example showing scaled units.
Digitizer Range [Volts]
-200 mV to +200 mV
-1 V to +1 V
-5 V to +5 V
-10 V to +10 V
-3.774
-18.87
-94.34
-188.7
Pa
Pa
Pa
Pa
to
to
to
to
+3.774
+18.87
+94.34
+188.7
Pa
Pa
Pa
Pa
44
3.2
Triggering
We will create a digital pulse that can be used as a trigger for the data acquisition
to occur. To send the trigger signal, you can configure another copy of the DAQ
Assistant Express VI to send the digital pulse, as shown in the bottom loop in
Fig. 4.4. Create the VI shown in Fig. 4.4 and configure each DAQ Assistant so
that the program runs properly. Configure the triggering DAQ Assistant to send
data out on Digital Port 0 Line 0. Then configure the other DAQ assistants,
via the triggering tab in the properties window, to begin on the rising edge of
a digital pulse on the PFI0 channel. (Trigger type should be digital edge.) On
the DAQs connector block, you can connect the PFI0 input port to the P0.0
output using a cable. Then, whenever the flow of the LabVIEW code reaches
the DAQ Assistant that sends the digital pulse, the PFI0 channel will see this
pulse and both other DAQ Assistant tasks will begin (assuming, of course, that
the LabVIEW code flow had previously reaches those icons and therefore set up
those tasks).
It is also possible to perform software triggering, by sending a previously
recorded signal into the Trigger and Gate express VI found in the Express >
Signal Manipulation submenu, and shown below in Fig. 4.5. This VI extracts a
portion of the incoming signal beginning whenever a specific threshold is reached
on a specified channel. As shown in Fig. 4.5, the extracted portion can include a
certain number of samples, or can last until another Stop Trigger threshold in
reached. The extracted portion can also be configured to include an additional
fixed number of samples before the Start Trigger, by entering a certain number
of Pre-samples. It is even possible to make this number negative, so that the
extracted portion does not begin until a specified number of samples after the
trigger threshold is reached.
3.2. TRIGGERING
45
Figure 3.4: A VI to simultaneously send and record signals to/from the DAQ,
with both events triggered to occur simultaneously.
Figure 3.5: Trigger and Gate express VI, for software triggering.
46
Figure 3.6: Configuration window for the Trigger and Gate express VI.
3.3
Measurement data can be written to or read from files in a number of different ways, and different file formats are supported. The file I/O functions are
assessed from the File I/O menu in the Functions palette. You can find functions to change directories, change file names, move files and more form this
menu.
(b)
VI
Express
47
Figure 3.7: Usage of the DAQ Assistant to record data at a specified rate
48
Configure this VI for use as a subVI by connecting both controls and the
indicator to terminals on the connector diagram, then save the VI. Test
your program by creating an array of numbers and making sure that the
averaging is successful, in both row and column configurations.
2. Write a second LabVIEW VI that creates a sinusoidal signal contaminated
by noise and then averages multiple instances of the signal to remove the
noise. You can use the Simulate Signal Express VI to create the signal,
placed within a for loop structure so that the signal is created many
times. Your front panel should feature:
Controls for the signals amplitude, the amplitude of the noise, and
the number of averages used to suppress the noise (the number of
times to run the for loop).
Indicators including:
49
50
You can use Shift Registers to assemble the 1D arrays generated by
the Simulate Signal vi into a larger and larger 2D arrays as the
loop repeats.
Call the subVI you created in step 1 to perform the averaging calculation.
Make sure the labels on your graphs are correct. Plot amplitude vs.
time if you can, otherwise, be sure to indicate the plots are amplitude
vs. sample number.
3. Modify your code so that instead of simply averaging the signal you created, it sends that signal to the DAQ, and then reads another (corresponding) signal back from the DAQ and performs the averaging on that
signal.
Test Your Program
On the DAQ connector box, connect the output directly to the input
so that your code will be sending and receiving essentially the same
signal.
Adjust the amplitude of the signal and the noise and perform the
same tests as step 2.
Set the amplitude of the signal and the artificially created noise to
zero and try to figure out how much noise is introduced by passing
the signal through the DAQ and the coaxial cable. This noise should
be pretty low.
51
Then, try sending a very weak signal (without any artificial noise)
through the DAQ and use multiple averages to extract it from the
noise that gets introduced along the way. With enough averages, you
should be able to measure a signal that is significantly smaller than
the noise in the system.
4. Add code that will save the averaged data to an external file that can be
read by other software. Test Your Program by running the VI and then
try opening the resulting data file in Word, Excel, or MATLAB.