Labview Object Oriented Programming

Encapsulation

Labview has evolved to offer classes with textual programming language capabilities of composition and inheritance. Class is like a cluster. It contains data such as boolean, numeric, arrays etc. Unlike clusters, we can’t simply unbundle class and access its data. So if an app data is enclosed within class, we can rest assured it is save under all heriarchies of calling/called vis. The only way to access class data is by creating ‘Methods’ which in labview are called ‘Dynamically Dispatched VIs’ and ‘Statically Dispatched VIs’. In programming language, such data protection is often termed ‘Data Encapsulation’.

“By placing application data within classes rather cluster, data is fully protected and can only be accessed by ‘methods’ which could be accessed directly from project explorer. This greatly improves code Debugging, readability and data protection.

Class can be created like any other item by right clicking in project explorer and selecting class from the menu. Here is an example how class appears along with methods to access class data in project explorer. labview classes

Notice icon next to Cycle.lvclass and Class – Under Body Wash.lvclass. This is the representation of class object in labview project explorer. Notice also the three virtual folders under Cycle Class – Data Member Access VIs, Dynamically Dispatched VIs and Statically Dispatched VIs. Complete Tutorial on Class structure is beyond the scope of this article but here is small definitions for each.

Data Member Access VIs

These VIs are simply used to access ‘Private’ data of class. Access could be either to read data or write data. As in example here, ‘Read Cycle Completion Done.vi’ is a vi that has a boolean output which is set/reset when boolean element in Cycle class is updated. I have also attached below the front panel of ‘Cycle.ctl’. As you can see it is merely a cluster of data but can only be accessed using Class Methods – Data Member Access, Dynamically/Statically Dispatched VIs.

class object

Class data is Boolean ‘Done’ which is accessed using ‘Read Cycle Completion Done.vi’ or ‘Write Cycle Completion Done’.vi. These Methods could be used to change the status of boolean Done. Like wise, cycle index could be accessed using ‘Write cycle index.vi’ or ‘Read cycle index.vi’. Notice also another control ‘Time Persistent Storage’. I have used this control to store elapsed time of timer. As you might expect, I plan to put the class wire on shift register so to continuously update and store elapsed time in Time Persistent Storage control.

Dynamically Dispatched VIs

Dynamically dispatched VIs are inherited by child classes but can be over-ridden. Notice in both Cycle class and Under Body Wash class above, the name of Dynamically dispatched vis is similar. For instance, Get Proximity Switch.vi and Update Cycle Indicators.vi. It is worth mentioning here that Cycle is a parent class and Under Body Wash is a child class. Similarly, any other wash cycle could be classified as child class of parent class ‘Cycle’. More on parent and child classes is towards the end of this article.

The dynamically dispatched vis help child classes to choose the conditions that apply to these child classes and update outputs/indicators that are relevant to child class. For example, Get Proximity Switch.vi could be used to select the Proximity Switch applicable to wash cycle Child Class ( in this case Under Body Wash), for a different wash cycle we might wish to over-ride selection again within Get Proximity Switch to select proximity switch applicable to the new wash cycle Child class.

Statically Dispatched VIs

Statically dispatched vis are inherited by child classes but they are not over-ridden. These are merely subvi that classes use to perform functionalities. They are used ‘as-is’. Rule of thumb is to create a Statically Dispatched VI if it is not a simple vi to access private data – Data Member Access VI – or a method that is required to be over-ridden by child classes – Dynamically Dispatched Method VIs. Consequently, any Initialiazation vi could be created as Statically Dispatched VI. Any algorithm or calculation vi could be created as Statically Dispatched VI.

Parent and Child Classes

I would like to briefly speak about parent and child classes in labview. Why should we create class in first place. When is the right time to create a class when we can live without creating classes?

Classes are excellent ways to protect application data as I explained above. Classes also are neat form of coding and code diagram looks a lot more pleasant. Classes save time as all child classes leverage from parent class and labview automatically creates dynamically dispatched vis. Classes provide repeatability, reusability, readability, reconfigurability. The benefits are unlimited.

Classes are to be created when we have identified in our application that different steps in process are identical and transition from one step to another require some conditions to be met. Car wash controller in above example was a perfect candidate for Classes. All wash steps were cycles and each cycle required car to be parked at corresponding stations for wash i.e Status of Car Wash Proximity Sensor. We were sure that all car wash steps could be designed by having a class ‘Cycle’ and passing status value of Car Wash Proximity Sensor for derived Child classes. Accordingly, we created Cycle class as parent and all car wash steps as child classes.

Another perfect candidate for Object oriented programming architecture is Automated Testing of Unit Under Test UUT. A UUT might be subjected to various different tests i.e an electronic device undergoes voltage, current, temperature, radiation, discharge, emission, CE marking and many different functionality tests based on type of UUT. We could simply create a class ‘Test’ and pass parameters as Dynamically Dispatched VIs.

In this article, I briefly introduced Labview Object Oriented Programming, Encapsulation and inheritance. These advanced architectures greatly improve code reliability, data protection and code readability. I hope discussion has been useful but feel free to drop a line if I could be of assistance.