Page 125 - The Definitive Guide to Building Java Robots
P. 125
Preston_5564C03.fm Page 106 Wednesday, October 5, 2005 7:21 AM
106 CHAPTER 3 ■ MOTION
// sample to move forward gate
public void forward() throws Exception {
lm32.setRawCommand(getTotalMove(legGroup1, DOWN));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup2, UP));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup2, FORWARD));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup1, BACKWARD));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup2, DOWN));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup1, UP));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup1, FORWARD));
lm32.move(getSpeedInMs());
lm32.setRawCommand(getTotalMove(legGroup2, BACKWARD));
lm32.move(getSpeedInMs());
}
public static void main(String[] args) {
try {
JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
Hexapod hex = new Hexapod(sPort);
hex.forward();
hex.forward();
sPort.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void forward(int ms) throws Exception {
if (getSpeedInMs() * 8 < ms) {
throw new Exception("Speed requested is less than minimum speed.");
}
int remaining = ms;
while (remaining > getSpeedInMs() * 8) {
forward();
remaining = remaining - getSpeedInMs() * 8;
}
Utils.pause(remaining);
}