Page 103 - The Definitive Guide to Building Java Robots
P. 103
Preston_5564C03.fm Page 84 Wednesday, October 5, 2005 7:21 AM
84 CHAPTER 3 ■ MOTION
public void cmd(int ch, int pos, int spd) throws Exception {
// ensure position is valid
if (pos < 0 || pos > 255) {
throw new Exception("position out of bounds");
}
// call createCmd then append to string buffer
cmds.append(createCmd(ch, pos, spd));
}
// allows for raw command string to be sent
public void setRawCommand(String rawCmd) {
cmds.append(rawCmd);
}
// this is the protocol for the command string for the LM32
public static String createCmd(int ch, int pos, int spd) {
String out = "#" + ch + "P" + getPw(pos) + "S" + spd;
return out;
}
// sets the LM32 busy for specific milliseconds
private void setBusy(long ms) {
// the set busy function
busy = true;
// gets time when it should be done
Date timeToRun = new Date(System.currentTimeMillis() + ms);
Timer timer = new Timer();
// schedules time to be run so busy can be set to false
timer.schedule(new TimerTask() {
public void run() {
busy = false;
}
}, timeToRun);
}
// accessor
public boolean isBusy() {
return busy;
}
// static utility method
public static int getPw(int pos) {
int pulsewidth;
double percent = (double) pos / 255;
double pwfactor = percent * 1500;