You are on page 1of 27

MAE 170 LabVIEW

Assignments

Fall Quarter 2010

UC San Diego
Department of MAE
Compiled By Kevin Mandich
Introduction

This document is meant to accompany the LabVIEW portion of the MAE 170 class at
UC San Diego. It includes all of the assignments which will be completed during the
quarter. In addition, it will provide some hints to make it easier to complete the
assignments.

This is not intended to be a complete manual for LabVIEW. If you don't come to
class, you may have a very hard time understanding what is expected, and you will
probably spend many frustrating hours at the computer trying to figure out how to make
the programs work. In contrast, if you do attend lectures, you can gain tips and hints
that will help to make the learning process a bit smoother.
Assignment 1
Calculator

Your first assignment is to create a program which takes two user-inputted


numbers, analyzes them, and provides an output upon execution of the program.
The program will add, subtract, multiply and divide the two numbers; it will take
the sine value of the first number; and it will display whether the first number is
greater than, equal to or less than the second number.

The Front Panel and Block Diagrams of an example of a successful program are
shown below.
Helpful Concepts:

Context Help

This is a feature of LabVIEW that explains how LabVIEW functions operate. When you
mouse over a function, the help for that function is displayed in the context help box.
You can get context help by pressing Ctrl + H on the keyboard, or by clicking on the
help menu, and selecting “Show Context Help”
Here the help for the Add function is displayed. The values for x and y are wired to the
function, and the sum is wired out. By convention, inputs are on the left side of
functions, and outputs are on the right.

Front Panel / Block Diagram

All LabVIEW programs have a front panel and a block diagram (also called a back
panel). When you create a new program, both are created automatically, and when you
save your program, both are saved. You can switch from one to the other by pressing
Ctrl + E.

The front panel provides an interface which allows the user to interact with the program.
Controls, such as knobs, slide bars and menu boxes allow the user to supply
information

to the program. Indicators, including graphs and dials, supply information to the user
about the state of the program.
The block diagram is where all of the programming takes place. Here, functions are
connected together by wires, in a manner similar to a circuit diagram. Whenever a
control or indicator is placed on the front panel, an corresponding icon is automatically
created on the block diagram. In the case of an indicator, data is wired to the icon on
the block diagram and displayed on the front panel. For controls, data is wired from the
control icon, and used elsewhere in the program.

Controls, Indicators and Constants

Data can be supplied to LabVIEW in two forms: Controls and constants. A control is
something that can be changed while the program is running, and it has some type of
icon on the front panel which the user manipulates to provide the input. A constant is
provided during programming, but cannot be changed at run time. LabVIEW returns
data to the user as indicators. These can be altered by the program while it runs, but
cannot be changed by the user.

Controls

Constants
Indicators

Automatically Creating Inputs and Outputs

There are two ways to find the constants, controls and indicators that you need for your
functions: the easy way and the hard way. The hard way is to search through all of the
menus until you get what you are looking for. This can take a long time, and has been
known to cause pulling of hair and gnashing of teeth.

The easy way to get the input or output that you are looking for is to have LabVIEW
create
it for you. This is a sure way to speed up the programming process, and save yourself a
lot of frustration.

To have LabVIEW create an input or output, first put a function on the block diagram,
then right click on the terminal you want to connect to, point to create, and choose
constant, control or indicator.
Remember, inputs can only be constants or controls, and outputs can only be
indicators.

Run Button

The small arrow in the upper left corner of the window is the run button. When this
button is white, it means that there are no errors in your vi, and it is ready to run. If you
click on the arrow when it is white, it will run your program.

When the arrow is gray with and appears broken, it means that your program has errors
in it. Rather than searching for the errors yourself, it is often easier to have LabVIEW
find them for you. To do this, click on the broken run arrow. Labview will display a list of
all the errors in your program. LabVIEW will even find the location of each error: double
click an item on the list, and LabVIEW will highlight its location in your code.

Run Arrow – No Errors

Run Arrow – Broken, with errors

Show a list of errors by clicking on the broken run arrow


Assignment 2
Analog to Digital Conversion

This assignment demonstrates the effect of discretizing an analog signal at


different resolutions. Your program will create half of a sine wave by calculating
180 discrete points and “connecting the dots”, as seen in the graph below. It is
left to the student to decipher why changing the number of bits changes the
resolution of the shape.

Only the graphical output of the VI is shown on the Front Panel. Your Front Panel
must also contain a Vertical Fill Slide to control the number of bits, as well as a
Numeric Indicator which displays the quantity 2^n, where n is the number of bits
given by the Vertical Fill Slide.
Helpful Concepts:

For Loops

A for loop in LabVIEW is similar to for loops in other languages, in that is repeats a
certain set of commands a given number of times. In LabVIEW, a for loop is actually
represented by a square “loop”: any instructions inside of the loop are repeated.

Visible Items

All controls have certain items which can be made visible or invisible, including numeric
indicators.

Waveform Charts

Display data that is wired into them in the form of vectors. A set number of points are
stored, and data is not overwritten until that set number of points has been sent.
The X and Y scales can be set to auto scale, or can be set to fixed lengths. To auto
scale, right click on either axis, and select “Auto scale X” or “Auto scale Y”. To adjust
the scales, double click on the lowest or highest value displayed, and type in the desired
value.
Meaning of Wire Colors and Sizes

The sizes and colors of the wires in LabVIEW are significant, and correspond to the
type of data contained within the wire. The size of the wire relates to whether the data is
a scalar, 1D vector or 2D vector. The color of the wire describes the type of variable
contained in the wire. For instance, a variable is contained in a pink wire, an integer is
contained in a blue wire, and a decimal number is contained in an orange wire.
In order to choose the type of number you wish for a numeric control to produce, right
click on the control and select Representation. The three values on the top line are Ext.,
Dbl, and SGL, and they stand for Extended precision, Double precision and Single
precision decimal numbers. They range from the most to least values of stored
accuracy.

Since these are all decimal numbers, any of these will result in an orange wire. The next
line down have I64 through I8, referring to 64 bit through 8 bit signed integer numbers.
These numbers can take on positive and negative integer valued. Since they are
integers, they will be represented by blue wires.

The third line down displays U64 through U8, meaning 64 bit through 8 bit unsigned
integers. These numbers can take on only positive integer values (and the 0 value).
Since these numbers are integers, they will also be represented by blue wires.
Assignment 3
Data Analysis using a Sub-VI

It is often very useful, when writing programs, to create functions which may be
called by other routines. This helps to prevent redundancies, and can make large
programs more organized and understandable. The LabVIEW analogy is called a
Sub-VI. In general, a Sub-VI is very similar to some of the functions we have
already used (i.e. sine, addition) in that the Sub-VI requires inputs, processes
them, and then returns the results as outputs.

In this assignment you are to create a program which reads two inputs, Number
of Samples and Amplitude. You will then create a Sub-VI which reads these two
inputs, analyzes them, and creates an output consisting of a 2-Dimensional array
of the sine and cosine waves created from the input information. Your main
program will read this output, unbundle the 2-D array into two 1-D arrays, read the
RMS values of each wave, and display the superimposed images of the sine and
cosine waves onto a Waveform Graph on your front panel.

The Front Panel and Block Diagram of the main program are shown below:
The Sub-VI is shown as the white box with the words “n/s”, “Wav”, and “Amp”
inside. As you can see, there are two inputs and one output.

The Sub-VI will be constructed using the following pseudo-code:

for i=1:n (n = number of samples)


sine(i)=amplitude*sin(i/10)
cosine(i)=amplitude*cos(i/10)
end for loop
2D_array=(sine,cosine) (combing the two vectors into one unit)

HINT: You can combine the 1-D arrays “sine” and “cosine” into a single 2-D array
using the Build Array function in LabVIEW.
Helpful Concepts

Useful Functions

• RMS:
Mathematics –Probability and Statistics – RMS
• Index Array
Array – Index Array

Creating a Sub-VI

• Make a vi from scratch as usual

• Once you have your inputs and outputs created go to the front panel, and right click
on the icon in the top right hand corner of the screen
o Click on Show Connectors
o If there are not enough inputs and outputs, right click, then point to
“patterns”, then choose the one you want

• Once you choose a pattern, connect each terminal to an input or output by first
clicking on the terminal, then clicking on the input or output.

• Next go to the block diagram, right click on the icon in the upper right hand corner,
and select “Edit Icon”. You should change the icon so that it will remind you how to
connect wires later on.

• Save the vi on your computer



• Once you have created a sub-vi, you can place it in another vi from the back panel
using the functions palette, by clicking on All Functions – Select a vi. Simply browse
for the vi, then place it as you would any other function.

Build Array

LabVIEW, like many other programming languages, can handle arrays of data. Almost
any type of data can be made into an array, including numbers, strings and Booleans.
Arrays can include any number of dimensions. Since LabVIEW doesn't have a separate
data type for vectors, a one dimensional array is used. Once data is in an array, many
operations can be performed on that array, including sorting, searching and
transposing.

Arrays can be constructed using a function called “Build Array”. This function can be
found on the array palette, called “Build Array”.
Initially, the build array By dragging the bottom handle, it is
function has only one input possible to add as many inputs as necessary

It is possible to use either scalar values or arrays as inputs to the build array function. If
scalar inputs are used, the output is a 1D array. If several 1D arrays are put into a
“Bundle Array” function, the result will be a 2D array.

Scalar inputs create a 1D array 1D array inputs create a 2D array

It is also possible to append data onto an existing array. For instance, it is possible to
add a number of scalars to the end of a 1D array, or to stack two 1D arrays one after
the other, to create a longer 1D array. To do this, right click on the “Build Array”
function, and choose “Concatenate Arrays”

Choose “Concatenate Arrays” With “Concatenate Inputs” enabled,


to append data to an the dimension of the output is the
existing array same as the greatest input dimension
Assignment 4
Event Structures & While Loops
Event structures perform a pre-determined series of operations once a
specific event is triggered (i.e. clicking the mouse or pressing ENTER). You
can think of them as for loops than only perform one iteration when an event
is triggered.

While loops are likewise similar to for loops. However, instead of a pre-
determined number of iterations as defined in a for loop (the parameter N),
While loops run continuously until a specified Boolean condition is met (i.e. a
STOP button is pushed).

Your program this week will read an input from a Vertical Slide Fill and display
the results on a Waveform Chart, as shown below. Note that the Chart will
only update when the value of the Slide Fill is changed. This is the triggering
event.
The pseudo-code for the block diagram is shown below:

while stop button is not pushed


if vertical slide is changed
chart value = vertical slide fill value
plot the chart_value
else if vertical_slide is not changed
wait 10 ms (timeout event)
end event
end while loop

HINT: To use the timeout feature (wait 10 ms in the code), simply wire a
constant number, read in milliseconds. into the small blue hourglass at the top
left corner of the event case. This sets the time before which the loop can
enter another iteration.

Once the time is up, and if an event has not happened, it will trigger the
timeout case which is, in our case, blank. Thus, if you do not move the slide
rule, nothing will happen.

Helpful Concepts

Creating an Event

When you create an event loop, the only event you start with is the timeout event.
To add an event, right-click on the text box at the top of the loop and choose “Add
Event Case.” In this Edit Events window you will first choose an Event Source and
then an Event. Note that you can also change any other events you have added to
the loop with in this window.

To scroll between different events in your loop, either press one of the horizontal
arrows at the top of the loop or left-click the text box at the top, and choose which
event you want to view.

Setting up a While Loop

When you create a while loop, you can see a tiny red circle in a green box in the
lower left corner. This is the Boolean condition for the loop. The default value is
“Stop if True.” You can right-click this to change to the opposite, “Continue if True,”
but we will use the default.

If you connect a Boolean control to this (i.e. a STOP button on the front panel) you
can then stop the loop from running at any time during the program’s operation by
pressing this button.
Assignment 5
Shift Registers, Clusters, Build Array, and X-Y Graphs
The purpose of this assignment is to create a graph which will be updated
when either of two knobs are rotated. One knob will provide the X position of
a cursor, and the other, the Y position. The cursor should leave behind it a
line showing all previous locations of the cursor, which will be present for the
entire time that the VI is running.

One possible Front Panel looks like this bootleg Etch-a-Sketch (please note
you don’t have to customize your VI at all; the default X-Y graph will suffice):
Pseudo-Code:

while stop button is not pushed


old_x_vector=new_x_vector (shift register)
old_y_vector=new_y_vector (shift register)
if x knob value is changed
new_x_vector=(old_x_vector,current_x_knob_value)
new_y_vector=(old_y_vector,current_y_knob_value)
else if y knob value is changed
new_x_vector=(old_x_vector,current_x_knob_value)
new_y_vector=(old_y_vector,current_y_knob_value)
else if nothing changes
new_x_vector=old_x_vector
new_y_vector=old_y_vector
end if
plot new x vector vx. Y vector on an X-Y graph
store new_x_vector for next iteration (shift register)
store new_y_vector for next iteration (shift register)
end while

Note: the notation (A,B) signifies that the variable B should be concatenated
with the variable A, to create a vector. Here, the value B is added to the vector
A, which contains all of the previous values.

Helpful Concepts:

Shift Registers

Shift registers provide a way to store a value during one iteration of a loop, and retrieve
the value during the next iteration of the loop. Shift registers can be used with for loops
as well as while loops. Shift registers can store almost all data types, including strings,
numbers and Boolean. They can also store arrays of these data types.

The easiest way to create a shift register is to create a for loop or a while loop, then
right click on the side of the loop, and choose “Add Shift Register”. Wiring a variable or
constant into the left terminal, from outside the loop, provides an initial value. \

Clusters

Clusters are a way of combining several pieces of data into one package. It is possible
to have different types of data in one cluster, or a cluster can contain multiple instances
of the same data type. The XY graph, described below, only has one input terminal, and
requires a cluster of two arrays: an array of x coordinates and an array of y coordinates.
The easiest way to build a cluster is by using the bundle function. This function can be
found on the functions palette, under “Cluster, Class and Variant.” The bundle function
only has two terminals initially, but you can add more terminals by dragging the bottom
of the icon, as shown:

The bundle function only It is possible to add more terminals


has two terminals initially by dragging the bottom of the icon

XY Graph

An XY graph is just what it sounds like, a figure which displays a number of points that
are supplied as X,Y pairs. The XY graph can be found on the front panel, on the
controls palette, under the graph menu. The input format for an XY graph is an array of
X coordinates bundled together with an array of Y coordinates, to make a cluster of two
arrays.

An array of X coordinates is bundled with an array of Y


coordinates, and the cluster of two arrays is plotted on the
XY Graph.
Assignment 6

Tunnels & Indexing

For this assignment you will be creating what is known as a Lissajous Figure.
Pseudo-Code:

for i = 0 to 3
f_ratio = 4-i
for j = 0 to 89
phase = 4*j
k = 0
angle_1 = 0
while angle1 < 720
angle2 = f_ratio*angle_1 + phase
sinewave1 = cos(angle_1*pi/180)
sinewave2 = cos(angle_2*pi/180)
chart sine waves
angle_1 = 2*k
k = k + 1
end
graph sinewave1 vs sinewave2
wait 100 milliseconds
end
end

Helpful Concepts:

Tunnels and Indexing:

Any time that a signal is wired into or out of a structure (i.e. a for loop, while loop, case
structure, etc.), a small square, called a tunnel, is created. When using for loops and
while loops, it is possible to choose either a vector or scalar output by altering the output
tunnel.

If a signal is wired out of a for loop or while loop, the value for that signal will be
calculated each time that the loop iterates. Thus it is possible to have many values
calculated for a single output. It is up to the programmer to decide whether to use all
calculated values, or to use only the last value calculated. This selection is made by
choosing either an indexed or non-indexed output tunnel. An indexed tunnel provides all
calculated values, while an non-indexed tunnel gives only the last calculated value.
Non-indexed Rich-click, choose “Enable Indexing” Indexed Tunnel
Tunnel
Assignment 7
Local Variables and Sequence Structures
In this program you will pre-assign a username and password. If, upon execution
of the program, the username and password typed into the text input prompts are
correct, a message will pop up saying “Message Granted.” Otherwise, the user
will receive an “Access Denied, Try Again” message.
Pseudo Code:

username = ______
password = ______
while stopsign = false
if OK button value changes
if Username == username and Password == password
Popup Dialog: Access Granted
stopsign = True
else if OK button values changes
popup dialogue: Access Denied, Try Again
stopsign = false
end if
else if CANCEL button value changes
stopsign = True
else
stopsign = False (the Timeout event)
end if
end while
Helpful Concepts:
Sequence Structure

A sequence structure carries out a sequence of events. There are two types of
sequence structures: flat and stacked. In a flat structure, as shown in the block
diagram, there are multiple frames which execute in a left-to-right sequential order.
Code within the first frame will execute completely. After it is finished, the program
moves onto the next frame, and so on.

A stacked structure works in the same way, except you only see one frame at a time,
much like the event structure. There is a text box at the top of the loop that will tell you
which frame you are on.

To add or delete a frame in either structure, simply right-click the border of the loop and
choose the corresponding option. You can add a sequence either before or after the
frame you are currently on.

Local Variables

A local variable allows you to read from or write to a variable, whether it is a control or
an indicator. Essentially, with a local variable, you can access a Front Panel object as
both an input and an output.

To create a local variable, simply right-click a string control in the Front Panel, choose
CREATELocal Variable, and place the local variable box onto your Block Diagram by
left-clicking where you want it to be placed.

You might also like