CS320n –Visual Programming More LabVIEW Control Structures.

19
CS320n –Visual Programming More LabVIEW Control Structures

Transcript of CS320n –Visual Programming More LabVIEW Control Structures.

Page 1: CS320n –Visual Programming More LabVIEW Control Structures.

CS320n –Visual Programming

More LabVIEW Control Structures

Page 2: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 2

What We Will Do Today• Learn about other LabVIEW control

structures

Page 3: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 3

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

Page 4: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 4

Case Structure

Page 5: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 5

Case StructureCurrent Case Sub Diagram. Can only view one case at a time.

Selector input.Will change based on datatype of input:boolean, numericString

Page 6: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 6

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

Page 7: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 7

Block Diagram Choose sub diagram by clicking on increment / decrement arrowsor clicking on the down arrow and selecting from the menu.

Page 8: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 8

Use Sub VIs• Add the C to F and F to C

sub VIs to the block diagram

Page 9: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 9

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

Page 10: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 10

Completed Block Diagram

Page 11: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 11

Completed Front Panel

Page 12: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 12

One More Thing…• Any way to make instrument more user

friendly than input label of “Temp In” and output label of “Converted Temp”?

Page 13: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 13

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

Page 14: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 14

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

Page 15: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 15

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

Page 16: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 16

Sequence Structure

Flat Sequence structure with 2 moreframes 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 functionallyequivalent.

Page 17: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 17

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.

Page 18: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 18

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– 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)

1,1

x

y

1,0

0,1

. inside

. outside

Page 19: CS320n –Visual Programming More LabVIEW Control Structures.

Visual Programming LabVIEW Control Structures 19

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