LabVIEW Design Patterns
LabVIEW Design Patterns
Reliability
Many have been used for years they are tried and true Refer to large development community and resources online
3
Caution
You can needlessly complicate your life if you use an unnecessarily complex design pattern. Do not forget the most common design pattern of all data flow!
Basic Tools
Loops Shift registers Case structures Enumerated constants Event structures
Todays Discussion
As we look at each design pattern, we will discuss
A problem we are trying to solve Background How it works Technical implementation Demonstration Use cases/considerations
Design Patterns
Basic State machine Event-driven user interface Producer/consumer Advanced Object-oriented programming
State Machine
I need to execute a sequence of events, but the order is determined programmatically.
Background
Static Sequence Dynamic Sequence: Distinct states can operate in a programmatically determined sequence
10
Vending Machine
Initialize Wait
Change Requested Quarter Deposited Total <50 Nickel Deposited Dime Deposited Total <50 Total <50 No Input
Change
Quarter
Total 50 Total >50
Dime
Total 50
Nickel
Total 50
Vend
Exit
Total = 50
11
12
How It Works
Case structure has a case for every state Transition code determines next state based on results of step execution
FIRST STATE
Step Execution
Shift registers used to carry state
Transition Code
NEXT STATE FIRST STATE
13
Step Execution
Step Execution
14
State Machine
DEMO
15
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 the LabVIEW Statechart Module to abstract this process for more sophisticated applications
16
LabVIEW Intermediate I
Background
Procedural-driven programming
Performs a set of instructions in sequence Requires polling to capture events Cannot determine order of multiple events
Event-driven programming
Determines execution at run time Waits for events to occur without consuming CPU Remembers order of multiple events
18
How It Works
Event structure nested within loop Blocking function until event registered or time-out Events that can be registered:
Notify events are only for interactions with the front panel Dynamic events implement programmatic registration Filter events help you to screen events before they are processed
19
How It Works
1. Operating system broadcasts system events (mouse click, keyboard) to applications Event structure captures registered events and executes appropriate case Event structure returns information about event to case Event structure enqueues events that occur while it is busy
2.
3. 4.
20
21
DEMO
22
Recommendations
Use Cases
UI: Conserve CPU usage UI: Ensure you never miss an event Drive slave processes
Considerations
Event structures eliminate determinism Avoid placing two event structures in one loop Remember to read the terminal of a latched Boolean control in its value change event case
23
LabVIEW Intermediate I
Producer/Consumer
I have two processes that need to execute at the same time, and I need to make sure one cannot slow the other down.
How It Works
Master loop tells one or more slave loops when they can run Allows for asynchronous execution of loops Data independence breaks data flow and permits multithreading Decouples processes Thread 1
Thread 2
Thread 3
25
26
Queues
Adding Elements to the Queue
Dequeueing Elements
28
Producer/Consumer
30
Producer/Consumer
DEMO
31
Recommendations
Use cases Handling multiple processes simultaneously Asynchronous operation of loops Considerations Multiple producers one consumer One queue per consumer If order of execution of parallel loop is critical, use occurrences
33
Object-Oriented Programming
35
36
37
38
Laser Printer
Printer
Inkjet
Copy Machine
39
40
DEMO
42
Recommendations
Use cases Applications needing high-level modularity or scalability Memory conservation when loading subVIs Considerations More complex; requires strict architecture Not needed for limited applications
43
45
Solution: Producer/consumer We should use the producer/consumer architecture because we have multiple tasks that run at different speeds and cannot afford to be slowed down. Each of the external readings will be in separate producer loops and the data processing and logging will be in the consumer loop.
47
Solution: Object-oriented programming We should use object-oriented programming with a factory that produces the proper number of each type of 3D object. Because we do not know how many will be produced beforehand and they all share some similar properties, dynamically creating these objects from an object-oriented programming factory is the most efficient solution.
48
DEMO
49
Resources
Example Finder New>>Frameworks>>Design Patterns ni.com/statechart ni.com/labview/power Training
LabVIEW Intermediate I and II
51
Experienced User
Advanced User
LabVIEW Advanced I
Certifications
Certified LabVIEW Associate Developer Exam
If you are unsure take the - Quick LabVIEW quiz - Fundamentals exam ni.com/training
52
Certification
53
54
Next Steps
Visit ni.com/training Identify your current expertise level and desired level Register for appropriate courses
$200 USD discount for attending LabVIEW Developer Education Day!
55
Advanced Intermediate
Specialty
Foundation
At Your Convenience
Self-paced course kits On-demand training modules located in the Services Resource Center
ONE PRICE
Questions?