Tải bản đầy đủ (.pdf) (441 trang)

defining and deploying software processes

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (17.63 MB, 441 trang )

The Definitive Guide to
Building Java Robots
■■■
Scott Preston
Preston_5564Front.fm Page i Friday, October 7, 2005 5:14 AM
The Definitive Guide to Building Java Robots
Copyright © 2006 by Scott Preston
All 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 retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN: 1-59059-556-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Coke®, Pepsi®, and 7 UP® are registered trademarks of The Coca Cola Company, PepsiCo Beverages of
North America Inc., and Cadbury Beverages North America, respectively.
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Steve Anglin
Technical Reviewer: Simon Ritter
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Sofia Marchant
Copy Edit Manager: Nicole LeClerc
Copy Editor: Mike McGee
Assistant Production Director: Kari Brooks-Copony
Production Editor: Katie Stence
Compositor: Susan Glinert
Proofreader: April Eddy
Indexer: Carol Burbo
Artist: April Milne


Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at in the Source Code section.
Preston_5564Front.fm Page ii Friday, October 7, 2005 5:14 AM
iii
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ix
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
■CHAPTER 1 A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Serial Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
■CHAPTER 3 Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
■CHAPTER 4 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
■CHAPTER 5 Speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
■CHAPTER 6 Vision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
■CHAPTER 7 Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
■CHAPTER 8 Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
■CHAPTER 9 Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
■APPENDIX A The Definitive Guide API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
■APPENDIX B Microcontroller Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

■APPENDIX C Robot Parts Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Preston_5564Front.fm Page iii Friday, October 7, 2005 5:14 AM
Preston_5564Front.fm Page iv Friday, October 7, 2005 5:14 AM
v
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ix
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
■CHAPTER 1 A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Organizing Your Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Java Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3 Organizing Your Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
■CHAPTER 2 Serial Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1 A Simple Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2 Concurrent Serial Port Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3 Creating a Web Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4 Serial Communications with a Microcontroller . . . . . . . . . . . . . . . . . . 37
2.5 Modeling a Microcontroller with Java . . . . . . . . . . . . . . . . . . . . . . . . 40
2.6 Bluetooth Serial Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.7 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
■CHAPTER 3 Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.1 Servo Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.2 Wheeled Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.3 Pan and Tilt Mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.4 Advanced Servo Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

3.5 The Robot Arm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.6 Legged Robots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.7 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Contents
Preston_5564Front.fm Page v Friday, October 7, 2005 5:14 AM
vi
■CONTENTS
■CHAPTER 4 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
4.1 Orientation Sensors (Compass) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
4.2 Switch Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
4.3 Distance Sensors (IR and Sonar) . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.4 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
■CHAPTER 5 Speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.1 Speech Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
5.2 Speech Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
5.3 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■CHAPTER 6 Vision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
6.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
6.1 Image Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
6.2 Basic Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
6.3 Color Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
6.4 Advanced Image Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
6.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
■CHAPTER 7 Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
7.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
7.1 Navigation Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
7.2 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
7.3 Obstacle Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

7.4 Indoor Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
7.5 Outdoor Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
7.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
■CHAPTER 8 Other Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
8.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
8.1 Running Your Java Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
8.2 Scheduling Your Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
8.3 Ant Building and More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
8.4 Database Access with JDBC and MySQL . . . . . . . . . . . . . . . . . . . . 299
8.5 Using and Installing Tomcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Preston_5564Front.fm Page vi Friday, October 7, 2005 5:14 AM
■CONTENTS
vii
8.6 Java Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
8.7 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
■CHAPTER 9 Sample Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
9.0 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
9.1 Miscellaneous Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
9.2 Webcam Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
9.3 Remote Control Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
9.4 Diagnostic Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
9.5 Navigation Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
9.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
■APPENDIX A The Definitive Guide API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
■APPENDIX B Microcontroller Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
■APPENDIX C Robot Parts Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Preston_5564Front.fm Page vii Friday, October 7, 2005 5:14 AM
97022d2480fe4a63cfdfa123a6e70098
Preston_5564Front.fm Page viii Friday, October 7, 2005 5:14 AM

ix
About the Author
■SCOTT PRESTON works as a Java architect in Columbus, Ohio, where he
resides with his wife, Emily, and dog, Castle. Scott has over 20 years of soft-
ware and electrical experience, including positions with the U.S. Navy, Bank
One, CompuServe, UUNET, and Covansys Inc., in addition to running his
own robotics company, Preston Research LLC. Scott also gives lectures
about robotics at COSI (Center of Science and Industry) and manufactures
small Java-enabled robots called CubeBots®. Scott is a member of the Java
Community Process and an alumnus of The Ohio State University.
Preston_5564Front.fm Page ix Friday, October 7, 2005 5:14 AM
Preston_5564Front.fm Page x Friday, October 7, 2005 5:14 AM
xi
About the Technical Reviewer
■SIMON RITTER is a Java technology evangelist at Sun Microsystems. Simon has been in the IT
business since 1984 and holds a bachelor of science degree in physics from Brunel University in
the UK. Originally working in the area of UNIX development for AT&T UNIX System Labs and
then Novell, Simon moved to Sun in 1996. At that time, he began working with Java technology
and has since divided his time between Java technology development and consultancy. He now
specializes in emerging technologies, including grid computing, RFID, robotics, and smart
sensor networks. Simon and his performing Java-powered LEGO robots have appeared before
audiences worldwide.
Preston_5564Front.fm Page xi Friday, October 7, 2005 5:14 AM
Preston_5564Front.fm Page xii Friday, October 7, 2005 5:14 AM
xiii
Acknowledgments
I would like to especially thank my loving wife, Emily, for putting up with me while I wrote this
book and for all the hours I spent at the PC and downstairs with the robots when I could have
been spending it with her.
Second, I would like to thank Steve Anglin, Simon Ritter, Sofia Marchant, Michael McGee,

and Katie Stence for helping me write this book, and to readers and others at Apress who have
been a joy to work with.
Third, I would like to thank Ken Gracey from Parallax, Inc., and Jim Frye from Lynxmotion,
Inc., for supplying me with various parts, components, and advice during the writing of this book.
Fourth, I would like to thank my mother for passing on her common sense and buying me
an Atari 400 in 1980, and my father for passing on his engineering smarts and helping me build
the original Feynman in 2002.
Fifth, I would like to thank my in-laws, Frikkie and Karen Roets, for their hospitality last
Christmas (and always), and for the use of their office to contact Apress about writing this book.
Sixth, I would like to thank my high-school science teacher, Jan Greissinger, for inspiring
me to love science as much as I do today, and also Guy Kawasaki for his books and e-mails,
which inspired me to write this book.
Finally, I could not end without mentioning my friends Harry and Crissy, Ron and Sophia,
Mark and Maria, Bard and Ann, Mark and Tracy, and John and Kristi. I was meaning to spend
more time with you this summer, and even come out and visit those who are a little more than
a drive away. Plan on that in 2006.
Scott Preston
Preston_5564Front.fm Page xiii Friday, October 7, 2005 5:14 AM
Preston_5564Front.fm Page xiv Friday, October 7, 2005 5:14 AM
97022d2480fe4a63cfdfa123a6e70098
xv
Introduction
Notes on Style
I admit I was a programmer before I started building robots. So my perspective may be some-
what skewed in the direction of a programmer. However, I also didn’t want this book to be from
a purely software engineering perspective. I wanted to keep the text balanced between robotics
and programming and not get too cute with either discipline, though from time to time I’m
afraid I may have indulged myself.
Who Should Read This Book
If you want off-the-shelf robot components, free software, and development tools, this is the

book for you. You can download all the software—it’s GPL (General Public License) or Apache
License—and you can purchase the components from your favorite robot supplier and/or
hobby shop. The following sections outline the experience you should have to get the most out
of the book.
Your Programming/Java Experience
I could say that you should have a good understanding of object-oriented techniques and Java
before getting started with this book, but if you’re like most roboticists, you’ll likely learn as you
go, and by following the various examples I’ve included within these pages.
Of course, if you don’t have a background in Java, you will undoubtedly experience a
learning curve before things begin to click. If you start getting lost due to the vocabulary or the
complexity of the examples, just purchase one of the beginning Java books from Apress. They’re
excellent. If things still don’t make sense, send me an e-mail or visit my web site and post a
question.
Your Robot Building Experience
For those picking up this book, you should hopefully be an intermediate robot builder already;
be familiar with microcontroller concepts, servo controllers, electronic speed controls, and
sensors; and have either built a robot from scratch or from a kit. I can recommend a few robots
from Lynxmotion, Inc., or if you want to build one from scratch, take a look at some of the
Apress books penned by David Cook.
Preston_5564Front.fm Page xv Friday, October 7, 2005 5:14 AM
xvi
■INTRODUCTION
How This Book Is Structured
I’ve structured this book as if I were sitting down to build a Java robot. I start out by reviewing
the basics of communication, and then discuss how to get the robot to move, hear, see, and
navigate, before exploring how to optimize code and create shortcuts.
I’ve divided each chapter into subtopics that progress from easy to difficult. Each subtopic
includes the following:
• An introduction to the topic
• A detailed discussion of the code example

• A code example demonstrating the topic
• A section or chapter summary, if needed
What You Will Need
While you can use this book quite effectively without every item named in the following table,
the required and optional items shown will help you facilitate the examples in this book.
I also provide a list of what you need in each chapter’s introduction.
Platform and Version Notes
I developed this book and its examples with the Sun Java Standard Edition 1.4.2 SDK, using the
Eclipse 3.02 IDE running on Microsoft Windows XP. However, if you don’t have a Microsoft
operating system, you can easily port the examples in this book to Linux, Macintosh, or any OS
running a JVM.
Table 1. Items Needed for Book Examples
Required Optional
Java 1.4 SDK DLink DBT-120 or other Bluetooth adapter
Java APIs (comm, advanced imaging, speech, etc.) EB500 Bluetooth Transceiver from Parallax
Parallax Javelin or BASIC Stamp Pan and Tilt Camera Kit from Lynxmotion
MiniSSC-II Servo controller Lynxmotion SSC-32 Servo controller
Parallax Board of Education (BOE) or other carrier
board
Lynxmotion Extreme Hexapod 2
Web camera
Sound card and microphone
Small mobile robot
Miscellaneous robot sensors (sonar, infrared,
bump, etc.)
Preston_5564Front.fm Page xvi Friday, October 7, 2005 5:14 AM
■INTRODUCTION
xvii
You can download the latest SDK from
and the latest version of Eclipse at www.eclipse.org/downloads/index.php.

While you can use J2SE 5.0 or Eclipse 3.1, both were not ready at the time this book was
written, so the programs have not been tested with those versions.
Book Road Map
The chapters of this book build upon each other, with the primary goal being robot navigation,
which we tackle in Chapter 7. However, you can really expand the capability of your robot by
using speech as explored in Chapter 5, or by using some of the items discussed in Chapter 9.
I draw on topics from all of the previous chapters in Chapter 9, while simultaneously introducing
some fun and powerful programs. See Figure 1.
Figure 1. Book road map
Preston_5564Front.fm Page xvii Friday, October 7, 2005 5:14 AM
xviii
■INTRODUCTION
Updates and Software
I will constantly be updating my site at www.scottsbots.com/definitiveguide. Please stop by to
visit, download new source code and examples, and get links to Internet resources. I have also
included a special section for purchasing robots or parts needed for this book.
Preston_5564Front.fm Page xviii Friday, October 7, 2005 5:14 AM
1
■ ■ ■
CHAPTER 1
A Primer
“Everything should be made as simple as possible, but not simpler.”
— Albert Einstein
1.0 Introduction
Before you begin programming your robot with Java, you should consider some things that will
make your Java robot experience much more enjoyable. They include the following:
• A configured personal computer and various software*
•A serial link
• A microcontroller
•A robot

■Note Be sure your PC has a fast Internet connection as I will often refer to links from the Internet for
download or reference.
To begin, I’ll walk you through a setup similar to mine. Forgive me non-Windows users, all
screen captures are from Windows XP. When the occasion arises, I’ll mention an alternative
way of achieving the same thing with a UNIX-based operating system.
Personal Computer Setup
To facilitate use of your personal computer for this book, you should do the following.
Download and install the Java Standard Edition 1.4.2 from:
• />Download and install Eclipse 3.02 from:
• />Preston_5564C01.fm Page 1 Friday, September 16, 2005 6:36 AM
2
CHAPTER 1
■ A PRIMER
If you have any other Java Runtimes installed on your machine, make sure you set your
Eclipse preferences to this path, as shown in Figure 1-1.
Figure 1-1. The Eclipse JRE setup
Download the latest source code for this book at:
• www.scottsbots.com/definitiveguide
You can add this JAR to your project. This way you should be able to call the classes from
your own project with your own programs.
Serial Link Setup
If you don’t have a serial port (some new computers only come with USB connectors),
I recommend you get a USB nine-pin serial adapter, shown next in Figure 1-2.
Preston_5564C01.fm Page 2 Friday, September 16, 2005 6:36 AM
CHAPTER 1 ■ A PRIMER
3
Figure 1-2. The IOGear USB Serial Adapter (GUC232A)
The Microcontroller Setup
You should have a microcontroller you can program in PBASIC. I used the BASIC Stamp 2 for
all the examples in this book because it does the job well. It was my first microcontroller, and it

cost the least.
The BASIC Stamp 2 (shown in Figure 1-3) operates at 20MHz and can execute approximately
4,000 instructions per second. It has 16 I/O pins, plus two dedicated serial pins, one for transmit,
and one for receive.
Figure 1-3. The Parallax BASIC Stamp 2
You can purchase one from www.parallax.com for $49.
Preston_5564C01.fm Page 3 Friday, September 16, 2005 6:36 AM
97022d2480fe4a63cfdfa123a6e70098
4
CHAPTER 1
■ A PRIMER
The Microcontroller Programmer Setup
All examples in this book are in PBASIC because it’s the most popular Microcontroller language at
the moment. You can also find many examples and sample code on the Internet and at the
Parallax site. At the back of the book, I’ve included a quick reference for PBASIC as well as a
Javelin Stamp version of the examples.
You can download the latest version of the programmer from www.parallax.com. Figure 1-4
shows a picture of a sample program loaded into the BASIC Stamp Windows editor.
Figure 1-4. The BASIC Stamp Windows editor
The Robot Setup
If you do not have a robot and would like one in a kit, several fine specimens can be found at
www.lynxmotion.com, www.parallax.com, or www.prestonresearch.com. For most of the
examples in this book, I use a differential drive robot or CubeBot (as shown in Figure 1-5).
Preston_5564C01.fm Page 4 Friday, September 16, 2005 6:36 AM
CHAPTER 1 ■ A PRIMER
5
Figure 1-5. A CubeBot
Summary
Hopefully you have everything you need to get started. I’m now going to start by explaining the
thought process behind robotics programming (in other words, getting your robot to do stuff).

Then I’ll talk about some concepts in Java you should know about before embarking on a
robotics project. Finally, I’ll show you an example of how to begin modeling your software in
a way that’s both easy to use and effective.
1.1 Organizing Your Behavior
What does your robot do? This is the number one question people ask when I tell them I have
a robot. My answer five years ago would be something like, “It moves around a room and
avoids the furniture.” Then people would say “Oh…,” and I would afterward talk about the
technology or the software, which never kept their attention.
Thus, I wanted something for my robot to do besides these basic things. I wanted it to do
something that would be cool, something like a simple human task, something I could tell
people about and not get blank stares in return. So, I picked the chore of getting a beverage
from the refrigerator.
Preston_5564C01.fm Page 5 Friday, September 16, 2005 6:36 AM
6
CHAPTER 1
■ A PRIMER
I thought that moving to the refrigerator would not be that difficult, but as I started to
compile a list of tasks, then subtasks, and then detailed steps, I realized that I had better start
by organizing my thoughts around what I wanted to program (see Figure 1-6). How I organized
them (my thoughts) requires a little explanation, so here’s the definition of those terms:
• Events: These are the things that cause a robot to do something. Does it hear a word, see
something, or get a request from the network, or is there a scheduled task, or something
like that.
• Tasks: These are the things that events trigger. So if the robot gets a request to move
someplace, the top level task would be to move. The move task would then have to call a
subtask or another task to help.
• Subtasks: These are the things that help the task. So, if the task is to move from position A to
position B, a robot must know what direction it’s facing or consequently turn and face
that direction. Subtasks may call other subtasks or send or receive data packets from the
robot’s subsystems.

• Data packets: These are the final level of granularity in our task organization. A data
packet sends information to a controller to command the robot’s motor controls, or
sensors, whose sole purpose is to get and receive data from the robot’s subsystems or
peripherals.
Figure 1-6. A behavioral model of events, tasks, subtasks, and data packets
Preston_5564C01.fm Page 6 Friday, September 16, 2005 6:36 AM

×