jgauchia / IceNav-v3

ESP32 Based GPS Navigator with OSM offline maps. (Under development)
GNU General Public License v3.0
55 stars 11 forks source link

Mpu9250 initial support #32

Closed hpsaturn closed 1 year ago

jgauchia commented 1 year ago

Checked. Minor adjustments to the log output.

jgauchia commented 1 year ago

Comment about magnetic declination updated

jgauchia commented 1 year ago

Check this link: https://github.com/kriswiner/MPU9250/tree/master/AK8963_as_slave

Uses a function (in MadgwickFilter file) than fuses acceleration, rotation rate, and magnetic moments to produce a quaternion-based estimate of absolute device orientation -- which can be converted to yaw, pitch, and roll

MadgwickQuaternionUpdate(-ax, +ay, +az, gx*pi/180.0f, -gy*pi/180.0f, -gz*pi/180.0f, my, -mx, mz);

a -> acceleration values g-> gyro values m-> magnetometer values

It's seems to return quaternion in an global array (q[])

float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion

And with this code it's seems to calculate yaw (heading)?

    a12 =   2.0f * (q[1] * q[2] + q[0] * q[3]);
    a22 =   q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3];
    a31 =   2.0f * (q[0] * q[1] + q[2] * q[3]);
    a32 =   2.0f * (q[1] * q[3] - q[0] * q[2]);
    a33 =   q[0] * q[0] - q[1] * q[1] - q[2] * q[2] + q[3] * q[3];
    pitch = -asinf(a32);
    roll  = atan2f(a31, a33);
    yaw   = atan2f(a12, a22);
    pitch *= 180.0f / pi;
    yaw   *= 180.0f / pi; 
    yaw   += 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04
    if(yaw < 0) yaw   += 360.0f; // Ensure yaw stays between 0 and 360
    roll  *= 180.0f / pi;
    lin_ax = ax + a31;
    lin_ay = ay + a32;
    lin_az = az - a33;

If this calculation works for you to obtain the angle, it would be great to be able to use it myself with my compass module and the MPU6050 accelerometer/gyroscope that I have and currently don't use. In other words, we could perform the same calculation for both, your module and mine (compass and accelerometer/gyroscope combinated).

hpsaturn commented 1 year ago

Thanks! I will try it tomorrow :)