Go Down

### Topic: [Solved]Problem reading MPU-6050 pitch and yaw (Read 472 times)previous topic - next topic

#### akiskaps

##### Mar 02, 2019, 01:26 amLast Edit: Mar 04, 2019, 11:59 pm by akiskaps
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.

#### jremington

#1
##### Mar 02, 2019, 01:34 am
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.

#### akiskaps

#2
##### Mar 02, 2019, 01:55 am
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.

#### jremington

#3
##### Mar 02, 2019, 01:57 amLast Edit: Mar 02, 2019, 01:59 am by jremington
Very good, then. Simply subtract the offsets and have fun flying your drone.

#### akiskaps

#4
##### Mar 02, 2019, 03:00 amLast Edit: Mar 02, 2019, 07:05 pm by akiskaps
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.

Code: [Select]
`//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.  }`

#### jremington

#5
##### Mar 02, 2019, 04:47 pm
For informed help, please read and follow the directions in the "How to use this forum" post.

#### jremington

#6
##### Mar 02, 2019, 06:39 pm
Quote
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.

#### akiskaps

#7
##### Mar 02, 2019, 07:08 pmLast Edit: Mar 02, 2019, 07:09 pm by akiskaps
Problem solved.Thank you a lot for your help and time. I just had to substract the wrong values at this part of code.

Code: [Select]
`//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]`

Go Up