Page 248 - The Definitive Guide to Building Java Robots
P. 248
Preston_5564C07.fm Page 229 Monday, September 26, 2005 5:38 AM
CHAPTER 7 ■ NAVIGATION 229
public IRReadings(String readings) {
String[] values = readings.split("~");
left = new Integer(values[0]).intValue();
right = new Integer(values[1]).intValue();
}
public String toString() {
return "left=" + left + ",right=" + right;
}
}
The final reading is from all sonar and infrared detectors at the same time. The constructor
takes a string of value Ir1~Ir2~ Sonar1~Sonar2~Sonar3. See Example 7-5.
Example 7-5. DistanceReadings.java
package com.scottpreston.javarobot.chapter7;
import java.io.Serializable;
public class DistanceReadings implements Serializable {
public SonarReadings sonar = new SonarReadings();
public IRReadings ir = new IRReadings();
public DistanceReadings(String readings) throws Exception {
String[] values = readings.split("~");
ir.left = new Integer(values[0]).intValue();
ir.right = new Integer(values[1]).intValue();
sonar.left = new Integer(values[2]).intValue();
sonar.center = new Integer(values[3]).intValue();
sonar.right = new Integer(values[4]).intValue();
}
public String toString() {
return ir.toString() + "," + sonar.toString();
}
}
I will leave the discussion of GPSReadings.java until section 7.5, “Outdoor Navigation.”
For the NavStamp class in Example 7-6, this should look very familiar to the classes I created in
Chapter 2. The command bytes at the top match the bytes expected in the BASIC Stamp program.
The other static primitive PING_CYCLE_TIME will be used by navigation classes that need to
know how long to wait until the microcontroller is finished getting sensor data.
The constructor uses the JSerialPort interface I created in Chapter 2. The other methods
correspond to getting specific data from the microcontroller, for example: