I receive the RotationMatrix from my CMMotionManager and want to calculate my phone's heading from it, e.g. 38 degrees North (and avoid CLLocationManager), in order to make a more accurate compass:
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error) {
CMRotationMatrix rm = deviceMotion.attitude.rotationMatrix;
// Get the heading.
motionHeading = M_PI + atan2(rm.m22, rm.m12);
motionHeading = motionHeading*180/M_PI;
However this only works when the phone is flat on the table. What should i use as formula to allow for all phone's positions in my hand, being in landscape mode, and the device looking above or under the horizon ?
Thanks.