Lego mindstorms nxt-g programming guide pdf

Date published: 


By combining the power of the LEGO building system with the LEGO MINDSTORMS Education technology, teams of students can design, build, program, and. DOWNLOAD PDF Master the NXT-G language for programming LEGO MINDSTORMS robots. LEGO MINDSTORMS NXT-G Programming Guide, 2nd Edition is the perfect reference for NXT-G, and is now fully updated to cover the very. Power up your mind: learn faster, work smarter / Bill Lucas. p. cm. ways in which you can power up your mind and impr Load more similar PDF files.

Language: English, Spanish, Japanese
Country: Iceland
Genre: Art
Pages: 359
Published (Last): 25.03.2016
ISBN: 249-2-67813-184-3
PDF File Size: 18.11 MB
Distribution: Free* [*Regsitration Required]
Uploaded by: DACIA

9343 downloads 30945 Views 24.78MB ePub Size Report

LEGO MINDSTORMS NXT-G Programming Guide is suitable for young programmers, age 10 and up, as well as parents and teachers Download book PDF. LEGO MINDSTORMS NXT-G Programming Guide, Second Edition. Copyright © by James Floyd For the LEGO MINDSTORMS team, past and present. James Kelly's LEGO MINDSTORMS NXT-G Programming Guide, Second Edition DRM-free; Included format: PDF; ebooks can be used on all reading devices.

This second edition is fully-updated to cover all the latest features and parts in the NXT 2. It also includes exercises at the end of each chapter and other content suggestions from educators and other readers of the first edition. Readers 10 years old and up learn to apply NXT-G to real-life problems such as moving and turning, locating objects based upon their color, making decisions, and much more. Perfect for for those who are new to programming, the book covers the language, the underlying mathematics, and explains how to calibrate and adjust robots for best execution of their programming. Please note:

Show all. Table of contents 26 chapters Table of contents 26 chapters Robots and Programs Pages Program Structure Pages Hello World!

Pages Record and Playback Pages Make Some Noise! True or False? Feedback Pages Wait for It! Round and Round Pages Decisions, Decisions Pages Stop It! Pick a Card, Any Card Pages Apples and Oranges Pages Inside or Out? Basic Text Pages Basic Math Pages Staying Alive Pages One final thing I want to mention about pseudo-code is that each instruction you give the robot should be as simple as possible.

Take a look at the next two examples and tell me which one has the simpler instructions: SPOT, move forward about 10 inches; turn left 90 degrees, and start moving forward; then start looking for a black object with your Ultrasonic sensor, because I want you to stop when you find a black object; then turn right 90 degrees, and move backward 2 feet, OK?

SPOT, move forward 10 inches and stop. Now turn left 90 degrees. Starting moving forward, and turn on your Ultrasonic sensor. Stop when you find a black object. Turn right 90 degrees and stop. Now move backwards 2 feet and stop.

Which example is less complicated to read? If you said Example 2, you are right. When writing pseudocode, break down your instructions into short and simple statements for your robot. This will make it easier for you to convert your pseudo-code to an actual NXT-G program.

Why are they called blocks? Those are blocks! The block on the left with gears is the MOVE block. You will use these blocks and many more to properly program your robot to follow your instructions. If you can remember one thing from this chapter, it should be this: Programming your robot will be much easier if you take the time to write down the pseudo-code.

If it helps, pretend your robot has ears, and tell it what you want it to do. Write down these instructions, and keep them short and simple. Exercises Now, I want to give you some practice in writing pseudo code. Ask a friend or teacher or parent to help you with the following two exercises. Possible solutions to the exercises in the book can be found at the end of each chapter. If possible, switch roles with your partner for both exercises.

This will help you write pseudo code as well as see how easy or hard it is to follow instructions. Exercise Space the objects with approximately two feet between them and label the object on the left Object 1 and label the object on the right Object 2.

Next, have your partner sit in front of the table, with one object to his left and the other object to his right. Write down a bit of pseudo-code and give it to your partner that will instruct him or her while seated to pick up Object 1 and move it to a location near Object 2.

Then have your partner pick up Object 2 and move it to the approximate original location of Object 1. Place Objects at Corners Ask your partner to stand in one corner of a room any corner is fine and call it Starting Point. Give your partner two objects labeled Object 1 and Object 2. Write pseudo-code that instructs your partner to move along the wall to his right and place Object 2 on the floor at the first corner he encounters.

Have your partner continue moving to the next corner not back to the Starting Point where he will place Object 1 on the floor. Have your partner reverse direction and return to the Starting Point. Were there any parts of your pseudo-code that confused your partner or vice versa? The more details, the more likely it will be that your robot succeeds in its endeavors. Exercise Solutions There is no single hard-and-fast solution to each of the exercises in this chapter.

Many solutions are possible. I present two such possible solutions here. Your own solutions may be different in some details, but overall they should be similar to the ones presented here. Exercise Following is one possible solution to the task of instructing your partner to move one object next to another: Pick up Object 1 to your left.

Place Object 1 to the right of Object 2. Pick up Object 2 on your right. Place Object 2 in the approximate original location of Object 1.

Exercise Following is a series of steps that should result in your partner dropping one object at each of two corners. Your own solution may vary, but it should be similar to that presented here. Walk along the wall to your right and stop at the first corner you encounter.

Place Object 2 on the floor. Without returning to the Starting Point, walk along the wall to the next corner and stop. Place Object 1 on the floor. Reverse your direction and, following the walls, return to the Starting Point. Let me now give some pseudo-code to SPOT: Click the Go button, and the HelloWorld program is open and ready. In some instances, the figures are from version 2. In most cases, the workspace, buttons, menus, and other tools are identical between versions.


In either case, when something is specific to either version 1. Start a new program called HelloWorld Figure shows the new program opened see the tab called HelloWorld in the upper left corner? Are you ready for this? The configuration panel is where you will be doing most of the programming work for your robots. The configuration panel allows you to turn on and off certain things as well as receive feedback. Image, Text, Drawing, or Reset. Click the dropdown menu, and select Text from the options listed.

Change the text to Hello World! Now, using the File menu, select Save, and use the Browse button to choose a location to save the file on your computer. Click the Save button when you are finished. Ideally, however, you should have worked through all the Robo Center or Robo Educator projects to familiarize yourself with this function.

Just get in a habit of saving often. Waiting for the Results After the program is uploaded, select it from the File section, and press the orange button also called the Enter button on the Brick to run the program. Did you see it? Why does this happen? The good news is that this is very easy to fix, so let me update the pseudo-code before I continue: The WAIT block does exactly what it says—it waits.

Now run the program. Sometimes, a section will not be visible until other options are selected. The Display section only has one configurable item—a Clear checkbox. If you leave the box unchecked, any text or graphics you configure the DISPLAY block to put on the LCD will display on the screen along with whatever is currently displayed, instead of replacing it. This is useful when you want text to appear on multiple lines; you can use multiple DISPLAY blocks to keep adding text to make sentences and even paragraphs.

With the Action section, you have four options in the drop-down menu: Image, Text, Drawing, and Reset. When you select Image in the drop-down menu, the File section is displayed; this section gives you access to a collection of small built-in pictures that can be displayed on the LCD screen see Figure Choose an image from the File section to place on the LCD.

By clicking and holding the image in the preview pane on the right side of the configuration panel, you can drag the image around the small pane and place it wherever you wish. The next option in the Action drop-down menu is Text. The third option in the Action drop-down menu is Drawing see Figure You can choose to draw a line or a circle or to place a single point on the LCD screen, so your artistic talents will be somewhat limited.

To create a detailed drawing, you would have to place dozens or more DISPLAY blocks one after the other, and the combination of lines, circles, and points would create the image. A better solution for owners of the 2. But the Drawing options are available to 1. The Drawing option can be used to place points, lines, and circles. To use the Drawing tool, select Point, Line, or Circle from the Type section this section only appears if you have selected Drawing in the Action drop-down menu.

For the point, you can drag it around the Preview pane and place it anywhere. You can also use the X and Y coordinates to place the point more accurately. For owners of the 1. Click anywhere in the Preview pane to draw a line from that point to the place where you clicked. You can change the end point 10,10 by entering new coordinates in the X and Y boxes. You can also type in X and Y coordinates for the other end of the line for more accurate control over it.

For 2. A line is added automatically—simply click anywhere in the Preview pane and the line will be redrawn with one of its ends terminating on the spot you clicked. Likewise, you can use the X and Y boxes to manually select a starting point for the line as well as use the End Point X and Y boxes to manually enter an ending point for the line.

Finally, for the circle, you have the option of changing the radius of the circle by typing the number in the Radius text box. Drag the circle around the Preview pane to place it properly. Use the X and Y boxes to manually enter the center point of the circle.

The final option in the Action drop-down menu, Reset, is useful when you would like to clear the LCD screen of any items. If you click the bottom-left edge of a block, this section will drop down and reveal the data hub see Figure Click the section again, and the data hub will close.

It might take some practice to find the correct place to click, so try it a few times until you get used to opening and closing the data hub. What is this data hub? The data hub allows you to draw data wires from one block to another using data plugs. Data wires and plugs will be covered in much more detail in Chapter 7, but for now, all you really need to know is that wires can connect blocks to share data.

Data plugs are places on the block where you will connect wires. So there will be a data plug on one block with a wire going out and another data plug on a different block with a wire coming in. Data wires can carry information such as text, numbers, and other values. Well, items such as the text displayed or the radius of a circle can all be configured without using the configuration panel.

Mindstorms guide lego pdf programming nxt-g

Remember that when you draw a circle you can specify the radius of the circle in the configuration panel? That last plug corresponds to the radius of a drawn circle hover your mouse pointer over a plug, and it will tell you what it is.

This can happen for many reasons. Before you begin using these data wires for more advanced programming, however, you need to understand the basics of the programming blocks. If you get stuck, the answers are at the end of this chapter.

For this program, I want you to enter one line of text near the top of the LCD screen, a picture your choice below the text, and have both items displayed for 25 seconds before the program ends.

When the square is completed, have the robot pause for 10 seconds before the program ends. Exercise Solutions Following are some possible exercise solutions.

Remember, your solutions may be somewhat different. Results are what matter. Very often, there is more than one way to get to the same end result. Exercise Figure shows the program for one possible solution. Figure also shows the configuration panel for the first of the three program blocks. Figures and show the panels for the other two blocks. If the Clear box remains checked, the text will be erased and only the image will be displayed. Exercise Figures through show the eight configuration panels for the eight blocks used in the program I wrote as my own solution to Exercise Figure shows the panel for the first block.

Figures through show the panels for subsequent blocks. Do you see how the coordinates are manually entered to make the lines all match up perfectly? Try to modify the coordinates to draw a perfect square. Any robot that you design that uses one or more motors will use the MOVE block.

The configuration panel will appear in the lower left corner of the screen see Figure You must plug motors into these ports in order for them to work properly. Motors have numerous options including how fast they spin Power and how long or far they spin Duration.

The Steering control has a small bar that can be dragged left or right. By default, it is midway between the selected motors B and C so there will be no tendency to turn in either direction. Moving Forward and Backward Before I move on, I want to bring to your attention the subject of motor spin direction.

It shows a motor in two different orientations. A MOVE block can configure a motor to spin in two different directions note the direction settings for each. All motors can spin forward and backward. When you program, you have to take into consideration the orientation of the motor. In Figure , the motor on the left has the up arrow selected in the Direction section on the MOVE block configuration panel. Be sure to keep this in mind when building and programming your bots.

As an example, look at Figure I always use Port A for my third motor. Pick a method for connecting your motors to your ports, and try to always use it. This can be helpful for troubleshooting and to remind you which motors will be used. Always remember to check your MOVE blocks and make certain that the ports that are selected are the motors you wish to be controlled by that particular block. Keep in mind that the Stop option is not permanent for your program.

It simply stops whatever motors are being controlled at the moment. Depending on its setting, you can configure a robot to move in a small or large circle or just spin in place.

Try it! Drag the slider all to the way to the left. Save your program, upload it to your robot, and run the program. Which direction did the robot spin? Drag the slider all the way to the right. Save, upload, and run the program again. Did the robot spin in the opposite direction? You can also program your robot to drive in a circle; the size of the circle depends on how far you drag the Steering control left or right: Go ahead and try this, too.

Drag the Steering slider to the left but not all the way. Upload the program, and run it. Did the robot move in a small or large circle? Try it again, but this time, move the Steering slider to a different location before you upload and run the program. Did the robot move in a smaller or larger circle? Power Settings Next on the configuration panel is the Power section see Figure The Power setting range is 0 to You can type a value into the Power text box or drag the sliding bar to the right to decrease power or to the left to increase power.

Most uses of the Power setting will involve increasing or decreasing the spin speed of a motor. But there is one additional consideration, and that is lifting or pushing strength. The motor will not spin as fast as it would if there were no resistance, but you may find that you need that extra power for the motor to successfully lift the object.

The same goes for pushing. Surface conditions also affect power; climbing a hill will take more power and possibly slow the robot. Also, whether a surface is smooth or rough can affect power; for example, you need more power to move over carpet than wood flooring.

Change the Power setting, and play around with the Steering slider. See how fast or slow you can program your robot to make a circle.

This will give you a better understanding of how the Power setting will affect your future bots. Do keep in mind, too, that the Power setting will also affect the life of your batteries. Speed comes at the sacrifice of battery power. When testing your robots, I recommend setting your MOVE block power values down to 20, 30, or 40 to conserve battery power.

But if you want your batteries to last longer, use a lower Power setting whenever possible. There are four options in the Duration drop-down menu: Unlimited, Degrees, Rotations, and Seconds see Figure From the Duration section, you can choose to have your motors spin forever by clicking the dropdown menu and choosing Unlimited. When the Duration is set to Unlimited, a single MOVE block will continue to spin its motors until the program ends or until you stop it.

If you set the MOVE block Duration to Degrees, you must enter a value in the text box for the number of degrees for the motor s to spin. The value must be 0 or greater; it cannot be negative, but this limitation is simple to fix. Just experiment with this concept and it will start to make sense. If the Duration is set to Rotations, the same rules apply. You cannot enter a negative value for rotations, but any value of 0 and higher is acceptable. One thing you can do with Rotations is use fractional or decimal values.

For example, you could configure a motor to spin 2. You may be wondering why you would ever want to configure a motor to spin 2. The last option in the Duration section is Seconds. When you choose this option, you must specify the number of seconds for the MOVE block to spin a motor or motors.

Like rotations, you can also specify fractional times such as 3. There are two options: Brake and Coast. Braking is useful if you need your robot to stop quickly and accurately at a specific point. However, keep in mind that this takes battery power. You can configure motors to brake or coast.

Then choose the Coast option.

LEGO MINDSTORMS NXT-G Programming Guide - Second Edition by James Floyd Kelly - PDF Drive

You definitely want to try this out! Now, when I upload and run this program, SPOT rapidly moves forward 10 rotations about 6 feet and comes to a quick stop. See Figure for the block programming of the pseudo-code. Program your robot to move forward for 10 rotations at a Power setting of 50, and set it to Brake. Now, run the program, and mark its stopping position with tape as well. Next, run the same program, but change the Brake option to Coast.

How far did it go beyond the previous stopping position? Finally, reduce the Power setting a little bit, and run the program again with the Coast option. How far did it go beyond the stopping position this time? Keep reducing the Power setting and running the program until the robot stops at the original stopping position. Why am I asking you to do this? Recall that I told you that the Brake option uses up battery power.

Programming guide mindstorms pdf lego nxt-g

This test shows you that you can save battery power by reducing the Power setting and keeping the Coast option. Running tests like this will help you to figure out how best to program your robot to save battery power and to correctly perform its programmed actions! Drive in a Circle Program your robot to roll forward in a circular pattern to the left or right using rotations for the Duration option. The curves of the S can be as large or as short as you like. Exercise Solutions Following are two possible solutions to the exercises.

Remember that my solutions may vary somewhat from your own. Exercise Figures through show the program and the three configuration panels for the three blocks used in this program. Notice that the MOVE block has its steering control dragged slightly to the right but not all the way; when I uploaded this to my robot, it caused the robot to move in a counterclockwise direction.

Notice that the first MOVE block has its steering control dragged slightly to the right with a Duration setting of 3 rotations. This will have the robot rolling forward in a counterclockwise direction. After 3 rotations of the motors, the second MOVE block will have the robot roll forward for 3 rotations, but this time the steering control is dragged to the left slightly. This will have the robot rolling forward in a clockwise direction, finishing the S-shaped path.

Feel free to do what you like. I have motor A in Port A spinning a small propeller like an airplane on the front of the robot see Figure I know it seems like common sense, but I still need to say it: First, choose the Record option in the Action section.

Next, we need to specify a name for the recorded movement. I then want motor A to spin the propeller a few times. Give your recorded movement a unique name.

The name you type in the Name text box is the name of a file that will be stored on the Brick. This file must be stored on the Brick in order for you to later play back the movement, so try to make the name memorable and easy to understand.

Now, look at Figure The Recording section of the configuration panel is where you will specify which ports should be monitored. In my example, motors B and C will move my robot around, and motor A will spin the propeller. So I want to select all the ports. Configure the motor ports to monitor and record.

Pdf nxt-g guide lego programming mindstorms

You can type in the number of seconds you want to record or click the up and down arrows with your mouse to select the number in the Time section. Enter the number of seconds to record in the Time section. You can record anywhere from one second up into the hundreds of minutes.

Is this realistic? Not really. And even recording a few minutes of movement will probably not leave much memory for your actual program. Place the robot at its starting position, and press the Run button for your new program. Using your hands, guide the robot through the movements you wish your robot to perform. I then turn the robot to the left and stop. If you come close to the number of seconds you configured, you can simply leave the recording time alone.

Most importantly, if you originally configured too much time, reduce the number of seconds you entered in the Time section; because the recording process will continue to run until the time is over, the file stored on the Brick will be larger than it needs to be. Now, let me show you how to play back the file. This time, however, select the Play option in the Action section see Figure Rather than type the name of the file, you can select it from the list. If you do not have your NXT Brick connected, you will need to remember the name of the file and type it in the Name section.

Configure your robot to play back the recorded movement. The only other section that can be configured now is the Name section. Type the name of the file that contains the recorded movements in the Name section see Figure Enter the name of the file you created during the Record process.

Next, you need to save the new program and upload it to your Brick. Before you run the program, place your robot in the original starting position or wherever you like , and press the Run button to run the program. The robot will begin to move and will match the movements you recorded earlier. Configure the times properly, and you can synchronize it to a speech given on the robot and its different components. Record a Cha-cha-cha Movement Record your robot performing a short 3—4 seconds cha-cha-cha movement—just wiggling back and forth in short movements.

Roll Forwards and Backwards First Program your robot to roll forwards a few inches and then backwards a few inches using MOVE blocks… and then do a cha-cha-cha movement. Have your robot perform this set of actions one more time. Chapter 6 will show you how to give your robot the ability to talk and make some noise!

Possible Solutions to Exercises Below are the solutions for the exercises I gave you earlier. Exercise 7 Figure contains the program and the single configuration panel used in this program. Save this program and upload it to your robot. Run the program and perform the back and forth movements for no more than five seconds. Exercise 8 Figures through contain the program and the configuration panels for the blocks used in this program.

This instructs the robot to roll forward one rotation and then roll backwards one rotation… and then do the cha-cha-cha movement. The last three blocks are simply repeats of the first three. Sound can be used to give a bot more personality. Think about how boring R2-D2 would be without all the chirps and whistles.

Action Settings The first section I want to cover is the Action section. You have two options: Sound File or Tone. See the section called File? Click one of the sound files, and if your computer has speakers, you will hear the sound file play. There are over unique sound files that you can select from the list.

The Sound File option allows you to select a sound from the File section. If you follow the instructions carefully, any new sounds you add will appear in the File listing in Figure Tone Now, select the other option, Tone.

Notice that the File section changes to a section called Note see Figure The Tone option allows you to specify tones from the Note section. Note Settings The Note section provides you with a few options. The easiest option is to simply click one of the piano keys. You will hear the tone played if your computer has speakers attached.

Notice that when you click on a key, the note you click is displayed in the text box above the keys in the form of a letter: The other option available in the Note section is the ability to specify how long the note will play.

Type a number in the text box for the number of seconds to play the note. Now, let me explain each of these remaining sections. This section has two options: Play and Stop see Figure Select it, and any sound file or tone you selected in the Action section will play. Not too difficult, right?

The Stop option requires a little more explanation. To do this, I need to jump ahead to the Function section see Figure If the box is checked, the Sound File or Tone will continue to play until your program ends, or until another SOUND block is reached with the Stop option selected in the Control section. If the box is unchecked, the sound file or tone will play only one time.

Volume Settings OK, now for the Volume section. You can see in Figure that the Volume control can be changed either by using the slider or by typing a value 0 to in the text box.

You will have to experiment with the Volume control to determine what works best for your robots, but keep in mind that loud sounds will use up more battery power than sounds played at a lower volume. When you have selected a sound file or tone to play and the Repeat box in the Function section is not checked, the Wait for Completion checkbox is available. Let me give you an example using pseudo-code: SPOT, play me a C note for ten seconds and then move forward five rotations.

SPOT will first play a C note for ten seconds. Go ahead and create and run this same program on your bot. What happens? Well, I pressed the Run button and the C note started to play. But before the C note stopped, motors B and C started spinning. What happened? Go back to Figure , and notice that the Wait For Completion box is unchecked.

Take a look again at the pseudo-code: SPOT, play a C note for ten seconds and then move forward five rotations. So, I make this change see Figure and run the program again. Now you can add sound files and tones to your robots to give them more personality. But before we finish this chapter, let me also tell you that sounds can be used when testing your robots.

For example, in a complex program you can drop in a SOUND block to let you know when the robot has reached a certain portion of the program. The newest version of NXT-G comes with a built-in tool called Sound Editor, which will allow you to create your own sounds for use with your robots—up to five seconds in length. Click on the Tools menu and select Sound Editor and the new tool will open, as shown in Figure You must connect an external microphone to your computer or laptop if it does not have one already built in.

Press the Record button and begin recording your voice or other sound effect.

Press the Record button again to stop the recording process. I can use the Play button to listen to my recording. A recorded sound appears as a waveform in the center of the screen. You next use the Left Clip and Right Clip bars indicated in Figure to edit down your recorded sound to a five-second clip.

If the sound wave between the two clip bars is red in color, the sound file is still too long. Continue to drag the left and right clip bars until the sound wave turns green. Reduce the length of your sound using the clip bars.

Click on the Save button and a window will appear, like the one in Figure Give your sound file a name save it to your computer. Give your sound a short but easy-to-remember name and click the Save button. As you can see in Figure , the sound Dangerous appears in the list.

Search for your sound file in the File listing. There are a few more things you can do with the Sound Editor. You can click the Open button and open an existing sound file either one of the pre-configured sounds or one of your own and perform more editing on it by using the left and right clip bars to increase or decrease its length.

RSO file extension. This means you can go out on the Internet and search for sound effects saved in the. RSO format that other NXT owners have created and are sharing; if you find one you like, save it to your hard drive, open Sound Editor, click the Open button, browse to the location of the sound file, and use the controls to edit it as needed. You can find my solutions at the end of the chapter. Exercise Write a program for your robot that will have it spin in circles at the same time that it plays one sound file over and over.

Set a limit for the MOVE block so it will stop after 20 rotations and the program will end. Now, before we investigate any new blocks, I want to go over the different ways your robots receive input—this includes motors, sensors, timers, and the buttons on the Brick. Possible Solutions to Exercises Following are a couple of possible solutions to the exercises.

Remember, your solutions may not exactly match my own. Exercise Figures and show the program and the two configuration panels used in my solution to Exercise After 15—20 rotations, the program will terminate. Some of the questions I hear the most are related to data hubs and wires—and for good reason!

And wires can be just as difficult to figure out. In this chapter, I want to take a short break from learning about any new NXT-G programming blocks and give you some background and tips on how to use data hubs and wires.

I hope that any confusion you have will be cleared up by the end of this chapter. This block is shown in Figure It can hold one color. This block holds Blue. It will always hold Blue and nothing else. There is no way to change the color. Think of the solid wall surrounding the block as keeping information from coming into the block or leaving the block.

LEGO MINDSTORMS NXT-G Programming Guide (eBook, PDF)

A Simple Solution What would make my color block useful to us? Well, first, it would be nice to be able to change the color. I might not be able to do anything else with the block at this point, but at least it will contain my favorite color! To do this, I need a way to access the wall surrounding the block. One of the things the block in Figure lacks is a way to get inside the block and change Blue to Green. This color keyboard is a strange type of keyboard, though; it will only let me type colors.

Now I can type Green. Later, if I want to change to Yellow or Red, I can simply type the new color, and the block will change. This screen is just like my weird keyboard; it will only display a color. Figure shows my new color screen connected to the Green block. I can change the color the COLOR block holds but only by using this special keyboard, and this keyboard will let me type in only colors. This special screen will display only colors and nothing else, not names or types of food.

If I detach the color keyboard, can I still display the color inside? Yes, but only if I keep the color screen attached. If I detach the color screen, can I still change the color inside the block? Yes, again, but only if I keep the color keyboard attached. There are some programming words for you in that description: When thinking about blocks, always remember that any information that is provided to a block is input.

Any information that the block can give out share can be considered output. Take a look at the fake CUP block in Figure The CUP block is a little more complex. The CUP block can hold three pieces of information: The COLOR block has an easier way for me to provide input to the block and to receive output from the block. The CUP block has a hub for connecting things. You can see in the figure that there are three input ports also called plugs on the left side of the hub and three output ports on the right side.

These are where I will plug in keyboards, screens, and other items. Figure shows three blocks with their hubs expanded. Normally an NXT-G block has its hub closed. Each of those little square plugs you see running down the left side of a hub is an input data port. The small square plugs running down the right side are output data ports. Some blocks have more data ports than others, but the ports all work the same; they are simply a way to send data into a block and receive data out of a block.

For the Color input plug, I can only connect something that supplies a color. We already know that a color keyboard will work. I could connect a color keyboard and change the color of the cup from Blue to Green. Well, it has a data hub, too; it was just hidden inside the block. The input plug on the left side of the COLOR block is where I can plug in a color keyboard to change the color inside the block. The output plug on the right can be connected to a color screen, but, in truth, it can be connected to any input plug that can accept a color.

Notice the CUP block has an input plug that will accept a color! A logic keyboard is a very special keyboard—it can only be used to provide Yes or No answers not Maybe or Sometimes—only Yes or No.

An Example What I would like to do with the CUP block is to connect it to a screen that will display one of two things but not both: To do this, I can use a screen to display the color and height that are provided by the CUP block Figure shows my setup so far. Everything is hooked up and ready to use. But before I display the color and height, I need another special block that can examine the contents of the cup and determine if it is empty or not empty.

It takes a Yes or No answer logic and, depending on the answer, performs action 1 or action 2. Action 1 will occur if the answer is Yes the cup is empty ; action 2 will occur if the answer is No for more information on logic, feel free to jump ahead to Chapter 8.

I can use this block to examine the contents of the CUP block. It will first look at the data plug labeled Empty. It then performs the actions required for a Yes answer.

Both types of data input and output can be provided by you by typing information in or selecting options in a configuration panel , or the data can be provided by other blocks using wires. I could go further and create a bunch of fake types of input. When it comes to NXT-G, you only need to know about three types of data: Letters, words, sentences, and even numbers can be considered text.

Numbers can be positive or negative, and sometimes they are limited to integers only numbers like -3, 0, 4, 8, or 10 and no numbers with decimal points like 4. This can be Yes or No another way to say it is True or False. The good news is that if you ever drag a wire from one plug to an incompatible plug if you try to drag a wire from a Text plug to a Logic plug, for example , the wire will be broken.

By that, I mean that the wire will become a dashed gray line, indicating that you made a mistake. You can see this in Figure That input data port is looking for a number specifically, a value of 1, 2, or 3 where 1 equals Motor A, 2 equals Motor B, and 3 equals Motor C. This, too, will fail, as that input port is looking for a value of 1, 2, or 3.

The Power input data port can receive a value from 0 to anything over is reduced to and anything less than 0 is converted to 0. Now, if you correctly connected a wire, the wire will have a color. This color depends on the type of data being sent over the wire: It takes practice to drag and connect wires from plug to plug.

Sometimes, the wires will do strange things and go off in strange directions. If you hover the mouse pointer over a data plug, it will show you the name of the data plug something like Empty or Height in my examples.

My last bit of good news is that you cannot ruin a program with incorrect wires! No worries! Wires can save you time by allowing you to use existing data over and over again; wires can be split, meaning you can split one wire and provide two different blocks with the same data! The splitting occurs automatically when NXT-G detects you are dragging a wire from a data plug that already has a wire attached. You can see an example of this in Figure Data wires can be split and provide the same data to more than one port.

You also need to know that the help documentation contains a complete description of every data port, including a picture of each data port, what type of data it can send and receive and any limitations that exist such as a range of numbers or length of text.

Consult the Help documentation to discover the type of data a port can accept. Wires can also go in the other direction, so you can send an output wire from the end of your program all the way back to an input plug at the start of your program!

Keep your eyes open throughout this book to learn some new ways to use wires. Up next in Chapter 8 is a short discussion on a method robots use for making decisions, using Yes and No answers.

What is the difference between the following two questions? Well, there are a lot of differences: My real point in asking these two questions is to point out that the first question is open-ended; the sky could be blue, or gray, or any number of answers. The second question, however, has only two possible answers: Yes or No. When it comes to programming your robots, you need to understand that many times your robots can only provide you with those two answers: At other times, your robots can understand only a Yes or No answer.

SPOT, what color is the box in front of you? SPOT, what is the position of your Touch sensor button? SPOT, is the color of the box in front of you blue? Yes [appears on the LCD screen]. SPOT, is your Touch sensor button pressed? No [appears on the LCD screen].

LEGO MINDSTORMS NXT-G Programming Guide - Second Edition

Another way of saying this is that SPOT prefers to communicate using logical responses; a logical response is simply Yes or No. On or Off! SPOT, is your Ultrasonic sensor detecting an object six inches in front of you?

SPOT, is your Right button being pressed? What does all this have to do with programming, though? As an example, take a look at Figure If you are seeing only one data output plug on the data hub, click the data hub again to expand it to its full size. What this tells you is that this data plug can provide output using a data wire in the form of a Logic data type.

But how do you know if the output will be Yes or No? How do you know what it means when a sensor returns one answer or the other? Simple—the answer is based on what you are monitoring with the sensor. As an example, take a look at the new block in Figure Logic, Number, or Text. Connecting two blocks with a wire When connecting blocks in a program using data wires, always keep in mind that a data wire will work only if it is connected to input and output plugs that expect the same data type Logic, Number, or Text.

I also need to point out that many blocks hold either a True or False value as a default setting. The Logic data type can be found in many blocks, especially the sensor blocks see Figure It has been replaced with an extra Touch sensor.

Likewise, the NXT 2. Keep in mind that many blocks use a Yes or No answer to control actions. For the MOVE block, you can configure the direction forward or backward using the configuration panel; you simply click the Up or Down arrow in the Direction section to assign a spin direction.

But the direction of spin can also be controlled by sending a Yes or No signal to the Direction Data Plug see Figure A Yes signal is interpreted as Forward and a No signal is interpreted as Backward.

The same concept works for the Next Action section—you can use a Yes signal to indicate that the robot should brake when the spin action is completed or a No signal to tell the robot to coast! How might you use this? It has the Touch sensor mentioned earlier in this chapter. When I run the program, if I hold down the Touch sensor button the motors will spin in the forward direction for ten seconds before the program ends.

You can see in the Touch sensor configuration panel in Figure that I could have changed the Action to Released by selecting that option. If I had done this, what do you think would happen if I pressed the button when I started the program? A No signal means the motors will spin backwards! Your robots will be able to interact with objects, avoid walls, detect light or dark rooms, and much more.

And many of these abilities hinge on your robot being able to use Yes and No logic responses to make decisions. Examine it and determine what actions the robot will take given the following situation: If the lights are on and bright enough, the reading will usually be around 70—80, meaning the Light sensor will detect a light value greater than 50 and send a Yes or True signal to the MOVE block.

A Yes signal tells the motors to Brake. So, the Yes signal will override the Coast configuration panel setting and have the robot brake instead. What about the Light sensor? So, when the program runs, the motors will spin for three rotations at a Power setting of 50 and then the motors will brake. The robot will sit for five seconds and then the program will end.

Would it surprise you to learn that all of these items are able to provide some sort of feedback to the NXT Brick? The sensors are a little obvious; a sensor is designed to respond to external conditions such as light, touch, color, or sound and report this information to the Brick. But what about motors? And how can a timer be used as input or feedback to the Brick? All these questions have answers, and this chapter provides them. A traffic light has three conditions: A light switch has two conditions:

Related Documents

Copyright © 2019 vitecek.info.