16
LabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering, Mississippi State University

LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

LabVIEW Case and Loop StructuresABE 4423/6423Dr. Filip ToAg and Bio Engineering, Mississippi State University

Page 2: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Recap Previous Homework – Following Instruction

Create a Pressure Conversion VI that takes a value in PSI from the user and converts it to five other pressure units (Inch H2O, Inch Hg, Bar, Pascal, and mmHg). Use Gage Indicators (it looks like a round pressure gage) for displaying the results and make each indicator shows its digital value as well. Make the numeric objects to have SGL (single precision number) representation, and make the format to show 2 decimal points. Also make the block diagram objects be in small icons. Name your object logically, you may display captions instead of labels on the front panel in order to be more descriptive. Align the objects in the front panel and in the block diagram and tidy up the wires in the block diagram and space them equally. Other cosmetics of the objects are up to you.

Put your name on the front panel, run your program so that your printout shows it. Follow the instructions in How To Print LabVIEW in PDF, include front panel and block diagram and all hidden frames if any, and submit the PDF printout in the designated Drop Box.

Page 3: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Purpose of Case and Loop Structures

A Case Structure is used to do conditional execution of code segments (sub-diagrams) based on a selection criteria. A CASE structure works just like IF THEN ELSE constructs in traditional (text based) programming.

A Loop structure is used to control the execution of program that requires repetition or iterations of a block of code. The number of repetitions is controlled by some conditions such as count or occurrence of a given condition.

Case and Loop structures are LabVIEW objects in Structures category, they are used to control flow of execution of a VI

Page 4: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

The Structure Category in Functions PalletStructure functions are Block Diagram objects in the Functions Pallet. They have no Front Panel Representation.

Page 5: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Example Scenario – Operating on Two Operands

The scenario: We have two numeric inputs: Operand1 and Operand2, and we have one numeric output: Answer. We have a Text Ring that contains the following items: “Add”, “Subtract”, ”Multiply”, “Divide”, “Sum of Square”, and Square of Sum”

We want to perform Answer = Operand1– Operand2 if Subtract is chosen in the Text Ring; We want to perform Answer = Operand1^2 + Operand2^2 if Sum of Square is chosen in the Text Ring, etc.

We have to have six subsets of code that we want to execute conditionally based on the selection in the Text Ring. We will use a case structure in the program and we have six cases (conditions) to do.

Page 6: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Case StructureA CASE structure is a multi-layer frames of containers with a sub-diagram in each frame. When there is no sub-diagram inside a frame it is a DO NOTHING case. Each layer of frame in the case structure is a CASE, it contains the code needed to execute when that case is selected (like the scenario presented previously, one case will contain the code to do Operand1-Operand2), and each of the other cases will have the code to do each of the other operations).

A CASE structure has a Case Selector and a Case ID for each CASE.

The Case Selector (the “?” icon at the frame border) is wired to data for selecting individual cases, and that data can be numeric, Boolean, or String. For the scenario presented previously the Case Selector should be connected to a numeric data (the output of the Text Ring) because we have six cases: 0, 1, 2, 3, 4, and 5. If the Case Selector is connected to a Boolean data then we have only two cases: TRUE, and FALSE.

Page 7: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Case StructureRegardless of the data type of a Case Selector, a case structure must have a DEFAULT case. Any case can be assigned to a default case by appending “,default” in the Case ID box. A case ID with only DEFAULT in it will execute when not of the other cases meets the condition specified by the Case Selector. A case structure can have only one DEFAULT case.

Recommendation: Implement the scenario presented previously on your own before test#1…

Page 8: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Case Structure Diagram

Case Structure

Frame

Case Selector. Case type:

Boolean (Green)

Case Identification

The code inside this frame is executed when the data of the Case

Selector is TRUE otherwise the FALSE case is executed.

Page 9: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Different Case Selector Data TypeBoolean Case; wire =

Green; two case: True/False

Numeric Case; Blue (Integer); case ID =

numeric; Can have many case as you like, one

Default Case

String Case; Pink; case ID = string in quotes; one default

case; number of cases unlimited

Case ID is Boolean

Page 10: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

In-class Example – Boolean CaseWrite a program with RED and Green LED (Boolean) indicators and two Boolean controls (switches) so that when the first switch is TRUE, the RED led is active (ON/True) and Green is inactive (OFF/False) when the second switch is FALSE (off), and GREEN led is active (ON/True) while RED is inactive when that switch is in True position. Both LEDs are off when the first switch is FALSE (the first switch serves as an enabler).

Why didn’t I put the LED indicators inside

the Case structure?

Page 11: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

In-class Example: Numeric CaseWrite a VI with an integer numeric control and a String indicator. Make the string indicator shows “NUMBER ONE” if the value in the Numeric control is 1, “NUMBER FIVE” if the value in the Numeric control is 5, and “DUNO THIS NUMBER” if the value is anything else.

How do I add, remove cases ?How do I make the numeric

input Blue or ORANGE?Is 0 needed in the default

case?

Page 12: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

In-class Example: String CaseWrite a VI that has one string control called “Letter” and three LEDs, red green and blue. If the data in Letter is “a” or “A” the green LED is on and the others are off; if “z” or “Z” the blue LED is on and others are off; if the letter is “m” or “M” the green and red LEDs are on and others are off; “x” or “X” all LEDs are off, and all other letters will cause all LEDs to turn on.

Page 13: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Common Knowledge about Case Structure

Can CASE structures be nested? Yes

Can CASE structures be in parallel? Yes

How deep of a nested CASE structure is allowed? No limit

How do you do a case that says “execute this case if a value is between 1 and 19”? 1…19 (one dot dot dot 19)

How do you do a case that says “execute this case if the value if greater than or equal to 6”? Find this in HELP

When a wire is connected to the frame of a case structure a square connector is created what’s this? Tunnel

Data sent into a case structure does not have to be wired in all the cases.

Data sent out from a case structure MUST be wired in all cases (all cases must be fully defined). Not fully defined cases is indicated by the Output Tunnels (the tunnels that send data out of the case structure) look like squares that are not filled (white spot in the middle)

Page 14: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Select Function

Select function is a block diagram object that is used to send data from one of two sources. Unlike Case structure it has only two input terminals and one Boolean selector Terminal. It only conveys data and it does not execute sub-diagram like a Case structure.

Example:

Page 15: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

HomeworkCreate a VI that performs computation in a two-parameter area calculator with pictures of shapes. It calculates the area of a shape using the two input parameters (Numeric controls) and the shape chosen by the user.The shapes are: triangle (base and height), Cone (radius and height), and rectangle (width and length). Use a Text Ring object as the data source to select the shape to be computed, use String indicators with the labels hidden and position them on top of each input parameters. The contents of these indicators are to be changed programmatically. For example: They display Base and Height if a triangle is selected, they display Radius and Height if cone is selected, etc.) The two input parameters are numeric controls with their labels made hidden (because you are using the string indicators to overlay on top of them). In addition to the two numeric controls and two string indicators, your VI should also have one numeric indicator for displaying the computation result (the area), and a 2D Picture object (Graph -> Controls -> 2D Picture) for showing the shape. Your VI displays the picture of the shape being computed in addition to the result of computation. Make the size of the picture is 200 x 200 pixels.

So, the user select a shape from the RING, click RUN, the area is computed, the input parameter texts show the appropriate strings and the picture of that shape is shown.

You must read the context help and the detailed help of the 2D picture object in order to figure out how to draw shapes programmatically if you have never done it yet. Feel free to use your internet resources for this.

Page 16: LabVIEW Case and Loop Structuressdt13.abe.msstate.edu/classes/abe44232017/notes/LabVIEWCaseStructure.pdfLabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering,

Hint: Drawing Shape ProgrammaticallyFront Panel: Graph èControls è2D Picture, adjust the properties so that you know its size (example: make it 200 x 200 pixels)

Draw a Circle in the 2D Picture Box: Block Diagram: Graphics and Sound èPicture Functions è Draw Circle By Radius. Wire the New Picture to the 2D Picture object. Create a constant for Picture Center and make it 100,100 (rihgt-click on the Pixel Center terminal and choose CREATE CONSTANT); Create a constant for Radius and make it 50, run it and see what it looks like.

You may have to draw multiple objects (like cone).