Page 153 - The Definitive Guide to Building Java Robots
P. 153
Preston_5564C04.fm Page 134 Wednesday, October 5, 2005 7:22 AM
134 CHAPTER 4 ■ SENSORS
// get distance method
public int ping() throws Exception {
// calling super execute() method
String heading = execute(new byte[] { CMD_INIT, (byte) distSensor },
getSonarDelay());
// since returning heading as one, two or three bytes
String[] h2 = heading.split("~");
String heading2 = "";
for (int h = 0; h < h2.length; h++) {
// convert each byte to char which I append to create single number
heading2 = heading2 + (char) new Integer(h2[h]).intValue();
}
// return 3 chars like '123' which is 123 degrees
return new Integer(heading2).intValue();
}
public int getIR() throws Exception {
return ping(CMD_IR);
}
public int getSRF() throws Exception {
return ping(CMD_IR);
}
public int get6500() throws Exception {
return ping(CMD_IR);
}
// since different delay for each compass
private int getSonarDelay() {
int delay = 0;
if (distSensor == CMD_IR) {
delay = 100;
}
if (distSensor == CMD_SRF) {
delay = 150;
}
if (distSensor == CMD_6500) {
delay = 250;
}
return delay;
}
public int getDistSensor() {
return distSensor;
}
public void setDistSensor(int distSensor) {
this.distSensor = distSensor;
}