57
ni.com

Introduction to OOP for LabVIEW Programmers

Embed Size (px)

Citation preview

Page 1: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 1/57

ni.com

Page 2: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 2/57

ni.com

Using OOP in Measurement Systems

Page 3: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 3/57

ni.com

Common Problem: Software becomes difficult to

maintain over time… 

Initial investment in

software architecture,

training and processes

Page 4: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 4/57

Page 5: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 5/57

ni.com

Configure Acquire Measure

The journey to OOP starts sooner than you think… 

Conceptually, many LabVIEW applications start with a simple,

synchronous set of operations (many times all on a single diagram)

Page 6: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 6/57

ni.com

Configure  Acquire Measure

Start Task Read Samples Stop Task

Savvy programmers will often use SubVIs to wrap API calls to things

like device drivers or loading configuration from a file

Page 7: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 7/57

ni.com

Configure Acquire Measure

Task Samples

Clock Raw Data

Graph

DAQ Task

These SubVIs typically expect certain inputs to do their jobs – many

times these inputs are the output of previous operations, or a single

input is shared by multiple VIs in this sequence

Page 8: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 8/57

ni.com

Measurement System

Configure Acquire Measure

Task Samples

Clock Raw Data

Graph

DAQ Task

 As the application becomes more sophisticated, it’s common to wantto iterate on certain tasks within a loop..

Page 9: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 9/57

ni.com

Measurement System

Configure Acquire Measure

I/O Samples

Clock Raw Data

Graph

DAQ Task

…or perhaps the entire system 

Page 10: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 10/57

ni.com

Measurement System

Configure

Task

Clock

Configure Event Case

Eventually, automation requires that we be able to programmatically

control the order of operations and/or restart the system, which

requires a state-machine-like pattern that passes data between states

Page 11: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 11/57

ni.com

Measurement System

 Acquire Event Case

 Acquire

Samples

Raw Data

The scope of data these operations has access to can be seen by

examining the shift registers in a loop

Page 12: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 12/57

ni.com

Measurement System

Measure Event Case

Measure

Graph

Page 13: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 13/57

ni.com

Measurement System

Event Case

I/O

Samples

Clock

Raw Data

Task

 As the functionality grows, so too does the scope of data. Eventually it

becomes helpful to contain all of this data in a cluster, giving us a very

clearly defined data-scope for this process

Page 14: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 14/57

ni.com

Measurement System

Event Case

I/O

Samples

Clock

Raw Data

Task

Configure

I/O

Cloc

k

Task

The methods within this state machine retrieve the information they

need from the cluster and update values as necessary

Page 15: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 15/57

ni.com

Measurement System

Event Case

I/O

Samples

Clock

Raw Data

TaskConfigure

Unbundling and bundling data can be encapsulated by the VI, giving

the user a clean, simple top-level interface (the type definition)

Page 16: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 16/57

ni.com

Configure Acquire Measure

I/O

Samples

Clock

Raw Data

Task

Graph

This simple illustration shows how these three operations we started

with now just act upon the data within the cluster

These VIs are explicitly coupled to this data

Page 17: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 17/57

ni.com

I/O

Samples

Cloc

k

Raw Data

Task

Measurement

Class=

Measurement

Data A class contains data, plus

methods (VIs) that are allowed

to act upon and modify the data.

VIs that do not belong to the class

cannot act upon the data 

 A class is basically a cluster  

Page 18: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 18/57

ni.com

Configure Acquire Measure

Graph

Measurement

The object wire can be passed into any VI that has the class on the

connector pane, but only VIs that belong to the class can directlybundle and/or unbundle the data

Page 19: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 19/57

ni.com

I/O

Samples

Cloc

k

Raw Data

Task

Class Data Cluster

Configure  Acquire Measure

Methods that can act upon

the class’s data cluster  

 Appearance in Project

Class constant

These VIs are now owned by the class. They are

transported as a cohesive library of code

Page 20: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 20/57

ni.com

DemonstrationCreating a New Class

Page 21: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 21/57

ni.com

Measurement System

Event Case

I/O

Samples

Clock

Raw Data

TaskConfigure

Returning to our basic system, this implementation using a clsuter

effectively becomes… 

Page 22: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 22/57

ni.com

Measurement System

Case

ConfigureMeasurement

Page 23: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 23/57

ni.com

Measurement System

Case

ConfigureMeasurement

What if you want a different definition of how these methods should act?In this example, consider the different ways in which a ‘measurement’ might be implemented 

Page 24: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 24/57

ni.com

Measurement

Temp Strain

These are children of the measurement class

Finite measurement of a

single channel Applies stimuli before

acquiring value

Inheritance Allows Descendant Classes to Modify, Extend and

 Add Functionality of a Parent

Page 25: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 25/57

ni.com

DemonstrationDefine inheritance and view the class hierarchy diagram

Page 26: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 26/57

ni.com

Measurement System

Case

ConfigureMeasurement

What if you want a different definition of how these methods should act?In this example, consider the different ways in which a ‘measurement’ might be implemented 

Page 27: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 27/57

ni.com

Measurement System

Case

Start Task Read Samples Stop Task

 AcquireTemp

‘Acquire’ is Dynamically Dispatched 

Page 28: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 28/57

ni.com

Measurement System

Case

StimulateOutput

Sweep Inputs

 Acquire

Start AI Task

Start AO Task

Strain

‘Acquire’ is Dynamically Dispatched 

Page 29: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 29/57

ni.com

Strain.lvclass:

Acquire.vi

Temp.lvclass:

Acquire.vi

Resistance.lvclass:

Acquire.vi

Understanding Dynamic Dispatch

2610

 Acquire

Strain

Temp

Resistance

Page 30: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 30/57

ni.com

DemonstrationIllustrate dynamic dispatch

Page 31: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 31/57

ni.com

Configure Acquire Measure

Graph

Configure Acquire Measure

Graph

I/O

Samples

Cloc

k

Raw Data

Task

Measurement Type

? ? ?

Q: Isn’t this the same thing as using case structures inside these VIs ?

Page 32: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 32/57

ni.com

 A: It’s conceptually similar, but there are extremely

important differences…(NO)

To understand the difference, first consider the impact ofintroducing a new measurement in the non-OOP example.

Configure Acquire Measure

Graph

I/O

Samples

Clo

ck

Raw

DataTask

Measurement Type

? ? ?

2. We have to modify all of these VIs

1. We have to add a new type to the enum

3. We probably have to add new elements to

this data cluster

Page 33: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 33/57

ni.com

Configure Acquire Measure

Graph

I/O

Samples

Clock

RawDataTask

Measurement Type

? ? ?

 As the scope of the data cluster expands, we arepassing data into large segments of code inside

the cases that should not have access to it. Our

data is not protected

I/O2

Trigger

DIO

Introducing a new measurements requireschanges within the VIs, as well as the calling code

(sometimes referred to as a framework). This

makes code very costly to maintain and brittle 

Page 34: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 34/57

ni.com

Sibling Classes Have Unique Data Scope

Measurement

Strain Temp

I/O

Bridge Type

Task

Thermocouple

Samples

Data unique to these specific measurements, plus

‘exposed’ data of parent measurement 

Data that every measurement

needs to have

excitation

Page 35: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 35/57

ni.com

Configure Acquire Measure

Graph

I/O

Samples

Clock

Raw

DataTask

Measurement Type

? ? ?

One of the biggest differences: this new

functionality has to be added at edit time.

What if you want to load a new measurement into

your calling system at run-time? 

I/O2

Trigger

DIO

Yes, you can dynamically load the VIs called by these VIs, but you have to

have pre-defined the data they have access to. The data in the cluster wirecannot be changed at run-time, as the connector pane must match exactly.

Page 36: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 36/57

ni.com

Configure Acquire Measure

Graph

 At edit-time, LabVIEW shows us the wire of the

parent class we have said will be passed along

this wire (in this example: Measurement.lvclass)

Page 37: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 37/57

Page 38: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 38/57

ni.com

The Basics of an Object Factory

 AB

C Generic Measurement

Location on Disk

Where Measurements

Classes are Stored

Objects Loaded Into Memory

 A B C

  Parent

  Children

Page 39: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 39/57

ni.com

Group Exercise

“ About that ‘Graph’ output… what if my measurements output different data types?”  

Configure Acquire Measure

Boolean

Temp Configure Acquire Measure

Graph

Strain

XDynamically dispatched VIs must have the same connector pane. You cannot have a

different data type output on ‘Measure.’ So how do we solve this problem? 

Page 40: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 40/57

ni.com

Q: How can an instance of the measure method return

information that is appropriate and specific to the measurement

class it belongs to?

Measure

Consider the following requirements:

• We have to pass different data-types at-run

time out of the measure method

• We always know a specific measurement will

return a certain type of data

• We need to be able to display that data to auser in an appropriate format

• We need to pass the data back to a framework

that implements a pre-defined interface for

operations like ‘dispaly,’ ‘save,’ etc… 

Is a variant a valid solution?• We can pass any data-type on a variant wire

• How do we know how to display the data on a variant wire to a user?

• If we pass the variant to our calling code, how will it know how to save it to

disk or display it to the user?

?

Page 41: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 41/57

ni.com

Use a Class Hierarchy

Measurement Result

Strain Result Temp Result Resistance Result

Defines methods all results

should be able to define, such

as ‘Save,’ or ‘Display’ 

Each has a unique private data cluster to store the result of a measurement and

defines how that data is stored or displayed using dynamically dispatched methods

that override the interface defined by the parent ‘Measurement Result’ 

Page 42: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 42/57

ni.com

General Best-Practice: Don’t Use Variants 

Variants are typically used when different types of data

have to travel down a single wire. Anytime you feel theneed to do this, consider replacing the wire that would be a

variant with a class hierarchy.

Why? Classes still enforce strict data-types at edit-time,thereby ensuring no run-time errors. Variants do not, and

therefore increase the likelihood of run-time errors.

Page 43: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 43/57

ni.com

Configure Acquire Measure

But I’d like to.. 

• run the same measurement class on different

machines, which have different hardware

• continue development on a machine with no access

to hardware

• be able to add a new device of a certain type without

modifying the measurement

 All measurements will need to use hardware.

Measurement Class

Page 44: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 44/57

ni.com

Configure Acquire Measure

 A measurement is defined assuming certain

classes of devices are available, but without

knowing exactly which instrument.

Temp

DMM

Class

In this example, this measurement strategy uses a DMM. It

assumes an instance of a DMM implements a pre-defined

interface, without knowledge of how that interface is implemented

Configure

CurrentSource

 Autozero Read

Page 45: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 45/57

Page 46: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 46/57

ni.com

Configure Acquire Measure

Other measurements may require different classes of hardware, or perhaps multiple

devices (ie: stimulus/response measurements), but we can’t change the connector pane

of methods we want to override (like Acquire.vi).

Measurement

Class

The objects passed along this wire must all

be children of the same parent class?

Page 47: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 47/57

ni.com

Configure  Acquire Measure

 All measurements use an array of hardwareMeasurement

Class

But different classes of hardware would

definitely not implement the same interface

Page 48: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 48/57

ni.com

Sample Hardware Class Hierarchy

Hardware

ScopePower Supply DMM Generator

Simulated 34401aPXI-4110 PXIe-5185PXI 4070Simulated Simulated Simulated

Page 49: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 49/57

ni.com

Configure Acquire Measure

Methods can cast hardware objects to specific children at edit-

time using the ‘to more specific’ primitive 

Measurement

Class

DMM

SCOPE

The dark blue wire can be passed into theinterfaces for the specific device classes

Page 50: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 50/57

ni.com

DemonstrationUse these concepts in a real system

Page 51: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 51/57

ni.com

Summary of Most Important Concepts

•  Always be thinking about data scope – keep it cohesive andsmall

• Classes create define data scope and a set of functions thatare allowed access to data

• Consider using a class hierarchy to replace a massive datastructure

• Dynamic dispatch allows child classes to override a parent’smethod and reuse others

• Dynamic dispatch occurs at run-time, whereas polymorphismoccurs at edit-time

• Use parent classes to define the interfaces children shouldimplement

• If you find yourself using a lot of variants, consider a classhierarchy

•  And finally, classes are not as big of a leap as you might think – we hope you agree after this presentation!

Page 52: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 52/57

ni.com

Want to Learn More?

Prefer a live instructor?

Find a classroom course near you

at ni.com/training 

The Object-Oriented Design

training course is available Online!

Visit ni.com/training/self-paced tolearn more

Trained LabVIEW Users reported developing 50% faster and

spending 43% less time on maintenance

Page 53: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 53/57

ni.com

In a worldwide survey, Certified LabVIEW

Developers (CLD) reported:

…as a result of NI Certification 

0% 20% 40%

54% reported improvement in work quality

45% reported improved peer perception

30% got new project opportunities

Validate your skillsand differentiate

yourself from your

peers with NI

certification.

Join the 10,000 + NI Certified Professionals

Start preparing now at

ni.com/training/certification_prep 

Email [email protected] 

To Schedule Your Exam

Page 54: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 54/57

ni.com

Looking for More?

 Attend a CLA or CLD Summit to:

• Network and exchange best practices with other

certified professionals and NI engineers

• Participate in highly technical presentations

• Get exclusive opportunities to meet with NIdevelopers

• Take the recertification exam for free

Learn more at ni.com/cla-summit 

You must be certified to attend a Summit.

Email [email protected] to register for

an exam near you.

Missed the CLA Summit this year?

Page 55: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 55/57

ni.com

Missed the CLA Summit this year?

Get certified for next year!

The Certified LabVIEW Architect (CLA) Summit brings

together some of the world’s best NI LabVIEW programmers

to discuss architectures, preview new features, and network

with other CLAs and members of NI R&D.

Learn more at ni.com/cla-summit 

Email [email protected] to register for

an exam near you.

Page 56: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 56/57

ni.com

 Already CLAD Certified?

You’re immediately eligible to take the CertifiedLabVIEW Developer exam. Start preparing now!• Join a local user group

• Prepare using resources on Developer Zone

ni.com/training/certification_prep 

• Time yourself during practice exams

Note: CLAD certification must be current to take the CLD

exam

Email [email protected] to register for

an exam near you.

Page 57: Introduction to OOP for LabVIEW Programmers

8/10/2019 Introduction to OOP for LabVIEW Programmers

http://slidepdf.com/reader/full/introduction-to-oop-for-labview-programmers 57/57