如何将欧拉角转换为四元数并从四元数返回相同的欧拉角?

How to convert Euler angles to Quaternions and get the same Euler angles back from Quaternions?

我正在使用欧拉角按 XYZ 的顺序旋转 n 3D 形状,这意味着对象首先沿 X 轴旋转,然后 Y 然后 Z。我想将欧拉角转换为四元数,然后使用一些 [最好] Python 代码或一些伪代码或算法从四元数得到相同的欧拉角。下面,我有一些代码将欧拉角转换为四元数,然后将四元数转换为欧拉角。但是,这并没有给我相同的欧拉角。

我认为问题是我不知道如何将 偏航、俯仰和滚动 关联到 X、Y 和 Z 轴。另外,我不知道如何更改代码中的转换顺序以正确地将欧拉角转换为四元数,然后将四元数转换为欧拉角,以便我能够获得相同的欧拉角。有人可以帮我解决这个问题吗?

这是我使用的代码:

此函数将欧拉角转换为四元数:

def euler_to_quaternion(yaw, pitch, roll):

        qx = np.sin(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) - np.cos(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
        qy = np.cos(roll/2) * np.sin(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.cos(pitch/2) * np.sin(yaw/2)
        qz = np.cos(roll/2) * np.cos(pitch/2) * np.sin(yaw/2) - np.sin(roll/2) * np.sin(pitch/2) * np.cos(yaw/2)
        qw = np.cos(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)

        return [qx, qy, qz, qw]

这会将四元数转换为欧拉角:

def quaternion_to_euler(x, y, z, w):

        import math
        t0 = +2.0 * (w * x + y * z)
        t1 = +1.0 - 2.0 * (x * x + y * y)
        X = math.degrees(math.atan2(t0, t1))

        t2 = +2.0 * (w * y - z * x)
        t2 = +1.0 if t2 > +1.0 else t2
        t2 = -1.0 if t2 < -1.0 else t2
        Y = math.degrees(math.asin(t2))

        t3 = +2.0 * (w * z + x * y)
        t4 = +1.0 - 2.0 * (y * y + z * z)
        Z = math.degrees(math.atan2(t3, t4))

        return X, Y, Z

我使用它们如下:

import numpy as np
euler_Original = np.random.random(3) * 360).tolist() # Generate random rotation angles for XYZ within the range [0, 360)
quat = euler_to_quaternion(euler_Original[0], euler_Original[1], euler_Original[2]) # Convert to Quaternion
newEulerRot = quaternion_to_euler(quat[0], quat[1], quat[2], quat[3]) #Convert the Quaternion to Euler angles

print (euler_Original)
print (newEulerRot)

打印语句为 euler_OriginalnewEulerRot 打印不同的数字,我不希望出现这种情况。例如,如果 euler_original 包含以弧度表示的 (0.2, 1.12, 2.31) 之类的数字,我得到这个四元数 --> [0.749, 0.290, -0.449, 0.389] 并将四元数转换为欧拉角得到这个 --> (132.35, 64.17, 11.45) 这是大错特错。我想知道如何解决这个问题?

虽然我有兴趣通过修改上面的代码来使其工作,但是,我更愿意学习如何正确地建立方程式。这样我就知道如何获得正确的四元数,即使应用欧拉角的旋转顺序(XYZ --> YZX 等)发生了变化。

主要问题:

The input order of euler_to_quaternion is different to the output order of quaternion_to_euler

前者取角度顺序为Z, Y, X(yaw, pitch, roll),后者returns X, Y, Z。修复:

def euler_to_quaternion(roll, pitch, yaw):
# or
euler_to_quaternion(euler_Original[2], euler_Original[1], euler_Original[0])

小问题

euler_to_quaternion takes radians whereas quaternion_to_euler returns degrees.

本身并不是一个真正的问题,但最好将角度保持为弧度,因为大多数库函数都使用它们。

X = math.atan2(t0, t1)
Y = math.asin(t2)
Z = math.atan2(t3, t4)

我们可以使用 scipy.spatial.transform 中的 Rotation

from scipy.spatial.transform import Rotation

# Create a rotation object from Euler angles specifying axes of rotation
rot = Rotation.from_euler('xyz', [90, 45, 30], degrees=True)

# Convert to quaternions and print
rot_quat = rot.as_quat()
print(rot_quat)

结果将是:

[ 0.56098553  0.43045933 -0.09229596  0.70105738]

那么,你也可以用欧拉角取回来:

print(rot.as_euler('xyz', degrees=True))

这导致:

[90. 45. 30.]

作为最后的检查,根据上面计算的四元数创建一个旋转对象并将其作为欧拉角:

rot = Rotation.from_quat(rot_quat)

# Convert the rotation to Euler angles given the axes of rotation
print(rot.as_euler('xyz', degrees=True))

这导致:

[90. 45. 30.]