Lab view redux

advertisement
LabVIEW 2
Objective
This lab will give a more detailed introduction of the LabVIEW software. It will not go into device
communications, but rather it will give a more programming/logical approach to the G (graphical)
coding scheme. Using the ideas of random numbers, this lab will show you how to make a ‘coin
flipping experiment’. Using this approach, you will then be asked to try something with four equally
probable events rather than two. Along the way you’ll explore case structures, For loops and shift
registers.
Equipment and Components
LabVIEW!
Prelab (25 points) – Due at the beginning of lab
1. Suppose pair of fair dice is rolled 10 times. Find the probability that 7 will show at least
once. Show your work and final answer on the data sheet. Either express the solution in
decimal form or keep in expanded form.
2. Bring a floppy or two to lab.
3. It’s not required, but if you want to, you can start on the step-by-step instructions for Part 1
before coming to lab.
Part 1: Coin Flipper
As promised, first we’ll create a coin-flipping simulator.
1. Open LabVIEW by finding it in the Start Menu or on the desktop. The shortcut should be
National Instruments LabVIEW.
2. Create a new VI. This will open a new Front Panel window.
3. From the Controls Palette place three (3) digital indicators on the Front Panel. Name them
“Number of Heads”, “Ratio”, and “Number of Flips Completed”. The digital indicators can
be found under Controls  Num Inds  Num Ind.
4. Change the precision on the “Number of Heads” and “Number of Flips Completed” to 0 bits.
This is done by right clicking on the indicator and choosing Format & Precision and
entering 0 into the Digits of Precision field.
5. Place a Digital Control on the Front Panel (Controls  Num Ctrls  Num Ctrl and name
it “Number of Flips Wanted”. Change the precision to 0 bits the same way as before.
6. Arrange the indicators and control in the following matter as shown in Figure 1.
1
Figure 1: Front Panel after adding the control and indicators
7. Switch to the Diagram Window by using the Window  Show Block Diagram command
(or you can just click on the Block Diagram window to make it current) from the menus.
8. Draw a For Loop on the Block Diagram Window using the All Functions  Structures 
For Loop. Draw the loop structure large enough to fill most of the screen. This will allow
room for the rest of the components.
9. Draw a Case structure (All Functions  Structures  Case )in the middle of the For
structure. This structure doesn’t need to be all that large. Keep the cases as “True” and
“False”. Arrange the indicators and control as shown in Figure 2. Compare the Block
Diagram window with that of the figure below.
2
Figure 2: Diagram Window after drawing the Case structure
10. Add an addition block and a division block (Functions  Numeric  Add and Divide)
inside of the For Loop structure.
11. Change the case to “False” and add an addition block inside of the Case structure.
12. Right click on the edge of the For Loop structure and select Add Shift Register.
13. Now add a Random Number generator from the All Functions  Numeric  Random
Number (0 – 1). (Pick the icon that looks like a pair of dice. Now add a Round to Nearest
Integer box from the Functions Palette by All Functions  Numeric  Round to Nearest.
14. Add a Comparison block to the Diagram Window inside the For Loop structure using the
Functions Palette under All Functions  Comparison  Equal?. This will allow us to
compare two items. In this example it will be the number created to 0.
15. At this point it would be a good idea to add a time delay to the For Loop. Do this using the
Functions Palette under All Functions  Time & Dialog  Wait (ms). Move the parts so
that they are comparable to Figure 3.
3
Figure 3: Diagram after addition of the comparison block
16. At this point,
a. create a constant on the Wait (ms) box on the left side with a value of 100,
b. a constant on the upper half of the addition sign with a value of 1,
c. a constant on the addition sign inside the case structure with a value of 1 and
d. a constant on the Equal? box with a value of 0.
Creating constants is done by resting the mouse cursor over the desired connector pin and
right clicking. Choose Create  Constant from the pop up menu, then type the desired
constant in the little box that appears attached to the connector. After this is done the
diagram window will look like that of Figure 4.
4
Figure 4: Diagram Window after adding the constants
17. Connect the left side shift register to the bottom side of the addition block inside the Case
structure. Then wire the output of the addition sign to the right side shift register. (Note:
when you make the first wire connection from the left side of the shift register to the bottom
input of the adder, you’ll see a red X on the wire and an error message that complains that
you have a Boolean input (the case statement) connected to a “void” output (the shift
register). The software doesn’t like this kind of ambiguity. Don’t worry, it resolves itself
when you wire the output of the adder to the right side of the shift register and the red X goes
away)
18. Change the case from “0” to “1” and wire the left side shift register to the right side shift
register by going through the ‘tunnels’ on the Case structure. Note that two things have
happened. First the case statement changed from False/True to 0/1. And second, the adder
icon isn’t present in the “True/1 case. The wire jumps across the case statement from the
little box on the left to the little box on the right of the case statement box.
19. Create a constant on the left side shift register and give it a value of 0. Make sure that the
constant is outside of the For Loop structure.
20. Wire the simulation up as shown in Figure 5. This will complete the Diagram Window and
the simulation will be ready to run.
5
Figure 5: Completed simulation Diagram Window
21. Return to the Front Panel by using the Window  Show Front Panel command from the
menus or click on the Front Panel window.
22. Choose a number of trials to run the simulation with and type this into the “Number of Flips
Wanted” box or left click on the little switch to increment the control. (Note: when you
increment past 9 the number representation changes to 1E1 (10) until you click enough times
to get to 2E1 (20). You can have a little more resolution in the representation by right
clicking on the number in the box and selecting Representation  I32 from the drop down
dialog box that appears Now you can simulate the program by pushing the Run button on
the tool bar. Note the number of heads and the ratio values.
23. Fill out the table on the data sheet using the values for “Number of Flips Wanted”. To make
the simulation go faster change the value of the constant that is connected to the Wait (ms)
block. Setting this constant to zero will result in no delay and cause the simulation to run
much faster.
24. Have your TA initial your data sheet after you’ve simulated it.
25. On your data sheet, write a few sentences about what a shift register is and what it does in
this application. Also, describe the function of the “Round to nearest Integer” function.
6
Part 2: 4-Event Simulator
Create and simulate a program that will test and tally four equally probable events rather than two. In
this case, each event will have a 25% chance of occurring instead of a 50% chance like a coin. One
way of doing this is to make the probability range for each even equally divided between 0 and 1. As
a hint, the structure for the range of .25 to .5 is shown in Figure 6 below. Don’t pay attention to the
broken wires in this simulation, and just remember that this is a hint, not a direct application of the
simulation. For two of the ranges, you will need embedded cases, and in two you will not. But the
entire simulation will be run inside of a For Loop.
Figure 6: Hint for the Assignment of four events
The above is only one way of doing this, but there are a lot of other ways. You can use logic gates,
embedded case structures, or parallel case structures, or you can use just one case structure with four
different cases.
One more note: When determining how many shift registers you’ll need, remember that we only kept
track of one value in Part 1, so we only needed one shift register.
When you finish:
Have your TA initial your data sheet after you demonstrate it.
Fill in the table on your data sheet.
7
Write a paragraph on your data sheet about how your circuit works and the logic behind it.
8
Lab 6: LabVIEW 2 Version 7
Name_____________________________
Section________
Prelab (due at the beginning of lab)
1. Probability of at least one 7:
Part 1: Coin Flipper
1.
Table 1: Values for running the simulation
Number of Flips
10
50
100
1000
Number of Heads
Ratio
2. TA initials: ________
3. What is a shift register, and what does it do? What purpose does it serve in this program?
4. In one step you connected the output of the random number generator to a device that rounds to the
nearest integer. What purpose does this rounding function serve?
9
Part 2: 4-Event Simulator
1. TA initials: _______
2.
Table 2: Values for simulation of Assignment
Values
10
50
100
1000
‘Ones’
Ratio of ‘Ones’
‘Twos’
Ratio of ‘Twos’
‘Threes’
Ratio of ‘Threes’
‘Fours’
Ratio of ‘Fours’
3. Write a paragraph on your data sheet about how your circuit works and the logic behind it.
10
Download