Post · 2 min read· pp. 115–116

Quaternion.from_rpy

Quaternion.from_rpy(angles: ndarray) → ndarray : Quaternion from given RPY angles.

code
The quaternion can be constructed from the Aerospace cardanian angle
sequence that follows the order ϕ→θ→ψ\phi\to\theta\to\psi,
where ϕ\phi is the **roll** (or *bank*) angle, θ\theta
is the **pitch** (or *elevation*) angle, and ψ\psi is the
**yaw** (or *heading*) angle.

The composing quaternions are:

qX=(cos⁡ϕ2sin⁡ϕ200)qY=(cos⁡θ20sin⁡θ20)qZ=(cos⁡ψ200sin⁡ψ2)\begin{split}\begin{array}{rcl} \mathbf{q}_X &=& \begin{pmatrix}\cos\frac{\phi}{2} & \sin\frac{\phi}{2} & 0 & 0\end{pmatrix} \\ && \\ \mathbf{q}_Y &=& \begin{pmatrix}\cos\frac{\theta}{2} & 0 & \sin\frac{\theta}{2} & 0\end{pmatrix} \\ && \\ \mathbf{q}_Z &=& \begin{pmatrix}\cos\frac{\psi}{2} & 0 & 0 & \sin\frac{\psi}{2}\end{pmatrix} \end{array}\end{split}
The elements of the final quaternion q=qZqYqX=qw+qxi+qyj+qzk\mathbf{q}=\mathbf{q}_Z\mathbf{q}_Y\mathbf{q}_X = q_w+q_xi+q_yj+q_zk are obtained as:
qw=cos⁡ψ2cos⁡θ2cos⁡ϕ2+sin⁡ψ2sin⁡θ2sin⁡ϕ2qx=cos⁡ψ2cos⁡θ2sin⁡ϕ2−sin⁡ψ2sin⁡θ2cos⁡ϕ2qy=cos⁡ψ2sin⁡θ2cos⁡ϕ2+sin⁡ψ2cos⁡θ2sin⁡ϕ2qz=sin⁡ψ2cos⁡θ2cos⁡ϕ2−cos⁡ψ2sin⁡θ2sin⁡ϕ2\begin{split}\begin{array}{rcl} q_w &=& \cos\frac{\psi}{2}\cos\frac{\theta}{2}\cos\frac{\phi}{2} + \sin\frac{\psi}{2}\sin\frac{\theta}{2}\sin\frac{\phi}{2} \\ && \\ q_x &=& \cos\frac{\psi}{2}\cos\frac{\theta}{2}\sin\frac{\phi}{2} - \sin\frac{\psi}{2}\sin\frac{\theta}{2}\cos\frac{\phi}{2} \\ && \\ q_y &=& \cos\frac{\psi}{2}\sin\frac{\theta}{2}\cos\frac{\phi}{2} + \sin\frac{\psi}{2}\cos\frac{\theta}{2}\sin\frac{\phi}{2} \\ && \\ q_z &=& \sin\frac{\psi}{2}\cos\frac{\theta}{2}\cos\frac{\phi}{2} - \cos\frac{\psi}{2}\sin\frac{\theta}{2}\sin\frac{\phi}{2} \end{array}\end{split}
Warning The Aerospace sequence ϕ→θ→ψ\phi\to\theta\to\psi is only one of the [twelve possible rotation sequences](https://en.wikipedia.org/wiki/Euler_angles#Tait.E2.80.93Bryan_angles) around the main axes. Other sequences might be more suitable for other applications, but this one is the most common in practice. Parameters: : **angles** (*numpy.ndarray*) – 3 cardanian angles, in radians, following the order: roll -> pitch -> yaw. Returns: : **q** – Quaternion from roll-pitch-yaw angles. Return type: : numpy.ndarray Examples ```default

from ahrs import DEG2RAD # Helper variable to convert angles to radians q = Quaternion() q.from_rpy(np.array([10.0, 20.0, 30.0])*DEG2RAD) # Give roll-pitch-yaw angles as radians. array([0.95154852, 0.23929834, 0.18930786, 0.03813458])

code

    It can be corroborated with the class [DCM](https://ahrs.readthedocs.io/en/latest/quaternion/dcm.html), which represents a Direction
    Cosine Matrix, and can also be built with roll-pitch-yaw angles.

    ```default
>>> from ahrs import DCM
>>> R = DCM(rpy=[10.0, 20.0, 30.0])     # Here you give the angles as degrees
>>> R
DCM([[ 0.92541658,  0.01802831,  0.37852231],
     [ 0.16317591,  0.88256412, -0.44096961],
     [-0.34202014,  0.46984631,  0.81379768]])
>>> q.from_DCM(R)
array([0.95154852, 0.23929834, 0.18930786, 0.03813458])
code
With both approaches the same quaternion is obtained.