将四元数转换为 Android 坐标中的欧拉角

问题描述

我使用以下代码在 Android 智能手机 (YXZ) 中将四元数转换为欧拉角,反之亦然。但是,当我从 quaternion_to_euler_android 获取输出并将其作为 euler_to_quaternion_android 的输入时,我得到了不同的结果。

def euler_to_quaternion_android(r):
    (yaw,pitch,roll) = (r[0],r[1],r[2])
    qy = np.sin(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) + np.cos(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
    qx = 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 [qw,qx,qy,qz]

def quaternion_to_euler_android(q):
    (w,x,y,z) = (q[0],q[1],q[2],q[3])
    t0 = +2.0 * (x * z - y * w)
    t1 = +1.0 - 2.0 * (x * x + y * y)
    roll = math.atan2(-t0,t1) #Math.atan2(-R[6],R[8]);
    t2 = +2.0 * (y * z + x * w) #q2_q3 + q1_q0;
    t2 = +1.0 if t2 > +1.0 else t2
    t2 = -1.0 if t2 < -1.0 else t2
    pitch = math.asin(-t2)
    t3 = +2.0 * (x * y - w * z)
    t4 = +1.0 - 2.0 * (x * x + z * z)
    yaw = math.atan2(t3,t4) #Math.atan2(R[1],R[4]);
    return [yaw,roll]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)