[Python] Quaternion to Euler μνΈλ³ν
κΈ°μ‘΄ ν¬μ€ν
μμ νλ² μΈκΈνλ μ μ΄ μλ€
μ¬κΈ°μμ νμΈκ°λ₯ !
νμ§λ§β¦ rosμ python3μ λμμ μ¬μ©νλ €λ tf.transformations ν¨μ μ¬μ© μ μλ¬κ° λλ€ γ
γ
κ·Έλμ μ§μ μμμΌλ‘ ꡬν΄λ³΄μ
Quaternion to Euler
μ½λ μλ¨μ λ€μκ³Ό κ°μ΄ μ λ ₯ν΄μ£Όμ
import math
def euler_from_quaternion(x, y, z, w):
"""
Convert a quaternion into euler angles (roll, pitch, yaw)
roll is rotation around x in radians (counterclockwise)
pitch is rotation around y in radians (counterclockwise)
yaw is rotation around z in radians (counterclockwise)
"""
t0 = +2.0 * (w * x + y * z)
t1 = +1.0 - 2.0 * (x * x + y * y)
roll_x = math.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
pitch_y = math.asin(t2)
t3 = +2.0 * (w * z + x * y)
t4 = +1.0 - 2.0 * (y * y + z * z)
yaw_z = math.atan2(t3, t4)
return roll_x, pitch_y, yaw_z # in radians
Euler to Quaternion
μ½λ μλ¨μ λ€μκ³Ό κ°μ΄ μ λ ₯ν΄μ£Όμ
import numpy as np # Scientific computing library for Python
def get_quaternion_from_euler(roll, pitch, yaw):
"""
Convert an Euler angle to a quaternion.
Input
:param roll: The roll (rotation around x-axis) angle in radians.
:param pitch: The pitch (rotation around y-axis) angle in radians.
:param yaw: The yaw (rotation around z-axis) angle in radians.
Output
:return qx, qy, qz, qw: The orientation in quaternion [x,y,z,w] format
"""
qx = np.sin(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) - np.cos(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
qy = 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 [qx, qy, qz, qw]
λ !
Leave a comment