Sie sind auf Seite 1von 5

Engr 101: LabVIEW Tutorial Session 2

During the lab session you will work through this tutorial. You will follow along making LabVIEW
code as you go. Please be sure to answer all the questions and follow all the steps. If you get
stuck during the tutorial, raise your hand immediately and ask for help as you will not be
able to complete the next step if you can’t complete the current step. You will be marked at the
end of the lab session – your instructor will simply make sure you have written all the programs
you are supposed to write and they work, and will check to make sure you have answered the
questions in the handout. If so you get full marks.

1. Loop Review
Make a new VI with a for loop as indicated below. This is similar to what you did in session 1.
Save it as AnotherLoop.VI. Include a delay of at least 1000 ms.

Set N to 10 and run the VI to remind yourself what it does. Set up a connector pane with two
inputs and two outputs and make N an input.

2. Arrays, Array Maths, and Graphs


2.1 Exercise
Make a Graph of the function y = 3x2 - 12x +1 from x = 0 to x = 10 in steps of 0.1. Save the result
as Function.VI. If you finish early right-click on your graph and the plot legend (where it says
plot 0) and explore various options.

2. 2 Function Stats
Use the array max function to find the minimum of the function and the x location of the
minimum. Use the array sum and array size functions to find the average value of the function
over the interval x = 0 to x = 10. Save the result as FunctionStats.VI.

1
3. Shift Registers
3.1 Setting Up a Shift Register
Close the function VI and open AnotherLoop.VI. Save a copy as ShiftRegister.VI (you’ll see
where the name comes from shortly). Now we will add a shift register to our VI. Shift registers
keep track of the results of previous iterations of the loop. Right-click on the left wall of the for
loop and select add shift register.

You might want to change the delay to about 5000 ms. Set N = 100. Push the light bulb icon
(highlight execution feature) and run the VI until you are satisfied that you understand how it
works and what it does. When you are satisfied, push the stop button and write a clear
explanation below. Raise your hand if you need help.

3.2 A Problem …
Remove the 1000 ms delay and deselect the Highlight Execution feature (the light bulb) so your
VI runs fast. Run it several times. See a problem? Sometimes you want this behaviour, but
often you don’t. You can fix this by initializing the shift register.

What is the sum of the numbers 1 + 2 + 3 …. + 10,000? You may need to resize the numeric
display Result to see the entire number. Just grab the right edge of the indicator and drag. You
can also change the formatting (right-click) to decimal or scientific notation if you wish. If you get
49,995,000 you got the wrong answer. See if you can figure out why. Ask for help if needed.

2
4. While Loops
4.1 What a While Loop Does
Now we will explore the use of while loops instead of for loops. While loops run until some
condition is met. Something like this: “loop while the stop button has not been pushed.” Put a
Boolean control on your panel. Perhaps use a stop button, but any Boolean control will work.
Set up a while loop as indicated below. Save as StopMe.VI. Hit run and then stop using your
stop button, not the LabVIEW stop sign icon (abort execution).

4.2 CPU Time


Pretty cool, but there’s a big problem. Well, it’s easy to solve. Hit the run button and then Ctrl-
Alt-Del and look at the Task Manager and then Performance. What percentage of the CPU time
is being used? Ouch.

Add a delay of 50 ms to your loop and repeat. What percentage of the CPU time is in use now?

Whenever you use a loop to wait for user input make sure you include a short delay. The
user won’t notice a delay of 50 ms.

5. Conditionals

5.1 StopMe Again


Any programming environment will have to have facilities for conditionals or tests. Is x>y? That
sort of thing. Let’s put a time limit on your loop. We will stop the loop when the user pushes the
stop button or after 10 seconds with no response. How many iterations would have occurred by
10 seconds?

S up a conditional to stop the loop after so many iterations OR after the user pushes the button.
Try your code to make sure it works. It should look something like the program below.

3
5.2 Annoying Your User
Without changing your program, describe the functionality if the or gate is changed to an and
gate. Write your prediction.

Now make the change and run. Does it work as expected? If not, explain where your reasoning
went wrong. Contrary to fairly common opinion, programmers generally do not deliberately
annoy their users.

5.3 Exercise
Use a while loop with a shift register to turn an LED on and off on your front panel. The LED
should be on for 0.5 sec then off for 0.5 sec etc until the user pushes the stop button or 20
seconds have expired. Save the result as Blinky.VI.

6. Is the Random Number Generator Fair?

6.1 Counting Random Numbers


Use a for loop to generate an array of 10,000 random numbers using the random number
function under numeric functions (dice icon). Count the number (out of 10,000) of random
numbers equal to or above 0.5 using a case structure to add one to the count if the random
number generated in the current iteration of the loop is greater than or equal to 0.5. Is the
fraction about 50%? Save your VI as RandomCount.VI Note there are other ways to write this
program.

4
6.2 Random Number Stats
Exercise: Starting with a new, blank VI, use the array functions to find the maximum, minimum,
average, and standard deviation of the random numbers. The standard deviation formula
appears below. Yes, LabVIEW has built-in functions to do all of this, but we want to learn how to
program maths in LabVIEW so we will not use the built-in functions. Instead, use the formula
below:
1
N 2 2
 ∑ ( xi − x) 
StdDev =  i =1  where x is the average, xi are the individual data points, and N
 N −1 
 
 
is the number of data points. This is called the sample standard deviation. Save your VI as
RandomStats.VI.

7. If Time Permits
Modify FunctionStats.VI to circle the minimum on your graph (from section 2.2). Also, draw a
line across the graph at the average value. Save as FunctionDisplayStats.VI

Das könnte Ihnen auch gefallen