Upload
leonardosirino
View
10
Download
0
Tags:
Embed Size (px)
DESCRIPTION
LVOOP
Citation preview
ni.com
ni.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.
Translation: OOP concepts, when used correctly, decrease the risk and effort required to add functionality to an existing system. They do this, in part, by:- Minimizing the scope of changes that have to be introduced to an existing system to add new functionality
- Restricting access to data to methods that have explicitly been permitted access
- Making it possible to add and load new behaviors dynamically that implement existing interfaces
- Enable reuse and extension of pre-existing functionality
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)
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
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
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 want to iterate on certain tasks within a loop..
ni.com
Measurement System
Configure Acquire Measure
I/O Samples
Clock Raw Data
Graph
DAQ Task
…or perhaps the entire system
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
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
ni.com
Measurement System
Measure Event Case
Measure
Graph
ni.com
Measurement System
Event Case
I/O
Samples
Clock
Raw DataTask
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
ni.com
Measurement System
Event Case
I/O
Samples
Clock
Raw DataTask
Configure
I/O
Clock
Task
The methods within this state machine retrieve the information they need from the cluster and update values as
necessary
ni.com
Measurement System
Event Case
I/O
Samples
Clock
Raw DataTask
Configure
Unbundling and bundling data can be encapsulated by the VI, giving the user a clean, simple top-level interface (the type
definition)
ni.com
Configure Acquire Measure
I/O
Samples
Clock
Raw DataTask
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
ni.com
I/O
Samples
Clock
Raw DataTask
Measurement Class=
Measurement Data A class contains data, plus
methods (VIs) that are allowedto act upon and modify the data.
VIs that do not belong to the classcannot act upon the data
A class is basically a cluster
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 directly bundle and/or unbundle the data
ni.com
I/O
Samples
Clock
Raw DataTask
Class Data Cluster
Configure Acquire Measure
Methods that can act upon the class’s data
clusterAppearance in Project
Class constant
These VIs are now owned by the class. They are transported as a cohesive library of code
ni.com
DemonstrationCreating a New Class
ni.com
Measurement System
Event Case
I/O
Samples
Clock
Raw DataTask
Configure
Returning to our basic system, this implementation using a clsuter effectively becomes…
ni.com
Measurement System
Case
ConfigureMeasurement
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
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
ni.com
DemonstrationDefine inheritance and view the class hierarchy diagram
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
ni.com
Measurement System
Case
Start Task Read Samples Stop Task
AcquireTemp
‘Acquire’ is Dynamically Dispatched
ni.com
Measurement System
Case
StimulateOutput
Sweep Inputs
Acquire
Start AI Task
Start AO Task
Strain
‘Acquire’ is Dynamically Dispatched
ni.com
Strain.lvclass:Acquire.vi
Temp.lvclass:Acquire.vi
Resistance.lvclass:Acquire.vi
Understanding Dynamic Dispatch
2610
Acquire
Strain
Temp
Resistance
ni.com
DemonstrationIllustrate dynamic dispatch
ni.com
Configure Acquire Measure
Graph
Configure Acquire Measure
Graph
I/O
Samples
Clock
Raw DataTask
Measurement Type
? ? ?
Q: Isn’t this the same thing as using case structures inside these VIs ?
ni.com
A: It’s conceptually similar, but there are extremely important differences…(NO)
To understand the difference, first consider the impact of introducing a new measurement in the non-OOP example.
Configure Acquire Measure
Graph
I/O
Samples
Clock
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
ni.com
Configure Acquire Measure
Graph
I/O
Samples
Clock
Raw DataTask
Measurement Type
? ? ?
As the scope of the data cluster expands, we are passing 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 requires changes within the VIs, as well as the calling code (sometimes referred to as a framework). This makes code very costly to maintain and brittle
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
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 wire cannot be changed at run-time, as the connector pane must match exactly.
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)
ni.com
Configure Acquire Measure
Graph?
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.
LabVIEW can load a new child at run-time. The class will bring its methods and it’s 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
AB
C Generic Measurement
Location on DiskWhere Measurements
Classes are Stored
Objects Loaded Into Memory
A B C
Parent
Children
ni.com
Group Exercise“About that ‘Graph’ output… what if my measurements output different data types?”
Configure Acquire Measure
Boolean
TempConfigure 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?
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 a user 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?
?
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’
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 the need 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.
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
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
ConfigureCurrent Source
Autozero Read
ni.com
DMM Class
Agilent 34401a
PXI-4070
Simulated DMM
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?
ni.com
Configure Acquire Measure
All measurements use an array of hardware
Measurement Class
But different classes of hardware would definitely not implement the same interface
ni.com
Sample Hardware Class Hierarchy
Hardware
ScopePower Supply
DMM Generator
Simulated
34401aPXI-4110
PXIe-5185
PXI 4070Simulate
dSimulate
dSimulate
d
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 the interfaces for the specific device
classes
ni.com
DemonstrationUse 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 parent’s 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?
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 to learn more
Trained LabVIEW Users reported developing 50% faster and spending 43% less time on maintenance
ni.com
In a worldwide survey, Certified LabVIEW Developers (CLD) reported:
…as a result of NI Certification
Series1
0% 10% 20% 30% 40% 50% 60%
54% reported improvement in work quality
45% reported improved peer perception
30% got new project opportunities
Validate your skills and 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] Schedule Your Exam
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 NI developers
• 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.
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.
ni.com
Already CLAD Certified?
You’re immediately eligible to take the Certified LabVIEW 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.
ni.com