Hey All,
I'm at my wits' end with this sensor. I'm trying to get a tilt-compensated magnetic heading reading from an MPU 9150 (http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/IMU/PS-MPU-9150A.pdf). The current libraries for it enable the DMP functionality and give me access to both the quaternions generated by the DMP and raw magnetic values. I'm using the sample code to keep things simple for now (using "MPU6050_9Axis_MotionApps41.h" to get the magnetic data).
Here's the final code:
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
mpu.dmpGetMag(mag, fifoBuffer);
float magComp[3];
float heading;
float phi = ypr[2];
float theta = ypr[1];
float mx = mag[1];
float my = mag[0];
float mz = -mag[2];
heading = atan2((mz * sin(phi) - my * cos(phi)), mx * cos(theta) + my * sin(theta) * sin(phi) + mz * sin(theta) * cos(phi));
The last four lines are of consequence (imo):
the 3 m_ assignments translate the mag axes to match the accel+gyro (the x and y are rotated, and the z is inverted). The last line is directly from FreeScale's doc on tilt compensation: http://cache.freescale.com/files/sensors/doc/app_note/AN4248.pdf (equation 22).
The problem is that the resulting heading value seems to be greatly correlated with both pitch and roll, and doesn't seem to correspond to what I would consider correct magnetic directions.
Any ideas here?