8. Introduction to Programming with LabVIEW

LabVIEW programs are represented by files called VIs, which stands for Virtual Instrument. A VI may be the main VI that starts a program, or it may be a sub-VI that is called by another VI. Regardless, all VIs have two parts: a Front Panel and a Block Diagram.

Front Panel

This is the window that users will see when they run the program. It includes controls where the user operate the program or change its parameters. It also includes indicators to display the outputs from the VI. If users might actually view this front panel, then controls and indicators can be graphical in nature. For the sub-VIs where users will not typically view the front panel, the controls and indicators are usually simple values.

Block Diagram

The logic of the VI is shown in this window. Icons on the diagram describe operations to perform on the data. Wires show how the data flows from one icon on the screen to another. Data flows from the controls on left to the indicators on the right.

Note

If only the Front Panel is shown, press <cntrl-e> to open the block diagram.

8.1. Temperature Conversion

Here is a simple desktop LabVIEW program to help learn the basics. This program will only run on a regular computer, but future programs will have components that will run our robot as well as the computer.

First create a blank LabVIEW project. When we write programs that are downloaded to the robot, it will be important that we always work from a project in LabVIEW rather than just opening individual VIs.

Right click in the front panel to pull up the controls window and under the numeric category, add two thermometers to the front panel.

../../_images/Therm_control.png

Hold the mouse over the Celsius Thermometer and click the right mouse button.

../../_images/control_properties.png

Select to change it from on indicator to a control. Select properties and change the scale to -30 to 50. Also change the scale of the Fahrenheit thermometer. You may want to make them larger also.

Now look at the block diagram. Right click anywhere in the block diagram to pull up the available functions. Under the Numeric category, select the icons to convert a temperature in Celsius to the Fahrenheit scale. Numbers are entered by right clicking over an input terminal and selecting Create Constant. Wires are created by left clicking at a source terminal and again at a destination terminal.

../../_images/numeric.png

You can run the program now. In the front panel, move the Celsius control to a desired value and click on the run arrow.

This program only does one calculation and the stops. If we want it be an interactive program, we can put a while loop around the code (Programming -> Structures). We also need to add timing to the loop so that it does not take all of the CPU’s time (Programming -> Timing). We can also add a stop button to stop the program (from the front panel under Boolean control).

../../_images/therm1.png

Note

Using a timed while loop can be called polling. It is not the best solution for computer applications. There are event based solutions that significantly reduce the load on the computer. However, this is a start and the event based solution does not work on robots.

The wait time for each loop iteration is critical. Too short of a wait will put a heavy load on the computer, while too long of a wait will make the program seem sluggish to the user. Times between 100 to 500 ms seem to work well.

8.2. Make A SubVI

Let’s make the calculation part of the VI a SubVI. SubVIs can make a VI look neater by hiding some of the details and allowing reuse of code. Think of a subVI as a function in text based programming languages.

Drag the mouse pointer over the equation to select the desired SubVI part. Select Create SubVI under the Edit pull-down menu.

../../_images/create_SubVI.png

LabVIEW gives SubVIs a standard look. Let’s put a name on the icon so it shows what it does. Double click on the SubVI icon to edit it’s front panel. double click the icon shown in the upper right of the front panel to pull up the icon editor. You might give it a light color background and type a name to display.

../../_images/SubVI_icon.png

You might also want to change the terminals where input and output connect to the SubVI. Here is a video showing how this is done

../../_images/SubVITerms.png

8.3. Switch Statements

Just for fun, let’s add some Boolean lights to the front panel and some logic to the block diagram to interpret the tempature in terms of how comfortable it is. From the front panel, select the round LED from the Boolean Controls. Add five Boolean indicators to the front panel and program the block diagram to always light one and only one. Comparison and boolean operators are available to help. The case structure might also be helpful. Notice that with the case structure, it is necessary to program both the True and False conditions and that boolean constants are needed.

../../_images/therm2.png
../../_images/therm3.png