Page 249 - The Definitive Guide to Building Java Robots
P. 249
Preston_5564C07.fm Page 230 Monday, September 26, 2005 5:38 AM
230 CHAPTER 7 ■ NAVIGATION
• getCompass(): Gets an int back as a heading. This will tell the robot what direction it is
facing relative to magnetic north.
• getIr(): Gets the infrared sensors at the base of the robot.
• getSonar(): Gets the sonar at the top of the robot.
• getSonarIR(): Gets both the sonar and infrared information from the robot.
• getGpsLongitude(), getGpsLatitude(), and getGps(): These will be discussed later in
section 7.5.
Example 7-6. NavStamp.java
package com.scottpreston.javarobot.chapter7;
import com.scottpreston.javarobot.chapter2.Controller;
import com.scottpreston.javarobot.chapter2.JSerialPort;
import com.scottpreston.javarobot.chapter2.Utils;
import com.scottpreston.javarobot.chapter2.WebSerialClient;
public class NavStamp extends Controller {
// command bytes to microcontroller
public static byte CMD_INIT = 100;
public static byte CMD_COMPASS = 101;
public static byte CMD_SONAR = 102;
public static byte CMD_IR = 103;
public static byte CMD_IR_SONAR = 104;
public static byte CMD_GPS_LAT = 105;
public static byte CMD_GPS_LON = 106;
public static byte CMD_DIAG = 107;
public static int PING_CYCLE_TIME = 200;
public NavStamp(JSerialPort port) throws Exception {
super(port);
}
// get compass reading
public int getCompass() throws Exception {
String heading = execute(new byte[] { CMD_INIT, CMD_COMPASS }, 175);
String[] h2 = heading.split("~");
String heading2 = "";
for (int h = 0; h < h2.length; h++) {
heading2 = heading2 + (char) new Integer(h2[h]).intValue();
}
return new Integer(heading2).intValue();
}