New domain and blog

New domain and blog
Please head over to my new domain to view my blog and current projects

Tuesday, 10 December 2013

LabVIEW: Getting started with LVOOP

I have been trying to get a handle on LVOOP for many months now but have been battling to get the concept into my head. I understand the concept of OOP, or at least I think I do, but really battling to convert that into the LabVIEW world.

Here is a list of resources that I used to try and figure it out:

There have been other articles that I have used, but none that I could find started from a blank project to build a basic application. 

So I set about to try write one from scratch. I used the example from the video of a Mammal Parent class with a few Child classes. Here is the UML diagram of what I had planned. I found a great online tool to create the UML diagrams. Have a look here.


As can be seen from the above diagram, I have a project with five classes, each has Dynamically Dispatched vi’s and one has vi’s for Data Member Access.

So that is the idea of my simple program, here is a step-by-step list of what I did:

Create a blank project and add five virtual folders. Add folders with the same names to the project directory on disk.

Create main.vi which will be your application.


Create the Mammal.lvclass inside the Mammal folder.


Save the class in the Mammel folder on the disk. 

Next you need to create the methods for Move() and Speak() using the Dynamic Dispatch Template.


Add a blank string indicator and wire it to the connector pane. Do this for Move.vi and Speak.vi.


You have now created the Parent class with two methods that can be overridden by the Child classes.

The next step is to create the Dog, Cat and Human classes that will inherit from Mammal class and the Baby Human class that will inherit from the Human class.

Dog, Cat, Human classes:

Create the new classes and leave the control blank. Save the classes to their folders that have been created on the disk. Then edit the Inheritance settings in the class properties dialog. Each class needs to inherit from Mammal.lvclass.


Create a ‘VI for Override’ vi for Move and Speak. Edit the block diagram for each to look like this. In total three extra Speak and three extra Move vi’s should be created. One each for the Dog, Cat and Human classes.



The string constant in the No Error case gets changed for each method. This is the string that will be displayed in the main application.

Baby Human class:

Create a new class called Baby Human.lvclass and set it to inherit from Human.lvclass.

Open the ‘Baby Human.ctl’ control and add two strings to the cluster. Name them Movement and Voice. By adding controls here, we will be able to change the string that gets displayed at runtime. (Note that the Movement and Voice was hard coded in Dog, Cat and Human)


Next you need to make four vi’s for Data Member Access. These vi’s will be public and used to read and write the private data.


Select ‘Read and Write’ for Movement and Voice. Edit the icon for each if you want and then save the created vi’s.

Next you need to create the vi’s that override the Human Move.vi and Speak.vi. Follow the steps above to create ‘VI for Override’. They should look like this.



All of the above steps should set up the classes that are needed to write main.vi. I am going to write a simple application which creates an object of each class, builds an array and then displays each Move and Speak value in a For Loop. When we add the Baby Human class, we need to Set the values for Movement and Voice before we bundle the array.

For each object that gets passed into the For Loop, the associated Move and Speak method will be dynamically called. 



If you change the Movement and Voice of the Baby Human class on the front panel, that change will be displayed when the application is run. 

After going through this exercise I think I am starting to get a better understanding of LVOOP. I have a very long way still to go, but I now feel that I am at least on the pathway. I hope this has made sense and is able to help someone getting started. 

If I made mistakes, please leave a comment so that I can correct it. You can get a copy of my project here.

Greg

Thursday, 5 December 2013

LabVIEW: Creating Custom Templates and Sample Projects

The new ‘Create Project’ feature added to LabVIEW 2012 is great and I use it a lot when starting a program. However, I found myself making common changes to the templates before I started writing a program. The main changes were removing the ‘Something’ buttons and events. This isn’t a lot of work but does take time and becomes rather tedious.

One solution is to use the Create Project feature with a custom template. It is rather easy to set up and works really well after getting over a few minor hurdles.

I started off with creating a new QMH project from the default template. I made a few changes to the project and then saved the project how I wanted the template to work.


Some of the changes that I made were:
  • Removing the Something and Something Else buttons, events and cases
  • Changed the queue message from a String to a Type Def Enum. This makes the Consumer case a lot easier to set up and eliminates spelling mistakes
  • Added a case to read in a configuration XML file
  • Replaced constants with variable constants
  • Remove the Stop button to use the ‘X’ to stop the program

Once you have the template set up, save the project and close it. Navigate to the ‘LabVIEW Data’ folder in ‘My Documents’. Inside this folder, create new folders with the following structure. 
--ProjectTemplates
      |--CategoryOverides
      |--MetaData
      |--Source

Place your template into the Source folder. Make sure that Auto Populating Folders is DISABLED in the .lvproj. This caused me a lot of troubles getting set up.

The next thing to do is create an xml file in the MetaData folder. Call this file TemplateMetaData.xml. I used the default xml file as a template to get started. This is what my file looks like:

To get an explanation on the different keywords, have a look at this white paper from NI.
Once you have the xml file saved in the MetaData folder and the template project in the Source folder, you should be ready to go.



As can be seen from above, my custom project is now added to the Create Project wizard. Just follow the normal wizard to create a new project and get programming right away.

The main problem that I ran into was setting my folders to auto populate in the template. Once I switched that off everything ran smoothly.

Have fun creating new templates and projects.

Greg