Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

The definitive guide to building Java robots
Nội dung xem thử
Mô tả chi tiết
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 [email protected], or
visit http://www.springeronline.com.
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 [email protected], or visit http://www.apress.com.
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 http://www.apress.com 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 software 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