tDEC LabVIEW Lab 4

advertisement
LabVIEW
- Practicum 4
Creating and Using SubVI's
Resources - This lab requires LabVIEW files: Temperature_Converter.VI and 4_Thermometers.vi.
Instructions - Carefully work through each of the following examples. Then try the Achievement Test
at the end to check your understanding.
Contents
Educational Objectives
Introduction
Example 1 - Review - Create a VI to convert Fahrenheit to Celsius and Kelvin.
Example 2 - Editing the Default Icon
Example 3 - How to define the connector terminals
Example 4 - How to use a SubVI
Example 5 - Conversion of Several Temperatures
Achievement Test
Educational Objectives
After working through this lab the student should be able to:
1. Create a custom icon for a SubVI.
2. Assign the terminals on the connector pane using the wiring tool
3. Choose a SubVI using the Select a VI... icon
.
.
Introduction
This practicum focuses on how to create and use SubVI's in your LabVIEW programs. SubVI's are the
LabVIEW version of subprograms. The use of SubVI's offers two significant advantages.
1. In programs requiring the same code many times, the use of SubVIs allows us to only program the
code once.
2. Many of our examples have used virtual instruments such as the virtual thermometer. When it is time
to replace the virtual thermometer with a real one, we need only replace its icon with that for the
SubVI which really acquires the data.
LabVIEW 4 - 1
Example 1 - Temperature Converter
The simple temperature converter you create in this section, will be used in a later example. The program
takes a Fahrenheit reading as input and outputs both the corresponding Celsius and Kelvin readings.
The temperature conversion equations are:
5
C = ( F - 32)
9
K = C + 273.15
OK - Let's get started. Open a new LabVIEW document and create the following temperature converter.
By now you should be able to create any VI just given a picture of its front panel and block diagram as
below. Be sure to use the labeling tool
to add the title and the little arrow that makes the purpose
(conversion) of this VI much clearer.
Be sure to test your VI by verifying that it correctly converts for 32 and 212 degrees Fahrenheit, the
freezing and boiling points of water, respectively. Finally, be sure to save your work using the file name
Temperature_Converter.vi
Example 2 - Creating Your Own Icon
The block diagram below, shows one possible use of our Temperature_Converter VI. In this example,
four temperature readings are charted and each has to be converted from Fahrenheit to Celsius. Of
course, you could just wire all the code from example 1 four times. Instead, we simply converted the
Temperature_Converter.VI into a SubVI and used it for each conversion. Do not create the four
temperature readings VI now. We will return to it later.
LabVIEW 4 - 2
Custom Icons - Notice that a custom icon
has been
created for our temperature conversion program. This new icon
makes it clear what the SubVI does and also communicates where
to wire to obtain either the Celsius C or the Kelvin K outputs. In
this example, we show how to create the above custom icon for the
temperature converter VI you created in example 1.
Block Diagram
If you have closed it, open the file Temperature_Converter.vi that you created in Example 1.
The figure below and to the right shows its front panel.
The Default Icon - The icon
in the upper right corner of
this VI is the default icon created by LabVIEW for your VI. The
number 1 on this icon indicates that this was the first VI we
created in this session of LabVIEW. Unfortunately, nothing about
the appearance of this default icon suggests that your VI is useful
for temperature conversion nor does it suggest where to wire the
terminals. The goal in this example is to create the new custom
icon
which better conveys the purpose of the VI.
Front Panel
To edit the icon you can either:
1. Double-click on the default icon.
or
2. Pop-up on the default icon
(Command+Click) and then choose
Edit Icon from the resulting pop-up
menu as shown.
Whether you choose to double-click or pop-up on the default icon (and then choose edit icon) you will be
presented with the Icon Editor dialog box shown below.
LabVIEW 4 - 3
Palette of Paint Tools - On the left of the Icon Editor window appears a palette
containing standard paint tools. For example, the icon
indicates the select tool
which one uses to select all the pixels within a rectangle and
indicates the text tool
which is used for drawing text. You should also recognize other familiar paint tools such
as the Paint Bucket and the tools for drawing lines, curves and rectangles.
First we will need to delete most of the old icon - we will keep only the outside rectangle which will make
a nice frame for our custom icon. To do this, select the unwanted pixels using the select tool
and
then press the delete key as shown below.
Select unwanted pixels
Next use the Text Tool
Blank Icon with Frame Remaining
to draw the letters F , C and K as shown to the
right. Of course these letters stand for the Fahrenheit, Celsius and Kelvin
scales. If the letters are a little miss aligned, you can move them around by
selecting their pixels and then using the arrow keys on the keyboard. Now
use the line and pencil tools to create the arrow which indicates the
conversion from F to either C or K.
OK - That will do for the black and white icon. LabVIEW recommends that you always create at least the
black and white icon because commercial and industrial users of your programs may wish to run them on
monochrome equipment. If you do not have the black and white icon, no icon will appear!
Next we will modify our black and white icon to create a 16 color icon as shown below. While in the Icon
Editor window, select the option to edit the 16 color icon, then copy the black and white icon by clicking
on the appropriate ‘Copy from button’ and finally fill in the background with a nice color of your choice
LabVIEW 4 - 4
using the Paint Bucket Tool.
Click on OK and return to the front panel of your VI. Your custom
icon
should appear in the upper right corner of the front
panel as shown below. This icon makes it clearer that your
program can convert Fahrenheit to either Celsius or Kelvin.
Importing Graphics - Those of us who are not artistically inclined may be a little worried about whether
we can create nice looking icons. The good news is that you can copy any icon, paste it in and then
modify as necessary. LabVIEW will convert the graphic to a 32 ¥ 32 pixel icon.
LabVIEW 4 - 5
Example 3 - How to Define The Connector Terminals
Pop-up on your custom icon and choose Show Connector. Instead of the icon, you should now see the
default connector
. Notice the connector has one white rectangle on the left and two on the right. By
default, LabVIEW creates one rectangle (terminal) for every control and indicator on your front panel.
Thus there is one rectangle for the Fahrenheit control and one for each of the Celsius and Kelvin indicators. (But which is which?) LabVIEW let's the user control which terminal on the connector corresponds
to which control or indicator. That is, we must assign the terminals - just as, for example, the various terminals on an amplifier or computer cable need to be defined.
Each terminal on the connector is defined using three clicks of the wiring tool
. We will assign the
large terminal on the left to the Fahrenheit input, the small terminal on the top right to the Celsius indicator and the small terminal on the right to the Kelvin indicator as shown below.
Fahrenheit
Control
Celsius Indicator
Kelvin Indicator
Assigning the Fahrenheit Terminal.
Using the wiring tool
, click on the large rectangle on the left of the connector. It should turn black
to indicate it has been selected. Then click on the Fahrenheit control to select it - do not drag the
mouse - just click again! Finally deselect the Fahrenheit control by clicking anywhere on the front panel.
The terminal on the connector should now turn orange
to indicate it has been successfully
assigned.
Assigning the Terminals for the Celsius and Kelvin Indicators
Similarly, use three clicks with the wiring tool to assign the small rectangle on the top right of the connector to the Celsius indicator and then assign the remaining rectangle to the the Kelvin indicator. When you
are done, all three terminals on the connector should appear orange
to indicate they have been
assigned. That's it, we are now ready to use your temperature conversion VI as a SubVI in any other program.
LabVIEW 4 - 6
Example 4 - How to use a SubVI
To illustrate how you can use your SubVI in another program, we will first demonstrate it with a simple
example. We will create a program that just converts Fahrenheit to Celsius.
Open a new LabVIEW file and place a Fahrenheit Control and Celsius indicator on the front panel as
shown below.
Now examine the block diagram shown above. The Fahrenheit input is wired directly into the left terminal of our SubVI and the desired Celsius value is wired to the indicator. Since we don't need the Kelvin
value, its terminal is not wired. The only mystery still do be told is how to import your SubVIs into
another program as we have above. The answer lies on the Functions Palette in the form of the Select a
VI... icon
. Clicking on this icon displays a dialog box which you can use to select any VI to import
into your programs. Click on the Select a VI icon now and select Temperature_Converter.vi which we
created in Example 1. Place the SubVI on the block diagram and complete the wiring as shown above.
Test Drive! Input 212o degrees on the front panel, turn on execution highlighting and run your VI. Watch
to see the 212o F flow into your SubVI and the desired 100o Celsius flow out of it. Finally, alter the front
panel and the wiring so that the VI displays both the Kelvin and the Celsius readings.
LabVIEW 4 - 7
Example 5 - Pesky Europeans
CNN’s London bureau reports to its European audiences on weather conditions in America.
But those pesky Europeans insist only the Celsius scale shall be used for all weather reports.
Help out the London bureau of CNN by building a VI to convert temperatures for American cities into
Celsius from Fahrenheit. Today’s readings are:
New York
100 °F
Philadelphia
90 °F
Chicago
80 °F
Your front panel should look like this. Obviously, it has not been run yet since 0°F ≠ 0°C.
There are no tips for this one. You are on your own. However, we will point out that you can create interesting effects like the above arrows, labels and boxes using the Decorations subpalette available from the
Controls Palette.
LabVIEW 4 - 8
LabVIEW Lab 4 Quiz A
Name
ID #
Section
Grade:
1. Sketch the back panel for a subVI which converts a Fahrenheit temperature F to a temperature C in
Celsius. The front panel is already given.
(2 points)
Front Panel
Block Diagram
2. Create (using LabVIEW) the following icon. Show it to your TA for credit.
(1 point)
3. Make a sketch showing how to connect two input temperatures F1 and F2 in Fahrenheit to Celsius
using your subVI from question 1. The front panel is already given.
(2 points)
Block Diagram
Front Panel
LabVIEW 4 - 9
LabVIEW Lab 4 Quiz B
Name
ID #
Section
Grade:
1. Sketch the back panel for a subVI which converts a Celsius temperature C to a Fahrenheit temperature
F. The front panel is already given. The conversion is given by: F = 32 + 9C/5
(2points)
Block Diagram
Front Panel
2. Create (using LabVIEW) the following icon. Show it to your TA for credit.
(1 point)
3. Make a sketch showing how to connect two input temperatures C1 and C2 in Celsius to Fahrenheit
using your subVI from question 1. The front panel is already given.
(2 points)
Front Panel
Block Diagram
LabVIEW 4 - 10
Download