Page 251 - The Definitive Guide to Building Java Robots
P. 251
Preston_5564C07.fm Page 232 Monday, September 26, 2005 5:38 AM
232 CHAPTER 7 ■ NAVIGATION
// test all methods
public static void main(String[] args) {
try {
WebSerialClient com = new WebSerialClient("10.10.10.99", "8080", "1");
NavStamp s = new NavStamp(com);
System.out.println("diag=" + s.diagnostic());
Utils.pause(500);
System.out.println("compass=" + s.getCompass());
Utils.pause(500);
System.out.println("ir=" + s.getIR().toString());
Utils.pause(500);
System.out.println("diag=" + s.getSonar().toString());
Utils.pause(500);
System.out.println("all dist=" + s.getSonarIR());
s.close();
System.out.println("done");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
Next is the program for the BASIC Stamp. In the main label, it waits for a start byte of 100,
following which it waits for the next commands.
■Note Because the SSC is hooked to the same serial connection as the BASIC Stamp, the SSC might send
a byte of 100 to the SSC for a position. However, because the Stamp is looking for two bytes in the 100s, it
will ignore the second byte to the SSC, if there is one, since it will be a 255 sync byte (see SSCProtocol.java
in Example 3-3).
The first section of this program initializes variables for all constants, working variables,
and return variables. You can see that the constants defined below correspond to the BASIC
Stamp 2 pin out in Table 7-1.
The second section consists of the main program area, where it looks and waits for an
input request byte[] from the NavStamp class and then branches to the subroutine depending
on the command.
The third section consists of subroutines specifically designed to get infrared, sonar, and
compass readings, and then return the output to the NavStamp class in the form of a serial
byte[]. See Example 7-7.