Okay, so I'm creating a hexapod with arduino and two servo drivers. there are 18 servos driving the legs. I want to do this really well, so I'm trying to use classes so i can say something like left_front.goToPosition(x,y,z);
but I can't get my code to verify... I want to control each servo in an object (no idea at all if that's what you call it), and then control each leg (group of 3 servos) in its own object. I thought I should use an array of legs and servos, but this is very new to me. Thanks in advance!
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm_0 = Adafruit_PWMServoDriver(0x40);
Adafruit_PWMServoDriver pwm_1 = Adafruit_PWMServoDriver(0x41);
Adafruit_PWMServoDriver drivers[2] = {pwm_0, pwm_1};
/*
* 0 | 3
* 1 | 4
* 2 | 5
*/
//int servos[6];
int minim = 250;
int maxim = 750;
sErVo servos[18] = {
sErVo(0, 0, minim, maxim),
sErVo(1,0,minim, maxim),
sErVo(2,0,minim, maxim),
sErVo(4,0,minim, maxim),
sErVo(5,0,minim, maxim),
sErVo(6,0,minim, maxim),
sErVo(8,0,minim, maxim),
sErVo(9,0,minim, maxim),
sErVo(10,0,minim, maxim),
sErVo(0,1,minim, maxim),
sErVo(1,1,minim, maxim),
sErVo(2,1,minim, maxim),
sErVo(4,1,minim, maxim),
sErVo(5,1,minim, maxim),
sErVo(6,1,minim, maxim),
sErVo(8,1,minim, maxim),
sErVo(9,1,minim, maxim),
sErVo(10,1,minim, maxim),
};
void setup() {
// put your setup code here, to run once:
pwm_0.begin();
pwm_1.begin();
}
void loop() {
// put your main code here, to run repeatedly:
}
double degToPulse(double deg, double servoMin, double servoMax){
double pulseLen = map(deg, 0, 180, servoMin, servoMax);
return pulseLen;
}
void writeToServo(int driverNum, int servoNum, double angle, double servoMin, double servoMax){
drivers[driverNum].setPWM(servoNum, driverNum, degToPulse(angle, servoMin, servoMax));
}
class sErVo
{
int driverNum;
int servoNum;
long servoMin;
long servoMax;
public:
sErVo(int n, int d, long minimum, long maximum){
servoNum = n;
driverNum = d;
servoMin = minimum;
servoMax = maximum;
}
void setAngle(long angle){
writeToServo(driverNum, servoNum, angle, servoMin, servoMax);
}
};
class leggy
{
int leggyNum;
float Z_Offset;
float coxaLength;
float femurLength;
float tibiaLength;
sErVo coxaServo;
sErVo femurServo;
sErVo tibiaServo;
long gama;
long alpha;
long beta;
//long L_1;
long len;
public:
//takes pre-initialized sErVo objects.
leggy(int servo_indexes[3], int legNum, float coxaLen, float femurLen, float tibiaLen, float z_off){
Z_Offset = z_off;
coxaServo = servos[servo_indexes[0]];
femurServo = servos[servo_indexes[1]];
tibiaServo = servos[servo_indexes[2]];
leggyNum = legNum;
coxaLength = coxaLen;
femurLength = femurLen;
tibiaLength = tibiaLen;
}
//sets leg to point relative to the leg itself, not the robot.
//that should come from another class
//y is a magnitude, the distance from the bot, not a location. so
//the left and right of the bot have +y coordinates.
void setLegToPoint(float x, float y, float z){
//math to set gama alpha and beta here
gama = atan(x/y);
len = sqrt(sq(Z_Offset) + sq(y - coxaLength));
long alpha_1 = acos(Z_Offset/len);
long alpha_2 = acos((sq(tibiaLength) - sq(femurLength) - sq(len))/(-2*femurLen*len));
alpha = alpha_1 + alpha_2;
beta = acos((sq(len) - sq(tibiaLength) - sq(femurLength))/(-2*tibiaLength*femurLength));
setAngles(gama, alpha, beta);
}
void setAngles(long gama, long alpha, long beta){
coxaServo.setAngle(gama);
femurServo.setAngle(alpha);
tibiaServo.setAngle(beta);
}
};