iii The Unofficial Guide to LEGO® MINDSTORMS™ Robots Jonathan B. Knudsen O’REILLY Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo iv The Unofficial Guide to LEGO® MINDSTORMS™ Robots by Jonathan B. Knudsen Copyright: © 1999 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Editor: Mike Loukides Production Editor: Nicole Arigo Printing History: October 1999: First Edition. This book is published solely by O'Reilly & Associates, Inc., and its purpose is to enable you to creatively program LEGO® MINDSTORMS™ brand robots. This book is not sponsored by The LEGO® Group. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly & Associates, Inc. The association of the image of a mechanical toy rabbit with the topic of LEGO® MINDSTORMS™ robots is a trademark of O'Reilly & Associates, Inc. LEGO® is a registered trademark of The LEGO® Group. MINDSTORMS™ and Robotics Invention System™ are trademarks of The LEGO® Group. All other trademarks, service marks, and the like are the property of their owners. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 1-56592-692-7 [12/99] [M] vii Table of Contents Preface xi 1. Welcome to MINDSTORMS 1 What Is a Robot? 2 Mobile Robots 2 What Is MINDSTORMS? 6 What Now? 11 Online Resources 11 2. Hank, the Bumper Tank 14 About the Building Instructions 14 Building Instructions 16 A Simple Program 25 Wheels 27 Bumpers and Feelers 31 Gears 31 Multitasking 36 Online Resources 37 3. Trusty, a Line Follower 39 Building Instructions 40 Some Tricky Programming 44 The Light Sensor 48 Idler Wheels 50 Using Two Light Sensors 50 Online Resources 52 4. Not Quite C 53 A Quick Start 54 RCX Software Architecture 55 NQC Overview 58 Trusty Revisited 77 Online Resources 81 viii 5. Minerva, a Robot with an Arm 82 Building Instructions 83 Programming 103 Directional Transmission 107 Pulleys 109 Mechanical Design 110 Two Sensors, One Input 112 Where Am I? 113 Online Resources 115 6. pbFORTH 116 Replacement Firmware 116 pbFORTH Overview 117 About Forth 121 pbFORTH Words 126 An Expensive Thermometer 137 Minerva Revisited 138 Debugging 142 Online Resources 143 7. A Remote Control for Minerva 145 Two Heads Are Better Than One 145 The Allure of Telerobotics 146 Building Instructions 147 Programming the Remote Control 151 Programming Minerva 154 Online Resources 157 8. Using Spirit.ocx with Visual Basic 159 You May Already Have Visual Basic 159 About Spirit.ocx 160 Calling Spirit.ocx Functions 161 Immediate and Delayed Gratification 163 Programs, Tasks, and Subroutines 164 Tips 165 Retrieving the Datalog 168 Online Resources 171 ix 9. RoboTag, a Game for Two Robots 173 Building Instructions 174 Subsumption Architecture 179 Online Resources 188 10. legOS 189 About legOS 189 Development Tools 190 Hello, legOS 192 Function Reference 193 New Brains for Hank 204 Development Tips 211 Online Resources 213 11. Make Your Own Sensors 216 Mounting 216 Passive Sensors 219 Powered Sensors 221 Touch Multiplexer 224 Other Neat Ideas 226 What About Actuators? 226 Online Resources 227 A. Finding Parts and Programming Environments 230 B. A pbFORTH Downloader 235 C. Future Directions 240 Index 242 xi Preface This is a book for people who build and program LEGO® robots with the Robotics Invention System (RIS)™ set. This book is the answer to the question, "How can I push this thing as far as it will go?" Once you've built a few robots and written a few programs, you'll probably be itching for more: more complex robots, more powerful programming environments, more sensors, and more fun. This book will take you there. About This Book For many of us, plastic LEGO bricks are the best toy money can buy. When I was five and broke my leg, a little LEGO set was the high point of my six-week convalescence. I grew up building spaceships and planetary rovers, wearing grooves in the ends of my fingernails from endlessly putting together and taking apart my creations. In high school, I shifted into the TECHNIC™ product line—what could be better than cars with real shifting and pistons that worked? In the Fall of 1998, The LEGO Group released the Robotics Invention System (RIS), a set that was part of a new product line called MINDSTORMS™. This set entered the world like a lightning bolt—finally, the chance to make LEGO models that moved, sensed, and thought! The LEGO Group made 80,000 of these sets in 1998 and sold every one. Although The LEGO Group was aiming for young adults, 11 and older, the RIS has also hypnotized many people in their 20s, 30s, and beyond. A vibrant, inventive online community sprang up around MINDSTORMS robots. In some ways, this book is an introduction to the most important developments in that community—alternate programming environments and advanced building techniques. But this book goes farther than that, painting a backdrop of the theories and practices of mobile robotics. xii Building and programming robots is exhilarating. It's fun to build something that moves and thinks, in a sense; at the same time, you're learning a lot about how things work, mechanically, and how to write programs that can deal with the real world. This book is designed to take you to the next level of building and programming, all in the spirit of fun and learning. This book's chapters come in two basic flavors. Five chapters have robot projects, complete with building instructions and programs. Four chapters describe various programming environments for LEGO MINDSTORMS robots, including code examples and debugging tips. The first and last chapters don't fit in either category. Here's a description of each chapter in this book: Chapter 1, Robotics and MINDSTORMS, introduces the field of mobile robotics and describes how the LEGO MINDSTORMS Robotic Invention System fits in the larger picture of the field. Chapter 2, Hank, the Bumper Tank, is the first building project—a tank-style robot that avoids obstacles in its path. This chapter discusses basic mechanical features like gears and bumpers. Chapter 3, Trusty, a Line Follower, covers a slightly trickier robot—a line-follower. It uses a light sensor to follow a black line on the floor. Chapter 4, Not Quite C, introduces the Not Quite C (NQC) language. NQC is an excellent environment for programming robots. The chapter includes descriptions of NQC's functions as well as many examples. Chapter 5, Minerva, a Robot with an Arm, contains another building project—by far the most complex robot in the book. You'll learn about directional transmissions and other neat stuff. Chapter 6, pbFORTH, discusses an innovative programming environment based on a language called Forth. Chapter 7, A Remote Control for Minerva, is another project-based chapter. Using a second robot kit, you can build a remote control for the robot from Chapter 5. Chapter 8, Using Spirit.ocx with Visual Basic, talks about how to control and program your robots using Microsoft's Visual Basic. Chapter 9, RoboTag, a Game for Two Robots, shows how to create a pair of robots that play tag. Chapter 10, legOS, discusses legOS, a programming environment that enables you to program your robots with C, C++, or assembly code. Chapter 11, Make Your Own Sensors, describes how you can build sensors for your robots easily and inexpensively. xiii Appendix A, Finding Parts and Programming Environments, describes various parts you can get to expand your RIS set and where to find them. It also includes a summary of the programming environments that are available for RIS. Appendix B, A pbFORTH Downloader, contains the source code for a program that downloads Forth code to your robots. It's a supplement to Chapter 6. Appendix C, Future Directions, describes some interesting emerging technologies related to LEGO robots. These are ideas or projects that weren't fully baked as this book went to press. About the Examples Versions This book covers a handful of rapidly evolving technologies. The versions used in this book are as follows: RCX Version 1.0 NQC Version 2.0b1 pbFORTH Version 1.0.7 legOS The March 30, 1999 build, a patched version of 0.1.7 Downloading All of the examples in this book can be downloaded from http://www.oreilly.com/catalog/lmstorms/ . This site also provides a listing of the "Online Resources" that appear at the end of each chapter. Font Conventions Constant width is used for: • Function and subroutine name • Source code • Example command-line sessions—the input you type is shown in boldface xiv Italic is used for: • Pathnames and filenames • New terms where they are defined • Internet addresses, such as domain names and URLs Boldface is used for the names of buttons. This is a note with information that supplements the text. This is a warning with a cautionary message for the reader. Request for Comments If you find typos, inaccuracies, or bugs, please let us know. You can reach O'Reilly by mail, telephone, fax, or email: O'Reilly & Associates, Inc. 101 Morris Street Sebastopol, CA 95472 (800) 998-9938 (in the U.S. or Canada) (707) 829-0515 (international or local) (707) 829-0104 (fax) [email protected] Please let us know what we can do to make the book more helpful to you. We take your comments seriously, and will do whatever we can to make this book as useful as it can be. Acknowledgments This book is the result of a crazy idea I had in mid-1998, when I first heard that the Robotics Invention System was coming. LEGO robots sounded like something O'Reilly readers would like to play with—why not write a book about them? I'd like to thank Mike Loukides and Tim O'Reilly for having the vision to believe in this book. Thanks to Mike, again, for excellent help and feedback.