kriswiner / MPU9250

Arduino sketches for MPU9250 9DoF with AHRS sensor fusion
1.04k stars 471 forks source link

MPU9250 tilt compensated compass and Y-Axis inclination #102

Open Lymkin opened 7 years ago

Lymkin commented 7 years ago

Is it possible to convert the quaternions calculated from your libray into a tilt compensated compass reading? I also would like to calculate Y-Axis inclination angle. I've tried every equation I could find on the internet for a couple of weeks now with no luck. Any insight if what I am trying to get is even possible? My lack of a PhD in mathematics just leaves me confused!

kriswiner commented 7 years ago

It is possible to convert the quats into yaw (heading) pitch and roll. The heading is a tilt-compensated compass reading. Is this what you mean? Pitch and roll are x- and y-axis inclination angles. What's the problem exactly?

On Fri, Dec 30, 2016 at 10:10 AM, Lymkin notifications@github.com wrote:

Is it possible to convert the quaternions calculated from your libray into a tilt compensated compass reading? I also would like to calculate Y-Axis inclination angle. I've tried every equation I could find on the internet for a couple of weeks now with no luck. Any insight if what I am trying to get is even possible? My lack of a PhD in mathematics just leaves me confused!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU-9250/issues/102, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qhkhVrG1foUw2N5kqiVDV-V5i5Jtks5rNUkBgaJpZM4LYUVl .

Lymkin commented 7 years ago

So, is yaw from your code suppose to already give the heading? yaw = atan2(2.0f (q[1] q[2] + q[0] q[3]), q[0] q[0] + q[1] q[1] - q[2] q[2] - q[3] q[3]);
pitch = -asin(2.0f
(q[1] q[3] - q[0] q[2])); roll = atan2(2.0f (q[0] q[1] + q[2] q[3]), q[0] q[0] - q[1] q[1] - q[2] q[2] + q[3] q[3]); pitch = 180.0f / PI; yaw = 180.0f / PI; yaw -= 1.15; // Declination at my location roll = 180.0f / PI;

Do I need to convert those values to get actual degrees? (0-360)? When my MPU is flat on a surface and rotated my yaw only shows a range from about 15.25 to 89.88.

I'm trying to measure azimuth and elevation to track the sun at a static location. So, I basically want to attach the MPU to a solar panel so I can feed azimuth and elevation to my controller and have it move the panel to that location based on MPU input.

Also, are these static values for all 9250's or something I have to derive? magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated magbias[1] = +120.; // User environmental x-axis correction in milliGauss magbias[2] = +125.; // User environmental x-axis correction in milliGauss

Thanks for your help!

kriswiner commented 7 years ago

Yaw pitch and roll are in degrees.

Did you calibrate your sensors?

How?

On Fri, Dec 30, 2016 at 10:38 AM, Lymkin notifications@github.com wrote:

So, is yaw from your code suppose to already give the heading? yaw = atan2(2.0f (q[1] q[2] + q[0] q[3]), q[0] q[0] + q[1] * q[1]

  • q[2] q[2] - q[3] q[3]); pitch = -asin(2.0f (q[1] q[3] - q[0] q[2])); roll = atan2(2.0f (q[0] q[1] + q[2] q[3]), q[0] q[0] - q[1] q[1]
  • q[2] q[2] + q[3] q[3]); pitch = 180.0f / PI; yaw = 180.0f / PI; yaw -= 1.15; // Declination at my location roll *= 180.0f / PI;

Do I need to convert those values to get actual degrees? (0-360)? When my MPU is flat on a surface and rotated my yaw only shows a range from about 15.25 to 89.88.

I'm trying to measure azimuth and elevation to track the sun at a static location. So, I basically want to attach the MPU to a solar panel so I can feed azimuth and elevation to my controller and have it move the panel to that location based on MPU input.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU-9250/issues/102#issuecomment-269806435, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qgg58wDYKYTwuobINT4P7F9g29sYks5rNU-IgaJpZM4LYUVl .

Lymkin commented 7 years ago

Good point....I did not calibrate, but I do not see how to perform a calibration within the code. I'm using MPU9250BasicAHRS.ino example

I think I was editing my previous post after you replied.

Are these static values for all 9250's or something I have to derive? Is this where the calibration values come into play? magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated magbias[1] = +120.; // User environmental x-axis correction in milliGauss magbias[2] = +125.; // User environmental x-axis correction in milliGauss

kriswiner commented 7 years ago

Don't use those static values, that is for a particular device, not yours. Use this sketch:

https://github.com/kriswiner/MPU-9250/blob/master/MPU9250_MS5637_AHRS_t3.ino

It has calibration functions in it.

Plot Mx vs Mz to see if calibrated. You shoukd see a circle centered on the origin if properly calibrated. See:

https://github.com/kriswiner/MPU-6050/wiki/Simple-and-Effective-Magnetometer-Calibration

On Fri, Dec 30, 2016 at 11:12 AM, Lymkin notifications@github.com wrote:

Good point....I did not calibrate, but I do not see how to perform a calibration within the code. I'm using MPU9250BasicAHRS.ino example

I think I was editing my previous post after you replied.

Are these static values for all 9250's or something I have to derive? Is this where the calibration values come into play? magbias[0] = +470.; // User environmental x-axis correction in milliGauss, should be automatically calculated magbias[1] = +120.; // User environmental x-axis correction in milliGauss magbias[2] = +125.; // User environmental x-axis correction in milliGauss

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU-9250/issues/102#issuecomment-269810385, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qg6ylVYIizLjfraBswbW1s6xSUYCks5rNVevgaJpZM4LYUVl .

Lymkin commented 7 years ago

Calibration definitely helped for pitch as it seems pretty accurate, but yaw still not reliable. I can put the MPU on a flat surface and rotate it 180 degrees and the values are only changing about 90 degrees. Unfortunately I cannot plot anything as I'm running this off of an ESP8266 and I don't have a display other than serial output at the moment.

Here is some output: MPU9250 9-axis motion sensor... MPU9250 I AM 71 I should be 71 MPU9250 is online... x-axis self test: acceleration trim within : 0.3% of factory value y-axis self test: acceleration trim within : 0.9% of factory value z-axis self test: acceleration trim within : 1.2% of factory value x-axis self test: gyration trim within : -2.5% of factory value y-axis self test: gyration trim within : 0.2% of factory value z-axis self test: gyration trim within : 0.0% of factory value Calibrate gyro and accel accel biases (mg) -3.54 -9.83 5.55 gyro biases (dps) -0.73 -0.22 0.82 MPU9250 initialized for active data mode.... AK8963 I AM 48 I should be 48 AK8963 initialized for active data mode.... Mag Calibration: Wave device in a figure eight until done! Mag Calibration done! mag biases (mG) 89.90 124.47 140.43

Here is rotation of what should be 90 degrees around zero, but it shows only about 45 degrees either way: ax = -86.36 ay = 23.62 az = 995.24 mg gx = 0.07 gy = -0.05 gz = -0.70 deg/s mx = -17 my = 550 mz = 105 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.02 Gyro temperature is 29.1 degrees C Yaw, Pitch, Roll: 0.95, 4.96, 1.36 rate = 629.44 Hz ax = -86.18 ay = 23.25 az = 998.35 mg gx = 0.03 gy = -0.08 gz = -0.74 deg/s mx = -8 my = 555 mz = 138 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.01 Gyro temperature is 29.2 degrees C Yaw, Pitch, Roll: 0.09, 4.93, 1.34 rate = 633.74 Hz ax = -86.61 ay = 21.61 az = 997.31 mg gx = 0.08 gy = -0.02 gz = -0.89 deg/s mx = -23 my = 552 mz = 149 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.02 Gyro temperature is 29.2 degrees C Yaw, Pitch, Roll: 1.55, 4.96, 1.24 rate = 635.80 Hz ax = -100.65 ay = 28.87 az = 1000.61 mg gx = -2.37 gy = -0.98 gz = 26.52 deg/s mx = -70 my = 548 mz = 48 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.07 Gyro temperature is 29.2 degrees C Yaw, Pitch, Roll: 6.89, 4.83, 1.44 rate = 634.39 Hz ax = -144.29 ay = 33.81 az = 994.45 mg gx = -2.75 gy = -0.71 gz = 35.93 deg/s mx = -122 my = 514 mz = 57 mG q0 = -0.99 qx = -0.01 qy = -0.04 qz = -0.12 Gyro temperature is 29.2 degrees C Yaw, Pitch, Roll: 12.20, 4.75, 1.49 rate = 634.14 Hz ax = -42.30 ay = 5.43 az = 1003.23 mg gx = -1.75 gy = -1.54 gz = 23.83 deg/s mx = -187 my = 488 mz = 36 mG q0 = -0.98 qx = 0.00 qy = -0.04 qz = -0.17 Gyro temperature is 29.3 degrees C Yaw, Pitch, Roll: 18.79, 4.99, 0.67 rate = 634.69 Hz ax = -92.04 ay = 19.71 az = 996.89 mg gx = -0.64 gy = -0.37 gz = 5.81 deg/s mx = -228 my = 436 mz = 76 mG q0 = -0.97 qx = 0.00 qy = -0.04 qz = -0.25 Gyro temperature is 29.3 degrees C Yaw, Pitch, Roll: 28.33, 4.92, 1.12 rate = 640.26 Hz ax = -108.40 ay = 19.17 az = 991.52 mg gx = -2.78 gy = -0.11 gz = 26.28 deg/s mx = -248 my = 369 mz = 60 mG q0 = -0.95 qx = 0.01 qy = -0.04 qz = -0.30 Gyro temperature is 29.4 degrees C Yaw, Pitch, Roll: 34.08, 4.75, 0.66 rate = 645.93 Hz ax = -75.07 ay = 5.68 az = 1004.15 mg gx = -0.70 gy = -0.27 gz = 6.65 deg/s mx = -269 my = 294 mz = 98 mG q0 = -0.93 qx = 0.01 qy = -0.04 qz = -0.37 Gyro temperature is 29.4 degrees C Yaw, Pitch, Roll: 42.34, 4.47, 0.53 rate = 643.03 Hz ax = -83.86 ay = 9.03 az = 1001.22 mg gx = -0.07 gy = -0.17 gz = -0.39 deg/s mx = -253 my = 277 mz = 100 mG q0 = -0.93 qx = 0.01 qy = -0.04 qz = -0.36 Gyro temperature is 29.5 degrees C Yaw, Pitch, Roll: 40.78, 4.70, 0.47 rate = 642.78 Hz ax = -82.03 ay = 7.08 az = 998.35 mg gx = 0.04 gy = -0.21 gz = -0.34 deg/s mx = -269 my = 279 mz = 95 mG q0 = -0.93 qx = 0.01 qy = -0.04 qz = -0.37 Gyro temperature is 29.5 degrees C Yaw, Pitch, Roll: 42.41, 4.66, 0.43 rate = 642.95 Hz ax = -80.69 ay = 8.91 az = 997.19 mg gx = -0.05 gy = -0.26 gz = -0.18 deg/s mx = -242 my = 248 mz = 79 mG q0 = -0.93 qx = 0.01 qy = -0.04 qz = -0.37 Gyro temperature is 29.5 degrees C Yaw, Pitch, Roll: 42.48, 4.71, 0.46 rate = 642.55 Hz ax = -81.05 ay = 8.06 az = 1002.99 mg gx = -0.10 gy = -0.22 gz = -0.27 deg/s mx = -255 my = 243 mz = 78 mG q0 = -0.92 qx = 0.01 qy = -0.04 qz = -0.39 Gyro temperature is 29.5 degrees C Yaw, Pitch, Roll: 44.24, 4.72, 0.35 rate = 645.27 Hz ax = -78.25 ay = 6.96 az = 997.07 mg gx = 2.94 gy = 0.37 gz = -30.75 deg/s mx = -271 my = 310 mz = 79 mG q0 = -0.95 qx = 0.01 qy = -0.04 qz = -0.32 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 36.51, 4.73, 0.50 rate = 641.91 Hz ax = -91.13 ay = 16.91 az = 997.31 mg gx = 2.53 gy = 0.46 gz = -30.54 deg/s mx = -222 my = 445 mz = 88 mG q0 = -0.98 qx = 0.00 qy = -0.04 qz = -0.21 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 23.57, 4.98, 0.92 rate = 641.77 Hz ax = -83.37 ay = 22.22 az = 1000.06 mg gx = 0.91 gy = -0.47 gz = -7.46 deg/s mx = -91 my = 533 mz = 55 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.08 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 8.57, 5.00, 1.34 rate = 645.76 Hz ax = -83.50 ay = 24.41 az = 998.54 mg gx = -0.09 gy = -0.06 gz = -0.94 deg/s mx = -59 my = 541 mz = 62 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = -0.06 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: 5.22, 4.77, 1.40 rate = 640.34 Hz ax = -90.94 ay = 17.58 az = 995.85 mg gx = 3.11 gy = 0.01 gz = -25.48 deg/s mx = 26 my = 544 mz = 83 mG q0 = -1.00 qx = -0.01 qy = -0.04 qz = 0.02 Gyro temperature is 29.6 degrees C Yaw, Pitch, Roll: -3.89, 5.13, 1.08 rate = 638.08 Hz ax = -119.38 ay = 28.26 az = 991.46 mg gx = 3.87 gy = 3.49 gz = -38.80 deg/s mx = 167 my = 479 mz = 55 mG q0 = -0.98 qx = -0.02 qy = -0.05 qz = 0.17 Gyro temperature is 29.7 degrees C Yaw, Pitch, Roll: -20.34, 6.09, 1.43 rate = 631.06 Hz ax = -103.21 ay = 23.80 az = 996.22 mg gx = 0.45 gy = -0.10 gz = -7.36 deg/s mx = 235 my = 375 mz = 34 mG q0 = -0.96 qx = -0.02 qy = -0.05 qz = 0.27 Gyro temperature is 29.7 degrees C Yaw, Pitch, Roll: -32.28, 5.88, 1.26 rate = 630.63 Hz ax = -104.06 ay = 19.29 az = 1000.55 mg gx = -0.11 gy = -0.31 gz = -1.44 deg/s mx = 258 my = 344 mz = 29 mG q0 = -0.95 qx = -0.02 qy = -0.05 qz = 0.32 Gyro temperature is 29.7 degrees C Yaw, Pitch, Roll: -38.36, 5.85, 1.03 rate = 629.53 Hz ax = -102.66 ay = 18.86 az = 994.38 mg gx = -0.17 gy = -0.18 gz = 0.01 deg/s mx = 248 my = 326 mz = 43 mG q0 = -0.94 qx = -0.02 qy = -0.05 qz = 0.32 Gyro temperature is 29.7 degrees C Yaw, Pitch, Roll: -38.89, 5.90, 1.00 rate = 630.05 Hz ax = -141.97 ay = 10.38 az = 995.00 mg gx = 0.55 gy = -0.20 gz = -8.06 deg/s mx = 255 my = 319 mz = 50 mG q0 = -0.94 qx = -0.03 qy = -0.05 qz = 0.33 Gyro temperature is 29.7 degrees C Yaw, Pitch, Roll: -39.50, 6.03, 1.00 rate = 632.66 Hz ax = -103.45 ay = 16.17 az = 996.83 mg gx = -0.24 gy = -0.13 gz = 0.11 deg/s mx = 260 my = 324 mz = 38 mG q0 = -0.94 qx = -0.03 qy = -0.05 qz = 0.33 Gyro temperature is 29.8 degrees C Yaw, Pitch, Roll: -39.44, 5.93, 1.01 rate = 633.16 Hz ax = -100.71 ay = 17.21 az = 998.17 mg gx = -0.26 gy = -0.17 gz = 0.09 deg/s mx = 269 my = 315 mz = 32 mG q0 = -0.94 qx = -0.03 qy = -0.05 qz = 0.34 Gyro temperature is 29.8 degrees C Yaw, Pitch, Roll: -40.75, 5.89, 0.99 rate = 634.98 Hz ax = -102.29 ay = 17.03 az = 995.00 mg gx = -0.26 gy = -0.12 gz = 0.07 deg/s mx = 273 my = 326 mz = 22 mG q0 = -0.94 qx = -0.03 qy = -0.05 qz = 0.34 Gyro temperature is 29.8 degrees C Yaw, Pitch, Roll: -40.46, 5.92, 0.95 rate = 634.85 Hz ax = -105.22 ay = 16.48 az = 993.04 mg gx = 0.35 gy = 0.08 gz = -7.43 deg/s mx = 262 my = 308 mz = 46 mG q0 = -0.94 qx = -0.03 qy = -0.05 qz = 0.33 Gyro temperature is 29.8 degrees C Yaw, Pitch, Roll: -40.25, 5.94, 0.97 rate = 633.52 Hz ax = -104.31 ay = 16.05 az = 994.20 mg gx = -0.13 gy = -0.19 gz = -0.12 deg/s mx = 273 my = 283 mz = 74 mG q0 = -0.93 qx = -0.03 qy = -0.05 qz = 0.36 Gyro temperature is 29.8 degrees C Yaw, Pitch, Roll: -43.76, 5.98, 0.90 rate = 633.12 Hz

What orientation should the sensor be in to calibrate? What axis of movement for the figure 8? Could my sensor be bad as calibrations seem wildly different everytime.

matherp commented 7 years ago

The critical thing is to get the relative orientations correct in the inputs to the sensor fusion

For a MPU-9250 it should be

ax, ay, az, gx, gy, gz, my, mx, -mz

note the negative sign on mz. This should be applied after all the corrections for scale and offset are applied.

Here is my code in Basic for this demo https://www.youtube.com/watch?v=4eBtP6cpqU4&feature=youtu.be

Note the use of sine, cos and atan2 to get a rolling average of readings whilst solving the 359-0 problem

Hope this helps

DO`
readMPU9250(0)

' SensorFusion Madgwick ax, ay, az, gx, gy, gz, my, mx, -mz, pitch, roll, yaw', beta SensorFusion Mahony ax, ay, az, gx, gy, gz, my, mx, -mz, pitch, roll, yaw', Kp, Ki ' 'rolling average of yaw angles (0-359) ' if yaw<0.0 then yaw=yaw+ 2*pi ctemp=cos(yaw) stemp=SIN(yaw) yawMAc=yawMAc-yawSMAcos(temp%)/MA yawMAs=yawMAs-yawSMAsine(temp%)/MA yawMAc=yawMAc+ctemp/MA yawMAs=yawMAs+stemp/MA yawSMAcos(temp%)=ctemp yawSMAsine(temp%)=stemp yawMA=deg(atan2(yawMAs,yawMAc)) heading = yawMA - declination gui startline origin% origin% = (origin% +272) mod 544 drawpointer(heading, origin%) temp%= (temp% +1) mod MA LOOP`