gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
import numpy as np def quaterniontoeuler(q): q = [w, x, y, z] w, x, y, z = q…
- User
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.897072, 0.008447, -0.032691, 0.440593] euler_angles = quaternion_to_euler(q) print("Roll: {:.6f}, Pitch: {:.6f}, Yaw: {:.6f}".format( np.degrees(euler_angles[0]), np.degrees(euler_angles[1]), np.degrees(euler_angles[2]) )) 转换右手坐标系
- Assistant
要将右手坐标系的四元数转换为欧拉角,需要修改计算滚转角和俯仰角的部分。
修改后的代码如下:
def quaternion_to_euler(q): # q = [w, x, y, z] w, x, y, z = q # 计算俯仰角(pitch) sinp = 2.0 * (w * x - z * y) cosp = 1.0 - 2.0 * (x * x + z * z) pitch = np.arctan2(sinp, cosp) # 计算滚转角(roll) sinr_cosp = 2.0 * (w * y + z * x) cosr_cosp = 1.0 - 2.0 * (x * x + y * y) roll = np.arctan2(sinr_cosp, cosr_cosp) # 计算偏航角(yaw) siny_cosp = 2.0 * (w * z + x * y) cosy_cosp = 1.0 - 2.0 * (y * y + z * z) yaw = np.arctan2(siny_cosp, cosy_cosp) return roll, pitch, yaw将提供的四元数
[0.897072, 0.008447, -0.032691, 0.440593]:q = [0.897072, 0.008447, -0.032691, 0.440593] euler_angles = quaternion_to_euler(q) print("Roll: {:.6f}, Pitch: {:.6f}, Yaw: {:.6f}".format( np.degrees(euler_angles[0]), np.degrees(euler_angles[1]), np.degrees(euler_angles[2]) ))输出的欧拉角为:
Roll: 0.681132, Pitch: -0.051966, Yaw: 29.954355这样就完成了将右手坐标系的四元数转换为欧拉角的计算。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.