I wrote a code to convert quaternions to z-y-x Euler angles in Python

I wrote a code to convert quaternions to z-y-x Euler angles in Python

I had to calculate the z-y-x Euler angles from the quaternion.

Moreover, since it is a matter that must be written in Python 2.7, you cannot use a convenient one like scipy.spatial.transform.Rotation.

When I looked up the formula, I found C ++ sample code on Wikipedia.

Wikipedia / Quaternion to Euler Angles Conversion

I wrote it in Python

I wrote the code for Python 2.7 by referring to the sample code on Wikipedia.

import math
import numpy as np
from pyquaternion import Quaternion

def quaternion_to_euler_zyx(q):
    """
Quaternion z-y-Convert to x-based Euler angles.

    Parameters
    ----------
    q : Quaternion
Quaternion(pyquaternion format)

    Returns
    -------
    np.array
        z-y-x Euler angles
    """

    # roll :x-axis rotation
    sinr_cosp = 2 * (q[0] * q[1] + q[2] * q[3])
    cosr_cosp = 1 - 2 * (q[1] * q[1] + q[2] * q[2])
    roll = math.atan2(sinr_cosp, cosr_cosp)

    # pitch :y-axis rotation
    sinp = 2 * (q[0] * q[2] - q[3] * q[1])
    if math.fabs(sinp) >= 1:
        pitch = math.copysign(math.pi / 2, sinp)
    else:
        pitch = math.asin(sinp)

    # yaw :z-axis rotation
    siny_cosp = 2 * (q[0] * q[3] + q[1] * q[2])
    cosy_cosp = 1 - 2 * (q[2] * q[2] + q[3] * q[3])
    yaw = math.atan2(siny_cosp, cosy_cosp)
    
    #Euler angles
    retrun np.array([
        math.degrees(roll), 
        math.degrees(pitch), 
        math.degrees(yaw)
    ])

Dependent libraries

numpy 1.16.6

This is the one used for vector calculation. 1.16.6 seems to be the latest among Python 2.7 support.

pyquaternion 0.9.5

I use pyquaternion to calculate Quaternion.

numpy-quaternion seems to be more compatible with numpy, but I couldn't install it in Python 2.7 environment.

Scipy.spatial.transform.Rotation is easy for Python 3 or later

If you use scipy.spatial.transform.Rotation, you can write it more simply.

import numpy as np
from pyquaternion import Quaternion
from scipy.spatial.transform import Rotation as R

def quaternion_to_euler_zyx(q):
	r = R.from_quat([q[0], q[1], q[2], q[3]])
	return r.as_euler('zyx', degrees=True)

However, scipy.spatial.transform.Rotation doesn't seem to support Python 2.7. Sorry.

at the end

I usually touch only Unity's z-x-y Euler angles, so the z-y-x system was quite confused.

In creating this article, I referred to the following articles. Thank you very much!

Recommended Posts

I wrote a code to convert quaternions to z-y-x Euler angles in Python
I wrote the code to write the code of Brainf * ck in python
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
A memo that I wrote a quicksort in Python
I want to create a window in Python
I wrote a class in Python3 and Java
I wrote "Introduction to Effect Verification" in Python
I want to convert a table converted to PDF in Python back to CSV
I made a script in python to convert .md files to Scrapbox format
Convert cubic mesh code to WKT in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to write in Python! (2) Let's write a test
I tried to implement a pseudo pachislot in Python
I made a code to convert illustration2vec to keras model
I want to randomly sample a file in Python
I want to work with a robot in python.
[Python] Created a method to convert radix in 1 second
I wrote python in Japanese
Convert python 3.x code to python 2.x
I wrote Python code to create a table (view) dependency diagram (PlantUML) from SQL
I tried to implement a one-dimensional cellular automaton in Python
I wrote a program quickly to study DI with Python ①
I tried "How to get a method decorated in Python"
I wrote a script to get a popular site in Japan
I tried to make a stopwatch using tkinter in python
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I made a script in Python to convert a text file for JSON (for vscode user snippet)
Convert markdown to PDF in Python
I wrote Fizz Buzz in Python
I wrote the queue in Python
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I wrote the stack in Python
I tried to convert a Python file to EXE (Recursion error supported)
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
Do a non-recursive Euler Tour in Python
I want to print in a comprehension
I made a payroll program in Python!
[Python] How to convert a 2D list to a 1D list
Convert psd file to png in Python
I tried to implement PLSA in Python 2
I want to build a Python environment
How to get a stacktrace in python
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
Convert from Markdown to HTML in Python
I wrote Project Euler 1 in one liner.
I created a password tool in Python.
Convert absolute URLs to relative URLs in Python
I made a web application in Python that converts Markdown to HTML
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
Note: [Python3] Convert datetime to a string in any format you like
I was addicted to creating a Python venv environment with VS Code
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
[Python] I wrote a simple code that automatically generates AA (ASCII art)
I made a CLI tool to convert images in each directory to PDF