+ All Categories
Home > Documents > September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let...

September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let...

Date post: 04-Jun-2018
Category:
Upload: vuongthien
View: 214 times
Download: 0 times
Share this document with a friend
14
© 2009 National Technology and Science Press Let There Be Light 2 Better to light a candle than to curse the darkness.—Chinese Proverb The Hello World program demonstrates only the output aspect of a computer pro- gram. Now let’s write a program that has all three parts—input, process, and output. We can start with the Hello World program to save some time. If you already have LabVIEW running, close any files you have open and return to the Getting Started screen. Otherwise, launch LabVIEW and wait for the Getting Started screen, as shown in Figure 2.1. Opening Files In the Open area of the screen you should see the Hello World.vi file. Click on it, and the program will open. Only the Front Panel window of an existing file opens, because that is normally the program’s user interface. Nobody usually cares about the details of the underlying Block Diagram. Of course this is not true for NXT programs, and you’ll need to open the Block Diagram manually. In the Window dropdown menu pick Show Block Diagram, as shown in Figure 2.2. Figure 2.3 shows the While Loop with the Display Text Function Block that repre- sents the core of the Hello World program. The program only displays the same thing over and over again, which is pretty boring. Why don’t we display something more interesting by showing the intensity of light as measured by the NXT Light Sensor? Deleting First we need to delete the String Constant block. Locate the cursor at a point to the left and above the block and, while holding the left mouse button down, drag a selection box over the whole block and some of its wire, as shown in Figure 2.4. Don’t include 17
Transcript
Page 1: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Let There Be Light

2Better to light a candle than to curse the darkness.—Chinese Proverb

The Hello World program demonstrates only the output aspect of a computer pro-gram. Now let’s write a program that has all three parts—input, process, and output.We can start with the Hello World program to save some time. If you already haveLabVIEW running, close any files you have open and return to the Getting Startedscreen. Otherwise, launch LabVIEW and wait for the Getting Started screen, as shownin Figure 2.1.

Opening FilesIn the Open area of the screen you should see the Hello World.vi file. Click on it, andthe program will open. Only the Front Panel window of an existing file opens, becausethat is normally the program’s user interface. Nobody usually cares about the detailsof the underlying Block Diagram. Of course this is not true for NXT programs, andyou’ll need to open the Block Diagram manually. In the Window dropdown menupick Show Block Diagram, as shown in Figure 2.2.

Figure 2.3 shows the While Loop with the Display Text Function Block that repre-sents the core of the Hello World program. The program only displays the same thingover and over again, which is pretty boring. Why don’t we display something moreinteresting by showing the intensity of light as measured by the NXT Light Sensor?

DeletingFirst we need to delete the String Constant block. Locate the cursor at a point to the leftand above the block and, while holding the left mouse button down, drag a selectionbox over the whole block and some of its wire, as shown in Figure 2.4. Don’t include

17

Page 2: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

18 LabVIEW for LEGO MINDSTORM

Figure 2.1Getting Started Screen

Figure 2.2Showing the Block Diagram

Figure 2.3Hello World Program

Page 3: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 19

Figure 2.4Selecting the Constant String Block and Wire

Figure 2.5Selected Constant String and Wire

any of the Display Text block. When you let up on the button, the screen should looklike Figure 2.5. You only need to hit the Delete key on the keyboard to delete both theblock and the wire.

The Light SensorConnect the NXT Light Sensor to the NXT Sensor Input Port 3 with a cable, as shownin Figure 2.6. The NXT Light Sensor has a built-in red LED light source that can beused to reflect light off nearby objects to sense their presence or color. The sensorcan’t really detect different colors; it can only detect differences in how much light

Figure 2.6NXT Light Sensor Connected to NXT Sensor Input Port 3

Page 4: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

20 LabVIEW for LEGO MINDSTORM

reflects off an object. Closer objects are brighter than further ones, light-color objectsare brighter than dark ones, and redder objects will seem brighter than blue ones.The LED can be turned off so that the sensor measures only the ambient light, like aphotographic light meter.

The Light Sensor function block is located in the NXT Library Input palette, asillustrated in Figure 2.7. There are blocks for the other NXT Sensors as well as LegacySensors from the old LEGO MINDSTORMS RCX family.

All of the connections for the block are shown in Figure 2.8. You can see a connectionfor the Input Port Number, and you can determine whether to turn on the built-inLED with the Generate Light connection on the left. The default Input Port Numberis 3, and you don’t need to supply an input to this connection as long as you have thesensor plugged into Port 3. The default condition of the Generate Light is True, whichmeans the LED will be lit while the program is running. The measured light level isthe Intensity output connection on the right, and it will have Integer values from 0 to100. Notice the wire color for Integer is blue.

Place the Light Sensor block in the While Loop, as shown in Figure 2.9, with somespace between them. You can’t directly connect the two blocks together, because theLight Sensor Intensity output is an Integer number and the Text Input to the DisplayText Block is a String. We will need another function block to do the conversion.

Figure 2.7Input Palette

Figure 2.8Light Sensor Block Connections

Page 5: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 21

Figure 2.9Place Light Sensor Block inside While Loop

Converting Numbers to StringsLook in the String palette for the String/Number Conversion subpalette. It will looklike Figure 2.10. There are blocks to convert Numbers to Decimal Strings and DecimalStrings to Numbers. The Decimal part of the name just means that the numbers arein base ten.

Pick the Number-to-Decima-String block and move it onto the Block Diagram.As you move the block around between the Light Sensor and Display Text blocks,LabVIEW might try to automatically wire connections. Be careful to place the conver-sion block only when the connection from the String output of the block is wired tothe Text input of the display block (see Figure 2.11). Don’t panic if the wire did notautomatically connect. I’ll show you how to manually wire blocks next.

Figure 2.10String/Number Conversion Palette

Figure 2.11Conversion Block Automatically Wired

Page 6: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

22 LabVIEW for LEGO MINDSTORM

Manually Connecting WiresMove the cursor around the Intensity output of the Light Sensor until it becomes theConnect Wire tool. Hold the left mouse button down and sweep up until the ConnectWire tool is over the Number input of the conversion block, as shown in Figure 2.12.Make sure you have not accidentally wired to the width input of the conversion block,as shown in Figure 2.13. If you did, move the Connect Wire tool near the wire andthen hold the right mouse button to select Delete Wire Branch. When you are done,the three blocks will look like Figure 2.14.

This would be a good time to save the program with a new descriptive file name,such as Light Meter. When you are done with that, go to the NXT Terminal (see Figure2.15) and Compile, Download, and Run the program.

Figure 2.16 shows what the NXT display should look like, except that the numberwill be different depending on your light level. The Display Text block is not erasingthe display for each new number. It is only writing the new number over the old one.This is not a problem unless the number becomes 100. The units place “zero” will beleft on the display even when the light level drops below 100. A cure for this wouldbe to wire a True Constant block to the Clear Display input of the Display Text block.

The language that comes with the LEGO MINDSTORMS NXT is called NXT-G.Figure 2.17 shows the Light Meter programmed in NXT-G. You can see the similarity

Figure 2.12Wiring the Intensity Output to the Number Input

Figure 2.13Delete the Wire If You AccidentallyConnect It to the Width Input

Figure 2.14The Proper Wires

Figure 2.15Compile, Download, and Run

Page 7: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 23

Figure 2.16NXT Display for the Light Meter

Figure 2.17The Equivalent NXT-G Program

between this and your LabVIEW program. Now I’ll give you a little taste of somethingyou can’t do in NXT-G.

DebuggingDebugging is the art of fixing problems with computer programs. Programs runningon the NXT are particularly difficult to debug because the NXT has only a simpledisplay to monitor values with. If you have ever written a complex NXT-G program,

Page 8: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

24 LabVIEW for LEGO MINDSTORM

you’ll know what I’m talking about. On the other hand, LabVIEW provides severalhandy tools for debugging using your computer.

ControlsMove the cursor over the Light Sensor block near the Generate Light input. Whenthe cursor becomes the Connect Wire tool (see Figure 2.18), hold down on the rightmouse button and the menu shown in Figure 2.19 will appear. Select Create and thenControl. A gray box will emerge that is already wired to the Generate Light input.Your program should now look like Figure 2.20.

Figure 2.18Connect Wire Tool Next to Generate Light Input

Figure 2.19Adding a Control

Figure 2.20Control Added to the Program

Page 9: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 25

Figure 2.21Pushbutton Control

Switch windows to the Front Panel (see Figure 2.21) and you will find that LabVIEWhas automatically installed a pushbutton on it. Controls live on the Front Panel justas Function Blocks live on the Block Diagram. Pushbuttons are a Boolean type ofinput Control. They allow you to change the values of variables while the program isrunning. In this case, the state of the Generate Light input will be controlled by thispushbutton. There is even a simulated green LED to tell you the state of the button.

IndicatorsOn the Block Diagram, move the Connect Wire tool next to the wire connecting theIntensity and the Number input, as shown in Figure 2.22. Hold down the right mousebutton (see Figure 2.23) and select Create and then Indicator. Another gray box willappear that is connected to the wire, as shown in Figure 2.24. The little ball at theintersection of the wires tells you that there is a connection; they are not just wiresthat happen to cross each other.

When you look at the Front Panel again, there will be a Numeric Indicator likethat in Figure 2.25 on it. You can move it around if you want to align it with the

Figure 2.22Connect Wire Tool Near Wire

Page 10: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

26 LabVIEW for LEGO MINDSTORM

Figure 2.23Create Indicator Menu

Figure 2.24Control Added to the Program

Figure 2.25Moving the Numeric Indicator Control

Page 11: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 27

pushbutton so it looks like Figure 2.26. This Front Panel is like a window into theNXT for debugging.

Instead of using the Compile, Download, and Run button on the NXT Terminal, usethe Debug button as shown in Figure 2.27. Just as before, it will take a little while for thewhole operation. When the NXT starts to execute the program, the Front Panel, shownin Figure 2.28, will show the same Intensity value as the NXT display, as illustratedby Figure 2.29.

Figure 2.26Final Debug Front Panel

Figure 2.27Start Debugging

Figure 2.28Front Panel While NXT Program Is Running

Page 12: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

28 LabVIEW for LEGO MINDSTORM

Figure 2.29Stop the Program with the NXT Stop Button

Now click on the Generate Light pushbutton on the Front Panel with your mouseand the LED on the Light Sensor will go on and off. I know this isn’t rocket science,but still that’s pretty cool. There are many other Control types than can be used tochange values in your program. You stop the program by pressing the Stop but-ton on the NXT or the Stop button in the NXT Terminal, which is to the right ofDebug.

ChartsOn the Front Panel select the Intensity Numeric Indicator and then right-click to selectReplace from the menu that appears. Replace will bring up a Tools palette (see Figure2.30) from which you should select Graph Indicators and then Chart. A large graph-like display will replace the simple numeric one. In the NXT Terminal click the Debugbutton again. Now the Front Panel will display a graph of the light value while theprogram is running. I made the example shown in Figure 2.31 by passing my fingersin front of the Light Sensor with the LED turned on.

Page 13: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

Chapter 2 • Let There Be Light 29

Figure 2.30Replacing the Numeric Indicator with a Chart

Figure 2.31Front Panel with Chart

Page 14: September 22, 2008 10:21 Gasperi Let There Be Light€¦ · September 22, 2008 10:21 Gasperi Let There Be Light 2 ... can’t really detect different colors; ... September 22, 2008

© 2009 National Technology and Science Press

September 22, 2008 10:21 Gasperi

30 LabVIEW for LEGO MINDSTORM

ReflectionsIn this chapter you have seen how to open existing programs, write programs thatget input from sensors, and debug programs. I hope you were particularly impressedwith debugging. We will be using it for a lot more than fixing programs. Now I’llshow you how to get things moving with the NXT motors.


Recommended