Page 121 - The Definitive Guide to Building Java Robots
P. 121
Preston_5564C03.fm Page 102 Wednesday, October 5, 2005 7:21 AM
102 CHAPTER 3 ■ MOTION
Code Discussion
I think you’ll find that a hexapod robot is nothing more than a few complex arms that have to
work together. The class that follows groups the two servos of the hexapod leg and adds some
movement methods for the leg.
The two fields are of type ServoPosition2: one for the horizontal motion and one for the
vertical.
The constructor does not take a JSerialPort because unlike the ComplexArm, the leg will
just be a complicated data structure. It will function to set and get commands to be used with
the LM32.
The methods up(), down(), forward(), and backwards() set the commands to the preset
positions defined by the servo positions or default values. The neural method does the same
for both servos. (See Example 3-19.)
Example 3-19. BasicLeg.java
package com.scottpreston.javarobot.chapter3;
public class BasicLeg {
// each leg has 2 servos
private ServoPosition2 vertServo;
private ServoPosition2 horzServo;
// generic constructor just taking pins
public BasicLeg(int vPin, int hPin)throws Exception {
vertServo = new ServoPosition2(vPin);
horzServo = new ServoPosition2(hPin);
}
// constructors with ServoPosition2's
public BasicLeg(ServoPosition2 vertServo, ServoPosition2 horzServo) {
this.vertServo = vertServo;
this.horzServo = horzServo;
}
// move leg up
public String up() {
return LM32.createCmd(vertServo.pin,vertServo.max,LM32.DEFAULT_SPEED);
}
// move leg down
public String down() {
return LM32.createCmd(vertServo.pin,vertServo.min,LM32.DEFAULT_SPEED);
}
// move leg forward
public String forward() {
return LM32.createCmd(horzServo.pin,horzServo.max,LM32.DEFAULT_SPEED);
}