Magnetometer - YAW - Reference Angle

Hello,
i've got a magnetometer and want to get yaw angle of reference point. For example : my magnetometer is pointet to North and the value is beetwen 0 (+-2) and 360 (+-2). If i rotate my magnetometer right or left the yaw value gets it's value proportional to the rotation ( rotating clockwise 30 degree, yaw value is 30 degree, rotating clockwise 390 degree, yaw value is 30), but if i try rotating otherwise values are getting crazy, the same problem is for another reference angle for example 60 , 180 , 250 degree.

H3_R=heading3*(PI/180);
x=cos(H3_R)-sin(H3_R);
y=sin(H3_R)+cos(H3_R);
H3_D=atan2(y,x);
H3_D=(H3_D*(180/PI))-45;
heading3=round(heading3);
H3_D=round(H3_D);

heading3 is the value of the magnetometer.

 if (flag==0 )
 {yaw1=round(heading2);flag=1;}
heading3=heading2-yaw1;

This script is getting at the beggining the reference(starting) value

Magnetometers must be calibrated to be useful as a compass.

Here is most comprehensive tutorial, a web search will reveal simpler, less accurate methods.

Thank you for feedback. The main problem is to get the new angle determinated by reference angle and actual compass value.

The main problem is to get the new angle determinated by reference angle and actual compass value.

I have no idea what this means, or what your posted code is supposed to do. Please explain.

To correct for magnetic versus true North, just subtract your magnetic declination from the measured yaw angle.

My goal is to measure YAW angle. If i rotate clockwise my magnetometer the YAW value incresing proportional to the compass value. Otherwise the value decreasing proportional. Max angle :180 min angle :-180 for the YAW value.

Measuring yaw could hardly be simpler. If the compass is held level, with X and Y axes in the horizontal plane, and assuming magx and magy are the calibrated values (with offset and scale corrected):

yaw = 180.0*atan2(magy, magx)/PI - declination; //mag declination in degrees

yaw=0 when the X axis points to magnetic North. You may wish to change the sign of yaw, so that it increases clockwise from North to East, as is conventional.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.