Introduction To Object Oriented Programming and Hardware Abstraction Layers
Introduction To Object Oriented Programming and Hardware Abstraction Layers
com
Using OOP in Measurement Systems
ni.com
Common Problem: Software becomes difficult to
maintain over time
Initial investment in
software architecture,
training and processes
ni.com
Why OOP is an Answer
When you want to build scalable, extensible systems.
ni.com
The journey to OOP starts sooner than you think
ni.com
Configure Acquire Measure
Savvy programmers will often use SubVIs to wrap API calls to things
like device drivers or loading configuration from a file
ni.com
Graph
Task Samples
DAQ Task
ni.com
Measurement System
Graph
Task Samples
DAQ Task
ni.com
Measurement System
Graph
I/O Samples
DAQ Task
ni.com
Measurement System
Task
Clock
Configure
ni.com
Measurement System
Samples
Raw Data
Acquire
ni.com
Measurement System
Graph
Measure
ni.com
Measurement System
I/O
Cloc Event Case
k
Samples
Raw Data
Task
ni.com
Measurement System
I/O
Cloc Event Case
k
Samples
Configure
The methods within this state machine retrieve the information they
need from the cluster and update values as necessary
ni.com
Measurement System
I/O
Cloc Event Case
k
Samples
Raw Data
Task
Configure
ni.com
This simple illustration shows how these three operations we started
with now just act upon the data within the cluster
I/O
Cloc
k
Samples Graph
Raw Data
Task
ni.com
A class is basically a cluster
I/O
Cloc
k Measurement
Samples
Raw Data
= Class
Task
Measurement
A class contains data, plus
Data
methods (VIs) that are allowed
to act upon and modify the data.
ni.com
Measurement
Graph
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 directly
bundle and/or unbundle the data
ni.com
Class Data Cluster
I/O Class constant
Cloc
k
Samples
Raw Data
Task
Appearance in Project
Configure Acquire Measure
ni.com
Demonstration
Creating a New Class
ni.com
Measurement System
I/O
Cloc Event Case
k
Samples
Raw Data
Task
Configure
ni.com
Measurement System
Case
Measurement Configure
ni.com
Measurement System
Case
Measurement Configure
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
ni.com
Inheritance Allows Descendant Classes to Modify, Extend and
Add Functionality of a Parent
Measurement
Temp Strain
ni.com
Measurement System
Case
Measurement Configure
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
ni.com
Acquire is Dynamically Dispatched
Measurement System
Case
Temp
Acquire
ni.com
Acquire is Dynamically Dispatched
Measurement System
Case
Strain
Acquire
ni.com
Understanding Dynamic Dispatch
Strain
Temp Acquire
610
2
Resistance
ni.com
Demonstration
Illustrate dynamic dispatch
ni.com
Graph
Q: Isnt this the same thing as using case structures inside these VIs ?
I/O
Cloc
k
Samples
Raw Data
? ? ? Graph
Task
Measurement Type
ni.com
A: Its conceptually similar, but there are extremely
important differences(NO)
I/O
Cloc 2. We have to modify all of these VIs
k
Samples
Raw
Data ? ? ? Graph
Task
Measurement Type
ni.com
I/O2
DIO
As the scope of the data cluster expands, we are
passing data into large segments of code inside
Trigger
the cases that should not have access to it. Our
I/O data is not protected
Clock
Samples
Raw
Data ? ? ? Graph
Task
Measurement Type
ni.com
Sibling Classes Have Unique Data Scope
Task Data that every measurement
I/O
needs to have
Samples
Measurement
Thermocouple
Bridge Type
excitation
Strain Temp
ni.com
One of the biggest differences: this new
I/O2
functionality has to be added at edit time.
DIO
What if you want to load a new measurement into
Trigger
your calling system at run-time?
I/O
Clock
Samples
Raw
Data ? ? ? Graph
Task
Measurement Type
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 wire
cannot be changed at run-time, as the connector pane must match exactly.
ni.com
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)
Graph
ni.com
We may pass any child of this class down this wire
at run-time. Dynamically dispatched methods will
execute the copy belonging to the run-time
instance.
?
Graph
LabVIEW can load a new child at run-time. The class will bring its methods and
its data cluster into memory when loaded. This makes it possible to add
functionality without modifying calling code. The code to load a child class is
referred to as a Factory.
ni.com
The Basics of an Object Factory
A
B Objects Loaded Into Memory
C Generic Measurement
Parent
ni.com
Group Exercise
About that Graph output what if my measurements output different data types?
Strain
Graph
Configure Acquire
X
Measure
Boolean
Dynamically 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?
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?
Consider the following requirements:
ni.com
Use a Class Hierarchy
Measurement 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
ni.com
General Best-Practice: Dont Use Variants
Variants are typically used when different types of data
have to travel down a single wire. Anytime you feel the
need to do this, consider replacing the wire that would be a
variant with a class hierarchy.
ni.com
All measurements will need to use hardware.
Measurement Class
ni.com
A measurement is defined assuming certain
classes of devices are available, but without
knowing exactly which instrument.
DMM
Class
ni.com
DMM
Class
ni.com
Other measurements may require different classes of hardware, or perhaps multiple
devices (ie: stimulus/response measurements), but we cant change the connector pane
of methods we want to override (like Acquire.vi).
Measurement
Class
Configure Acquire Measure
ni.com
Measurement
All measurements use an array of hardware
Class
ni.com
Sample Hardware Class Hierarchy
Hardware
ni.com
Measurement
Class Methods can cast hardware objects to specific children at edit-
time using the to more specific primitive
DMM
SCOPE
The dark blue wire can be passed into the
interfaces for the specific device classes
ni.com
Demonstration
Use these concepts in a real system
ni.com
Summary of Most Important Concepts
Always be thinking about data scope keep it cohesive
and small
Classes create define data scope and a set of functions that are
allowed access to data
Consider using a class hierarchy to replace a massive
data structure
Dynamic dispatch allows child classes to override a parents method
and reuse others
Dynamic dispatch occurs at run-time, whereas polymorphism occurs
at edit-time
Use parent classes to define the interfaces children
should implement
If you find yourself using a lot of variants, consider a class hierarchy
And finally, classes are not as big of a leap as you might think we
hope you agree after this presentation!
ni.com
Want to Learn More?
Trained LabVIEW Users reported developing 50% faster and
spending 43% less time on maintenance
Visit ni.com/training/self-paced to
learn more
ni.com
Join the 10,000 + NI Certified Professionals
ni.com
Looking for More?
ni.com
Already CLAD Certified?
ni.com