Ami*_*mir 4 python math graphics rotation quaternions
我旋转N使用欧拉3D形状在角XYZ的顺序意味着该对象沿第一旋转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]
Run Code Online (Sandbox Code Playgroud)
这将四元数转换为欧拉角:
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
Run Code Online (Sandbox Code Playgroud)
我使用它们如下:
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)
Run Code Online (Sandbox Code Playgroud)
打印报表打印不同的号码euler_Original和newEulerRot我不希望是这样的。例如,如果euler_original包含像(0.2, 1.12, 2.31)弧度这样的数字,我得到这个四元数 -->[0.749, 0.290, -0.449, 0.389]并将四元数转换为欧拉角给我这个 -->(132.35, 64.17, 11.45)这是非常错误的。我想知道我该如何解决这个问题?
虽然我有兴趣通过对上面的代码进行更改来使其工作,但是,我宁愿学习如何正确设置方程。这样我就知道如何获得正确的四元数,即使应用欧拉角的旋转顺序(XYZ --> YZX 等)发生变化。
我们可以使用Rotation从scipy.spatial.transform.
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)
Run Code Online (Sandbox Code Playgroud)
结果将是:
[ 0.56098553 0.43045933 -0.09229596 0.70105738]
Run Code Online (Sandbox Code Playgroud)
然后,您还可以将其恢复为欧拉角:
print(rot.as_euler('xyz', degrees=True))
Run Code Online (Sandbox Code Playgroud)
结果是:
[90. 45. 30.]
Run Code Online (Sandbox Code Playgroud)
作为最后的检查,从上面计算的四元数创建一个旋转对象并将其作为欧拉角:
rot = Rotation.from_quat(rot_quat)
# Convert the rotation to Euler angles given the axes of rotation
print(rot.as_euler('xyz', degrees=True))
Run Code Online (Sandbox Code Playgroud)
结果是:
[90. 45. 30.]
Run Code Online (Sandbox Code Playgroud)
主要问题:
输入顺序
euler_to_quaternion与输出顺序不同quaternion_to_euler
前者按顺序获取角度Z, Y, X(偏航、俯仰、滚动),后者返回X, Y, Z。使固定:
def euler_to_quaternion(roll, pitch, yaw):
# or
euler_to_quaternion(euler_Original[2], euler_Original[1], euler_Original[0])
Run Code Online (Sandbox Code Playgroud)
小问题
euler_to_quaternion获取弧度而quaternion_to_euler返回度数。
本身并不是一个问题,但最好将角度保持为弧度,因为大多数库函数都使用它们。
X = math.atan2(t0, t1)
Y = math.asin(t2)
Z = math.atan2(t3, t4)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15638 次 |
| 最近记录: |