Python中如何为旋转叠加绕Y轴-90度欧拉旋转得到正确四元数
需求说明
- 输入:未归一化的轴角(axis rotation)旋转量
- 输出:四元数(quaternion)格式的旋转结果,输出结果需要额外叠加绕Y轴旋转-90度的欧拉(euler)旋转
现有实现
#!/usr/bin/env python3 #from math import radians, degrees, cos, sin, atan2, asin, pow, floor #import numpy as np from scipy.spatial.transform import Rotation # 基础转换逻辑:轴角转四元数 r = Rotation.from_rotvec(rotation_by_axis).as_quat() r = Rotation.from_quat([-0.0941422, 0.67905384, -0.2797612, 0.67212856]) # 测试用例 print("输入(欧拉角形式): " + str(r.as_euler('xyz', degrees=True))) print("输出(欧拉角形式): " + str(r.apply([0, -90, 0])))
运行结果:
输入(欧拉角形式): [-83.23902624 59.33323676 -98.88314731] 输出(欧拉角形式): [-22.33941658 -74.31676511 45.58474405]
上述代码输出不符合预期,期望输出为[-83.23902624 -30.66676324 -98.88314731],需要找到正确的实现方法。
存在缺陷的临时方案
目前的临时方案仅在部分场景下生效,失效原因未知:
rotation = r.from_quat([rotation.x, rotation.y, rotation.z, rotation.w]) rotation = rotation.as_euler('xyz', degrees=True) print(rotation) rotation = r.from_euler('xyz', [rotation[0], rotation[1]-90, rotation[2]], degrees=True) print(rotation.as_euler('xyz', degrees=True)) rotation = rotation.as_quat()
该方案稳定性不足,部分场景会返回错误值,多组测试结果如下:
[ -8.25897711 -16.54712028 -1.90525288] [ 171.74102289 -73.45287972 178.09474712] [ -7.18492129 22.22525264 0.44373851] [ -7.18492129 -67.77474736 0.44373851] [ 7.52491766 -37.71896037 -40.86915413] [-172.47508234 -52.28103963 139.13084587] [ -1.79610826 37.83068221 31.20184248] [ -1.79610826 -52.16931779 31.20184248] [-113.5719734 -54.28744892 141.73007557] [ 66.4280266 -35.71255108 -38.26992443] [ -83.23903078 59.33323752 -98.88315157] [ -83.23903078 -30.66676248 -98.88315157] [ -9.67960912 -7.23784945 13.56800885] [ 170.32039088 -82.76215055 -166.43199115] [ -6.21695895 5.66996884 -11.16152822] [ -6.21695895 -84.33003116 -11.16152822] [ 0. 0. 0. ] [ 0. -90. 0. ] [ 0. 0. 0. ] [ 0. -90. 0. ]
错误结果样例:
[ -8.25897711 -16.54712028 -1.90525288] [ 171.74102289 -73.45287972 178.09474712]
正确结果样例:
[ -7.18492129 22.22525264 0.44373851] [ -7.18492129 -67.77474736 0.44373851]
该功能用于ROMP项目的位姿处理环节。
内容的提问来源于stack exchange,提问作者Sukombu
相关产品推荐
相关产品推荐

