Skip to content

Commit

Permalink
Add function to convert quaternion to euler
Browse files Browse the repository at this point in the history
  • Loading branch information
aeshub committed Apr 6, 2021
1 parent 7b99401 commit ec27d74
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/alitra/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from alitra.convert.quaternion import quaternion_to_euler
22 changes: 22 additions & 0 deletions src/alitra/convert/quaternion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from scipy.spatial.transform.rotation import Rotation

from alitra import Euler, Quaternion


def quaternion_to_euler(
quaternion: Quaternion, sequence: str = "ZYX", degrees: bool = False
) -> Euler:
"""
Transform a quaternion into Euler angles.
:param quaternion: A Quaternion object.
:param sequence: Rotation sequence for the Euler angles.
:param degrees: Set to true if the resulting Euler angles should be in degrees. Default is radians.
:return: Euler object.
"""
rotation_object: Rotation = Rotation(quaternion.as_np_array())
euler: Euler = Euler.from_array(
rotation_object.as_euler(sequence, degrees=degrees),
from_="robot",
to_="asset",
)
return euler
Empty file added tests/convert/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions tests/convert/test_quaternion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
import pytest

from alitra import Euler, Quaternion
from alitra.convert import quaternion_to_euler


@pytest.mark.parametrize(
"quaternion, expected",
[
(
Quaternion(x=0, y=0, z=0, w=1, frame="robot"),
Euler(psi=0, phi=0, theta=0, from_="robot", to_="asset"),
),
(
Quaternion(x=0.1830127, y=0.5, z=0.5, w=0.6830127, frame="robot"),
Euler(
psi=1.5707963, # Z
theta=0.5235988, # Y
phi=1.0471976, # X
from_="robot",
to_="asset",
),
),
],
)
def test_quaternion_to_euler(quaternion, expected):
euler_angles: Euler = quaternion_to_euler(
quaternion=quaternion, sequence="ZYX", degrees=False
)
assert np.allclose(euler_angles.as_np_array(), expected.as_np_array())

0 comments on commit ec27d74

Please sign in to comment.