LabVIEWSubVI
From GT RoboJackets
Contents |
Introduction
Similar to most other programing languages, LabVIEW VIs can be used as sub-functions in other VIs. These sub-functions are called sub-VIs and this tutorial will cover how to make and and use them.
Basics of a Sub-VI
There is nothing inherently special about a VI that makes it a sub-VI. Rather a VI can be called a sub-VI if its inside another VI. Below is an example of three sub-VIs: Encoder, TMG/PMG, and Timer inside a while loop in a larger VI. Each sub-VI is itself a VI with the same file extension of any other VI and could be ran independently. The only difference is that these sub-VIs have their outputs and inputs mapped to the connector pane. Later it will be shown what the connector pain is and how to take front panel inputs and outputs and add them to it.
Front Panel Components
LabVIEW VIs contain two key elements: the front panel and the block diagram. The block diagram is where all the implementation of a VI is done and the front panel is our sort of "window" into the internal workings defined in the block diagram. In terms of sub-VIs it is also where we will define the inputs and outputs of the VI. Take a look at the front panel shown below. On this panel there are several inputs and outputs. Some are numerical like lenPerRotation, while others like A are Boolean. There are also outputs like Pos (it should be noted that this VI is for reading an encoder.)
Lets start with a basic VI that runs a loop and increments a number until it reaches some value we will call maxValue. For now a constant will be used to define maxValue. Finally the loop will be set to run infinitely. Below is the block diagram of the simple VI.
This simple VI could be run but there would be no indication as to what its doing. This issue can be solved by adding some simple items to the front panel. A numeric control will be added to give the user control of maxValue, while a Boolean square LED is added to provide output.
Now a simple VI has been constructed and it will be used in the next section. Do take not of the fact it will run infinitely.
The Connector Pane
The connector pane is how front panel components are connected as outputs and inputs to a VI. On the upper right hand corner of the front panel view is the icon representing the VI (look at the icon circled in the graphic below.) Right clicking on this icon brings up a menu with the option 'Show Connector' Clicking on this menu item brings up the connector pane for the VI.
Up to 28 inputs/outputs can be connected to a connector pane. The arrangement of number of these inputs/outputs can be changed by right clicking on the connector pane and either going to add terminal, remove terminal, or patterns. See the graphic below for more detail.
Now to add the input and output created earlier to the connector pane simply click on the terminal in the pane where you'd like to add the input or output and then on the input or output itself.
Once all the inputs and outputs have been added to the connector pane in that manner you are done. That was easy!
Adding SubVIs to your project
Adding sub-VIs is even easier than creating them. In fact you've already done it! As mentioned earlier sub-VIs are just VIs used on a larger VI. Take the VI below.
To add the VI created earlier simply right click anywhere in the block diagram to bring up the functions menu, then select 'select VI', and finally select the VI you'd like to add and place it on the block diagram.
From here its a simple as connected lines to the input and output. Simple right!
Well not just yet. Remember how the VI was set to run infinitely? When this same VI is ran in a larger VI it will run infinitely with in the larger VI never exiting. One way to get around this is to make the VI run for one cycle by connecting a 'False' constant to the stop icon in the loop. This will mean that each time the main loop will run the sub-VI will also run once.
Conclusion
Sub-VIs are just regular VIs used in another VI and are virtually the same as any other VI except for the fact that inputs and outputs have been added to their connector pane.
Using sub-VIs is just like using any other module in LabVIEW.






