Top Banner
Design Patterns in LabVIEW Developer Days 2008
63

Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

Mar 11, 2020

Download

Documents

dariahiddleston
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: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

Design Patterns in LabVIEW

Developer Days 2008

Page 2: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

2

What Is a Design Pattern?

• A template or framework for LabVIEW code

• Widely accepted and well-known

• Easily recognizable

Page 3: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

3

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 tested

Large development community and resources online

Page 4: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

4

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

Page 5: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

5

Caution

You can needlessly complicate your

application if you use an

unnecessarily complex design

pattern

Don’t forget the most common

design pattern of all… dataflow

Page 6: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

6

Basic Tools

• Loops

• Shift Registers

• Case Structures

• Enumerated Constants

• Event Structures

Page 7: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

7

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

Page 8: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

8

Design Patterns

• Functional Global Variable

• State Machine / Statecharts

• Event Driven User Interface

• Producer / Consumer

• Queued State Machine – Producer / Consumer

Page 9: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

Functional Global Variables

How do I share data across a application

without using Global or Local Variables?

Page 10: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

10

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 11: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

11

Breaking Down the Design Pattern

• While loop

• Uninitialized shift

registers have memory

• Case structure

• Enumerated control

Page 12: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

12

DEMO

Uninitialized Shift Registers

Page 13: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

13

How It Works: Basic Actions

• Set the value of the shift register

INITIALIZE

INITIALIZE

Page 14: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

14

How It Works: Basic Actions

• Get the value currently stored in the shift register

GET

GET

Page 15: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

15

How It Works: Action Engine

• Perform an operation upon stored value and

save result

• You can also output the new value

ACTION

ACTION

Page 16: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

16

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

Technical Implementation

Page 17: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

17

DEMO

Functional 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 18: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

18

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 19: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

19

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 20: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

State Machine

I need to execute a sequence of events, but the

order is determined programmatically

Page 21: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

21

Background

Dynamic Sequence: Allows distinct states to operate in a

programmatically determined sequence

Static Sequence

Page 22: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

22

Soda Machine

Initialize

Wait

Change Quarter

DimeNickel

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 23: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

23

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 24: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

24

Transition Code

How It Works

FIRST STATE

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 upon results of step execution

Page 25: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

25

Transition Code Options

Step

Execution

Step

Execution

Step Execution

Page 26: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

26

DEMO

State Machine

Page 27: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

27

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

Page 28: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

28

Combine with I/O to implement:• Communication protocols

• Control applications

• User-interfaces

• Safety relevant logic

• Complex state machines

• FPGA logic

Deploy statecharts to:• Desktop PCs

• Real-time systems

• FPGAs

• Microprocessors

• Industrial touch panels

LabVIEW Statechart Module

Page 29: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

29

Differences between Statecharts and FSMs

Both contain the same basic concepts:

States

Transitions

Statechart adds additional concepts: Hierarchy

Concurrency

Event-based paradigm

Pseudostates & Connectors

Button Press

H

Based on the UML statechart diagram specification

Page 30: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

Event Driven User Interface

I’m polling for user actions, which is slowing my

application down, and sometimes I don’t detect them!

Page 31: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

31

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 32: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

32

How It Works

• 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 33: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

33

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 34: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

34

How It Works: Static Binding

• Browse controls

• Browse events per control

• Green arrow: notify

• Red arrow: filter

Page 35: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

35

DEMO

Event Driven User Interface

Page 36: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

36

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

Page 37: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

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 38: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

39

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

Page 39: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

40

Breaking Down the Design Pattern

• Data independent loops = Multithreading

• Master / slave relationship

• Communication and synchronization between

loops

Page 40: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

41

Loop Communication

• Variables

• Occurrences

• Notifier

• Queues

• Semaphores

• Rendezvous

Page 41: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

42

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 42: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

43

DEMO

Queues

Page 43: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

44

Producer / Consumer

Page 44: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

45

DEMO

Producer / Consumer

Page 45: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

46

Creating Multithreaded ApplicationsData independent loops automatically spawn separate threads in LabVIEW

Page 46: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

47

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

Page 47: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

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 48: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

49

Breaking Down the Design Pattern

• Event-driven user interface design pattern

• State machine design pattern

• Producer consumer design pattern

• Queued communication between loops

Page 49: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

50

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

How It Works

Page 50: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

51

Queues RecommendationsRefer to queues by name for

communication across VIsUse a cluster containing an

enum and variant as data-type

Page 51: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

52

Page 52: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

53

Master Queue

Page 53: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

54

Event-Driven

Producer Loop

Page 54: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

55

State and Data are

Enqueued

Page 55: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

56

State Machine

Consumer

Page 56: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

57

Additional Queues

(Q1 and Q2)

Page 57: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

58

States ‘Produce’ to

Additional Queues

Page 58: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

60

SubVIs Consume

Data from Q1 and Q2

Page 59: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

61

DEMO

Queued State Machine – Producer/Consumer

Page 60: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

62

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

Page 61: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

63

Adding Your Own Design Patterns

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

Page 62: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

64

Resources

• Example Finder

• New >> Frameworks >> Design Patterns

• ni.com/statechart

• ni.com/labview/power

• Training

LabVIEW Intermediate I & II

• White Paper on LabVIEW Queued State

Machine Architecture

Expressionflow.com

Page 63: Design Patterns in LabVIEWlabview360.com/.../files/authorid3/2009-06-05_191716/track_1_labview_design_patterns.pdfDesign Patterns in LabVIEW Developer Days 2008. 2 What Is a Design

Questions?