我有一个CMRotationMatrix *rot,想从矩阵中获取俯仰角、偏航角和翻滚角。你有什么好的建议吗?
谢谢。
谢谢。
使用四元数比欧拉角更好...可以使用以下公式从四元数中推导出翻滚、俯仰和偏航值:
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)
它可以实现为:
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));
其中radianstoDegrees是一个预处理器指令,实现方式如下:
#define radiansToDegrees(x) (180/M_PI)*x
这是为了将公式给出的弧度值转换为角度。
有关转换的更多信息可以在这里找到:tinkerforge和这里:四元数和欧拉角之间的转换。
我自己找到了答案:
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;
}
M_PI
将是更精确的π表示法,并且可以节省变量 ;) - Peter Kazazes