Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

四元数转欧拉角代码python

  1. User

    四元数转欧拉角代码python

  2. Assistant

    四元数到欧拉角的转换可以使用以下代码实现:

    import numpy as np
    
    def quaternion_to_euler(q):
        # q = [w, x, y, z]
        w, x, y, z = q
    
        # 计算俯仰角(pitch)
        sinp = 2.0 * (w * x + y * z)
        cosp = 1.0 - 2.0 * (x * x + y * y)
        pitch = np.arctan2(sinp, cosp)
    
        # 计算滚转角(roll)
        sinr = 2.0 * (w * y - z * x)
        cosr = 1.0 - 2.0 * (y * y + z * z)
        roll = np.arctan2(sinr, cosr)
    
        # 计算偏航角(yaw)
        siny = 2.0 * (w * z + x * y)
        cosy = 1.0 - 2.0 * (y * y + z * z)
        yaw = np.arctan2(siny, cosy)
    
        return roll, pitch, yaw
    

    使用方法:

    q = [0.924, 0.383, 0.0, 0.0]
    euler_angles = quaternion_to_euler(q)
    print("Roll: {:.2f}°, Pitch: {:.2f}°, Yaw: {:.2f}°".format(
        np.degrees(euler_angles[0]),
        np.degrees(euler_angles[1]),
        np.degrees(euler_angles[2])
    ))
    

    请注意,上述代码假设输入的四元数形式为[w, x, y, z]。如果你有其他形式的四元数, 请先转换为这种形式后再使用该代码进行转换。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.