Lecture Notes

Download Report

Transcript Lecture Notes

CS320n –Visual Programming
More LabVIEW Control Structures
What We Will Do Today
• Learn about other LabVIEW control
structures
Visual Programming
LabVIEW Control Structures
2
Case Structures
•
•
•
•
conditional execution
like a if – else in Alice or other languages
2 or more sub diagrams
the diagram that executes is based on the
input of a selector
• Selector can be boolean, numeric, or a
string
Visual Programming
LabVIEW Control Structures
3
Case Structure
Visual Programming
LabVIEW Control Structures
4
Case Structure
Current Case Sub Diagram. Can only view one case at a time.
Selector input.
Will change
based on data
type of input:
boolean, numeric
String
Visual Programming
LabVIEW Control Structures
5
Case Structure Example
• Create an instrument
that can convert
degrees F to degrees
C or degrees C to
degrees F
• Must choose which
conversion to make
• use a switch on front
panel to select which
conversion to perform
Visual Programming
LabVIEW Control Structures
6
Block Diagram
Choose sub diagram by clicking on increment / decrement arrows
or clicking on the down arrow and selecting from the menu.
Visual Programming
LabVIEW Control Structures
7
Use Sub VIs
• Add the C to F and F to C
sub VIs to the block
diagram
Visual Programming
LabVIEW Control Structures
8
Partial Block Diagram
Problems with tunnels. If data comes out from one case outputs a value, all
cases must have an output for that value. Not the same for input
Visual Programming
LabVIEW Control Structures
9
Completed Block Diagram
Visual Programming
LabVIEW Control Structures
10
Completed Front Panel
Visual Programming
LabVIEW Control Structures
11
One More Thing…
• Any way to make instrument more user
friendly than input label of “Temp In” and
output label of “Converted Temp”?
Visual Programming
LabVIEW Control Structures
12
Different Selectors
• In example selector was boolean
– only two cases, true or false
• selector can be numeric or string
• control for numeric selector can be any
numeric type but will get converted to int
• for string and numeric selector many
possible cases
• right click on selector to add cases
Visual Programming
LabVIEW Control Structures
13
Select Function
• simple alternative to if-else
type case structure
• a boolean input
• if boolean is true pass on
the value wired to t
• if boolean is false pass on
the value wired to f
• can handle other data
types
Visual Programming
LabVIEW Control Structures
14
Sequence Structure
• In LabVIEW, execution is driven by data
flow
• when the input data is ready, functions
execute
• sometimes no way of telling which order
things will execute
• in most programming languages, flow of
control is not driven by data, but is
sequential based on order of statements in
program
Visual Programming
LabVIEW Control Structures
15
Sequence Structure
Flat Sequence structure with 2 more
frames added.
Frames execute in order.
Alternative to Flat Sequence Structure is a
Stacked Sequence Structure.
Visually different in that only one frame visible at a time,
but functionally
equivalent.
Visual Programming
LabVIEW Control Structures
16
Class work
• This exercise makes use of loops, but
does not require case structures.
• Monte Carlo method for determining Pi
• pi is the ratio of the circumference of a
circle to its diameter, no matter which
circle you use to compute it.
Visual Programming
LabVIEW Control Structures
17
Monte Carlo method
• imagine a quarter of a
unit circle.
(radius = 1)
• pick points at random
– random value of x and y,
between 0 and 1
• use distance formula to
determine if point is
inside or outside circle
1,1
0,1
. outside
. inside
y
– square root of (x^2 + y^2)
• tally up points inside
circle
• pick lot of points
• estimate of pi = 4 *
(points inside / total
number of points)
Visual Programming
LabVIEW Control Structures
x
1,0
18
Class work
• write a LabVIEW program that picks
random points to estimate pi.
• Allow control for number of random points
to pick
• display resulting estimate of pi
Visual Programming
LabVIEW Control Structures
19