For each axis X, Y and Z point the axis North and take a reading R1, then turn the magnetometer 180 degrees and take reading R2. Take the average of the two readings as the offset for that axis.
Your code will then take a reading and subtract the offset for each axis. X= Xraw- Xoffset, Y= Yraw- Yoffset, Z= Zraw- Zoffset.
Use the resulting Z, Y and Z values in all calculations that follow. If you repeat the North and then rotate proceedure then
Xn =-Xs, Yn=-Yz, and Zn=-Zs, where the subscripts s and n stanf for North and South.
The yaw axis should be positive in the Northern hemisphere and negative in the Southern menisphere. If not negate it.
//Read the magnetometer X,Y, and Z
//Subtract the offsets
X1=X-Xoffset; Y1=Y-Yoffset; Z1=Z-Zoffset;
//Assign the XYZ to roll pitch and yaw axis
var Sroll=1; var Spitch=-1; var Syaw=1;
roll=Sroll*X1; pitch=Spitch*Y1; yaw=Syaw*Z1;
heading=180/Math.PI * Math.atan2(pitch,roll);
if(heading <0) heading+=360;
//add the declination to get true heading
Note the change in the argument order of the atan2 function from the orignal post.
© Espruino, powered by microcosm.
Report a problem