Issue about servo moves

Hello everyone. I made a simple gimbal by using arduino uno, 3 MG996R servos and MPU6050. Actually everything works fine except the Z axis servo. The X and Y axis servos moves correctly and fine but Z axis servo cannot keep the balance and shakes.

Here’s the code

#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>

//Kutuphaneleri ekledik
MPU6050 mpu;
Servo servoX;
Servo servoY;
Servo servoZ;

//MPU6050 sensorunu ve uc eksende hareket edecek servo motorları tanimladik

void setup() {

  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();
  servoX.attach(9);  // X eksenine bağlı servo
  servoY.attach(10); // Y eksenine bağlı servo
  servoZ.attach(11);  // Z eksenine bağlı servo

}
//seri ve I2C haberlesmeyi baslattik, servolarin hangi pinlerde bagli oldugunu tanimladik

void loop() {
  int16_t ax, ay, az, gx, gy, gz;
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy,&gz);

  //MPU6050 sensorunden gelen ivme(a) ve hiz(g) verilerini aldik

  if (isnan(ax) || isnan(ay) || isnan(az) || isnan(gx) || isnan(gy) || isnan(gz)) {

    Serial.println("Sensör verileri okunamıyor!");
    delay(10);
    return;
  }
  //gelen verilerde okunamayan bir deger varsa ekrana bir hata mesaji gonderdik

  float accX = ax / 16384.0;
  float accY = ay / 16384.0;
  float accZ = az / 16384.0;
  float angleX = atan2(accX, sqrt(1 - accX * accX)) * 180.0 / PI;
  float angleY = atan2(accY, sqrt(1 - accY * accY)) * 180.0 / PI;
  float accZnorm = constrain(accZ, -1.0, 1.0); // Sınırları -1 ile 1 arasında tut
  float angleZ = asin(accZnorm) * 180.0 / PI;
  //aldigimiz ivme verilerini kullanarak x,y ve z eksenindeki acilari hesapladik
  angleX = constrain(angleX, -90, 90);
  angleY = constrain(angleY, -90, 90);
  angleZ = constrain(angleZ, -90, 90);
  //elde ettigimiz acilari servolarin hareket acilari icerisinde sinirladik

  int servoPositionX = map(angleX, -90, 90, 0, 180);
  int servoPositionY = map(angleY, -90, 90, 0, 180);
  int servoPositionZ = map(angleZ, -90, 90, 0, 180);
  //hesaplanan aci degerlerini servolarin kabul ettigi aci araliklarinda sinirladik
  servoX.write(servoPositionX);
  servoY.write(servoPositionY);
  servoZ.write(servoPositionZ);
  //servo motorlari belirlenen acilara hareket ettirdik
  Serial.print("Açı (X): "); Serial.print(angleX);
  Serial.print(" | Açı (Y): "); Serial.print(angleY);
  Serial.print(" | Açı (Z): "); Serial.print(angleZ);
  Serial.print(" | Servo X Pozisyonu: "); Serial.print(servoPositionX);
  Serial.print(" | Servo Y Pozisyonu: "); Serial.println(servoPositionY);
  Serial.print(" | Servo Z Pozisyonu: "); Serial.println(servoPositionZ);
  //kodun ve kurdugumuz sistemin saglikli calisip calismadigini gormek icin sensor verilerini       ve motorlarin hareket ettigi acilari serial monitor'e yazdirdik
 delay(10); //sensor verilerinin ve motorlarin 10 milisaniyede bir guncellenmesini sagladik
}

Print servoPositionZ out to see how it changes.


Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.

  • You cannot power motors from the Arduino 5V pin !

:scream:

It is clearly not working good.

If you don't want to know about your mistakes then don't bother asking a question.

I tried to power the servos directly from the battery. but that didn't change anything. the 11 pinned servo was still the problem. That is I thought the problem might be the code. If you want to help I'm open to listen to you. Otherwise I am not going to argue with you.

Quite honestly you can't say that without any detail and testing of your circuit.

As to your code, yes it is flawed. You seem to make the assumption that you can measure where a servo is at any instant during its movement. You can't do this with the servo you have. All you will see is the last command you sent to the servo. So yes your code is a mess.

But there is absolutely no point in trying to sort your code out until you get the hardware correct.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.