LEGO MINDSTORMS NXT-G Programming Guide - Second Edition

Transcription

â ‡CYANâ ‡ MAGENTAâ ‡YELLOWâ ‡ BLACKâ ‡ PANTONE 123 CLEGO MINDSTORMS NXT-G: Programming Guide, Second EditionISBN 978-1-4302-2976-65 29 9 9US 29.99Shelve in RoboticsUser level: Beginner–IntermediateSOURCE CODE ONLINE9 781430 229766James Floyd Kelly,author of:LEGO MINDSTORMS NXT:The Mayan AdventureLEGO MINDSTORMS NXT 2.0:The King’s TreasureBuild Your Own CNC Machinewww.apress.comthis print for content only—size & color not accurateNNX CCXT- ovG 1 er.0 s&2.0LEGO MINDSTORMS NXT-GProgramming GuideMaster the NXT-G languagefor programming LEGOMINDSTORMS robots.ProgrammingGuidehe book you’re holding is all about programming LEGO MINDSTORMS robots.The kits for LEGO MINDSTORMS NXT and NXT 2.0 provide the parts, motors, andsensors to build a wide array of robot designs. But just putting components togetherisn’t enough. A robot needs to do things! The NXT-G programming language gives yourrobots life! And this book is going to help you master the NXT-G software.NXT-G is a graphical programming language, with drag-and-drop features thatallow you to design, test, and upload instructions to your robots. LEGO MINDSTORMSNXT-G Programming Guide, 2nd Edition is the perfect reference for NXT-G, and is nowfully updated to cover the very latest updates in the software. It's your key to pushingNXT-G to its maximum potential.You’ll begin to create programs right from Chapter 1. You’ll learn to organize yourthoughts, and to create programs that require less troubleshooting. You’ll learn aboutthe basic programming blocks, the powerful My Block, and the new tools available inNXT 2.0. This second edition provides 25 new exercises to hone and test your newfound programming skills. You’ll find complete instructions for building a test-robotand complete solutions for every exercise.LEGO MINDSTORMS NXT-GProgramming Guide, 2nd Edition isuseful to both new and experiencedNXT-G programmers who are looking for information on the updatesfound in NXT-G 2.0. NXT-G 1.0 userswill also find this updated book useful, as over 90% of the content is stillrelevant to the 1.0 version of the software, including most of the exercises.We hope you enjoy the book. Butmost importantly, we hope you takewhat you learn and create your ownamazing programs and push yourrobots (and your imagination) to thelimits.LEGO MINDSTORMS NXT-GTTechnology in Action KellyJames Floyd KellySecond Editiontrim size 7.5" x 9.25"â spine 0.75"â 336 page count

LEGO MINDSTORMSNXT-G Programming GuideSecond Edition James Floyd Kelly

LEGO MINDSTORMS NXT-G Programming Guide, Second EditionCopyright 2010 by James Floyd KellyAll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-4302-2976-6ISBN-13 (electronic): 978-1-4302-2977-3Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbolwith every occurrence of a trademarked name, logo, or image we use the names, logos, and images onlyin an editorial fashion and to the benefit of the trademark owner, with no intention of infringement ofthe trademark.The use in this publication of trade names, trademarks, service marks, and similar terms, even if they arenot identified as such, is not to be taken as an expression of opinion as to whether or not they are subjectto proprietary rights.President and Publisher: Paul ManningLead Editor: Jonathan GennickTechnical Reviewer: Fay RhodesEditorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,Matt Wade, Tom WelshCoordinating Editor: Laurin BeckerCopy Editor: Mary Ann Fugate, Katie StenceCompositor: Mary SudulIndexer: Potomac Indexing, LLCArtist: April MilneCover Designer: Anna IshchenkoDistributed to the book trade worldwide by Springer Science Business Media, LLC., 233 Spring Street,6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm.com, or visit www.springeronline.com.For information on translations, please e-mail rights@apress.com, or visit www.apress.com.Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.eBook versions and licenses are also available for most titles. For more information, reference ourSpecial Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.The information in this book is distributed on an “as is” basis, without warranty. Although everyprecaution has been taken in the preparation of this work, neither the author(s) nor Apress shall haveany liability to any person or entity with respect to any loss or damage caused or alleged to be causeddirectly or indirectly by the information contained in this work.The source code for this book is available to readers at www.apress.com. You will need to answerquestions pertaining to this book in order to successfully download the code.

For the LEGO MINDSTORMS team, past and present

Contents at a Glance Chapter 1: Robots and Programs.1 Chapter 2: Program Structure.11 Chapter 3: Hello World!.17 Chapter 4: Get Movin’.31 Chapter 5: Record and Playback.43 Chapter 6: Make Some Noise!.51 Chapter 7: Wired! .63 Chapter 8: True or False?.75 Chapter 9: Feedback .85 Chapter 10: Wait for It! .101 Chapter 11: Round and Round .113 Chapter 12: Decisions, Decisions.131 Chapter 13: Stop It!.147 Chapter 14: Pick a Card, Any Card.153 Chapter 15: Apples and Oranges.163 Chapter 16: Inside or Out? .177 Chapter 17: Yes? No? Maybe? .191 Chapter 18: Title Anything You Like .201 Chapter 19: Basic Text .221 Chapter 20: Basic Math.229 Chapter 21: Staying Alive.237 Chapter 22: Your Own Filing Cabinet .241 Chapter 23: Calibration .253 Chapter 24: Motoring .259 Chapter 25: Messages.267 Chapter 26: My Block Is Your Block .277 Appendix A: Math Basics for NXT .289 Appendix B: SPOT Building Instructions .293 Appendix C: Image Editor.305Index.311iv

CONTENTSContentsContents at a Glance.ivAbout the Author . xvAbout the Technical Reviewer . xviAcknowledgments . xviiIntroduction . xviii Chapter 1: Robots and Programs.1What Is a Robot?.1What Is a Program? .1Programs are for Humans Too.3Programs are Merely Instructions .3The Challenge of Programming .4NXT-G.5 Chapter 2: Program Structure.11What Do I Mean by Structure?.11Planning Your Program’s Structure .11Moving Into Real Code .13Exercises .14Exercise 2-1. Move One Object Nearer to Another .14Exercise 2-2. Place Objects at Corners .14What’s Next?.14v

CONTENTSExercise Solutions .15Exercise 2-1.15Exercise 2-2.15 Chapter 3: Hello World!.17Programming the DISPLAY Block.17Waiting for the Results .20Doing More with DISPLAY.21Understanding Data Hubs .23Exercises .26Exercise 3-1. Display Your Own Text.26Exercise 3-2: Draw a Rectangle .26What’s Next .27Exercise Solutions .27Exercise 3-1.27Exercise 3-2.28 Chapter 4: Get Movin’.31The MOVE block.31Moving Forward and Backward.32Stopping .34Steering .35Power Settings .35Duration Settings .36Braking and Coasting .37Exercises .38Exercise 4-1. Drive in a Circle .38Exercise 4-2: Make an “S” .39What’s Next?.39vi

CONTENTSExercise Solutions .39Exercise 4-1.39Exercise 4-2.40 Chapter 5: Record and Playback.43The RECORD/PLAY Block .44Recording Movement.46Replaying Movement .46Having Fun with Record/Play.47Exercises .47Exercise 5-1: Record a Cha-cha-cha Movement .47Exercise 5-2: Roll Forwards and Backwards First.47What’s Next?.48Possible Solutions to Exercises .48Exercise 7 .48Exercise 8 .48 Chapter 6: Make Some Noise!.51The SOUND Block.51Action Settings .52Note Settings .53Control Settings .53Function Settings.53Volume Settings.53Wait Settings .54Custom Sounds.56Exercises .59Exercise 6-1.59Exercise 6-2.59What’s Next?.59vii

CONTENTSPossible Solutions to Exercises .59Exercise 6-1.59Exercise 6-2.60 Chapter 7: Wired! .63The Problem.63A Simple Solution .64The NXT-G Solution.65An Example.69Types of Data.71Variables .72A Real NXT-G Block.72What’s Next?.74 Chapter 8: True or False?.75One or the Other .75Sensors: Yes or No.76Variables and the Logic Type.77Using Logic to Configure Settings.80Exercise 11 .82What’s Next?.83Exercise Solution .83 Chapter 9: Feedback .85What’s Your Condition? .85Configuring the Sensors .86NXT Touch Sensor .87RIS Touch Sensor .89viii

CONTENTSNXT Sound Sensor.90NXT Light Sensor .90RIS Light Sensor .91NXT Ultrasonic Sensor .91NXT Rotation Sensor.92RIS Rotation Sensor.92NXT Color Sensor.93RIS Temperature Sensor.94Other Input Types .94Data Hub Power.95Exercise 9-1.97What’s Next?.98Exercise Solution .98 Chapter 10: Wait for It! .101The WAIT Block.101The LIGHT SENSOR WAIT Block .104The NXT BUTTONS WAIT Block .105The ROTATION SENSOR WAIT Block .106The SOUND SENSOR WAIT Block .106The TIMER WAIT Block.107The TOUCH SENSOR WAIT Block .107The ULTRASONIC SENSOR WAIT Block .108The COLOR SENSOR WAIT Block.109Exercise 10-1.109What’s Next .110Exercise Solution .110ix

CONTENTS Chapter 11: Round and Round .113Do It Again and Again and Again . . . .113Nested Loops .121Exercise 11-1.124What’s Next?.128Exercise Solution .128 Chapter 12: Decisions, Decisions.131Left or Right? Door One or Door Two? .131Flat and Tabbed Views .133Choices .135More Than Two Conditions .137Default Decisions.142Execution! .143Exercise 12-1 Left or Right? .144What’s Next?.144Exercise Solution .144 Chapter 13: Stop It!.147The STOP Block .147A Looping Example .147An Unconditional STOP .149A SWITCH to a STOP .149STOP for Testing .150What’s Next?.151x

CONTENTS Chapter 14: Pick a Card, Any Card.153The RANDOM Block .153The NUMBER TO TEXT Block.155Exercise 14-1.159What’s Next?.159Exercise Solution .159 Chapter 15: Apples and Oranges.163Logic .163A Random Example.163The COMPARE Block .166The Display of Logic Values .169A Test Run .172Exercise 15-1 Happy or Sad .173What’s Next?.173Exercise Solution .174 Chapter 16: Inside or Out? .177The RANGE

allow you to design, test, and upload instructions to your 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 latest updates in the software. It's your key to pushing NXT-G to its maximum potential. You'll begin to create programs right from Chapter 1.