Loops in LabVIEW (Timing the loop!) |
An Introduction To Using
LabView - 2 - A Timed Loop
In the first LabVIEW note
we examined some simple arithmetic operations in LabVIEW. In this note we will
examine how to write a simple program with a timed loop. After all, it isn't
enough just to do arithmetic. In most applications you need to do calculations
over and over again. Repetitive calculations are best done using a timed loop.
Timed loops are important
because you often encounter situations where you want to take measurements
repetitively at a constant time interval. Timed loops let you take data where
you can set the timing between measurements. Before getting to timed loops, we
will walk you through some simple loops, and we will end with some timed loops
that generate and display sinusoidal signals.
In this note - as in the
other LabVIEW notes, you could just read through the note, but you will do
better if you work along as we discuss what is happening.
The first thing you are
going to do is to construct a simple timed loop. You need to do the following.
Start LabView.
Choose New vi.
We will use some of
routines that we have written previously. You will need to have the
Diagram window active. Choose Window-Show
Diagram if the diagram is not active and
showing.
Choose
Window-Show Functions Palette. The
Functions palette is shown below.
We will write a loop that
executes for a number of times that you specify - eventually. But for
now, we are going to add a few things to make this run. First try to
run the vi. You should get an error message that the conditional
terminal is not wired. That's not the only problem - but it is the
problem that you will fix first.
To start, go to the
Front Panel, and add a Numeric Indicator
from the Numeric
menu. When you have the numeric indicator on the front panel, right
click the indicator and choose Find
Terminal. You will be taken to the diagram
and the terminal will be evident. Note that adding the indicator on the
front panel automatically adds a terminal for the indicator to the
diagram. There are some components that
can only be added - or deleted - from the front panel or the diagram
- even though they must appear on both.
Now, add a constant
to the Conditional Terminal by right-clicking on the terminal to add a
constant. Make that constant equal to True
using the hand tool.
At this point, your block
diagram should look like this.
Now, you can run the
program. All that happens is that the indicator shows the loop counter
- and it will show that each loop takes very little time!
As the numbers on the
indicator rapidly zip by, hit the Stop button (looks like a stop sign)
just under the menu.
That's your first excusion
into the world of loops. The loop you wrote should have executed very rapidly
and should have displayed a rapidly increasing number in the indicator on the
front panel. We can now add a timer to the loop to control how rapidly it
executes.
At this point you have a
loop that is timed, and since the input to the timer is 1000 (that's
milliseconds) it always runs at 1 second intervals. There are two things we
want to do to improve this program.
We will take care of those
two concerns next.
To set the time interval,
you need to add a numeric control on the front panel. Do that now.
NOTE: You need to use a control that permits floating point inputs if
you want to time the loop at intervals like 1.5 seconds. Add that
control now.
After adding the control,
right mouse click on the control and locate the terminal for the numeric
control you just added.
Then you need to wire the
control so that it controls the input to the timer block. Up above we
just used a constant of 1000 to get one second. If you input the time
interval in seconds, you will need to multiply the time interval that
the user inputs by a factor of 1000 (to get the timer interval in
milliseconds), so you will need a multiplier and a constant input to one
side of the multiplier equal to 1000.
Now, your block diagram should look
something like this.
Problems
P1
Modify the program above so that you can stop the program using an ON-OFF
control. Here's a hint. Try replacing the constant the is wired to the
conditional terminal with a control. Right-click the constant (set to TRUE in
the block diagram above) and change it to a control. Then find the control on
the front panel.
One final program - just to end this by
producing something that's getting close to useful. (The next lesson in this
series will take you to the world of IEEE-488 (GPIB) measurements, and when you
put them in a loop, then you have some interesting things that you can do.)
Anyhow, this last program will compute a sine wave and display it on the dial.
Here is the block diagram.
You should notice the following in this block
diagram.
In the previous vi we
computed the elapsed time and displayed it on the front panel. Here we take
that time and multiply it by a factor that is 2pf.
The 2pfactor is a constant built into LabVIEW. You can find it on the panel
of numeric operations, and you should check out all of the other
constants available.
The result of the
multiplications is the input to a sine function. You can find that function
on the numeric palette of functions in a sub-palette of trigonometric
functions. Check out what else is there as well.
Finally, the output of the
sine function is displayed on a Meter indicator. That indicator has a front
panel portion, and you should add it from the front panel.
Actually, the front panel
looks like this when you are done.
|