Page 86 - The Definitive Guide to Building Java Robots
P. 86
Preston_5564C03.fm Page 67 Wednesday, October 5, 2005 7:21 AM
CHAPTER 3 ■ MOTION 67
// pivot right
public void pivotRight(long ms) throws Exception {
pivotRight();
Utils.pause(ms);
stop();
}
// sample program
public static void main(String[] args) {
try {
// get instance of SingleSerialPort
JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
// create instnace of TimedDiffDrive
TimedDiffDrive diffDrive = new TimedDiffDrive(sPort);
// move forwrd 2 seconds
diffDrive.forward(2000);
// close serial port
sPort.close();
} catch (Exception e) {
// print stack trace and exit
e.printStackTrace();
System.exit(1);
}
}
}
The final thing I need for the differential drive to be complete is a speed control. However,
I won’t always have wheeled robots. I might want to implement navigational classes with legged
robots or other implementations of the differential drive (maybe even a car with an accelerator,
brake, and steering wheel). In this case, because I want to reuse my navigational classes (see
Chapter 7), I should create an interface and then use that interface in later classes.
The interface I’ll create is JMotion. This class has all the methods of BasicDiffDrive and
TimedDiffDrive, plus the methods for speed control. (See Example 3-8.)
Example 3-8. The JMotion.java Interface
package com.scottpreston.javarobot.chapter3;
public interface JMotion {
// forward
public void forward() throws Exception;
// reverse
public void reverse() throws Exception;
// pivot right
public void pivotRight() throws Exception;
// picot left
public void pivotLeft() throws Exception;