We used an LPC1768 mBed and Adafruit PWM Servo Driver board to control the two servos connected to the panel.
Code
//input an angle between -90 and +90
//n = pwm channel to change
//angle = angle to rotate to
//pulse = # ticks/4096 * frequency -> pwm duty cycle
//pwmDutyVar = servo center calibration variable
void setServoAngle(uint8_t n, float angle) {
if(angle >= -90 && angle <=90){
float pulse = angle*2 + pwmDutyVar;
pwm.setPWM(n, 0, pulse);
}
else {
pc.printf("Angle out of range/r/n");
}
}
double degTan (double deg) {
return tan(deg *3.14/180);
}
//input an angle betweeen 0 and 359 and a depth angle
void setPanelAngle(int panel, double angle, double depth) {
int servox = 2*panel;
int servoy = servox +1;
double posx = 1/abs(degTan(angle));
double posy = abs(degTan(angle));
if((angle >45 && angle < 135)||(angle > 225 && angle < 315)) {
posx = (int) 1/abs(degTan(angle));
posy = (int) abs(degTan(angle));
}
// pc.printf("posx:%f\r\n",posx);
if(posy > 1) {
posy = depth;
}
else {
posy = posy*depth;
}
if (posx > 1) {
posx = depth;
}
else {
posx = depth*posx;
}
//pc.printf("depth: %f, angle: %f, tan: %f, abs: %f, 1/abs:%f\r\n",depth, angle, degTan(angle), abs(degTan(angle)), posx);
if(angle >=0 && angle <180) { //set y
setServoAngle(servoy,posy);
}
else {
setServoAngle(servoy,-posy);
}
if (angle <270 && angle >=90) { //setx
setServoAngle(servox,-posx);
}
else {
setServoAngle(servox,posx);
}
}
No comments:
Post a Comment