[Solved]Problem reading MPU-6050 pitch and yaw

Hello, I am working on a drone that uses a MPU-6050 module for stabilization. I have calibrated it a lot of times but always when is at rest, the pitch and yaw are not zero(about 4-7). Thank you for your time, any advice is welcome.

P.S.
Let me know if anyone needs the part of code that calibrates the gyro or any other information.

The MPU-6050 does not have a magnetometer, so it can't produce a stable yaw reference. The starting yaw value is arbitrary, and will drift with time.

jremington:
The MPU-6050 does not have a magnetometer, so it can't produce a stable yaw reference. The starting yaw value is arbitrary, and will drift with time.

Thank you for your response. The readings are all stable and correct. The problem is that when in rest the yaw and pitch are not zero. That makes the drone to fly forward while is trying to stabilize it to 0.

Very good, then. Simply subtract the offsets and have fun flying your drone.

If you mean to substract the wrong values at every reading to get to 0 all axes then it is reading big values like -1990,1876 etc. Here is the part of the code.

//Gyro angle calculations
  //0.0000611 = 1 / (250Hz / 65.5)
  angle_pitch += gyro_pitch * 0.0000611;                                    //Calculate the traveled pitch angle and add this to the angle_pitch variable.
  angle_roll += gyro_roll * 0.0000611;                                      //Calculate the traveled roll angle and add this to the angle_roll variable.

  //0.000001066 = 0.0000611 * (3.142(PI) / 180degr) The Arduino sin function is in radians
  angle_pitch -= angle_roll * sin(gyro_yaw * 0.000001066);                  //If the IMU has yawed transfer the roll angle to the pitch angel.
  angle_roll += angle_pitch * sin(gyro_yaw * 0.000001066);                  //If the IMU has yawed transfer the pitch angle to the roll angel.

  //Accelerometer angle calculations
  acc_total_vector = sqrt((acc_x*acc_x)+(acc_y*acc_y)+(acc_z*acc_z));       //Calculate the total accelerometer vector.
  
  if(abs(acc_y) < acc_total_vector){                                        //Prevent the asin function to produce a NaN
    angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.296;          //Calculate the pitch angle.
  }
  if(abs(acc_x) < acc_total_vector){                                        //Prevent the asin function to produce a NaN
    angle_roll_acc = asin((float)acc_x/acc_total_vector)* -57.296;          //Calculate the roll angle.
  }
  
  //Place the MPU-6050 spirit level and note the values in the following two lines for calibration.
  angle_pitch_acc -= 0.0;                                                   //Accelerometer calibration value for pitch.
  angle_roll_acc -= 0.0;                                                    //Accelerometer calibration value for roll.
  
  angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;            //Correct the drift of the gyro pitch angle with the accelerometer pitch angle.
  angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;               //Correct the drift of the gyro roll angle with the accelerometer roll angle.

  pitch_level_adjust = angle_pitch * 15;                                    //Calculate the pitch angle correction
  roll_level_adjust = angle_roll * 15;                                      //Calculate the roll angle correction

  if(!auto_level){                                                          //If the quadcopter is not in auto-level mode
    pitch_level_adjust = 0;                                                 //Set the pitch angle correction to zero.
    roll_level_adjust = 0;                                                  //Set the roll angle correcion to zero.
  }

For informed help, please read and follow the directions in the "How to use this forum" post.

Please edit your post to add code tags.

If you mean to substract the wrong values at every reading to get to 0 all axes then it is reading big values like -1990,1876

This makes no sense at all. Post ALL the code that demonstrates the problem.

Problem solved.Thank you a lot for your help and time. I just had to substract the wrong values at this part of code.

//Place the MPU-6050 spirit level and note the values in the following two lines for calibration.
  angle_pitch_acc -= 0.0;                                                   //Accelerometer calibration value for pitch.
  angle_roll_acc -= 0.0;                                                    //Accelerometer calibration value for roll.
[code]