Docsity

The Definitive Guide to Building Java Robots - Scott Preston, Notas de estudo de Mecatrônica

Pré-visualização parcial do texto

Baixe The Definitive Guide to Building Java Robots - Scott Preston e outras Notas de estudo em PDF para Mecatrônica, somente na Docsity!

The Definitive Guide to

Building Java Robots

Scott Preston

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

  • ■ CHAPTER 1 A Primer Introduction xv
    • 1.0 Introduction
    • 1.1 Organizing Your Behavior
    • 1.2 Java Concepts
    • 1.3 Organizing Your Hardware
  • ■ CHAPTER 2 Serial Communication
    • 2.0 Introduction
    • 2.1 A Simple Serial Port
    • 2.2 Concurrent Serial Port Usage
    • 2.3 Creating a Web Serial Port
    • 2.4 Serial Communications with a Microcontroller
    • 2.5 Modeling a Microcontroller with Java
    • 2.6 Bluetooth Serial Communications
    • 2.7 Chapter Summary
  • ■ CHAPTER 3 Motion
    • 3.0 Introduction
    • 3.1 Servo Controllers
    • 3.2 Wheeled Motion
    • 3.3 Pan and Tilt Mechanisms
    • 3.4 Advanced Servo Control
    • 3.5 The Robot Arm
    • 3.6 Legged Robots
    • 3.7 Chapter Summary
  • ■ CHAPTER 4 Sensors vi ■C O N T E N T S
    • 4.0 Introduction
    • 4.1 Orientation Sensors (Compass)
    • 4.2 Switch Sensors
    • 4.3 Distance Sensors (IR and Sonar)
    • 4.4 Chapter Summary
  • ■ CHAPTER 5 Speech
    • 5.0 Introduction
    • 5.1 Speech Synthesis
    • 5.2 Speech Recognition
    • 5.3 Chapter Summary
  • ■ CHAPTER 6 Vision
    • 6.0 Introduction
    • 6.1 Image Capture
    • 6.2 Basic Image Processing
    • 6.3 Color Processing
    • 6.4 Advanced Image Processing
    • 6.5 Chapter Summary
  • ■ CHAPTER 7 Navigation
    • 7.0 Introduction
    • 7.1 Navigation Basics
    • 7.2 Localization
    • 7.3 Obstacle Detection
    • 7.4 Indoor Navigation
    • 7.5 Outdoor Navigation
    • 7.6 Chapter Summary
  • ■ CHAPTER 8 Other Topics
    • 8.0 Introduction
    • 8.1 Running Your Java Programs
    • 8.2 Scheduling Your Programs
    • 8.3 Ant Building and More
    • 8.4 Database Access with JDBC and MySQL
    • 8.5 Using and Installing Tomcat
    • 8.6 Java Scripting Languages ■C O N T E N T S vii
    • 8.7 Chapter Summary
  • ■ CHAPTER 9 Sample Programs
    • 9.0 Introduction
    • 9.1 Miscellaneous Utilities
    • 9.2 Webcam Programs
    • 9.3 Remote Control Programs
    • 9.4 Diagnostic Programs
    • 9.5 Navigation Programs
    • 9.6 Chapter Summary
  • ■ APPENDIX A The Definitive Guide API
  • ■ APPENDIX B Microcontroller Reference
  • ■ APPENDIX C Robot Parts Reference
  • ■ INDEX

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.

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

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.

xvi ■I N T R O D U C T I O N

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.)

xviii ■I N T R O D U C T I O N

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.

1

■ ■ ■

C H A P T E R 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:

  • http://java.sun.com/j2se/1.4.2/download.html

Download and install Eclipse 3.02 from:

  • http://download.eclipse.org/eclipse/downloads/index.php