Gyro Stabilizer with Arduino and 3-Axis Accelometers
There should be 3 axis accelometres and gyro sensor for gyro stabilizer . But scarcity of several equipment, I only use 3 axis accelometers . Sorry for my bad english.
You all succes guys!
You all succes guys!
Materials :
3x servo motors
1x gyro stand( you can print or build it )
1x arduino
1xarduino sheild
1x 3-axis accelometers
I use 2 servo's pins from dc motor sheild's and one servo pins from arduino.Because dc motor sheild can only control two servo motors.
Here you can see the basic scheme of gyro-stibilizer.
Painted blues are gyro stand
Painted reds are servos.
Servo 1 and Servo 2 has reverse rotation each others.Servo 1 is connected directly to the arduino's pins. Servo 2 and Servo 3 is connected via dc motor sheild.
Arduino codes:
Servo servo1;
Servo servo2;
Servo servo3;
#define MAXx 180
#define MINx 0
#define MAXy 170
#define MINy 0
#define MAXz 180
#define MINz 0
void Sol_on(void);
void Sol_arka(void);
void Sol_sabit(void);
void Sag_on(void);
void Sag_arka(void);
void Sag_sabit(void);
void Full_Sabit(void);
void Sadece_Arka(void);
void Sadece_On(void);
int xdeg ;
int ydeg ;
int zdeg ;
int p,u;
int e =90 , e1=90, e2 =90,t ;
int a,b;
void setup(){
servo1.attach(10); //5
servo2.attach(9);//dc motor sheild
servo3.attach(6);//dc motor sheild
servo1.write(90);
servo2.write(90);
servo3.write(90);
Serial.begin(9600) ;
}
float mapFloat(float x, float iMin, float iMax, float oMin, float oMax) {
return (x - iMin) * (oMax - oMin) / (iMax - iMin) + oMin;
}
void loop(){
long x , y , z ;
x = analogRead(A2);
y = analogRead(A1);
z = analogRead(A0);
delay(20);
float xCos = mapFloat(x, 391, 751, -1, 1);
float yCos = mapFloat(y, 420, 770, -1, 1);
float zCos= mapFloat(z, 770 ,605 ,0, 1);//770
xCos = constrain(xCos,-1,1);
yCos = constrain(yCos,-1,1);
zCos = constrain(zCos,0,1);
xdeg = float(acos(xCos) * 180 / PI );
ydeg = float(acos(yCos) * 180 / PI );
zdeg = float(acos(zCos) * 180 / PI );
if(xdeg>=MAXx)
xdeg=MAXx;
if(xdeg<=MINx)
xdeg=MINx;
if(ydeg>=MAXy)
ydeg=MAXy;
if(ydeg<=MINy)
ydeg=MINy;
if(zdeg>=MAXz)
zdeg=MAXz;
if(zdeg<=MINz)
zdeg=MINz;
delay(10);
if((e-5)<=ydeg && ydeg <=(e+5) ){
ydeg=e;
}
if((e1-5)<=xdeg && xdeg <=(e1+5) ){
xdeg=e1;
}
if((e2-5)<=zdeg && zdeg <=(e2+5) ){
zdeg=e2;
}
a = 180-zdeg;//ters
b =zdeg;//düz
t =20 ;
if(a>=120 && b<=60 ){
a=120;
b=60;}
if(b>=120 && a<=60 ){
b=120;
a=60;}
Serial.print(" ");
Serial.print(xdeg);
Serial.print(" ");
Serial.print(ydeg);
Serial.print(" ");
Serial.print(zdeg);
Serial.print(" ");
Serial.print(" ");
Serial.print(e1);
Serial.print(" ");
Serial.print(e);
Serial.print(" ");
Serial.print(e2);
Serial.print(" ");
delay(10);
p=0, u=0;
Sol_arka();
Sol_on();
Sol_sabit();
//-------------------
Sag_arka();
Sag_on();
Sag_sabit();
//-----------------
Full_Sabit();
Sadece_Arka();
Sadece_On();
e=ydeg;
e1=xdeg;
e2=zdeg;
Serial.println();
delay(50) ;
}
void Sol_on(){
if(ydeg >=0 && ydeg<74 && xdeg >=0 && xdeg<78&& p==0 ){
Serial.print("Sol On ");
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(a);
servo2.write(b);
delay(t);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(b);
}
}
void Sol_arka(){
if(ydeg >=0 && ydeg<74 && xdeg >110 && xdeg <= 180 ){
Serial.print("Sol Arka ");
servo3.write (180-ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(b);
servo2.write(a);
Serial.print(" Servo 1 =");
Serial.print(b);
Serial.print(" Servo 2 =");
Serial.print(a);
delay(t);
p=1;
}}
void Sol_sabit(){
if(ydeg >=0 && ydeg<74 && xdeg >=78 && xdeg<=110 && p==0 ){
Serial.print(" Sol Sabit");
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(a);
servo2.write(b);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(b);
delay(t);
}
}
void Sag_on()
{if(ydeg >100 && ydeg <= 180 &&xdeg >=0 && xdeg<78 && u ==0 ){
Serial.print("Sag On ");
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(a);
servo2.write(b);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(b);
delay(t);
}}
void Sag_arka(){
if(ydeg >100 && ydeg <= 180 && xdeg >110 && xdeg <= 180){
Serial.print("Sag ve arka ");
servo3.write (180-ydeg);
Serial.print(" Servo 3 =");
Serial.print(180-ydeg);
servo1.write(b);
servo2.write(a);
delay(t);
Serial.print(" Servo 1 =");
Serial.print(b);
Serial.print(" Servo 2 =");
Serial.print(a);
u=1;
}}
void Sag_sabit(){
if(ydeg >100 && ydeg <= 180 && xdeg >=78 && xdeg<=110 && u ==0){
Serial.print(" Sag Sabit ");
a=b=90;
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(b);
servo1.write(a);
servo2.write(b);
delay(t);
}}
void Sadece_On(){ // X eksen Sabit
if(ydeg >=74 && ydeg<=100 &&xdeg >=0 && xdeg<78 ){
Serial.print("Sadece On ");
ydeg=90;
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(a);
servo2.write(b);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(b);
delay(t);
}
}
void Sadece_Arka(){
if(ydeg >=74 && ydeg<=100 && xdeg >110 && xdeg <= 180 ){
Serial.print("Sadece Arka ");
ydeg=90;
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
servo1.write(b);
servo2.write(a);
Serial.print(" Servo 1 =");
Serial.print(b);
Serial.print(" Servo 2 =");
Serial.print(a);
delay(t);
}}
void Full_Sabit(){
if(ydeg >=74 && ydeg<=100&&xdeg >=78 && xdeg<=110 ){
Serial.print("Full Sabit ");
ydeg=90;
servo3.write (ydeg);
Serial.print(" Servo 3 =");
Serial.print(ydeg);
a=b=90;
servo1.write(a);
servo2.write(a);
Serial.print(" Servo 1 =");
Serial.print(a);
Serial.print(" Servo 2 =");
Serial.print(a);
delay(t);
}}
Yorumlar
Yorum Gönder