An Introduction to LabVIEW - National Instrumentssouthafrica.ni.com/sites/default/files/Introduction...

Preview:

Citation preview

An Introduction to LabVIEW

Object-Oriented Design Patterns

Jacques Cilliers

Application Engineer

Agenda

Overview

Producer Consumer Queued State Machine Without

Classes

Command Pattern (Object-Oriented State Machine)

Factory Method Design Pattern (Dynamically Load

Plugins)

Sending Messages to Plugins Using User Events

Object-Oriented Hardware Abstraction Layer

ni.com/largeapps

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…

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, or requirements

No ‘buddying’ or code reviews

Poor planning

No test plans

Poor error handling

No consistent style

Tight coupling or poor cohesion

Why Use Object-Orientation in

LabVIEW?

Encapsulate and protect data

Catch errors at compile time instead of run-time

Easily extend functionality

Eliminate large data clusters

Implement established design patterns

Map real-world problems to software

Understanding Object-Oriented

Programming

Class: A collection of data and the

methods that interact with that data.

Object: A specific instance of a class

Examples of Classes

Class: Car Person

Data:

Make

Model

Year

Mileage

First Name

Last Name

Date of Birth

Gender

Methods: Check Brakes

Rotate Tires

Change Oil

Get Full Name

Get Age

Examples of Objects

Class: Car Person

Object:

•1964 Ford Mustang

•2004 Honda Accord

•Adam Kemp

•Steven Harrison

What is a LabVIEW Class?

A LabVIEW Class is…

• A glorified cluster

• A user-defined data type

• A type of Project Library

Class: A collection of data and the

methods that interact with that

data

Object: A specific instance of a

class

Anatomy of a LabVIEW Class

Each LabVIEW class consists of:

A private data control (cluster)

Member VIs to access that data

Class file (.lvclass) stores class information

Private data control definition

List of member VIs

Properties of member VIs

Properties of the class itself (such as wire appearance)

What is Inheritance?

Example methods:

• Initialize

• Get Cargo Capacity Vehicle Class

(Parent)

Car Class (Child)

Truck Class (Child)

A truck is a type of vehicle. A car is a type of vehicle.

Inheritance Example

Vehicle Class

Car Class Truck Class

Chevy Model

2007 Model 2008 Model

Ancestors

Descendents

Vehicle.lvclass

Get Cargo Capacity.vi

Truck.lvclass

Get Cargo Capacity.vi

Car.lvclass

Get Cargo Capacity.vi

Understanding Dynamic Dispatch

2 6 10

State Machine versus Command Pattern These diagrams represent functionally equivalent code

Dynamic Dispatching Commands

Dynamic Dispatching Commands

The Basics of an Object Factory

A B

C Generic Plugin

Location on Disk

Where Plugins are Stored

Objects Loaded Into Memory

A B C

Parent

Children

User Interface State Machine

Event-Driven Loop

Indefinite dynamically loaded plug-ins

Self-Contained Application Framework

Plug-in State Machine

User Interface State Machine

Event-Driven Loop

Indefinite dynamically loaded plug-ins

Self-Contained Application Framework

Plug-In State Machine

Plugins Need To Receive Commands

We need to be able to send

individual plugins a

command

“You’re Being Displayed”

User Interface State Machine

Event-Driven Loop

Indefinite dynamically loaded plug-ins

Self Contained Application Framework

Plug-In State Machine

Plugins Need To Receive Commands

We may also want to

broadcast messages

“Everyone STOP!”

The Anatomy of Dynamic Events

Dynamic Events

Terminal

Defines

Data Type

VI Gets

Run on

Event

Data Sent

Multiple Loops Can

Register for Same

Event

LabVIEW API for Managing User Events

Using User Events

Register User Events With Listeners

Plugin Handler Launch Plugin.lvclass –

Execute.vi

Broadcast Event Every Plug-In Object

Stores this Event

Unicast Event Unique Event for Every

Object

Traditional

Benchtop

Modular PXI Simulated (software

only)

Abstract the Hardware in Software Mitigate Obsolescence With a Hardware Abstraction Layer

(HAL)

A plug-in architecture

makes it possible to

interchange hardware

without modifying code

Four factory patterns are used to

load the appropriate instrument

object at run-time Dynamically dispatched at run-

time

Hardware Abstraction White Paper How to Mitigate Hardware Obsolescence in Next-Generation

Test Systems

ni.com/largeapps

Applying Common Object-Oriented

(OO) Design Patterns to LabVIEW Singleton Pattern - Guarantee that a given class has only a single instance in memory

Channeling Pattern - To provide a guaranteed pre-processing/post-processing around some dynamic central functionality

Factory Pattern - Provide a way to initialize the value on a parent wire with data from many different child classes based on some input value, such as a selector ring value, enum, or string input

Hierarchy Composition Pattern - To represent a single object as a tree of smaller instances of that same object type

Delegation Pattern - To have two independent classes share common functionality without putting that functionality into a common parent class

Visitor Pattern - To write a traversal algorithm of a set of data such that the traversal can be reused for many different operations

Aggregation Pattern - To treat an array of objects as a single object and define special behavior for that particular type of array

Specification Pattern - To have different functionality for a class depending upon some value of the class without updating case structures throughout your VI hierarchy when you add another value

Decorator (Wrapper) Pattern - Extend a concrete object’s responsibilities dynamically or extend the features of an existing class without refactoring tested code

Strategy (Policy) Pattern - Change the behavior of a class's method without having to edit the method's implementation

Certified LabVIEW Developer

Exam

Certified LabVIEW

Architect Exam

Certified LabVIEW Associate

Developer Exam

Developer Senior Developer Software Architect

Project Manager

NI Certifications Align With Training

Managing

Software

Engineering

in LabVIEW

LabVIEW

OOP Advanced

Architecture

LabVIEW

Core 3

LabVIEW

Core 2

LabVIEW

Core 1

Download Examples and Slides

ni.com/largeapps

Software Engineering Tools

Development Practices

LargeApp Community

Recommended