Note that there are some explanatory texts on larger screens.

plurals
  1. POCompensating compass lag with the gyroscope on iPhone 4
    text
    copied!<p>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.</p> <p>Using <code>CMMotionManager</code> and its <code>CMDeviceMotion</code> object (<code>motionManager.deviceMotion</code>), I get the <code>CMAttitude</code> object. Correct me if I'm wrong (please), but here is what I've deduced from the <code>CMAttitude</code> object's <code>yaw</code> property (I don't need <code>pitch</code> nor <code>roll</code> for my purposes):</p> <ul> <li><code>yaw</code> ranges from <code>0</code> to <code>PI</code> when the phone is pointing downwards (as indicated by <code>deviceMotion.gravity.z</code>) and swinging counterclockwise and <code>0</code> to <code>-PI</code> when swung clockwise</li> <li>when the device is pointing upwards, <code>yaw</code> ranges from <code>-PI</code> to <code>0</code> and <code>PI</code> to <code>0</code>, respectively</li> <li>and from the compass data (I'm using <code>locationManager.heading.magneticHeading</code>), I see that the compass gives values from <code>0</code> to <code>360</code>, with the value increasing when swinging clockwise</li> </ul> <p>All right, so using all of this information together, I'm able to get a value I call <code>horizontal</code> that, regardless of whether the device is pointing up or down, will give values from <code>0</code> to <code>360</code> and increase when the device is swung clockwise (though I am still having trouble when <code>deviceManager.gravity.z</code> is around <code>0</code> -- the <code>yaw</code> value freaks out at this <code>gravity.z</code> value).</p> <p>It seems to me that I could "synchronize" the <code>horizontal</code> and <code>magneticHeading</code> values, using a calculated <code>horizontal</code> value that maps to <code>magneticHeading</code>, and "synchronize" the <code>horizontal</code> value to <code>magneticHeading</code> when I feel the compass has "caught up."</p> <p>So my questions: </p> <ul> <li>Am I on the right track with this? </li> <li>Am I using the gyro data from <code>CMDeviceMotion</code> properly and the assumptions I listed above correct? </li> <li>Why might <code>yaw</code> freak out when <code>gravity.z</code> is around <code>0</code>?</li> </ul> <p>Thank you very much. I look forward to hearing your answers!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload