Labview Design Patterns
Labview Design Patterns
ni.com/largeapps
Scalable: how simple is N + 1? Modular: is the application broken up into welldefined components that stand on their own? Reusable: is the code de-coupled from the current application well-enough such that it could be reused in a future project? Extensible: how painful is it to add new functionality? Simple: what is the simplest solution that satisfies all of the listed criteria and the requirements of the application?
5
Loops Shift Registers Case Structures Enumerated Constants Event Structures LabVIEW Classes
Design Patterns
Functional Global Variable State Machine / Statecharts Event Driven User Interface Producer / Consumer Queued State Machine Producer / Consumer
10
DEMO
11
Basic Actions
Set the value of the shift register
INITIALIZE
INITIALIZE
12
Basic Actions
Get the value currently stored in the shift register
GET
GET
13
Action Engine
Perform an operation upon stored value and save result You can also output the new value
ACTION
ACTION
14
How It Works
1. 2. 3. 4. 5. Functional Global Variable is a Non-Reentrant SubVI Actions can be performed upon data Enumerator selects action Stores result in uninitialized shift register Loop only executes once
15
Loop only executes once Only used in Initialize case Action determines which case is executed Examples of other actions
DEMO
16
Benefits: Comparison
Functional Global Variables Global and Local Variables
Prevent race conditions No copies of data Can behave like action engines Can handle error wires Take time to make
Can cause race conditions Create copies of data in memory Cannot perform actions on data Cannot handle error wires Drag and drop
17
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
18
State Machine
I need to execute a sequence of events, but the order is determined programmatically
Soda 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
20
Background
Static Sequence
21
22
Step Execution
Shift registers used to carry state
Transition Code
NEXT STATE
FIRST STATE
23
Step Execution
Step Execution
24
State Machine
DEMO
25
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.
26
Background
Procedural-driven programming
Set of instructions are performed in sequence Requires polling to capture events Cannot determine order of multiple events
Event-driven programming
Execution determined at run-time Waits for events to occur without consuming CPU Remembers order of multiple events
28
Notify events are only for interactions with the front panel Dynamic events allows programmatic registration Filter events allow you to screen events before theyre processed
29
How It Works
1. Operating system broadcasts system events (mouse click, keyboard, etc..) to applications Registered events are captured by event structure and executes appropriate case Event structure returns information about event to case Event structure enqueues events that occur while its busy
2.
3. 4.
30
31
DEMO
32
Recommendations
Use Cases
UI: Conserve CPU usage UI: Ensure you never miss an event Drive slave processes
Considerations
Avoid placing two Event structures in one loop Remember to read the terminal of a latched Boolean control in its Value Change event case When using subpanel controls, the top-level VI containing the subpanel control handles the event
33
Producer / Consumer
I have two processes that need to execute at the same time, and I need to make sure one cant slow the other down
Background
I want to execute code in parallel and at asynchronous rates, but I need to communicate between them!
I have two processes that need to execute at the same time, but I want them to be independent of one another, and I need to make sure one cant slow the other down
35
36
How It Works
One or more slave loops are told by a master loop when they can run Allows for a-synchronous execution of loops Data-independence breaks dataflow and allows multi-threading De-couples processes
37
38
Queues
Adding Elements to the Queue
De-queueing Elements
39
Producer / Consumer
40
Producer / Consumer
DEMO
41
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
42
44
How It Works
1. Events are captured by producer 2. Producer places data on the queue 3. State machine in consumer executes on dequeued data 4. Parallel SubVIs communicate using queue references
45
Queues Recommendations
Use a cluster containing an enum and variant as data-type Refer to queues by name for communication across VIs
46
47
Master Queue
48
49
50
51
52
53
55
DEMO
56
Recommendations
Use Cases Popular design pattern for mid to large size applications Highly responsive user interfaces Multithreaded applications De-coupling of processes Considerations Complex design
57
58
Resources
Example Finder New >> Frameworks Ni.com/labview/power Training
Expressionflow.com
59
LabVIEW Core 1
LabVIEW Core 2
LabVIEW Core 3
Certified LabVIEW Developer Exam
"Certification is an absolute must for anyone serious about calling himself a LabVIEW expert... At our organization, we require that every LabVIEW developer be on a professional path to become a Certified LabVIEW Architect." - President, JKI Software, Inc.
LargeApp Community