get pitch, yaw, roll from a CMRotationMatrix

隐身守侯 提交于 2019-12-03 04:38:35

问题


I have a CMRotationMatrix *rot and i would like to get the pitch, yaw, roll from the matrix. Any ideas how i could do that?

Thanks


回答1:


Its better to use the Quaternion than Euler angles.... The roll, pitch and yaw values can be derived from quaternion using these formulae:

roll  = atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z)
pitch = atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z)
yaw   =  asin(2*x*y + 2*z*w)

It can be implemented as:

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));
myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));

where the radianstoDegrees is a preprocessor directive implemented as:

#define radiansToDegrees(x) (180/M_PI)*x

This is done to convert the radian values given by the formulae, to degrees.

More information about the conversion can be found here: tinkerforge and here:Conversion between Quaternions and Euler angles.




回答2:


pitch, yaw, roll from the matrix. Any ideas how i could do that?

In which order? Pitch, yaw and roll, commonly called Euler angles, don't represent rotations unambigously. Depending on the order you carry out the individual sub-rotations you end up with completely different rotation matrices.

My personal recommendation: Don't use Euler angles at all, they just call for (numerical) trouble. Use a matrix (you already do) or a quaternion.




回答3:


Found it out myself:

CMAttitude *currentAttitude = motionManager.deviceMotion.attitude;    

        if (currentAttitude == nil)
        {
            NSLog(@"Could not get device orientation.");
            return;
        }
        else {
            float PI = 3.14159265;
            float yaw = currentAttitude.yaw * 180/PI;
            float pitch = currentAttitude.pitch * 180/PI;
            float roll = currentAttitude.roll * 180/PI;

        }


来源:https://stackoverflow.com/questions/9478630/get-pitch-yaw-roll-from-a-cmrotationmatrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!