diff --git a/sketch_mar8a.ino b/sketch_mar8a.ino new file mode 100644 index 0000000..4aafac9 --- /dev/null +++ b/sketch_mar8a.ino @@ -0,0 +1,139 @@ +#include +#include + +#define SERVOVELOCITY 1 + +Servo s6,s5,s4,s3,s2; +int x,y,z; + String s; + +float a0, a1, a2, a3, temp1, temp2, temp3, temp4; +int servo6Pos, servo5Pos, servo4Pos, servo3Pos, servo6Vel, servo5Vel,servo4Vel,servo3Vel; + +void moveToPos(int servo6End, int servo5End, int servo4End, int servo3End){ + + servo6Vel = servo6Pos > servo6End ? SERVOVELOCITY * (-1) : SERVOVELOCITY; + servo5Vel = servo5Pos > servo5End ? SERVOVELOCITY * (-1) : SERVOVELOCITY; + servo4Vel = servo4Pos > servo4End ? SERVOVELOCITY * (-1) : SERVOVELOCITY; + servo3Vel = servo3Pos > servo3End ? SERVOVELOCITY * (-1) : SERVOVELOCITY; + + bool b6 = false, b5 = false, b4 = false, b3 = false; + + while(true){ + + if(servo6Pos != servo6End){ + servo6Pos+=servo6Vel; + s6.write(servo6Pos); + Serial.println("S6: "+ String(servo6Pos) + " - " + String(servo6End)); + }else b6 = true; + + if(servo5Pos != servo5End){ + servo5Pos+=servo5Vel; + s5.write(servo5Pos); + Serial.println("S5: "+ String(servo5Pos) + " - " + String(servo5End)); + } else b5 = true; + + if(servo4Pos != servo4End){ + servo4Pos+=servo4Vel; + s4.write(servo4Pos); + Serial.println("S4: "+ String(servo4Pos) + " - " + String(servo4End)); + } else b4 = true; + + if(servo3Pos != servo3End){ + servo3Pos+=servo3Vel; + s3.write(servo3Pos); + Serial.println("S3: "+ String(servo3Pos) + " - " + String(servo3Pos)); + } else b3 = true; + + delay(200); + + if(b6 && b5 && b4 && b3){ + Serial.println("Moved!"); + return; + } + + } +} +void setup() { + //9600 GEHT NICHT! IN DIE DOKU + Serial.begin(115200); + //Serial.setTimeout(1); + s6.attach(6); + s5.attach(5); + s4.attach(4); + s3.attach(3); + s2.attach(2); + + + s6.write(10); + servo6Pos = 90; + s5.write(180); + servo5Pos = 90; + s4.write(90); + servo4Pos = 10; + s3.write(10); + servo3Pos = 170; + s2.write(90); + + + //Definition der Bestandteile des Arms für die Inverse Kinematik + Link base, upperarm, forearm, hand; + + base.init(20, b2a(0), b2a(180.0)); + upperarm.init(122, b2a(0.0), b2a(180.0)); + forearm.init(80, b2a(0.0), b2a(180.0)); + hand.init(170, b2a(0.0), b2a(180.0)); + + InverseK.attach(base, upperarm, forearm, hand); + + + + String s; + + +} + +void loop() { + // put your main code here, to run repeatedly: + s = Serial.readString(); + if(sscanf(s.c_str(),"%d %d %d",&x,&y,&z) == 3){ + + if(x == 0 && y == 0 && z == 0){ + moveToPos(10,170,90, 0); + } + if(x == -1 && y == -1 && z == -1){ + moveToPos(90, 150, 50, 100); + delay(3000); + s2.write(90); + moveToPos(10,170,10, 170); + } + + Serial.print("x: "); Serial.print(x); Serial.print("\n"); + Serial.print("y: "); Serial.print(y); Serial.print("\n"); + Serial.print("z: "); Serial.print(z); Serial.print("\n"); + + if(InverseK.solve(x, y, z, a0, a1, a2, a3)) { + Serial.println(a2b(a0)); + Serial.println(a2b(a1)); + Serial.println(a2b(a2)); + Serial.println(a2b(a3)); + moveToPos(a2b(-a0),a2b(-a1),a2b(a2),a2b(-a3)); + //s6.write(a2b(-a0)); + //s5.write(a2b(-a1)); + //s4.write(a2b(a2)); + //s3.write(a2b(-a3)); + delay(1000); + s2.write(0); + delay(2000); + moveToPos(10,150,10, 170); + } + } +} +float b2a(float b){ + return b / 180.0 * PI - HALF_PI; +} + +// Quick conversion from radians to the Braccio angle system +float a2b(float a) { + return (a + HALF_PI) * 180 / PI; +}