Table Of ContentTECHNOLOGY IN ACTION™
Beginning Robotics
Programming in
Java with LEGO
Mindstorms
—
Wei Lu
Beginning Robotics
Programming in Java
with LEGO Mindstorms
Wei Lu
Beginning Robotics Programming in Java with LEGO Mindstorms
Wei Lu
Keene, New Hampshire, USA
ISBN-13 (pbk): 978-1-4842-2004-7 ISBN-13 (electronic): 978-1-4842-2005-4
DOI 10.1007/978-1-4842-2005-4
Library of Congress Control Number: 2016959245
Copyright © 2016 by Wei Lu
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is
concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction
on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic
adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion
and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified
as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither
the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may
be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Aaron Black
Developmental Editor: Gary Schwartz
Technical Reviewer: Nanyan Wang
Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan,
Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, James Markham,
Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing
Coordinating Editor: Jessica Vakili
Copy Editor: Karen Jameson
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com , or visit w ww.springeronline.com . Apress Media, LLC is a California LLC and the
sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail rights@apress.com , or visit w ww.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at w ww.apress.com/bulk-sales .
Any source code or other supplementary materials referenced by the author in this text are available to
readers at w ww.apress.com . For detailed information about how to locate your book’s source code, go to
w ww.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary
Material section for each chapter.
Printed on acid-free paper
My wife Ling, for her endless love and support over the past fi ve years when
I worked on this book.
My daughter Julia and son Ryan, for giving their dad the best fun times
when testing all of the robot’s programs with them.
Contents at a Glance
About the Author .....................................................................................................xi
Acknowledgments .................................................................................................xiii
Introduction .............................................................................................................xv
■ Chapter 1: Introduction to Lego Mindstorms and leJOS ........................................1
■ Chapter 2: Introduction to Motor Sensors ...........................................................27
■ Chapter 3: Controlling Wheeled Vehicles .............................................................43
■ Chapter 4: Coordinators and Navigator API .........................................................65
■ Chapter 5: Depth-First Search Algorithm and
Its Implementation with Lego EV3 .......................................................................83
■ Chapter 6: Breadth-First Search and Its Implementation
with Lego Mindstorms .......................................................................................101
■ Chapter 7: Hill-Climbing Search and Its Implementation
with Lego Mindstorms .......................................................................................119
■ Chapter 8: Dijkstra’s Algorithm and Its Implementation
with Lego Mindstorms .......................................................................................139
■ Chapter 9: The A* Search Algorithm and Its Implementation
with Lego Mindstorms .......................................................................................167
■ Chapter 10: Introducing the Touch Sensor and Ultrasonic Sensor ....................183
■ Chapter 11: Introducing the Light Sensor and Color Sensor .............................193
■ Chapter 12: Introduction to Behavior Programming .........................................203
■ Chapter 13: Multithreading Programming with Java leJOS ..............................219
Index .....................................................................................................................231
v
Contents
About the Author .....................................................................................................xi
Acknowledgments .................................................................................................xiii
Introduction .............................................................................................................xv
■ Chapter 1: Introduction to Lego Mindstorms and leJOS ........................................1
Introduction to Lego Mindstorms ....................................................................................1
Introduction to leJOS .......................................................................................................3
Lego Mindstorms EV3 .............................................................................................................................3
Lego Mindstorms NXT ............................................................................................................................4
Lego Mindstorms RCX ............................................................................................................................4
JDK Installation ...............................................................................................................5
Testing the JDK Installation ..............................................................................................8
Installation of leJOS and Its Firmware on Lego EV3 ......................................................10
Eclipse IDE and Eclipse Plug-In for LeJOS EV3 ..............................................................16
Summary ........................................................................................................................25
■ Chapter 2: Introduction to Motor Sensors ...........................................................27
Basic Concepts of Java Programming ...........................................................................27
Introducing Motors ........................................................................................................28
Introducing the Motor Class ..........................................................................................29
Controlling Basic Movement Using Motors ...........................................................................................29
Using a Tachometer for Inertia Testing .................................................................................................31
Controlling the Accurate Rotation of Motors .........................................................................................33
Interrupting Rotation.............................................................................................................................34
vii
■ CONTENTS
Regulating the Motor Speed .................................................................................................................36
Tracing a Straight Line ..........................................................................................................................39
Other Motor Methods ............................................................................................................................41
Summary ........................................................................................................................41
■ Chapter 3: Controlling Wheeled Vehicles .............................................................43
Introduction to Navigation API .......................................................................................43
Basic Movement Using Pilot Classes ............................................................................45
Tracing Out a Square ...........................................................................................................................48
Tracing Out an Equilateral Triangle ......................................................................................................53
Tracing Out a Regular Hexagon ...........................................................................................................58
Summary ........................................................................................................................63
■ Chapter 4: Coordinators and Navigator API .........................................................65
Cartesian Coordinate System Basics .............................................................................65
Navigator API Functions ................................................................................................66
Controlling the EV3 Brick Hardware ..............................................................................69
Programming Practice with the LCD Display .................................................................73
Programming Practice with Key Presses ......................................................................74
Programming Practice with Navigator API ....................................................................76
Summary ........................................................................................................................81
■ Chapter 5: Depth-First Search Algorithm and Its Implementation with Lego EV3
................................................................................................................................83
Overview of DFS Algorithm ...........................................................................................83
leJOS EV3-Based DFS Algorithm ...................................................................................91
Summary ......................................................................................................................100
■ Chapter 6: Breadth-First Search and Its Implementation with Lego Mindstorms
..............................................................................................................................101
Overview of BFS Algorithm ..........................................................................................101
leJOS EV3-Based BFS Algorithm .................................................................................108
Summary ......................................................................................................................118
viii
■ CONTENTS
■ Chapter 7: Hill-Climbing Search and Its Implementation with Lego Mindstorms
..............................................................................................................................119
Introduction to Heuristic Search ..................................................................................119
Overview of Hill-Climbing Search ................................................................................123
leJOS EV3-Based Hill-Climbing Algorithm ...................................................................131
Summary ......................................................................................................................138
■ Chapter 8: Dijkstra’s Algorithm and Its Implementation with Lego Mindstorms
..............................................................................................................................139
Introduction to Dijkstra’s Algorithm ............................................................................139
leJOS EV3-Based Dijkstra’s Algorithm ........................................................................155
Summary ......................................................................................................................165
■ Chapter 9: The A* Search Algorithm and Its Implementation with Lego
Mindstorms ..........................................................................................................167
What Is the A* Algorithm? ...........................................................................................167
The Basic Idea of the A* Searching Strategy ...............................................................167
Practice for Path Planning Using the A* Algorithm ......................................................177
Summary ......................................................................................................................181
■ Chapter 10: Introducing the Touch Sensor and Ultrasonic Sensor ....................183
Sensor Classes ............................................................................................................183
Touch Sensor ...............................................................................................................184
Ultrasonic Sensor ........................................................................................................186
Programming Practice with Touch Sensor ..................................................................188
Programming Practice with Ultrasonic Sensor ...........................................................190
Summary ......................................................................................................................191
■ Chapter 11: Introducing the Light Sensor and Color Sensor .............................193
Light Sensor ................................................................................................................193
Color Sensor ................................................................................................................195
Programming Practice with the Color and Light Sensors ............................................197
Summary ......................................................................................................................202
ix
■ CONTENTS
■ Chapter 12: Introduction to Behavior Programming .........................................203
Introduction to Behavior Programming .......................................................................203
The Behavior API Functions ..........................................................................................205
Design Pattern of Behavior Programming ....................................................................206
Programming Practice with Behavior Programming ....................................................209
Summary ......................................................................................................................217
■ Chapter 13: Multithreading Programming with Java leJOS ..............................219
The Thread Concept ....................................................................................................219
Using Threads in leJOS ...............................................................................................220
Method start() .....................................................................................................................................222
Method isAlive () .................................................................................................................................222
Method sleep () ...................................................................................................................................222
Practice with Multithreading in Java leJOS ................................................................223
Summary ......................................................................................................................229
Index .....................................................................................................................231
x
About the Author
Wei Lu is an Associate Professor of Computer Science at Keene
State College in New Hampshire. He received his Ph.D. in Electrical
and Computer Engineering from the University of Victoria, Canada.
Prior to joining Keene State College, he was a Senior Researcher
with the German Research Centre for Artificial Intelligence (DFKI
GmbH) and worked with Q1 Labs Inc. (Security Systems Division,
IBM since October 2011) as a Secure Software Engineer. His general
areas of research interests include Artificial Intelligence and Cyber
Security. He has had more than 50 papers published by peer-
reviewed journals, book chapters, and conference proceedings. In
addition, he coauthored, N etwork Intrusion Detection and
Prevention: Concepts and Techniques (Springer, 2010) and has
served as a technical program committee member and a technical
reviewer for more than 70 international conferences and journals.
xi