Page 118 - The Definitive Guide to Building Java Robots
P. 118
Preston_5564C03.fm Page 99 Wednesday, October 5, 2005 7:21 AM
CHAPTER 3 ■ MOTION 99
// will check the servos to see if requested position is
// within parameters of servo
private void checkRange(int pin, int pos) throws Exception {
for (int i = 0; i < servos.size(); i++) {
ServoPosition2 tmpPos = (ServoPosition2) servos.get(i);
if (tmpPos.pin == pin) {
if (pos > tmpPos.max || pos < tmpPos.min) {
throw new Exception("Positions out of bounds for pin "
+ pin + ".");
}
}
}
}
public static void main(String[] args) {
try {
// get single serial port instance
JSerialPort sPort = (JSerialPort) SingleSerialPort.getInstance(1);
// create new ComplexArm
ComplexArm arm = new ComplexArm(sPort);
arm.rest();
arm.posA();
arm.posB();
sPort.close();
} catch (Exception e) {
// print stack trace and exit
e.printStackTrace();
System.exit(1);
}
}
}
Section Summary
Moving with a complex arm is just about all you will ever need with servo control. We have fine
control over position and movement speed as well as coordinated, precise movements.
In this section, I created the following classes:
• BasicArm.java: This class models the basic robot arm with a shoulder and an elbow.
• ArmTest1.java: This class shows how to move the basic arm to two different positions.
• ServoPosition2.java: This class extends the ServoPosition class created earlier for
minimum, maximum, and neutral values to be used in the ComplexArm.
• ComplexArm.java: This class uses the ServoPosition2 class and has a total for five axes of
movement versus the two in the BasicArm class.