ts2060 introduction to object-oriented programming in ni labview (1)

76
Introduction to LabVIEW Object-Oriented Programming Adam Kemp LabVIEW R&D

Upload: mruet04

Post on 08-Nov-2014

118 views

Category:

Documents


8 download

DESCRIPTION

labview object

TRANSCRIPT

Page 1: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Introduction to LabVIEW

Object-Oriented Programming

Adam Kemp

LabVIEW R&D

Page 2: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Agenda

•Object-oriented concepts

•Object-oriented programming in LabVIEW

•Example use of Object-oriented programming

Page 3: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Coupling and Cohesion

•Two useful measures for how modular, reusable,

and extensible your application is

•Coupling: How much separate components or •Coupling: How much separate components or

pieces of code depend on one another

•Cohesion: How closely the functions or features

within a component are related

Page 4: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Coupling

•High coupling means changing code in one piece

of code causes you to have to modify another

piece of code

•Low coupling means you can safely change code •Low coupling means you can safely change code

without affecting other code

•Low coupling is good

Page 5: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Cohesion

•Low cohesion means modules are poorly

defined and contain functionality which

does not belongdoes not belong

•High cohesion means each module has a

well defined function and all code within it

goes together

•High cohesion is good

Page 6: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Benefits of OOP

•Object Oriented Programming tends to:

–Produce code which is easier to write.

–Produce code which is easier to debug.–Produce code which is easier to debug.

–Produce code which will be easier to maintain.

–Lower coupling

–Higher cohesion

Page 7: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Object-Oriented Design?

Task-Oriented design Object-Oriented design

•Step-by-step instructions

•Sequence of tasks

•Areas of Responsibility

•Set of Objects•Sequence of tasks •Set of Objects

Page 8: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Two Descriptions of the Same Problem

•We want to test each board at the end of an

assembly line to make sure each is functional.

We want to send an e-mail to management for

any failed tests.

vs.

•We want the assembly line to produce boards.

Each board should test itself for functionality

and report any problem. The log should send an

e-mail to management for any failed tests.

Page 9: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Two Descriptions of the Same Problem

•We want to test each board at the end of an

assembly line to make sure each is functional.

We want to send an e-mail to management for

any failed tests.

vs.

•We want the assembly line to produce boards.

Each board should test itself for functionality

and report any problem. The log should send an

e-mail to management for any failed tests.

Page 10: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Object-Oriented Programming?

•Object Oriented Design

– Use of Object-Oriented concepts

•Object Oriented Programming•Object Oriented Programming

– Use of an Object Oriented Programming Language

– Alerts when Object Oriented concepts are violated

– Features which support use of Object-Oriented concepts

Page 11: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Key OOP Language Features

•Encapsulation

•Inheritance

•Dynamic Dispatching (aka “Polymorphism”)

Page 12: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Common OOP Languages

•C++

•C#

•Java

•Objective C•Objective C

•Perl

•Python

•LabVIEW 8.20 and later

Page 13: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Key Terms

•Class

•Object

•Method

Page 14: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What is a class?

•A class is a type

•A class consists of:

– Data type

– List of actions that work with that data (“methods”)– List of actions that work with that data (“methods”)

Page 15: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What is an object?

•An object is a value

•An object is a specific instance of a class

•There may be many objects (instances) of a given

classclass

Page 16: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What is a method?

•A method is an action which can be performed

on an object

•Belongs to a class

•Uses the data in an object•Uses the data in an object

Page 17: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Examples of classes

Class: Car Person

Data:

•Make

•Model

•Year

•First Name

•Last Name

•Date of BirthData: •Year

•Mileage

•Date of Birth

•Gender

Methods:

•Check Brakes

•Rotate Tires

•Change Oil

•Get Full Name

•Get Age

Page 18: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Examples of objects

Class: Car Person

Object:

•1954 Ford Mustang

•2004 Honda Accord

•Adam Kemp

•Steven Harrison

Page 19: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Review: Classes, objects, and methods

•A class is a type

•An object is a value

•A method is an action

Page 20: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is an “Object” In LabVIEW?

•An object is like a cluster

– Composite of other data

Page 21: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is an “Object” In LabVIEW?

•An object is like a cluster

– Composite of other data

– Uses bundle, unbundle

– Copies when the wire forks

Page 22: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is an “Object” In LabVIEW?

•An object is like a cluster

– Composite of other data

– Uses bundle, unbundle

– Copies when the wire forks

•Plus more!•Plus more!

– Custom Wire Appearance

Page 23: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

•An object is like a cluster

– Composite of other data

– Uses bundle, unbundle

– Copies when the wire forks

•Plus more!

What Is an “Object” In LabVIEW?

•Plus more!

– Custom Wire Appearance

– Functions to help use internal data

•The data type of an object is called its “class”

Page 24: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is a “LabVIEW Class”?

•A user-defined data type

•A special type of library

Page 25: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Anatomy of a LabVIEW Class

•Each LabVIEW class consists of:

– A data control (cluster)

– Member VIs to access that data

•Class file (.lvclass) stores class information

– Data control definition

– List of member VIs

– Properties of member VI

– Properties of the class itself (such as wire appearance)

Page 26: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Object versus class in LabVIEW

•An object is a specific instance of a class

•An object is data on the wire

Page 27: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Demo: Creating a New LabVIEW Class

Page 28: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Two related concepts:

1. Consolidation of data and methods into an

object

2. Hiding implementation details2. Hiding implementation details

Page 29: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Consolidation of data and methods into an object:

Page 30: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Hiding implementation details:

Accessor methods

Page 31: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 32: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 33: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 34: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 35: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 36: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Page 37: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Encapsulation?

Consolidation of data and methods into an object,

with restricted access to data

– Hides how an object works

– Easy to change data format

– Easier debugging– Easier debugging

– Hard to enforce without

language support

Page 38: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Encapsulation in LabVIEW

LabVIEW classes specify scope of contents:

– Public: Accessible by any VI

– Private: Only accessible by VIs within the same class

– Protected: Accessible by VIs in the same class and

descendants (more later)descendants (more later)

– Community: Accessible by “friends” (advanced use case)

Page 39: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Encapsulation in LabVIEW

LabVIEW classes specify scope of contents:

– Public: Accessible by any VI

– Private: Only accessible by VIs within the same class

Page 40: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Encapsulation in LabVIEW

LabVIEW classes specify scope of contents:

– Public: Accessible by any VI

– Private: Only accessible by VIs within the same class

Top-Level VI(outside class)

Public Method A

Private Method A

Private Method B

Public Method B

Private Method C

Private Method D

Page 41: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Encapsulation in LabVIEW

LabVIEW classes specify scope of contents:

– Public: Accessible by any VI

– Private: Only accessible by VIs within the same class

Top-Level VI(outside class)

Public Method A

Private Method A

Private Method B

Public Method B

Private Method C

Private Method D

Page 42: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Benefits of Encapsulation

•Encapsulated code is easier to write.

– Each component using data can use the most convenient

form of that data.

•Encapsulated code is more stable.

– Less code must change when data format changes.– Less code must change when data format changes.

•Encapsulated code is easier to maintain.

–Less code must change to introduce features and fix bugs.

•Lower coupling

Page 43: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Demo: Encapsulation in LabVIEW

Page 44: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Inheritance?

Human Class Feline Class•Get Age

•Get Gender

•Drive Car

•Get Age

•Get Gender

•Meow

Page 45: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Inheritance?

•Get Age

•Get GenderMammal Class

Human Class Feline Class•Get Age

•Get Gender

•Drive Car

•Get Age

•Get Gender

•Meow

Page 46: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

What Is Inheritance?

•Get Age

•Get GenderMammal Class

(parent)Inheritance

A feline is a

type of mammal.

A human is a

type of mammal.

Human Class

(child)

Feline Class

(child)

•Get Age

•Get Gender

•Drive Car

•Get Age

•Get Gender

•Meow

Page 47: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Inheritance Example

Ancestors

Descendants

Mammal

Human Feline

Big Feline

Lion Tiger

Housecat

Page 48: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Inheritance Example

Ancestors

Descendents

Mammal

Human Feline

Big Feline

Lion Tiger

Housecat

– Protected: only accessed by class and its descendants

Page 49: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Inheritance and wire types

- A wire of some class type can carry an object of

that class or any of the class’s descendants

- No data lost when this is done.

Page 50: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

To More Specific/Generic Class

Used to cast an object to another class

No data lost

Will return

an error at

run time

Page 51: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Inheritance in LabVIEW

•Public Inheritance

•Single Inheritance

•Library Properties Dialog•Library Properties Dialog

Page 52: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Inheritance in LabVIEW

Page 53: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Benefits of Inheritance

•Inherited code is easier to write.

– Child classes are already partially written (code reuse).

•Inherited code is more stable.

– Child classes can be built on proven foundations.

•Inherited code is easier to maintain.•Inherited code is easier to maintain.

– Reduced duplicate code by providing common methods in

a common parent.

•Higher cohesion

Page 54: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Demo: Inheritance in LabVIEW

Page 55: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Static vs. Dynamic Dispatching

•Static method – Defined by single VI throughout

the class inheritance hierarchy.

•Dynamic method – Defined by multiple VIs with •Dynamic method – Defined by multiple VIs with

the same name throughout the class hierarchy.

– Child classes override the method defined in the parent.

– Parent defines a contract

– Children fulfill the contract

Page 56: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatch

Mammal

Human Feline

*

Big Feline

Lion Tiger

Housecat

Page 57: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching

– Wire value at runtime determines which VI to call

– Code written against a set of promises made by parent.

Page 58: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching

– Wire value at runtime determines which VI to call

– Code written against a set of promises made by parent.

Page 59: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching

What makes a dynamic dispatch VI?

– Inside a class

Mammal

Page 60: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching

What makes a dynamic dispatch VI?

– Inside a class

– Inheritance hierarchy

– Same NameMammal

Human

Page 61: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching

What makes a dynamic dispatch VI?

– Inside a class

– Inheritance hierarchy

– Same Name

– Same connector pane

Mammal

– Same connector pane

– Dynamic Dispatch TerminalHuman

Page 62: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Call Parent Node

Used on child VI to invoke the parent VI

Page 63: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Benefits of Dynamic Dispatch

•Dynamically dispatched code is easier to write.

– Write code against the interface provided by the parent,

which then works with any child supporting that interface.

•Dynamically dispatched code is easier to maintain.

– New child classes can often be inserted into existing – New child classes can often be inserted into existing

frameworks without modifying the framework.

•Lowers coupling

Page 64: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Demo: Dynamic Dispatching

Page 65: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Example: Circuit Board Test

•Scenario

– LabVIEW-based circuit board test system

•Requirements

– Different types of boards must be tested– Different types of boards must be tested

– New types of boards will be added in the future

•Goals

– Maximize code reuse and system scalability

Page 66: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Demo: Board Test System

Page 67: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Additional Resources

• White papers on ni.com

• Webcast on ni.com

• Example Finder

• Other sessions:

– “Trends in NI LabVIEW Object-Oriented Programming”

– Tuesday 3:30 in 13 A/B (next session right here)

– “Practical Examples of NI LabVIEW Object-Oriented Programming

Classes and Use Cases”

– Tuesday 4:45 in 10C

Page 68: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Questions?

Page 69: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

LabVIEW OOP Compared With C++

Q: How do LabVIEW classes compare with C++?

A: Some (but not all) of the differences include:

– C++ has constructors and destructors; LabVIEW has no – C++ has constructors and destructors; LabVIEW has no

need for them.

Page 70: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

LabVIEW OOP Compared With C++

Q: How do LabVIEW classes compare with C/C++?

A: Some (but not all) of the differences include:

– C++ has constructors and destructors; LabVIEW has no – C++ has constructors and destructors; LabVIEW has no

need for them.

– C++ has function overloading (LabVIEW does not).

– C++ has multiple inheritance (LabVIEW does not).

Page 71: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

By-Value vs. By-Reference

Q: Why do LabVIEW classes use a by-value model

instead of by-reference model?

A: By-value model is a better fit for LabVIEW.

Examples:

– Maintains LabVIEW’s dataflow programming style.

– Avoids race conditions

– Reduces developer burden

– LabVIEW 2009: Data Value References

*”What's New in LabVIEW Object Oriented Programming”

*

Page 72: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Operating System and Target Support

Q: Which OSes do LabVIEW classes support?

Real-Time*Windows

Mobile / Touch Panel*

FPGA*

Macintosh

Linux

* LabVIEW 2009

Page 73: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching Overhead

Q: Does Dynamic Dispatching have a runtime cost?

• A little (constant, regardless of override count)

• Almost certainly less than alternatives• Almost certainly less than alternatives

Page 74: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Dynamic Dispatching Overhead

Q: Does Dynamic Dispatching have a runtime cost?

• A little (constant, regardless of override count)

• Almost certainly less than alternatives• Almost certainly less than alternatives

• Wire Parent’s VI as you would Descendants

Page 75: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Migrating to LabVIEW Classes

Q: How can I update an existing LabVIEW program

to use classes?

Page 76: TS2060 Introduction to Object-Oriented Programming in NI LabVIEW (1)

Migrating to LabVIEW Classes

Q: How can I update an existing LabVIEW program

to use classes?