ChrisIsBack
ChrisIsBack

Reputation: 225

CMAttitude roll and pitch calculation

The question itself is simple. When using the CMDeviceMotion motion it contains an object named attitude of type CMAttitude which contains roll and pitch. Roll and pitch describe the rotation of the object around the Y and X axis. When the iPhone lies on a table with the display pointing up roll and pitch are both 0. When rotating the iPhone around Y or X axis these values get updated.

I want to know how these values are calculated.There is a way to calculate roll and pitch from the current gravity vector of the device. The pitch seems to be: pitch = -asin(motion.gravity.y) but I can not figure out how to calculate the roll.

Thanks for your help

Upvotes: 0

Views: 2426

Answers (3)

Yup.
Yup.

Reputation: 1893

CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z)); // PITCH !!!
myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));

Upvotes: 4

Batti
Batti

Reputation: 425

pitch = atan2(motion.gravity.z, motion.gravity.y)
roll = atan2(motion.gravity.z, motion.gravity.x)

in this way the value of pitch it will be between 0 +pi and 0 -pi like roll

Upvotes: 0

Daniel Dickison
Daniel Dickison

Reputation: 21882

It's likely something like:

roll = -atan(x / z);

though you need to alter the sign depending on which orientation you're in, and be careful around the asymptotes where z=0.

I believe Core Motion in reality uses gyro measurements in addition to the accelerometer readings to calculate the attitude more accurately (and maybe also to deal with gimbal lock).

Upvotes: 0

Related Questions