从四元数到欧拉角以及向后的转换不正确 [英] Incorrect conversion from quaternions to euler angles and back

查看:285
本文介绍了从四元数到欧拉角以及向后的转换不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将角度轴表示形式转换为欧拉角.我决定检查并确保从转换中得到的欧拉角会回到原始的轴角.我打印出值,但它们不匹配!我已阅读 http://forum.onlineconversion.com/showthread.php?t=5408 http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles 以及类似版本该网站上的转化问题.

I am converting angles-axis representation to Euler angles. I decided to check and make sure that the Euler angles I got from the conversion would lead back to the original axis-angle. I print out the values, but they do not match! I have read http://forum.onlineconversion.com/showthread.php?t=5408 and http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles as well as similar conversion questions on this website.

在下面的代码中,我从角度"angle"和轴(rx,ry,rz)开始,然后将其转换为四元数(q0,q1,q2,q3).我将四元数转换为欧拉角(滚动,俯仰,偏航).然后要检查它,我将(roll,pitch,yaw)转换回cAngle和(cRx,cRy,cRz)的轴角.然后,我对(滚动,俯仰,偏航)进行一些边界检查,以将数字保持在-pi和pi之间,然后将它们打印出来.应该是cAngle = angle和(cRx,cRy,cRz)=(rx,ry,rz),但这两个都是错误的.

In the code below I start with angle 'angle' and axis (rx,ry,rz), then I convert it to quaternions (q0,q1,q2,q3). I convert the quaternions to euler angles (roll, pitch, yaw). Then to check it, I convert (roll,pitch,yaw) back to axis-angle as cAngle and (cRx,cRy,cRz). I then do some bounds checking on (roll, pitch,yaw) to keep the numbers between -pi and pi, and then I print them out. It should be that cAngle=angle and (cRx,cRy,cRz)=(rx,ry,rz), but these are both wrong.

我相信旋转是常见的Z * Y * X顺序.我的数学有什么问题吗?我计划最终在俯仰为0或PI时添加特殊情况,如 http: //www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/,但现在我认为问题是独立的.

The rotations are in order Z*Y*X as is common, I believe. Is there someting wrong with my math? I plan on eventually adding special cases for when pitch is 0 or PI as in http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/ but right now I think the problem is separate.

        //input is angle 'angle' and axis '(rx,ry,rz)'

        //convert rx,ry,rz, angle, into roll, pitch, yaw
        double q0 = Math.Cos(angle / 2);
        double q1 = Math.Sin(angle / 2) *Math.Cos(rx);
        double q2 = Math.Sin(angle / 2) * Math.Cos(ry);
        double q3 = Math.Sin(angle / 2) * Math.Cos(rz);
        double roll = Math.Atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2));
        double pitch = Math.Asin(2 * (q0 * q2 - q3 * q1));
        double yaw = Math.Atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3));

        //convert back to angle axis
        double cAngle = 2 * Math.Cos(Math.Cos(roll / 2) * Math.Cos(pitch / 2) * Math.Cos(yaw / 2) + Math.Sin(roll / 2) * Math.Sin(pitch / 2) * Math.Sin(yaw / 2));
        double cRx = Math.Acos((Math.Sin(roll / 2) * Math.Cos(pitch / 2) * Math.Cos(yaw / 2) - Math.Cos(roll / 2) * Math.Sin(pitch / 2) * Math.Sin(yaw / 2)) / Math.Sin(cAngle / 2));
        double cRy = Math.Acos((Math.Cos(roll / 2) * Math.Sin(pitch / 2) * Math.Cos(yaw / 2) + Math.Sin(roll / 2) * Math.Cos(pitch / 2) * Math.Sin(yaw / 2)) / Math.Sin(cAngle / 2));
        double cRz = Math.Acos((Math.Cos(roll / 2) * Math.Cos(pitch / 2) * Math.Sin(yaw / 2) - Math.Sin(roll / 2) * Math.Sin(pitch / 2) * Math.Cos(yaw / 2)) / Math.Sin(cAngle / 2));

        //stay within +/- PI of 0 to keep the number small
        if (roll > 3.1416) roll = -Math.PI + (roll - Math.PI);
        if (roll < -3.1416) roll = Math.PI + (roll - (-1) * Math.PI);
        if (pitch > 3.1416) pitch = -Math.PI + (pitch - Math.PI);
        if (pitch < -3.1416) pitch = Math.PI + (pitch - (-1) * 3.1416F);
        if (yaw > 3.1416) yaw = -Math.PI + (yaw - Math.PI);
        if (yaw < -3.1416) yaw = Math.PI + (yaw - (-1) * Math.PI);

        Console.WriteLine("original angle, axis " + angle + ": " + rx + ", " + ry + ", " + rz);
        Console.WriteLine("converted angle, axis " + cAngle + ": " + cRx + ", " + cRy + ", " + cRz);
        Console.WriteLine("quats " + q0 + ", " + q1 + ", " + q2 + ", " + q3);
        Console.WriteLine("roll,pitch,yaw:  " + roll + ", " + pitch + ", " + yaw);

推荐答案

我没有(也不会)检查您的代码. 即使您的代码正确,测试也至少有两个原因失败.

I didn't (and I won't) check your code. Even if your code is correct, your test fails for at least 2 reasons.

  • There are 2 ways to represent the same rotation with Euler angles. See also Euler angle to Quaternion then Quaternion to euler angle, that question is basically about the same problem you are having.

四元数具有所谓的双重覆盖属性:两个单位四元数对应于每个旋转.

Quaternions have the so-called double covering property: Two unit quaternions correspond to every rotation.

也就是说,即使您的转换正确无误,也可以得到另一种表示形式,而不是您最初使用的一种表示形式.不管您是从Euler角还是四元数开始.

That is, even if your conversions are correct, you can get the other representation and not the one you started with. It doesn't matter whether you started with Euler angles or quaternions.

如果要测试代码,建议检查单位基矢量的正交旋转.例如,将[1, 0, 0]适当旋转90度以获得[0, 1, 0].检查您是否真的得到了预期的[0, 1, 0]等. 如果您正确地获得了所有3个基向量的旋转,则您的代码很可能是正确的.

If you want to test your code, I suggest checking orthogonal rotations of the unit basis vectors. For example rotate [1, 0, 0] with 90 degrees appropriately to get [0, 1, 0]. Check whether you really get the expected [0, 1, 0], etc. If you got the rotations of all 3 basis vectors right then your code is most likely correct.

此测试的优点是明确;如果您弄乱了某些内容(例如,公式中的符号),则此测试可以帮助您找到很多错误.

This test has the advantage of being unambiguous and if you mess up something (for example a sign in a formula) this test helps you a lot in finding your mistake.

我不会使用Euler角,因为它们会破坏应用程序的稳定性.它们也不是很方便.

I wouldn't use Euler angles as they screw up the stability of your application. They are not very handy either.

这篇关于从四元数到欧拉角以及向后的转换不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆