Page 30 - The Definitive Guide to Building Java Robots
P. 30
Preston_5564C01.fm Page 11 Friday, September 16, 2005 6:36 AM
CHAPTER 1 ■ A PRIMER 11
Table 1-3. Ways to Pause a Java Program
Pause Method Description
Thread.sleep(ms) This is a static method and will tell the current Java thread to sleep
for a certain number of milliseconds. Keep in mind that this will
put all Java programs using this thread to sleep, so if you’re doing
multiple things like waiting for a serial event and checking a
parallel port reading, you’ll have to create a new thread.
Timer Task Another way of getting something to occur on a certain interval is
to create a new timer task that will occur at a fixed time in milli-
seconds from when it was invoked.
Loop Until Finished If you don’t know how long something will take to complete, but
don’t want to do anything until it’s finished, you can just put a
program in a loop. Make sure you insert a Thread.sleep(1); in the
loop so that way your PC does not have 100-percent CPU utiliza-
tion while waiting. It could prevent what you are waiting for (like an
image capture, voice command, and so on) from getting enough
cycles to do anything.
wait() and notify() If you have two cooperating threads—say, one thread for a serial
port, and another reading an image from your webcam—you could
use the wait() on your serial port and then notify() on the webcam
class. The wait() method will cause the current thread of the serial
port to wait until the webcam thread invokes the notify() or
notifyAll() method.
Now that we’re done with some of the important concepts in Java and we know how to
pause our programs, our final step is to organize our hardware in a way that makes modeling
our behavior easy.
1.3 Organizing Your Hardware
Both PCs and microcontrollers have their place in robotics. Each is good at some things and
poor at others, but with both you get more than you would from using each separately.
Microcontrollers are very good at talking to smaller electronics. So if you have sensors,
servos, or motor controllers, then use a microcontroller for this. Usually, they have many I/O
ports, anywhere from 8–40 depending on the type of microcontroller you use, where PCs are
limited to their communication ports. You’ll discover later how you can use a PC parallel port
to do some basic digital I/O, but when you start doing pulse width modulation (PWM) with a
PC parallel port, you’ll find yourself running into limitations.
PCs are very good at controlling decisions, storing data, interfacing with people, and using
multimedia. Though there are some chips out there that can do speech synthesis, and cameras
are available that can handle some basic color and object recognition, you’ll find they have
limited ability. Plus, your microcontroller’s ability to interface with people, store data, and
make decisions may leave a lot to be desired.