NI Home
Cart Cart | Help
Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Hands-On Exercise: Introduction to LabVIEW

VERSION 10 Published

Created on: Apr 24, 2008 10:31 AM by NI FIRST - Last Modified:  May 27, 2008 1:54 PM by NI FIRST

Overview:

The National Instruments LabVIEW graphical development environment helps create flexible and scalable design and control applications for your robot. With LabVIEW, you can:

1. Easily interface with input and output signals

2. Analyze data for meaningful information and advanced control

3. View and interpret results through intuitive displays and user interfaces.

In the field of robotic design, the LabVIEW graphical development environment provides a platform for dynamic system simulation and control design, real-time vision processing, and deployment to various execution targets, including your CompactRIO FRC controller.

The following exercise will provide you with an introduction to the tools and capabilities of LabVIEW. You will gain a general understanding of virtual instrumentation and how to implement code in LabVIEW. Skills include designing a user interface, using loop structures, and simple decision making in code.

Exercise 1: Introduction to LabVIEW

Expected Time to Complete: 10 minutes

Level of Difficulty: Beginner

Description:

LabVIEW programs are called virtual instruments, or VIs. In LabVIEW, you build a user interface, or front panel, with controls and indicators. After you build the user interface, you add code using VIs and structures to control the front panel objects. The block diagram contains this code.

Goal:

In the following exercise, you will build a VI that will add or subtract two numbers, depending on the user specification, and display the result. This exercise does not use any hardware.

Implementation:

1) Launch LabVIEW

  • Go to *Start*>>*Programs*>>National Instruments LabVIEW 8.5.

2) Open a New VI

  • In the Getting Started Window, select Blank VI.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1128/Picture+2.JPG
3) Add a Control to the Front Panel

  • Right-click on the blank space of the front panel to display the Controls palette.
  • Move the cursor over icons on the Controls palette to locate the Numeric Controls palette. Click the Knob control on the Numeric Controls palette.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1129/Picture+3.JPG
  • When you place the knob control on the front panel, its Label is highlighted. Name the numeric control Input A.

4) Repeat Step 3, this time, selecting a Vertical Pointer Slide. Name this control I+nput B+.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1130/Picture+4.JPG
You will need to display the result of adding or subtracting these two numbers.

5) Place a Numeric Indicator on the Front Panel.

  • Right-click to bring up the Controls Palette. Select Meter from the Numeric Controls palette and name it Result.

6) Change the Scaling of a Front Panel Object

  • Double-click the 0 on the indicator and change it to -10.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1131/Picture+5.JPG
You will need to specify whether to add or subtract Input A and Input B.

7) Add a Boolean Control to the Front Panel

  • Right-click and navigate to the Boolean Controls palette. Select a Vertical Toggle Switch.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1132/Picture+6.JPG

You can easily modify the appearance of front panel objects using the Positioning Tool, as shown below.

8) Change the size of a Front Panel object.


  • Hover over the Vertical Toggle Switch until the Positioning Tool appears.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1133/Picture+7.JPG
  • Click on the Vertical Toggle Switch to select the front panel object. Drag the double-arrow-Positioning Tool to resize the Vertical Toggle Switch.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1134/Picture+8.JPG
9) Change the labeling of a Front Panel Object.

  • Right-click the Vertical Toggle Switch. In the fly-out menu, select Visible Items and uncheck Label.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1135/Picture+9.JPG
  • Create a floating label for the Vertical Toggle Switch by double-clicking anywhere on the Front Panel. Type Add and position this label at the top-right of the Vertical Toggle Switch. Repeat this step to place a Subtract label at the bottom-left of the Vertical Toggle Switch.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1136/Picture+9.JPG
10) Increase the font of the Front Panel object labels.

  • Select text using the Positioning Tool and Press <Ctrl+=> to incrementally increase font size.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1139/Picture+10.JPG
11) Continue to use the Positioning Tool to edit and position the front panel objects. Your front panel should look something like that seen below.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1140/Picture+10.JPG
12) Switch to the block diagram window

  • Go to Window>>Show Block Diagram.

You will need a method of selecting between adding and subtracting Input A and Input B. You will use the Boolean Control to specify this in your code.

13) Place a Case Structure on the Block Diagram

  • Right Click on the block diagram to reveal the Functions Palette. Hover the mouse to reveal the Structures Palette and select the Case Structure.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1141/Picture+10.JPG
  • Click and drag the cursor to place the Case Structure to left of the Boolean control on the block diagram.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1142/Picture+10.JPG
In the Case Structure, only one subdiagram is visible at a time and the structure executes only one case at a time; an input value determines which subdiagram executes. The Case structure is similar to switch statements or if…then…else statements in text-based programming languages.

14) If the Boolean control is True, your code should add Input A and Input B. If False, your code should subtract Input B from Input A.

  • Hover the cursor over the terminal of the Boolean control until the Wiring Tool appears. Wire the Boolean control to the input of the Case structure.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1143/Picture+10.JPG
  • Place an Add function from the Numeric palette in the Function palette in the True case subdiagram of the Case Structure.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1144/Picture+10.JPG
  • Wire the values from Input A and Input B through the left-hand side of the Case Structure to the inputs of the Add function.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1145/Picture+10.JPG
  • Wire the output of the Add function to the right-hand side of the Case structure.
  • Click the case selector label at the top of the Case structure to show the False case subdiagram.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1146/Picture+10.JPG
  • Place a Subtract function in the False case subdiagram. Wire the values from Input A and Input B through the left-hand side of the Case Structure to the inputs of the Subtract function.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1150/Picture.JPG
  • Wire the output terminal from the Case structure to Result. Your block diagram should look something like as follows:
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1151/Picture.JPG
As you change the value of Input A and Input B, you want Result to update continuously.

Similar to a Do Loop or a Repeat-Until Loop in text-based programming languages, a While Loop executes a subdiagram until a condition occurs. Specifically, a While Loop executes the subdiagram until the conditional terminal receives a specific Boolean value. You will use a While Loop to update Result until the user specifies for the program to stop.

15) Repeat the add/subtract algorithm until the user selects to stop execution.


  • Place a While Loop from the Structure Palette around the block diagram logic.

http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1154/Picture.JPG
  • Right-click the terminal on the Stop Condition in the bottom right corner of the While Loop and select Create Control.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1155/Pic.JPG
  • Your block diagram should now look as follows:
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1156/Pic.JPG
Testing

1) Display the front panel


  • Switch to the front panel by pressing <Ctrl+e>. The front panel should now have the Stop button you created for your While Loop.

2) Run your VI and observe the program functionality.

  • Click the Run button at the top of the screen. The arrow will change to indicate that the VI is running.
http://decibel.ni.com/content/servlet/JiveServlet/downloadImage/1157/Picture.JPG
  • Change the values of the front panel numeric controls and toggle the Boolean switch to change the behavior of the program.

  • Observe the Result shown in the Meter indicator.

  • Click stop to end the program.

  • Close the VI when finished and do not save any files.

Average User Rating
(0 ratings)




Apr 25, 2008 4:19 AM Click to view Rob Steele's profile Rob Steele says:

Very nice beginning example program.

Apr 25, 2008 2:10 PM Click to view Billbo911's profile Billbo911 says:

Nice, easy and a great intro. We need more!!! Bring it on, please.

Apr 25, 2008 9:40 PM Click to view eek's profile eek says: in response to: Billbo911

Glad to hear you liked this tutorial! I helped put this together when NI originally offered this as part of our Hands-On Test-Drive stations at the FRC 2008 Championship. We are working to provide additional tutorials as soon as we can crank them out.
Keep in mind, there's plenty of tutorials to help get you started with LabVIEW programming in general. Just take a look at LabVIEW's Getting Started splash screen (that's what pops up when you initially launch LabVIEW). Click on anything under New to LabVIEW? and you'll keep yourself busy for a long time. And there's even more content on ni.com.
However, we're working hard to provide you with tutorials that are catered to the FIRST community. LabVIEW can do a lot of things and so we'd like to at least take a shot at narrowing them down to the topics we believe you will be most interested in, like controlling a robot.
So stay tuned, as these tutorials are already in development and on their way. We look forward to getting your thoughts in the future.