I've been experimenting with the compass and gyroscope on iPhone 4 and would like some help with an issue I'm having. I want to compensate for the slowness of the compass by using data from the gyroscope.
Using CMMotionManager
and its CMDeviceMotion
object (motionManager.deviceMotion
), I get the CMAttitude
object. Correct me if I'm wrong (please), but here is what I've deduced from the CMAttitude
object's yaw
property (I don't need pitch
nor roll
for my purposes):
yaw
ranges from0
toPI
when the phone is pointing downwards (as indicated bydeviceMotion.gravity.z
) and swinging counterclockwise and0
to-PI
when swung clockwise- when the device is pointing upwards,
yaw
ranges from-PI
to0
andPI
to0
, respectively - and from the compass data (I'm using
locationManager.heading.magneticHeading
), I see that the compass gives values from0
to360
, with the value increasing when swinging clockwise
All right, so using all of this information together, I'm able to get a value I call horizontal
that, regardless of whether the device is pointing up or down, will give values from 0
to 360
and increase when the device is swung clockwise (though I am still having trouble when deviceManager.gravity.z
is around 0
-- the yaw
value freaks out at this gravity.z
value).
It seems to me that I could "synchronize" the horizontal
and magneticHeading
values, using a calculated horizontal
value that maps to magneticHeading
, and "synchronize" the horizontal
value to magneticHeading
when I feel the compass has "caught up."
So my questions:
- Am I on the right track with this?
- Am I using the gyro data from
CMDeviceMotion
properly and the assumptions I listed above correct? - Why might
yaw
freak out whengravity.z
is around0
?
Thank you very much. I look forward to hearing your answers!