Visit us at www.syngress.com Syngress is committed to publishing high-quality books for IT Professionals and delivering those books in media and formats that fit the demands of our cus tomers. We are also committed to extending the utility of the book you purchase via additional materials available from our Web site. SOLUTIONS WEB SITE To register your book, visit www.syngress.com/solutions. Once registered, you can access our solutions@syngress.com Web pages. There you may find an assortment of valueadded features such as free e-books related to the topic of this book, URLs of related Web sites, FAQs from the book, corrections, and any updates from the author(s). ULTIMATE CDs Our Ultimate CD product line offers our readers budget-conscious compilations of some of our best-selling backlist titles in Adobe PDF form. These CDs are the perfect way to extend your reference library on key topics pertaining to your area of expertise, including Cisco Engineering, Microsoft Windows System Administration, CyberCrime Investigation, Open Source Security, and Firewall Configuration, to name a few. DOWNLOADABLE E-BOOKS For readers who can t wait for hard copy, we offer most of our titles in downloadable Adobe PDF form. These e-books are often available weeks before hard copies, and are priced affordably. SYNGRESS OUTLET Our outlet store at syngress.com features overstocked, out-of-print, or slightly hurt books at significant savings. SITE LICENSING Syngress has a well-established program for site licensing our e-books onto servers in corporations, educational institutions, and large organizations. Contact us at sales@syngress.com for more information. CUSTOM PUBLISHING Many organizations welcome the ability to combine parts of multiple Syngress books, as well as their own content, into a single volume for their own internal use. Contact us at sales@syngress.com for more information.
Owen Bishop
Elsevier, Inc., the author(s), and any person or firm involved in the writing, editing, or production (collectively Makers ) of this book ( the Work ) do not guarantee or warrant the results to be obtained from the Work. There is no guarantee of any kind, expressed or implied, regarding the Work or its contents. The Work is sold AS IS and WITHOUT WARRANTY. You may have other legal rights, which vary from state to state. In no event will Makers be liable to you for damages, including any loss of profits, lost savings, or other incidental or consequential damages arising out from the Work or its contents. Because some states do not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitation may not apply to you. You should always use reasonable care, including backup and other appropriate precautions, when working with computers, networks, data, and files. Syngress Media, Syngress, Career Advancement Through Skill Enhancement, Ask the Author UPDATE, and Hack Proofing, are registered trademarks of Elsevier, Inc. Syngress: The Definition of a Serious Security Library, Mission Critical, and The Only Way to Stop a Hacker is to Think Like One are trademarks of Elsevier, Inc. Brands and product names mentioned in this book are trademarks or service marks of their respective companies. KEY SERIAL NUMBER 001 HJIRTCV764 002 PO9873D5FG 003 829KM8NJH2 004 BAL923457U 005 CVPLQ6WQ23 006 VBP965T5T5 007 HJJJ863WD3E 008 2987GVTWMK 009 629MP5SDJT 010 IMWQ295T6T PUBLISHED BY Syngress Publishing, Inc. Elsevier, Inc. 30 Corporate Drive Burlington, MA 01803 Programming Lego Mindstorms NXT Copyright 2008 by Elsevier, Inc. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. Printed in the United States of America 1 2 3 4 5 6 7 8 9 0 ISBN 13: 978-1-59749-278-2 Page Layout and Art: SPi Publishing Services Copy Editor: Judy Eby For information on rights, translations, and bulk sales, contact Matt Pedersen, Commercial Sales Director and Rights, at Syngress Publishing; email m.pedersen@elsevier.com.
Author Owen Bishop is the author of over 80 books, mostly on electronics and computing, including the best-selling Commodore 64 Games Book. His most recent publication is the Robot Builder s Cookbook (Elsevier/Newnes, 2007). Owen s talent for introducing the world s of electronics and computing has long been proven fact with his textbooks, professional introductions and hobby guides being chosen by thousands of students, lecturers and enthusiasts. With Owen s clear program descriptions you can get your robot up and running quickly, even if you are a beginner, and have fun whilst doing so. Educated in England, at Bristol University and the University of Oxford, Owen has lived and worked in several countries as a science education adviser for UNESCO and the British Council. He is at present a freelance technical writer, living in Perth, Western Australia. v
Contents Chapter 1 Robots and Programs.................................... 1 Introduction................................................... 2 What is a Program?.............................................. 4 Designing a Robot............................................... 6 Specify the Robot s Environment................................. 7 Intelligent or Smart?........................................... 8 Summary...................................................... 9 Chapter 2 About NXT-G Programs................................. 11 Introduction.................................................. 12 Creating a Program............................................. 13 First Steps.................................................... 16 Out of Memory?............................................... 18 Coming for a Spin?............................................. 19 Tying it Together............................................ 19 Chapter 3 Data................................................. 23 Introduction.................................................. 24 Sources of Data................................................ 24 Variables................................................... 26 A Variable Has a Name, a Type, and a Value....................... 27 Arithmetic Operators......................................... 28 Logical Operators............................................ 28 Flag Variables............................................... 29 Chapter 4 Loops................................................ 31 Introduction.................................................. 32 Outside Events................................................. 33 Time Limit................................................ 34 Counting the Loops.......................................... 34 Dance Some More.............................................. 34 Use Loops to Make Actions Repeat................................. 36 Another Way to Use the Loop Counter............................ 36 Logical Conclusions............................................. 37 Use a Loop (or a Wait Block) to Wait for an Event to Occur............ 38 Nested Loops............................................... 38 The Wait Block............................................. 39 vii
viii Contents Chapter 5 Switches and Other Topics.............................. 41 The Switch Block.............................................. 42 Switch Blocks Direct Execution Along Alternative Pathways............... 44 Simultaneous Processing.......................................... 44 Diagnostic Programming......................................... 46 Chapter 6 Using the Light Sensor................................. 51 Introduction.................................................. 52 Light Sensor Directed Downward.............................. 54 Lined in...................................................... 57 Line Follower................................................. 58 Edge Follower................................................. 59 Light Sensor Directed Upward.................................. 60 Housefly.................................................. 61 Light Sensor Directed Forward..................................... 63 Lightfinder1................................................... 63 LightFinder2.................................................. 64 Almost an Interrupt.......................................... 65 LightFinder3.................................................. 66 LightFinder4.................................................. 67 Summary..................................................... 70 Chapter 7 Using the Touch Sensor and the NXT Buttons............... 71 Introduction.................................................. 72 Waiting for Contact............................................. 73 Bumper................................................... 74 Waiting for Input............................................ 75 Scanning the NXT Buttons....................................... 76 Entering Numbers.............................................. 79 Chapter 8 Using the Ultrasonic Sensor............................. 81 Introduction.................................................. 82 Avoidance Routine for Alpha Rex.................................. 82 Ultrasonic Search............................................... 84 Chapter 9 Sounds Received and Emitted............................ 87 Introduction.................................................. 88 Start and Stop................................................. 88 Start, Stop, and Turn............................................. 89 One Clap or Two?.............................................. 90 Playing a Tune (with a Little Dance)................................. 92 www.syngress.com
Contents ix Choreography................................................. 93 Song Snippets............................................... 94 Dance Snippets.............................................. 94 Chapter 10 The Magnetic Compass Sensor.......................... 95 Introduction.................................................. 96 Heading in the Right Direction.................................... 97 Polygon...................................................... 98 Circle....................................................... 99 Chapter 11 More Mathematics................................... 103 Introduction................................................. 104 Floating Point Arithmetic........................................ 104 Calculating Cosines............................................ 105 Calculating Sines.............................................. 107 Square Root................................................. 108 Factorials.................................................... 110 Combinations................................................ 113 X modulo Y.................................................. 114 Modulo Counting............................................. 116 Chapter 12 Navigation......................................... 119 Introduction................................................. 120 Distance Traveled.............................................. 120 Magnetic Compass............................................. 122 Edge Follower 1............................................ 122 No-go Response to a Line....................................... 125 Steering Alpha Rex............................................ 127 Marching Orders.............................................. 131 Rex On Line................................................. 132 Rex On Edge................................................ 134 Pathfinder................................................... 137 Cosine Rule................................................. 142 Chapter 13 Games Robots Play................................... 147 Introduction................................................. 148 Maze Runner Version 1........................................ 148 Maze Runner Version 2........................................ 153 Maze Runner Version 3........................................ 154 Maze Learner................................................. 157 Scissors, Paper, Stone........................................... 159 www.syngress.com
x Contents Signalling by Semaphore......................................... 166 Magic...................................................... 167 The Program................................................. 170 Chapter 14 Databases for Robots................................. 173 Introduction................................................. 174 Lookup Table for Weekdays...................................... 174 Writable Lookup Table.......................................... 176 Another Way of Using a Lookup Table.............................. 177 Reading Data from a File Access Block.............................. 178 Acknowledgements............................................ 183 Index........................................................ 185 www.syngress.com