PHYS 352 labview experiment

advertisement
PHYS 352 – LabVIEW Experiment
1
PHYS 352
Measurement Instrumentation and Experiment Design
General Instrumentation Experiment No 4
Instrument Control using LabVIEW
R Knobel, March 2009
Introduction:
In an earlier experiment, you learned the basics of controlling instruments over the RS232 serial communications standard through MATLAB. While this is a common way to
control instruments, the most common software used is a competing product called
LabVIEW, from National Instruments. This is a programming language specifically
developed for the control of instruments and for rapid development of “virtual
instruments”.
There are benefits and disadvantages to both MATLAB and LabVIEW. Programs in
MATLAB tends to take longer to develop and run more slowly than those developed in
LabVIEW. On the other hand, LabVIEW programs require a very different programming
style, and can become unwieldy for complicated systems. The purpose of this course is
to give you a basic introduction to each system, to allow you to use whichever one you
prefer or is most appropriate for the application.
In this experiment you will control a Keithley 2000 multimeter to measure the forward
voltage drop across a silicon diode when a constant current is sent through it. This will
be used as a temperature transducer.
Transducer:
The current through a diode is given by
I = IS (T )(e qV
kB T
"1)
(1)
where T is the temperature in Kelvin, V is the applied voltage, q is the charge of the
electron or hole, kB is Boltzmann’s constant and IS(T) is the reverse bias saturation
current at temperature T. The saturation current IS(T) varies approximately as
!
IS " I0e
#
Eg
kB T
(2)
where I0 is an approximate constant. So the voltage across the diode for a given current I
is:
$ I0 ''
1$
V
(T)
"
E
#
k
T
ln
& ))
& g
B
!
% I ((
q%
Thus, for a forward voltage less than
Eg
(3)
, the voltage decreases across the diode as the
q
temperature increases. This relationship holds approximately over quite a wide range,
from below 100K
! to above 400K, however there are some variations. Silicon diodes can
be used as very cheap thermometers, giving accuracy of a few percent. A readily
!
1
PHYS 352 – LabVIEW Experiment
2
available source of constant current is the resistance feature of a general purpose
multimeter such as the Keithley 2000. To measure the “resistance” of the diode, the
multimeter puts out a known current and measures the voltage. Note that the voltage
reported on the display of the instrument is the average voltage V I , and not the slope of
dV
the current voltage characteristic
at the point of the measurement.
dI
!
!
Figure 1: Keithley 2000 Data sheet1. Note the test current supplied for a chosen
resistance scale.
Calibration:
To perform a two point calibration of your diode:
1. Connect the positive lead of the diode to the black port of the multimeter and the
red lead of the diode to the red port of the multimeter with the clip probes.
2. Select “Two-wire ohms” measurement on the multimeter front panel with the
grey Ω2 button. The multimeter should read an on-scale number in ohms.
3. Do a quick check to see what the reading is as you change the range on the
multimeter. Change the range by pressing the range-up and range-down arrows
on the right front of the multimeter. This changes the supply current through the
diode as shown in figure 1. This “resistance” will change significantly. You
could plot a (very sparse) current vs. voltage graph for your diode vs. this number.
For our purposes, the 100 kΩ scale works well, giving 10 µA current.
4. Record the stable “resistance” reading when the diode is placed in ice-water and
in boiling water. These two numbers are the fixed points used to calibrate the
thermometer, assuming a linear relationship:
100°C
T (°C ) =
" (R(T ) ! R(0°C ) )
(3)
R(100°C ) ! R(0°C )
2
PHYS 352 – LabVIEW Experiment
3
5. Do a quick measurement and calculation of the resistance at room temperature. Is
it approximately correct? Does the measured temperature go up when held tightly
between your fingers?
RS-232 Communications:
We’ll use the RS-232 serial communication port on the computer and multimeter to
communicate. This is a common standard, though now becoming obsolete as the much
faster USB and Ethernet connections become more common. To set up the multimeter to
communicate with the computer, connect the RS-232 cable between the multimeter and
the COM port on the rear of the computer. To set the protocol on the multimeter:
1. Enter the menu by pressing SHIFT and then RATE /RS-232 on the multimeter
2. Set up the instrument to:
a. RS 232: ON
b. BAUD: 9600
c. FLOW: NONE
d. TX TERM: LF
by scrolling with the left and right arrow keys, changing the value with the up and
down arrow keys, and accepting a correct value using the enter key.
Programming in LabVIEW:
LabVIEW is full-fledged programming language, with many of the features in other
languages, however it has a very distinctive programming style. In LabVIEW,
programming is done graphically and variables are passed between commands by using
the analogy of wiring up a circuit. This is a rather natural way to program for instrument
control, however it means having to relearn programming, even if you are familiar with
other languages. Portions of this basic tutorial were taken from tutorials available at the
National Instruments web site2.
1. Launch LabVIEW
The Getting Started window appears when you launch LabVIEW. You can open
the LabVIEW Getting Started window by navigating to Programs » National
Instruments » LabVIEW 8.5 » LabVIEW . In the Getting Started window that
appears, choose New Blank VI. A VI is short for virtual instrument – these are
the programs or subroutines used in LabVIEW. You will create a VI to measure
resistance using the Keithley multimeter and then use that VI within another to
record the temperature.
LabVIEW displays two windows: the Front panel window (gray background) and
the block diagram window (white background). Examine the Front panel window.
The user interface, or Front panel, includes controls and indicators. You can
switch between the front panel window and block diagram window at any time by
pressing the <Ctrl-E> keys. You can also select Window » Show Front Panel or
Window » Show Block Diagram to switch between the two. The block diagram
appears with a white background and includes VIs and structures that control the
3
PHYS 352 – LabVIEW Experiment
4
front panel objects. Virtual Instruments (VIs) within another VI are termed
subVIs.
Figure 2: A blank VI. The front panel is in grey, the Block Diagram is in white
behind and the controls menu is to the left.
2. Read the resistance from the multimeter
Switch to the Block Diagram of the VI. Now the controls window is replaced
with the Functions window (see figure 3). These are a subset of the subroutines
and program control structures that can be used in the VI.
Click on Input. Click and drag the Instr Assist icon onto the Block Diagram.
(a)
(b)
Figure 3: (a) The Functions window. (b) The Input window.
The Instrument I/O Assistant helps to communicate with instruments over
various protocols, including RS-232. On the window that pops up use the dropdown box under Select an instrument to select COM1 – the first RS-232 port.
4
PHYS 352 – LabVIEW Experiment
5
Figure 4: Instrument I/O Assistant
Next, click Add Step at the top left and select Query and Parse from the window
that pops up. This means sending a request to the instrument (Query), receiving a
response, and parsing it into a usable variable.
Figure 5: The completed I/O Assistant window.
In the upper command window enter
:FETCH?
And select “\n” as the termination character. The fetch command retrieves the
latest reading from the multimeter (no matter what function is selected), and \n
represents line feed as the character ending each command.
Click Run this step. If all is working well, the middle of the window should
display some characters and the ASCII representation window should display the
number on the front of the multimeter. Click this window, which should bring
that number to the Value window. Select Data Type as Number, and type
“Resistance” as the Token name in the bottom left. After all this is done the
window should resemble figure 5. If not, there may be problems in
communicating between your computer and the instrument, and further
troubleshooting is needed. If it worked, click OK.
5
PHYS 352 – LabVIEW Experiment
6
You now have a function on the block diagram that will read the resistance from
the multimeter. To display this number on the front panel, right-click on the
Resistance box, scroll down to Create, and select Indicator (see figure 6).
Switch to the front panel and see that an indicator is there.
3. Run the program
You have now created a simple program to read the resistance. To run it, click
the
button at the top of the window. To run the program continually, click the
button. You should see a rapidly updating value of the resistance. Click the
button to stop the VI.
Figure 6: Creating an indicator for resistance.
4. Make this a VI
Any VI like this can be called from another VI, and have values passed both
ways. We’ll turn this simple VI into a sub-VI of a larger program. Right-click on
the
box at the top right of the front panel and select Show Connector. By
associating a connection from the icon to the resistance value, we can pass the
measured value to another VI. Click the blank square that appears and then click
the indicator where the resistance value is displayed. The square at the top right
should turn orange, showing that it is now associated with a floating point
number.
This VI is now complete. Save it to the hard drive.
Creating a VI to Display Temperature
Now that we have a very simple VI that reads the multimeter, we will use it as the heart
of a larger VI that will convert from resistance reading to temperature and then display
the results in a graph.
1. Create a new VI. On the front panel create two numerical controls by clicking the
Num Ctrls icon and dragging a Num Ctrl box to the front panel. Label this box
“100C Resistance”. Do this again and label the other “0C Resistance”. To label
6
PHYS 352 – LabVIEW Experiment
7
the boxes, simply begin typing after dragging the box. If you need to relabel later,
double-click above the control box.
2. Create two numerical indicators by clicking the up arrow at the top left of the
controls box, selecting Num Inds and dragging two Num Ind boxes to the front
panel. Label one “Resistance (Ohms)” and the other “Temperature (C)”.
3. Create an XY graph by clicking the blue up arrow again on the controls window,
selecting Graph Inds, and dragging an XY graph to the front panel at an open
space. Note that you can move all these boxes around to make it look nicer, and
change fonts/colours as needed. Relabel the Y-axis of the plot by double clicking
on the “Amplitude” text and type “Temperature (C)”. The front panel should now
look something like figure 7. Save the VI.
Figure 7: The front panel of the VI.
4. The front panel of our virtual instrument is now basically ready, yet it doesn’t do
anything yet. We need to build the back end which does the work. Switch to this
window with <Ctrl-E>.
7
PHYS 352 – LabVIEW Experiment
8
Figure 8: Thermometer Block Diagram at the start.
LabVIEW uses the analogy of wiring up a circuit to represent flow of
information. In Figure 8, we see that the output (represented by a triangle
pointing rightat the right side of an icon) of the Build XY Graph box is
connected to the input (represented by a triangle at the left side of an icon) of the
XY graph. The two controls (100C Reading and 0C Reading) and indicators
(Resistance and Temperature) are as yet not connected.
5. Create some space in the block diagram by clicking and dragging over the the
Build XY Graph and XY graph boxes to select them. Once selected, click
anywhere on the boxes and drag them over to the left under the other boxes,
freeing up space to create the main body of the program in the middle of the
window (figure 9).
6. Unlike most programming languages where the program flows from one line to
the next, LabVIEW Vis have no explicit order of operations unless we specify
one. Connecting “wires” between icons (representing subVIs) is one way to
impose this order, since a VI will only run once values have been sent to the
inputs. Another way to impose this order is to use a Sequence Structure. On the
Functions window, go to the top level by clicking the up arrow, and select Exec
Ctrl for execution control and click Flat Sequence. You can now click and drag
to create a window on the block diagram that looks like a strip of film. The
resemblance is intentional: think of this control as causing the VI to step through
each “frame” just like the frames in a movie. Create a large sequence structure
(see figure 9), since we need to put a lot of stuff inside.
8
PHYS 352 – LabVIEW Experiment
9
Figure 9: Sequence structure.
7. In the first frame of the sequence we’ll initialize the multimeter. On the functions
window click All Functions | Instrument I/O | Serial | Visa Configure Serial
Port and drag this near the top left of the sequence window. This configures the
baud rate, termination and other RS-232
parameters for the computer to be the same as for
the multimeter. As you move your mouse over the
terminals of the icon, your mouse pointer changes
to a spool, indicating that you can “wire” up this
VI, and the various input and output connections
for the VI become visible. Move the mouse over
the VISA Resource Name connection and right-click to bring up a menu. Scroll
down to Create and select Constant. This decides which RS-232 port on the
computer we are configuring. On the drop-down box that is created, select
COM1.
8. In the same SERIAL function window, click VISA Write and drag this near to
the first icon. We want to write to the same RS232 port that we just configured, so we need to
connect a wire from the Duplicate VISA
Resource Name on the Configure Serial Port
icon to the VISA Resource
Name on the VISA Write
icon. Move the mouse over
the first icon until duplicate VISA resource name is
displayed below. Click, then move the mouse until it is
over VISA Resource Name on the second icon. Click
again.
9. Now we want to send the initialization commands to the multimeter. Right click
on the write buffer input to the VISA Write icon, and select Create | Constant.
A pink box should appear which is the string that gets sent to the instrument. In
the box type:
9
PHYS 352 – LabVIEW Experiment
10
ABOR <Enter>
:CONF:RES <Enter>
:RES:RANGE 100E3 <Enter>
:INIT:CONT ON <Enter>
Which means abort any previous commands, read resistance on the 100 kiloOhm
scale, set to continually read resistance, and end the string of commands with a
line feed. You can save your VI now, and try running it. If all is working fine,
there should be no beeps from the multimeter, and it should switch from any other
mode to the 100 kiloOhm resistance.
10. Create the next frame of the sequence by right-clicking on the filmstrip-style
frame and select Add frame after. This will create another frame to the right of
the one with the initialization. You will likely have to do some rearranging to
make everything fit and to not have the sequence cover some of our other icons.
You can make the Block Diagram window as large as you like, however keeping
the program neat allows for easier troubleshooting, see figure 10.
11. The next frame will contain the loop to repeatedly read the resistance, the function
block to convert to temperature, and a dialog to save the data to a file. In the
functions window, click up several times until you are at the top level. Select
Exec Ctrl, and click While Loop. Create a while loop in the second frame of the
sequence that takes up approximately the middle 50% of the frame, though we
can resize later as needed. A while loop will repeatedly do whatever is contained
inside its rectangle until some condition is met. When we created this loop,
LabVIEW automatically created a “stop” button on the front panel that ends the
loop, which will suit our purpose.
Figure 10: Multimeter is initialized, and the next frame contains the while loop.
12. Next we need to put our indicators inside the while loop. Drag the Resistance,
Temperature and XY Graph (plus the box to build it) into the while loop.
10
PHYS 352 – LabVIEW Experiment
11
13. To add our resistance-reading sub VI to the current VI. In the functions window,
click the up arrow to get to the top (Functions) level, then select All Functions |
Select a VI… which brings up a dialog to find the file on the hard drive. Browse
to the folder where you stored the first VI and select it. Drop the VI in the loop
by clicking in an empty spot. Wire up the resistance indicator by connecting the
output of your subVI with the input to the resistance indicator (see Figure 11). At
this point, if all is working, you should be able to save the VI and run it, seeing
the resistance change rapidly on the front panel.
Figure 11: Inserting our subVI, and wiring to display resistance.
14. To convert from resistance to temperature, we will use a Formula Block. Go to
the top level of the functions window and click Arith/Compare, then click
Formula and drop the resulting box inside the while loop. This will bring up a
window that looks like figure 12. Label three input variables R, R0 and R100,
representing the resistance at the current temperature and the two calibration
resistances. In the formula box at the top type:
100/(R100-R0)*(R-R0)
which is our linear conversion formula. A green box at the top right indicates that
the formula has no syntax errors. If all looks good click OK.
11
PHYS 352 – LabVIEW Experiment
12
Figure 12: Formula node for converting from resistance to temperature.
15. To make the formula work, wire up the two controls for the set point resistances
to their respective inputs on the formula, and connect the current value of the
resistance to the R input of the formula. Feel free to move these controls on the
block diagram to a convenient location – either inside or outside the loop. Wires
which cross the boundary of the loop have a The Result can be connected to the
Temperature (C) indicator, as shown in figure 13. You should be able to run the
VI now and check that it gives a reasonable reading for the temperature. If ever
you see a broken arrow picture for the run button
, it means that there is an
error in the VI that will prevent it from running. Clicking on the broken arrow
button will bring up a list of these errors to help you troubleshoot.
12
PHYS 352 – LabVIEW Experiment
13
Figure 13: Wiring up the formula block
16. The last two things to do are to build the graph to display the temperature vs.
time, and to store the data to a file. In both cases, we need more than just the
latest data point, we need an array that contains all the data. We will build two
arrays: one for the temperature and another for the time. Every time the program
goes through the loop the latest points will be appended to the arrays. These
arrays will be sent to the graph, and when the loop terminates (i.e. when a user
clicks “stop” on the front panel) the data will be saved to a file. To begin we need
to initialize the two arrays. In the functions window, go to the top level and select
All Functions | Array | Initialize Array, and drag the icon to the left side of the
second frame, just outside the while loop. This subVI creates an empty array that
we will subsequently fill with our data. Right click on the element terminal,
scroll down to create, and select constant, and edit the constant to read 0 (zero).
Wire up this constant to the dimension size as well. You have now created an
empty 1-dimensional array (due to the dimension size being zero) of floating
point numbers (the type of constant you created when wiring up the element
terminal).
17. This empty array can be used for both the time and temperature data. The array
gets passed into the loop in a special way called a shift register. To create a shift
register, right-click on the left-hand edge of the while loop near the array initialize
icon and select Add Shift Register. Do this again to create a second one. The
created shift registers have two boxes with arrows: one on the left pointing down
, and one on the right pointing up . Wire up the Initialized Array output to
both of the shift registers (see figure 14). As the while loop starts, this initial
13
PHYS 352 – LabVIEW Experiment
14
empty array passed into the loop, and can be used inside the loop by connecting a
wire to the right hand side of the . We’ll append the new data to the loop, and
then connect the one-element-longer arrays to the right hand shift registers .
Whatever new value we send to this right hand end of the shift register will be
passed to the left hand side on the next iteration of the while loop. This allows us
to build up the arrays, one step at a time.
Figure 14: Initializing the arrays.
18. To build up the data arrays, drag two Build Array icons from the functions
window to a convenient location between the shift registers. These subVIs allow
us to add another element to each array. When your mouse pointer is over the
Build Array icon, the terminals will be displayed, as will blue boxes at the top
and bottom
. If your mouse pointer goes over one of the blue boxes, the
pointer will change to a double vertical arrow, indicating you can click and drag
to make the icon larger, adding more terminals. Click and drag on each icon to
give each icon two inputs
.
Connect each left shift register to the top terminal of the Build Array icons, and
the output (right) terminal to the right shift registers (figure 15). As the program
loops, new data gets added with each iteration to the end of the arrays. We can do
that now for one of the arrays: connect the temperature output of the formula
block to the lower terminal of one of the Build Array icons as seen in figure 15.
14
PHYS 352 – LabVIEW Experiment
15
Figure 15: Building the Temperature Array.
19. To record the time of each temperature reading, go up one level on the functions
window (to the All Functions level), and select Time and Dialog | Get
Date/Time in Seconds, and deposit one instance of this subVI just to the left of
the while loop, and another inside the loop. By subtracting these two times we
can get the time of each data point. Note that we could also drag another timing
subVI into the loop to control how often it executes, taking data every minute or
hour, by using the Wait subVI, but in this exercise we’ll just let the computer and
multimeter go as fast as they can.
20. To subtract the two times, go to the Numeric function window, accessible either
from the top level or from the All Functions level, and drag the Subtract subVI
near the two time icons inside the while loop. You want to subtract the time
the program started (from the icon outside the loop) from the time of the current
iteration (from the icon inside the loop) by wiring up the output terminals of both
of these timing icons to the bottom and top of the subtract subVI respectively (see
figure 16). As you will likely have noticed, the colour of the wires on the diagram
indicate the type of information being transmitted: orange is a double-precision
floating point number, pink is a string, blue an integer, green for Boolean (yes/no)
variables. The dull red of the wire for the time stamp indicates a special variable
type used only for time, however when wired up to the subtraction function, a
small grey circle at the input shows that the variable type has been converted
automatically to a double-precision floating point, which is fine for our purposes.
Connect the output of the subtraction to the empty lower terminal of one of the
Build Array subVIs.
21. Connect the output of the Build Array icon that was connected to the time
subtraction to the X Input of the Build XY Graph icon. Connect the output of
the temperature Build Array icon to the Y Input. In both cases, LabVIEW
automatically inserts a conversion subVI into the wire. This is because the Build
XY Graph box is an “Express VI” – a shortcut VI that uses a special variable
15
PHYS 352 – LabVIEW Experiment
16
type called Dynamic Data. This doesn’t affect our purpose, but if you want to
learn more about dynamic data, or any other advanced features of LabVIEW,
click on the help menu at the top of any LabVIEW window. One very useful way
to get help in LabVIEW is to go to this help menu and select Show Context
Help. This creates a window which will automatically display help on whatever
subVI or object is currently underneath the mouse pointer.
Figure 16: Time and Temperature arrays are built and wired up to the graph. The
Context Help window is displayed in the upper right.
22. Your VI should now run. Go to the front panel (Control-E), enter the values you
recorded for the resistance at 100°C and 0°C in their respective control boxes.
Run the VI by clicking the
box on the top of the front panel. You should see
a graph of the temperature of the diode. See if the numbers make sense, and that
it varies as you warm up or cool down the diode.
23. Stop the VI using the STOP button on the front panel. This button behaves
differently than the stop sign icon at the top of the window. Clicking the stop
button within the front panel stops the while loop from running, but not the VI. It
will allow us to add more steps to the VI, such as saving the data, once the loop
has been exited.
24. When the VI has been stopped, right-click on the graph and select Visible Items |
Graph Palette. This brings up a small toolbar that will allow you to pan and
16
PHYS 352 – LabVIEW Experiment
17
zoom in on the graph. Right click on the line in the legend at the top of the plot
. Here you can change the colour, symbol type, line style, etc. for
the graph. Choose a graph style where the symbols are displayed. Use the
magnifying glass on the graph palette
to zoom in on the data and see
how far apart in time the readings are, and what sort of variation in the
temperature is measurable.
25. To add the functionality of saving the data, go back to the block diagram (controlE) and go to the All Functions level in the functions window. Select File I/O |
Write to Spreadsheet File from the functions window and drag to the right side
of the while loop. This is a handy subVI that will store data in a column delimited
text format suitable for reading with a spreadsheet. There are several options that
can be set: format of the numbers, whether to create a new file or not, where to
save the file, etc. Most of these can be left at their default values. To save your
data using this routine you need to create a two-dimensional array from the two
one-dimensional arrays used previously. To do this you need another Build
Array subVI next to the Write to Spreadsheet File subVI. You can get one by
going to the appropriate level in the functions window and dragging the icon, or
simply selecting on one of the Build Array icons already on your front panel with
your mouse, typing <control-c> to copy it, clicking where you would like the
copy to go, and typing <control-v> to paste the copy. Wire up the top element of
the new Build Array subVI with the right shift register for the time array, and the
lower element with the right shift register for the temperature array. After the
while loop completes, this shift register terminal gives the final value of the array.
Connect the output of this Build Array subVI to the 2D Data input of the Write
to Spreadsheet File subVI.
26. One last change must be made: the Write to Spreadsheet File subVI as currently
configured will write the data in two rows with many columns. We would prefer
to have the data in two columns with many rows. To fix this, move your mouse
pointer over the Write to Spreadsheet File subVI and right click on the
Transpose? (no:F) terminal and create a constant. The (no:F) part of the label
means that the default behaviour if you leave this terminal unwired is to not
transpose the data. We need to do this, so click on the T part of the
constant
that was created, to switch it from “false” to true, indicated like
. When the
program is run you will be prompted to give a filename and path where the data
will be saved.
27. Save your VI. Run the VI from the front panel. Save some data and make sure
the file records properly. Compare the temperature you read with the value
recorded on a thermometer in the room. Estimate the time constant – how quickly
your thermometer responds to sudden changes (either up or down) in temperature.
Leave the thermometer for a few minutes with no obvious changes to the
temperature – how stable is the temperature in the room? Compare your readings
with those of another group – which is best done if the diodes are very close to
each other. What do you think are the most likely causes for the recorded
17
PHYS 352 – LabVIEW Experiment
18
temperatures to be different? What do you think are the most likely causes for the
measured resistances to be different?
1
Keithley 2000 Digital Multimeter Data Sheet, Revision F, available at
http://www.keithley.com/data?asset=3086
2
“Getting Started with LabVIEW Virtual Instruments”,
http://zone.ni.com/devzone/cda/tut/p/id/7001
18
Download