Top Banner
Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)
60

Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Dec 25, 2015

Download

Documents

Alison Clarke
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Introduction to

Basic LabVIEW Design PatternsElijah Kerry – LabVIEW Product ManagerCertified LabVIEW Architect (CLA)

Page 2: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

2

Why Should I Use One?Save time and improve the longevity and readability of your code.

Definition: A well-established solution to a common problem.What is a Design Pattern?

… or else…

Page 3: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

3

Page 4: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

4

Examples of Software Engineering Debt(just some of the most common LabVIEW development mistakes)

No source code control (or Project) Flat file hierarchy ‘Stop’ isn’t tested regularly Wait until the ‘end’ of a project to build an application Few specifications / documentation / requirements No ‘buddying’ or code reviews Poor planning (Lack of consideration for SMoRES) No test plans Poor error handling No consistent style Tight coupling, poor cohesion

ni.com/largeapps

Page 5: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

5

Designing for SMoRESCriteria for a well designed software application:

Scalable: how simple is N + 1? Modular: is the application broken up into well-

defined 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?

Page 6: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

6

You Should Already Be Familiar With..• Loops• Shift Registers• Case Structures• Enumerated Constants• Event Structures• LabVIEW Classes

Page 7: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

7

Design Patterns• Functional Global Variable• State Machine / Statecharts• Event Driven User Interface• Producer / Consumer• Queued State Machine – Producer / Consumer

Page 8: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Functional Global Variables

How do I share data across a application without using Global or Local Variables?

Page 9: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

9

Background: Global and Local Variables• Can cause race conditions• Create copies of data in memory• Cannot perform actions on data• Cannot handle error wires

Page 10: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

10

Breaking Down the Design Pattern

• While loop• Uninitialized shift

registers have memory• Case structure• Enumerated control

Page 11: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

11

DEMOUninitialized Shift Registers

Page 12: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

12

Basic Actions• Set the value of the shift register

INITIALIZE

INITIALIZE

Page 13: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

13

Basic Actions• Get the value currently stored in the shift register

GET

GET

Page 14: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

14

Action Engine• Perform an operation upon stored value and save

result• You can also output the new value

ACTION

ACTION

Page 15: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

15

How It Works1. Functional Global Variable is a Non-Reentrant SubVI2. Actions can be performed upon data3. Enumerator selects action4. Stores result in uninitialized shift register5. Loop only executes once

Page 16: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

16

DEMOFunctional Global Variables

Uninitialized shift register has memory

Action determines which case is executed

Only used in Initialize case

Loop only executes once

Examples of other ‘actions’

Page 17: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

17

Benefits: Comparison

Global and Local Variables• Can cause race conditions• Create copies of data in memory• Cannot perform actions on data• Cannot handle error wires• Drag and drop

Functional Global Variables• Prevent race conditions• No copies of data• Can behave like action engines• Can handle error wires• Take time to make

Page 18: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

18

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

Page 19: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

State Machine

I need to execute a sequence of events, but the order is determined programmatically

Page 20: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

20

Soda Machine

Initialize

Wait

Change QuarterDime

Nickel

Exit

Vend

Soda costs $0.50

No input

Quarter Deposited

Total < 50

Total >= 50

Change Requested Dime Deposited

Nickel Deposited

Total < 50 Total < 50

Total >= 50Total >= 50

Total > 50

Total = 50

Page 21: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

21

Background

Dynamic Sequence: Allows distinct states to operate in a programmatically determined sequence

Static Sequence

Page 22: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

22

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

Page 23: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

23

The Anatomy of a State Machine

Transition Code

FIRST STATE

NEXT STATE

Step Execution

Shift registers used to carry state

Case structure has a case for every state Transition code determines next state based on results of step execution

FIRST STATE

?

Page 24: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

24

Step Execution

Step Execution

Step Execution

Transition Code Options

Page 25: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

25

DEMOState Machine

Page 26: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

26

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.

Page 27: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Event Driven User Interface

I’m polling for user actions, which is slowing my application down, and sometimes I don’t detect them!

Page 28: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

28

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

Page 29: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

29

Breaking Down the Design Pattern

• Event structure nested within loop• Blocking function until event registered or timeout• Events that can be registered:

Notify events are only for interactions with the front panel Dynamic events allows programmatic registration Filter events allow you to screen events before they’re processed

Page 30: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

30

How It Works

1. Operating system broadcasts system events (mouse click, keyboard, etc..) to applications

2. Registered events are captured by event structure and executes appropriate case

3. Event structure returns information about event to case

4. Event structure enqueues events that occur while it’s busy

Page 31: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

31

How It Works: Static Binding

• Browse controls• Browse events per control• Green arrow: notify• Red arrow: filter

Page 32: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

32

DEMOEvent Driven User Interface

Page 33: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

33

RecommendationsUse 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

Page 34: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

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

Page 35: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

35

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 can’t slow the other down

Page 36: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

36

Breaking Down the Design Pattern• Data independent loops• Master / slave relationship• Communication and synchronization between loops

Page 37: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

37

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

Slave 1

Slave 2

Master

Page 38: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

38

Master / Slave: Loop Communication• Variables• Occurrences• Notifier• Queues• Semaphores• Rendezvous

Page 39: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

39

QueuesAdding Elements to the Queue

De-queueing Elements

Reference to existing queue in memory

Select the data-type the queue will hold

Dequeue will wait for data or timeout (defaults to -1)

Page 40: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

40

Producer / Consumer

Page 41: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

41

DEMOProducer / Consumer

Page 42: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

42

Recommendations

Use cases• Handling multiple processes simultaneously• Asynchronous operation of loopsConsiderations• Multiple producers One consumer• One queue per consumer• If order of execution of parallel loop is critical, use

occurrences

Page 43: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Queued State Machine & Event-Driven Producer / Consumer

I need to enqueue events from a user that control the sequence of events in a consumer loop

Page 44: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

44

Breaking Down the Design Pattern• Event-driven user interface design pattern• State machine design pattern• Producer consumer design pattern• Queued communication between loops

Page 45: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

45

Parallel SubVI 1

State Machine Consumer

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

Parallel SubVI 2

Parallel SubVI 3

Event-driven User Interface

Producer

Page 46: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

46

Queues RecommendationsRefer to queues by name for communication across VIsUse a cluster containing an

enum and variant as data-type

Page 47: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

47

Page 48: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

48

Master Queue

Page 49: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

49

Event-Driven Producer Loop

Page 50: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

50

State and Data areEnqueued

Page 51: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

51

State MachineConsumer

Page 52: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

52

Additional Queues (Q1 and Q2)

Page 53: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

53

States ‘Produce’ to Additional Queues

Page 54: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

55

SubVIs ConsumeData from Q1 and Q2

Page 55: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

56

DEMOQueued State Machine – Producer/Consumer

Page 56: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

57

Recommendations

Use Cases• Popular design pattern for mid to large size

applications• Highly responsive user interfaces• Multithreaded applications• De-coupling of processesConsiderations• Complex design

Page 57: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

58

Adding Your Own Design Patterns

C:\Program Files\National Instruments\LabVIEW 8.5\templates\Frameworks\DesignPatterns

Page 58: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

59

Resources• Example Finder• New >> Frameworks• Ni.com/labview/power• Training

LabVIEW Intermediate I & II• White Paper on

LabVIEW Queued State Machine Architecture Expressionflow.com

Page 59: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Certified LabVIEW Developer Exam

Certified LabVIEW Architect Exam

Certified LabVIEW Associate Developer Exam

LabVIEWCore 1

LabVIEWCore 2

LabVIEW Core 3

Advanced Architecturesfor LabVIEW

Developer Senior Developer Software Architect/ Project Manager

NI Certifications Align with Training

"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.

Managing Software

Engineering in LabVIEW

Page 60: Introduction to Basic LabVIEW Design Patterns Elijah Kerry – LabVIEW Product Manager Certified LabVIEW Architect (CLA)

Download Examples and Slides

ni.com/largeappsSoftware Engineering Tools

Development Practices

LargeApp Community