# MPU 9150 yaw problem

Hi Guys I am using the MPU9150 to build an AHRS. I am using the Sparkfun I2c Dev Lib for getting the raw data. The roll and pitch are working fine. The magnetometer yaw reading is pretty messed up. I am able to get some data but do not know how to get correct yaw. I am using the following equations:

m=T *cmps;

T=[cos(theta), sin(theta*sin(phi), sin(theta)*cos(phi); 0, cos(phi), -sin(phi); ... , ... , ...];

cmps - Normalized value from magnetometer m=magnetometer planar readings.

yaw=-atan2(m[1],m[0]);

However, it gives absurd values and changes suddenly too from say 0 to 180 0r -180.

Also, it changes smoothly if I move pitch it up or down.

Chandra

You need to ditch that method and start using quaternions.

Most of the published methods for getting a magnetic heading, assume that your sensor is being held more or less flat to start with. This might be a reasonable assumption in some cases ( holding a gps device or a compass in your hand, or on a aircraft already in more or less level flight ), but not in all cases.

You also need to consider how good the calibration of your magnetic field sensor is.

Any method that returns an angle ( presumably in degrees if you are having problems with + and - 180 ), is always going to have a point where it rolls over from one direction to the other. If you face 179 degrees and then turn 2 degrees to the right, you are going to expect to see either 181 degrees or -179 degrees, that's completely normal and your logic needs to deal with it.

Hi I understand that a shift from -180 to 180 is normal assuming I have a scale from -180 to 180 but a shift from 0 to +- 180 is unusual. It is giving completely arbitrary directions. I believe that whatever works for the quaternion must work for the euler angles as well. Leaving that aside, I tried the Madgwick scheme and the MPU9150_AHRS.ino available with Sparkfun_Breakout_Master which is based on Quaternions and even that did not give a reasonable yaw. The Yaw remains constant as I move around in horizontal ground but changes when I pitch it. I want to know the axis direction of the Magnetometer w.r.t. MPU 6050 as they are two separate sensors. Maybe, I am having some trouble with the axis directions.

Chandra :) :)

The datasheet is quite clear about how the directions work. I donâ€™t have it here, you can look it up.

The yaw angle can vary over a range of 360 degrees but the pitch only over a range of 180 ( 0 to 180 or -90 to +90 ).