1

I am using the MPU6050_light library for getting the data for my quad-copter. When I am obtaining the gyro values from the MPU6050 without running the motors, its giving correct values but on running the motors and getting the values, I am getting a huge spike on both axes. I am a newbie and a tenth-grader so i don't know so much about electronics. I tried using a Kalman filter but could not find any code which can help me and the Kalman Library was too complex. I think if I apply some kind of filter the problem will be solved. If any one knows any other filter and its code, if you can share it, it would be of great help. The code is as follows:

#include <SoftwareSerial.h>
#include <Servo.h>
#include <MPU6050_light.h>
#include <Wire.h>

double x = 0.0, y = 0.0, z = 0.0;
int c=0;

SoftwareSerial Slave(5,6);
Servo m1, m2, m3, m4, l;
MPU6050 mpu(Wire);

int throttle = 0;
int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
unsigned long timer;
int of = 0;

void setup()
{
  Slave.begin(9600);
  Serial.begin(9600);
  Wire.begin();
  byte stat = mpu.begin();
  del(1000);
  mpu.calcOffsets();
  del(2000);
  m1.attach(3, 800, 2000);
  m2.attach(9, 800, 2000);
  m3.attach(10, 800, 2000);
  m4.attach(11, 800, 2000);
  l.attach(12, 800, 2000);
  throttle = 0;
  normal();
  del(10000);
  l.detach();
  while (of == 0)
  {debug();}
}

void loop()
{
  normal();
  debug(); 
  
  if (of == 0)
  {
    safe_land();
  }
 
  getxyz();
  
  if (x < -5.0)
  {
    t1 = t1 + 1;
    t3 = t3 + 1;
    debug();
    normal();
    getxyz();
  }


  if (x > 5.0)
  {
    t2 = t2 + 1;
    t4 = t4 + 1;
    debug();
    normal();
    getxyz();
  }
  if (y < -5.0)
  {
    t4 = t4 + 1;
    t3 = t3 + 1;
    debug();
    normal();
    getxyz();
  }


  if (y > 5.0)
  {
    t2 = t2 + 1;
    t1 = t1 + 1;
    debug();
    normal();
    getxyz();
  }
 
}
void normal()
{
  m1.write(throttle + t1);
  m2.write(throttle + t2);
  m3.write(throttle + t3);
  m4.write(throttle + t4);

}
void safe_land()
{
  throttle = 0;
  t1 = 0;
  t2 = 0;
  t3 = 0;
  t4 = 0;
  normal();
  off();
}
void off()
{
  m1.detach();
  m2.detach();
  m3.detach();
  m4.detach();
  while (true);
}
void del(int tii)
{
  unsigned long tim = (unsigned long)(tii);
  unsigned long t = millis();
  while (millis() - t <= tim);
}
void getxyz()
{
  mpu.update();
  x = (double)(mpu.getAngleX());
  Serial.print("X:");
  Serial.println(x);
  y = (double)(mpu.getAngleY());
  Serial.print("Y:");
  Serial.println(y);
  //z = (double)(mpu.getAngleZ());
}

void debug()
{
  
  if (Slave.available())
  {
   c=(int)(Slave.read());
  }
   if(c<=180)
   {
    throttle=c;
   }
   if(c==200 || c==201)
   {
    of=c-200;
   }
}

I am also using two Arduino UNOs, hence the software serial...

4
  • what is your question? ... have you tried ignoring gyro data when the motors start up? – jsotola Jan 20 at 4:09
  • @jsotola I can't make the drone balance itself because the mpu6050 values are coming wrong when i am starting the motors... the gyro data at start should be 0.0 both x and y because they are on the ground but they are coming x:10 and y:-12 and not 0.0 – coder-ari Jan 20 at 5:01
  • that's why I said to ignore the the gyro when you are starting the motors .... try reading the gyro data after the motors started – jsotola Jan 20 at 8:21
  • @jsotola thnx will try and say – coder-ari Jan 20 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.