Hello everyone,
I am currently working on a code for a robot dog using Arduino Mega and a Servo shield. The code is for one leg whose upper and lower legs are both controlled by one servo . When I load the code, the servos just set their position to 160° (see code below) and stay there. Why does the code and/ or the leg not run the different X values (from 0 to 60) so that the angles (A and B) can be calculated?
P.S.: The movement that needs to be performed is a semicircle.
Thanks in advance
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define Frequenz 50
#define Umax 2000
#define Umin 500
#define PI 3.1415926535897932384626433832795
String Servochannel;
int X;
void setup() {
Serial.begin(9600);
Serial.println("Test5");
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(Frequenz); // Analog servos run at ~50 Hz updates
delay(10);
X = 0;
}
void setServo(int servo, int winkel) {
int duty, pulsweite;
duty = map(winkel, 0, 180, Umin, Umax);
pulsweite = int(float(duty) / 1000000 * Frequenz * 4096);
pwm.setPWM(servo, 0, pulsweite);
}
void loop() {
setServo(0, 160 );
setServo(1, 160 );
delay(1000);
IK();
}
void IK() {
int a = 204.0, b = 176.0; // a= upper leg lenght, b= lower leg lenght)
float A, B;
float Theta;
int h, d;
float Y;
h = 290;
{
for (X = 0; X <= 60; X += 1) {
Y = sqrt(900 + pow((X - 30), 2));
Theta = (50 + (atan(X / h) * 180) / PI);
d = abs(((h - Y) / (cos(Theta) * PI / 180)));
A = (acos((pow(a, 2) + pow(d, 2) - pow(b, 2)) / (2 * a * d)) * 180) / PI;
B = (acos((pow(a, 2) + pow(b, 2) - pow(d, 2)) / (2 * a * b)) * 180) / PI;
setServo(0, A);
setServo(1, B);
delay(3000);
}
}
}
[Sketch_robodog.pdf.pdf|attachment](upload://8XdqDzaswRqfmj3puI31AFfscfG.pdf) (40.3 KB)