11

I'm actually reposting a question from the iphone development boards. I didn't ask it, but I found it when trying to find its answer. Nobody had yet responded there, so I thought I'd try posting it here.

Thanks in advance for any help!

Does anybody know any specifics about the algorithm of computing the magnetic heading from the raw xyz values of CLHeading? I am sure there must be a general approach to this that works for different magnetometers as well.

Kevin Montrose
  • 22,191
  • 9
  • 88
  • 137
  • 1
    [Implementing a Tilt-Compensated eCompass using Accelerometer and Magnetometer Sensors](http://cache.freescale.com/files/sensors/doc/app_note/AN4248.pdf) - "This technical note provides the mathematics, reference source code and guidance for engineers implementing a tilt-compensated electronic compass (eCompass)." – Daniel Ballinger May 06 '12 at 19:03

1 Answers1

8

If you don't want to use the trueHeading value, and assuming that the compass lies perfectly horizontal, the heading can be computed this way:

  • (y>0) heading = 90 - atan2(x,y)*180 / pi
  • (y<0) heading = 270 - atan2(x,y)*180 / pi
  • (y=0, x<0) heading = 180.0
  • (y=0, x>0) heading = 0.0

(these are from http://www.magneticsensors.com/datasheets/an203.pdf)

... plus the heading must be corrected for Magnetic deviation and Magnetic declination

in case the compass is not horizontal, you should use only the horizontal component (you can not ignore the z axis) by projecting the (x, y, z) vector to a plane that is parallel to the earth surface and then compute the heading from this projected x and y.

Hope it helps.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
cube
  • 3,867
  • 7
  • 32
  • 52
  • Note in the above the divide by pi is missing and apple and Honeywell's x and y definitions are opposite. –  Jul 31 '09 at 15:39