52
Workshop 2 of 4 Welcome!

Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Embed Size (px)

Citation preview

Page 1: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Workshop 2 of 4

Welcome!

Page 2: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Survey While You Wait

ni.com/duke >>

Recent Documents >>

LabVIEW Workshop Participation Survey

Page 3: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Today's Topics

Review of Workshop 1

More Structures Sequence, Disable, Conditional, Case

Enumerations State Machine – Activity: build a state machine Timing Event Structure

Page 4: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 1

Which of the following are the three parts of a VI?

a) Front Panel

b) Project

c) Block Diagram

d) Connector Pane/Icon

Page 5: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 1

Which of the following are the three parts of a VI?

a) Front Panel

b) Project

c) Block Diagram

d) Connector Pane/Icon

Page 6: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Use LabVIEW Projects to:• Group LabVIEW files and non-LabVIEW files

• Create build specifications (i.e. stand-alone applications)

• Deploy or download files to targets (i.e. FPGA target)

6

Project Explorer Right Click!

Page 7: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Answer: a LabVIEW program

1. Front PanelUser interface (UI)

– Controls = inputs– Indicators = outputs

2. Block DiagramGraphical source code

– Data travels on wires from control terminals through functions to indicator terminals

– Blocks execute by data flow

3. Icon/Connector Pane• Graphical representation of a VI• Means of connecting VIs (subVIs)

What is a Virtual Instrument (VI)?

* Conn. pane available from FP only

Page 8: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 2

1. Which identifies the control or indicator on the block diagram?

a) Caption

b) Location

c) Label

d) Value

Page 9: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 2

1. Which identifies the control or indicator on the block diagram?

a) Caption

b) Location

c) Label

d) Value

Page 10: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 3

Which of the following are not a nodes?

a) Terminals

b) Structures (while loop, for loop)

c) subVIs

d) Wires

e) Functions

Page 11: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Review Question 3

Which of the following are not a nodes?

a) Terminals

b) Structures (while loop, for loop, etc.)

c) subVIs

d) Wires

e) Functions

Page 12: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Block Diagram Nodes

• Objects on the block diagram that have inputs and/or outputs and perform operations when a VI runs

• Analogous to statements, operators, functions, and subroutines in text-based programming languages

Functions • Fundamental operating

elements of LabVIEW

• Do not have front panels or block diagrams, but do have connector panes

• Double-clicking a function only selects the function – does not open it like a VI

• Has a pale yellow background on its icon

subVIs

• VIs that you build to use inside of another VI

• Any VI has potential to become a subVI

• Double-clicking a subVI will open it (exception: Express VIs – config. window opens)

• Icon represents subVI in main VI

Structures

• While loops, for loops, event structures

• More discussion later

Page 13: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Which function executes first: Add or Subtract?

a) Add

b) Subtract

c) Unknown

13

Review Question 4

Page 14: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Which function executes first: Add or Subtract?

a) Add

b) Subtract

c) Unknown

14

Review Question 4

Page 15: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Data Flow Block diagram execution is

dependent on the flow of data

Block diagram does NOT execute left to right

Node executes when data is available to ALL input terminals

Nodes supply data to all output terminals when done

If the computer running this code had multiple processors, these two pieces of code could run independently without additional coding

Page 16: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

True or False?

1. Arrays can contain only one data type.

2. An array must contain at least one element.

3. The Bundle function can be used to add elements to a cluster.

4. Clusters can contain both controls and indicators within the same shell; however, arrays cannot.

5. It is possible to have clusters containing arrays as well as arrays containing clusters.

16

Review Question 5

Page 17: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

True or False?

1. Arrays can contain only one data type. TRUE

2. An array must contain at least one element. FALSE

3. The Bundle function can be used to add elements to a cluster. FALSE

4. Clusters can contain both controls and indicators within the same shell; however, arrays cannot. FALSE

5. It is possible to have clusters containing arrays as well as arrays containing clusters. TRUE

17

Review Question 5

Page 18: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

• Clusters differ from arrays in that they are a fixed size

• Clusters can contain mixed data types; arrays contain only one data type

• Like an array, a cluster is either a control or an indicator and cannot contain a mixture of controls and indicators

18

Clusters vs. Arrays

Page 19: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Match the icon with its name and description.

1. Iteration terminal – tells how many time the loop has executed.

2. Count terminal – receives value to tell how of times a for loop will execute

3. Conditional terminal – tells the loop to stop if the condition is met

19

Review Question 6

Page 20: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Match the icon with its name and description.

1. Iteration terminal – tells how many time the loop has executed.

2. Count terminal – receives value to tell how of times a for loop will execute

3. Conditional terminal – tells the loop to stop if the condition is met

20

Review Question 6

Page 21: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Loops

While Loop Terminal counts iterations Always runs at least once Runs until stop condition is met

For Loop Terminal counts iterations Runs according to input N of

count terminal

While Loop

For Loop

i = zero indexed N = one indexed

Page 22: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

What will be the value in output once this VI finishes?

a) 11

b) 9

c) 3

d) 4

22

Review Question 7

Page 23: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

What will be the value in output once this VI finishes?

a) 11

b) 9

c) 3

d) 4

23

Review Question 7

Page 24: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Shift Registers - Access Previous Loop Data

• Available at left or right border of loop structures• Right-click the border and select Add Shift Register • Right terminal stores data on completion of iteration• Left terminal provides stored data at beginning of next iteration

- stacked: registers remember values from multiple previous iterations and carry those values to the next iterations (right-click -> Add Element)

Before LoopBegins

First Iteration

SecondIteration

LastIteration

Value 15InitialValue

Page 25: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

New Stuff

Page 26: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Sequence Structures Flat Sequence Structure

Executes frames from left to right and when all data values wired to a frame are available

The input of one frame can depend on the output of another frame

Stacked Sequence Structure

Stacks each frame so you see only one frame at a time and executes frame 0, then frame 1, and so on until the last frame executes

Returns data only after the last frame executes

Page 27: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Diagram Disable & Conditional Structures

Use the Diagram Disable structure to disable specific code on the block diagram Multiple subdiagrams possible

– only one enabled

Use the Conditional Disable structure to define conditions that indicate which code on the block diagram executes

Page 28: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Case Structure

• Have two or more subdiagrams or cases

• Execute and displays only one case at a time

• An input value determines which subdiagram to execute

• Similar to case statements or if...then...else statements in text-based programming languages

Case Selector Label

Selector Terminal

Page 29: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

•You can create multiple input and output tunnels

• Inputs are available to all cases if needed

• You must define each output tunnel for each case

Case Structure: Inputs & Outputs

Data Type Default Value

Numeric 0

Boolean FALSE

String Empty

Page 30: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Case Structure

Page 31: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

If the error cluster is wired to the selector, the border of the case structure automatically turns red or green for the error and non-error cases, respectively

Case Structure

Holy automatic coloring, Batman!

Page 32: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Enumeration

An enum represents a pair of values, a string and a numeric, where the enum can be one of a defined list of values

Page 33: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Custom Controls & Type Definitions

Control• No connection

between the one you saved and the instance in the VI

• Update the file, but the instances are not updated

Type Def• Connection between the

saved file and all instances

• Forces the data type of each instance to be identical (clusters, enum)

• Changes made to file will populate throughout each instance

Strict Type Def• Connection between

saved file and all instances

• Forces everything about an instance to be identical to the strict type definition, except:

• label • description• default value

- Use custom controls and indicators to extend the available set of front panel objects

Page 34: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Custom Controls & Type DefinitionsHow to create:

- Right click on the control; select Advanced >> Customize

- Select Type Def or Strict Type Def in the menu before saving

Page 35: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Let’s put it all together…

Page 36: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Scenario

Want to create a program that does the following:

1. Reads a voltage

2. Tells user to turn on power supply

3. Read the voltage again now that power is on

4. Tell the user to turn off the power.

Page 37: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Programming Designs: Sequential

Page 38: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Sequential Programming

Page 39: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Sequential Programming

Page 40: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Sequential Programming

Page 41: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Sequential Programming

• What if you need to change the order of the sequence?

• What if you need to repeat one item in the sequence more often than the other items?

• What if some items in the sequence execute only when certain conditions are met?

• What if you need to stop the program immediately, rather than waiting until the end of the sequence?

Page 42: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

• A state machine consists of a set of states and a transition function that maps to the next state

• Each state can lead to one or multiple states or end the process flow

While Loop

Case StructureShift Register

State Machine

Enum

Page 43: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

• State Functionality Code:

• this state was entered to perform what function?

• Transition Code:

• which state is next?

State Machine: Transition Code

Page 44: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Activity 1: Creating a state machine

1. Start a project

2. Create a new VI

3. Create a cluster on the front panel with 3 buttons:

- “Do This”

- “Do That”

- “Stop”

4. Create a state machine

• Make a type def. enum for the selector input

- Initialize

- Idle

- Do this

- Do that

- Stop

5. Run the VI

Functions for each state:- Initialize: turn on an LED that is

labeled “Running?”

- Idle: read button values, choose state based on button values

- Do this: send dialog to user that “This is Done”

- Do that: send dialog to user that “That is Done”

- Stop: end VI execution and turn “Running?” LED off

Page 45: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Timing Functions

• Control the frequency at which a loop executes

• Provide the processor with time to complete other tasks, such as processing the user interface

• Uses the operating system millisecond clock

10 ms 20 ms 30 ms

Page 46: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

VI Timing Types• Execution Timing

• Provides the design pattern with a function that specifically allows the processor time to complete other tasks

• In some cases, a Wait function is not necessary

• Use of Timeout inputs can provide execution timing

• Software Control Timing

• Timing (pauses, waits, time checks) you put in place to make the code execute after a certain amount of time.

• Example:• If you must acquire data for 5 minutes, you could remain in the acquisition state until the

5 minutes elapses. However, during that time you cannot process any user interface actions such as stopping the VI. To process user interface actions, you must implement timing (checking if 5 min has passed since the start of acquisition) so that the VI continually executes for the specified time

Page 47: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

47

Software Control Timing

Execution TimingVI Timing

Page 48: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Events and Event StructureEvent — An asynchronous notification that

something has occurred

48

Timeout Event Selector Label

Event Data Node Event Filter Node

• Event Data Node Identifies the data LabVIEW provides when the event occurs; similar to the Unbundle By Name function

• Event Filter Node Identifies the subset of data available in the Event Data node that the event case can modify

Page 49: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Notify EventsUser action has already occurredLabVIEW has processed the eventAvailable only in the Event Data node

Filter EventsUser action has already occurredLabVIEW has NOT processed the eventAllows you to override default behavior for eventAvailable in the Event Filter node and Event Data node

Notify and Filter Events

Page 50: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

EventsEvent Sources

Configured Events

Right-click the border and select Edit Events Handled by This Case

• Normally used in a While Loop

• Event structure handles exactly one event per iteration

• Sleeps when no events occur

Using and Configuring Event Structures

Page 51: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Notify Events (green arrow)User action has already occurred

Filter Events (red arrow)User performed action but event is not processed. Allows you to customize event handling.

Notify and Filter Events in Config. Window

Page 52: Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey

Better State Machine

Update your state machine to include:

- An event structure in the Idle state to monitor button presses instead of polling the value

- Add execution timing in the while loop so the processor can be free to update the front panel

- Add software timing in the Do This state so that it stays in the Do This state for 5 seconds. - Bonus: display the 5 second wait on the front panel