LabVIEW User Group
University of Bristol
Ben Lavasani
Academic Field Sales Engineer
NI UK
2
Agenda
• LabVIEW Design Patterns Overview
• Coffee break :)
• LabVIEW for Multi-Touch Applications - David Carberry
• LabVIEW in Teaching
• LabVIEW Tips and Tricks
3
LabVIEW Design Patterns Overview
What Is a Design Pattern?
• A template or framework for LabVIEW code
• Widely accepted and well-known
• Easily recognizable
5
Benefits of Using Design Patterns
Simplify the development process
Developers can easily understand code
Don’t have to “re-invent the wheel”
Pre-existing solutions to common problems
Reliability
Many have been used for years - they are “tried and true”
Large development community and resources online
6
Getting Started: How Do I Pick?
• Identify most important aspect of your application:
Processes that require de-coupling
Clean, easy to read code
Mission critical components
• Select a template based upon potential to improve
7
Caution
You can needlessly complicate your
life if you use an unnecessarily
complex design pattern
Don’t forget the most common
design pattern of all… dataflow!
8
Basic Tools
• Loops
• Shift Registers
• Case Structures
• Enumerated Constants
• Event Structures
9
Today’s Discussion
• As we look at each design pattern, we’ll discuss
A problem we are trying to solve
Background
How it works
Technical implementation
Demonstration
Use cases / considerations
10
Design Patterns
• Functional Global Variable
• State Machine / Statecharts
• Producer / Consumer
11
Functional Global Variables
How do I share data across a application
without using Global or Local Variables?
Background: Global and Local Variables
• Can cause race conditions
• Create copies of data in memory
• Cannot perform actions on data
• Cannot handle error wires
13
Breaking Down the Design Pattern
• While loop
• Uninitialized shift
registers have memory
• Case structure
• Enumerated control
14
Uninitialized Shift Registers
DEMO
15
How It Works: Basic Actions
• Set the value of the shift register
INITIALIZE
INITIALIZE
16
How It Works: Basic Actions
• Get the value currently stored in the shift register
GET
GET
17
How It Works: Action Engine
• Perform an operation upon stored value and
save result
• You can also output the new value
ACTION
ACTION
18
Technical Implementation
1. Functional Global Variable is a Non-Reentrant SubVI
2. Actions can be performed upon data
3. Enumerator selects action
4. Stores result in uninitialized shift register
5. Loop only executes once
19
Uninitialized shift register has memory
Loop only executes once
Only used in Initialize case
Action determines which case is executed
Examples of other ‘actions’
Functional Global Variables
DEMO
20
Benefits: Comparison
Functional Global Variables Global and Local Variables
• Prevent race conditions • Can cause race conditions
• No copies of data • Create copies of data in memory
• Can behave like action engines • Cannot perform actions on data
• Can handle error wires • Cannot handle error wires
• Take time to make • Drag and drop
21
Recommendations
Use Cases
• Communicate data between code without connecting wires
• Perform custom actions upon data while in storage
Considerations
• All owning VIs must stay in memory
• Use clusters to reduce connector pane
• Using stacked shift registers will track multiple iterations
22
State Machine
I need to execute a sequence of events, but the
order is determined programmatically
Background
Static Sequence
Dynamic Sequence: Allows distinct states to operate in a
programmatically determined sequence
24
Soda Machine
Initialize No input
Wait
Nickel Deposited
Change Quarter Deposited
Requested Dime Deposited
Total < 50
Total < 50 Total < 50
Change Quarter
Nickel
Dime
Total >= 50 Total >= 50
Total >= 50
Total > 50
Vend
Total = 50
Exit
Soda costs $0.50
25
Breaking Down the Design Pattern
• Case Structure inside of a While Loop
• Each case is a state
• Current state has decision making code that
determines next state
• Use enumerators to pass value of next state to
shift registers
26
How It Works
Case structure has a case for every state Transition code determines next state
based upon results of step execution
FIRST STATE
Step Execution
Shift registers used to carry state
Transition Code
NEXT STATE
FIRST STATE
27
Transition Code Options
Step
Execution
Step Execution
Step
Execution
28
State Machine
DEMO
29
Recommendations
Use Cases
• User interfaces
• Data determines next routine
Considerations
• Creating an effective State Machine requires the
designer to make a table of possible states.
• Use LabVIEW Statechart to abstract this process for
more sophisticated applications
30
Producer / Consumer
I have two processes that need to execute at the same time,
and I need to make sure one can’t slow the other down
How It Works
• One or more slave loops are told by
Master
a master loop when they can run
• Allows for a-synchronous execution
of loops
• Data-independence breaks dataflow
and allows multi-threading Slave 1
• De-couples processes
Slave 2
33
Breaking Down the Design Pattern
• Data independent loops = Multithreading
• Master / slave relationship
• Communication and synchronization between
loops
34
Loop Communication
• Variables
• Occurrences
• Notifier
• Queues
• Semaphores
• Rendezvous
35
Queues
Adding Elements to the Queue
Select the data-type the queue will hold
Reference to existing queue in memory
De-queueing Elements
Dequeue will wait for data or timeout (defaults to -1)
36
Producer / Consumer
37
Adding Your Own Design Patterns
C:\Program Files\National Instruments\LabVIEW 8.5\templates\Frameworks\DesignPatterns
38
Resources
• Example Finder
• New >> Frameworks >> Design Patterns
• ni.com/labview/power
• Expressionflow.com
• Visit ni.com/info and enter exhkqe
39
40
LabVIEW in Teaching
The NI LabVIEW Academy
41
What Is the NI LabVIEW
Academy?
The NI LabVIEW Academy program provides classroom
curriculum, instructional materials, and hands-on exercises to
high schools, community colleges, and universities for the
specific purpose of teaching LabVIEW.
LabVIEW Academy is for anyone seeking LabVIEW education
and knowledge through an academic institution.
42
What Does the NI LabVIEW Academy
Do?
Empowers institutions to teach LabVIEW
Emphasises LabVIEW professional certification
Increases the pool of qualified LabVIEW developers
43
NI LabVIEW Academy Program
Requirements
Instructor Requirements
Two instructors must be Certified LabVIEW Associate
Developers (CLADs) and teach at participating
organisations
Program Requirements
• Current teaching site license
• At least one dedicated classroom (a computer lab will suffice)
• 40 hours of classroom LabVIEW specific instruction time
• One PC per student (with LabVIEW software)
• NI DAQ equipment required for lab component (2:1 student ratio)
• Submit course syllabus to NI for approval
44
NI LabVIEW Academy Instructional
Materials
Instructor Materials
•LabVIEW Basics I & II Instructor Manual
•LabVIEW Basics I & II Lecture Slides
•LabVIEW Basics I & II Exercises and Solutions
•Instructor Version of Student Workbook
•50 LabVIEW Exam/Homework Questions
Student Materials
•LabVIEW Academy Workbook (student purchase)
• 300+ questions
•Recommended LabVIEW textbook (student purchase)
45
NI LabVIEW Academy Teaching Materials
Curriculum for both learning LabVIEW and teaching LabVIEW
NI LabVIEW Academy Teaching Materials
Recommended LabVIEW Textbooks
46
NI LabVIEW Academy CLAD Opportunity
The NI LabVIEW Academy gives students the opportunity to take
the CLAD exam as part of the program
49
The NI LabVIEW Academy Bridging the Gap
Academic Industry
“LabVIEW is getting more popular in academia “In our exhaustive search for qualified
and industry and many researchers and LabVIEW developers to fill key roles in our
companies are on the lookout for competent organization, we greatly anticipate the new pool
LabVIEW programmers. This program will of qualified candidates coming out of the
help bridge the gap between the two.” National Instruments LabVIEW Academy
– Khanjan Mehta, Professor, schools.“
Penn State University – Marvin Landrum, Section
Manager, Texas Instruments
50
ni.com/academy
51
52
Tips and Tricks to Speed NI LabVIEW Development
Useful Nuggets to Save You Time
Agenda
• 20 Tips and Tricks for LabVIEW Development
Beginner: Simple tricks to save time
Intermediate: LabVIEW tips you probably did not know about
Advanced: Useful nuggets to put you ahead of the game
54
/
1
20 Automatically Select the Right Tool
• Avoid manually switching among many tools
Operate Value Tool
Position/Size/Select Tool
Auto Tool
Edit Text Tool
Connect Wire Tool
55
/
2
20 Quickly Drop Palette Objects
Hold down Ctrl + Space to launch Quick Drop
• Ctrl + D – Create controls and indicators on
selected diagram object(s)
• Ctrl + R – Remove diagram object(s) and
reconnect wires
• Ctrl + T – Move control and indicator terminal
labels to the left and right sides
Demo
56
/
3
20 Design Pattern Templates
• Access via File New …
• Well-known designs
Producer/Consumer
State Machine
Queued Message Handler
57
/
4
20 Switch Terminal Wires Easily
Hold Down Ctrl
and Left-Click on Input Terminal
Note: This works only for functions with two inputs when both
inputs have already been wired
Demo
58
/
5
20 Easily Scroll Through Structures
• You can use Ctrl + Mouse Scroll to scroll through:
Case Structures
Event Structures
Stacked Sequence Structures
Diagram Disable Structures
Ctrl + Mouse Scroll Wheel
59
/
6
20 Quickly Find the Right Palette
• Right-click on a block diagram object for a palette shortcut
Add
Index Array
To More Specific Class
Demo
60
/
7
20 Easily Navigate Arrays
• Right-click on an array and select Visible Items >> Horizontal Scrollbar
(or Visible Items >> Vertical Scrollbar)
• To view last element, select Advanced >> Show Last Element
• Both horizontal and vertical scrollbars available (depending on array
dimensions)
Demo
61
/
8
20 Selective Insert Location
Right-Click Slightly Right-Click Slightly below
above Wire Wire
Wire Connected Wire Connected
to Lower Terminal to Upper Terminal
62
/
9
20 Block Diagram Clean-Up
• Click Clean Up Diagram button on toolbar or press
Ctrl + U
• Highlight a portion of the diagram for partial cleanup
• Right-click and select “Exclude from Diagram
Cleanup” option
Note: Only
available in
LabVIEW 8.6 and
later.
Demo
63
/
10
20 Quickly Wire Multiframe Structures
• Right-click an output tunnel and select “Linked Input Tunnel »
Create & Wire Unwired Cases”
Note: Only available in LabVIEW 8.6 and later
64
/
11
20 Easily Assess 2D Array Size
• Matrix Size function replaces old method
• Assess size of 2D array regardless of data type
Note: Only available in LabVIEW
2009 and later
65
/
12
20 Breakpoint Manager
• Select View » Breakpoint Manager
• Right-click and select Breakpoint » Breakpoint
Manager
Note: Only available in LabVIEW 8.6 and later
66
/
13
20 Quickly Find VIs in the Project Window
• Ctrl + Shift +E from an open VI with open project
Note: Only available in LabVIEW 2009 and later
67
/
14
20 Easily Add Enumeration Items
• Press Ctrl while hovering over an Enum to use the Text
Tool
• Use Shift + Enter to repeatedly add items
Shift + Enter
Demo
68
/
15
20 Edit Multiple Objects Simultaneously
• Highlight all desired front panel or block diagram
objects
• Right-click and select “Properties”
Demo
69
/
16
20 Automatically Create Control References
• Simply drag a control into a Control Refnum
• Automatically creates a class-specific, type-specific
reference
Note: To keep the original control, use Ctrl-Drag instead
70
/
17
20 Drag and Drop to Save Time
• Drag an image into your VI icon
• Select a file and drag into a path constant
• Take a URL from Internet Explorer and drag into a string constant
• Drag items from disk or Project Explorer into a LabVIEW block diagram
71
/
18
20 Quickly Browse Properties and Methods
• View >> Class Browser
• Shortcut: Ctrl + Shift + B
• Drag a property or method directly into your
VI
Demo
72
/
19
20 Implement a For Loop Progress Bar
• Place Progress Bar VI inside a for loop
• Opens automatically after a specified amount of time
• Download sample code from ni.com/forums (search for “For Loop Progress Bar”)
Demo
73
/
20
20 Automatically Analyse Your VIs
• Tools >> VI Analyzer >> Analyze VIs
– Check performance, style, UI, documentation, and more
– Save LabVIEW VI Analyzer settings for later use
Demo
74
Other Resources
• Darren’s LabVIEW Nuggets
(decibel.ni.com/content/docs/DOC-4002)
• LAVA: Favorite Tips and Shortcuts
(forums.lavag.org)
• LabVIEW Wiki
(labviewwiki.org)
75
76