python 四元数转欧拉角

#  -*- coding=utf-8 -*-
import math
import numpy as np

# 欧拉角转四元数
while(1):
    which_mode = input("欧拉角转四元数输入1,四元数转欧拉角输入2。\n请输入:")
    if(which_mode == "1"):
        print("\n源坐标系到目标坐标系旋转顺序为X,Y,Z,左手系.")
        r = float(input("绕x轴旋转角度:"))
        p = float(input("绕y轴旋转角度:"))
        y = float(input("绕z轴旋转角度:"))

        sinp = math.sin(math.radians(p/2))
        siny = math.sin(math.radians(y/2))
        sinr = math.sin(math.radians(r/2))

        cosp = math.cos(math.radians(p/2))
        cosy = math.cos(math.radians(y/2))
        cosr = math.cos(math.radians(r/2))

        w = cosr*cosp*cosy + sinr*sinp*siny
        x = sinr*cosp*cosy - cosr*sinp*siny
        y = cosr*sinp*cosy + sinr*cosp*siny
        z = cosr*cosp*siny - sinr*sinp*cosy

        print("x : {}".format(x))
        print("y : {}".format(y))
        print("z : {}".format(z))
        print("w : {}".format(w))
    elif(which_mode == "2"):
        print("请按顺序输入4元数")
        x = input("x:")
        y = input("y:")
        z = input("z:")
        w = input("w:")
        
        x = float(x)
        y = float(y)
        z = float(z)
        w = float(w)

        r = math.atan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y))
        r = r / math.pi * 180
        p = math.asin(2 * (w * y - z * x))
        p = p / math.pi * 180
        y = math.atan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z))
        y = y / math.pi * 180
        print("\n源坐标系到目标坐标系旋转顺序为X,Y,Z,左手系.")
        print("绕x轴旋转角度: {}".format(r))
        print("绕y轴旋转角度: {}".format(p))
        print("绕z轴旋转角度: {}\n".format(y))
     

PLUS

scipy库里自带的函数
四元数 -> 欧拉角
欧拉角->四元数
欧拉角->旋转矩阵
原坐标系到目标坐标系 , 右手系, 内旋(绕固定轴)

from scipy.spatial.transform import Rotation as R

def quaternion2euler(quaternion):
    r = R.from_quat(quaternion)
    euler = r.as_euler('xyz', degrees=True)
    return euler

def euler2quaternion(euler):
    r = R.from_euler('xyz', euler, degrees=True)
    quaternion = r.as_quat()
    return quaternion

def euler2rotation(euler):
    r = R.from_euler('xyz', euler, degrees=True)
    rotation_matrix = r.as_dcm()
    return rotation_matrix

  • 9
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: 答:可以使用以下代码实现python四元数欧拉角:from math import atan2,asin,pidef quaternion_to_euler(w, x, y, z): ysqr = y * y t0 = +2.0 * (w * x + y * z) t1 = +1.0 - 2.0 * (x * x + ysqr) X = 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 = asin(t2) t3 = +2.0 * (w * z + x * y) t4 = +1.0 - 2.0 * (ysqr + z * z) Z = atan2(t3, t4) return X, Y, Z ### 回答2: 要将四元数换为欧拉角,可以使用Python中的math库中的函数进行计算。下面是一个实现四元数欧拉角的简单代码示例: ```python import math def quaternion_to_euler(q): # 计算四元数的各个分量 w, x, y, z = q # 计算欧拉角的各个分量 roll = math.atan2(2*(w*x + y*z), 1 - 2*(x*x + y*y)) pitch = math.asin(2*(w*y - z*x)) yaw = math.atan2(2*(w*z + x*y), 1 - 2*(y*y + z*z)) # 将弧度换为角度 roll = math.degrees(roll) pitch = math.degrees(pitch) yaw = math.degrees(yaw) # 返回欧拉角 return roll, pitch, yaw # 示例四元数 quaternion = [0.5, 0.5, 0.5, 0.5] # 调用函数将四元数欧拉角 roll, pitch, yaw = quaternion_to_euler(quaternion) # 输出结果 print("Roll: ", roll) print("Pitch: ", pitch) print("Yaw: ", yaw) ``` 以上代码实现了将四元数换为欧拉角的功能。其中,四元数的分量w、x、y、z通过参数q传入函数quaternion_to_euler,并在函数内部进行计算。最后,将计算得到的弧度换为角度,并将欧拉角roll、pitch、yaw返回。在示例中使用了四元数[0.5, 0.5, 0.5, 0.5]进行换,并打印输出结果。 ### 回答3: 要将Python中的四元数换为欧拉角,您可以使用quaternion模块中的函数。以下是一个示例代码: ```python import math import numpy as np import quaternion def quaternion_to_euler_angle(q): # 将四元数换为旋矩阵 rotation_matrix = quaternion.as_rotation_matrix(q) # 从旋矩阵中提取欧拉角 # 使用numpy的arctan2函数计算yaw角 yaw = math.atan2(rotation_matrix[1, 0], rotation_matrix[0, 0]) # 使用numpy的arcsin函数计算pitch角 sin_pitch = -rotation_matrix[2, 0] cos_pitch = np.sqrt(rotation_matrix[2, 1]**2 + rotation_matrix[2, 2]**2) pitch = math.atan2(sin_pitch, cos_pitch) # 使用numpy的arctan2函数计算roll角 roll = math.atan2(rotation_matrix[2, 1], rotation_matrix[2, 2]) # 将弧度换为角度 yaw_deg = math.degrees(yaw) pitch_deg = math.degrees(pitch) roll_deg = math.degrees(roll) return [yaw_deg, pitch_deg, roll_deg] # 示例使用 q = quaternion.from_rotation_vector([0.1, 0.2, -0.3]) euler_angles = quaternion_to_euler_angle(q) print("欧拉角:", euler_angles) ``` 在示例中,我们首先导入必要的模块,然后定义了一个`quaternion_to_euler_angle`函数,该函数接受一个四元数作为输入并返回对应的欧拉角。函数内部使用`quaternion.as_rotation_matrix`函数将四元数换为旋矩阵,然后从旋矩阵中提取欧拉角。最后,我们通过调用示例来演示函数的使用,并打印出换后的欧拉角

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值