title: Calculating magnetic heading using raw accelerometer and magnetometer data
body: <p>I have an accelerometer and magnetometer each producing raw X, Y and Z readouts. From this I need to determine the magnetic heading of an object.</p>
  
  <p>I'm not that great at trig, but I've put together a formula that does respond pretty well to the rotation of the device, but also responds to movement that one would not think is relevant, such as angling the device in such a way that has no impact on the direction it is pointed. Such as laying it flat and "rolling" the device.</p>
  
  <p>I think the formula I have for calculating the magnetic heading is fine, but I think my pitch and roll radians for input are wrong.</p>
  
  <p>So I guess the core of my question (unless someone actually has a formula around that does this), is how do you calculate angles, in radians, using an accelerometer for pitch and roll.</p>
  
  <p>Then secondly, any info on the heading formula itself would be great.</p>