Post · 2 min read· p. 313

Chiaverini’s Method

ahrs.common.orientation.chiaverini(dcm: ndarray) → ndarray : Quaternion from a Direction Cosine Matrix with Chiaverini’s algebraic method [CS99].

code
Defining the unit quaternion as:

q=[qwqv]\mathbf{q} = \begin{bmatrix} q_w & \mathbf{q}_v \end{bmatrix}
where
qw=cos⁡(θ2)qv=sin⁡(θ2)r\begin{split}\begin{array}{rcl} q_w &=& \cos \big(\frac{\theta}{2}\big) \\ \mathbf{q}_v &=& \sin \big(\frac{\theta}{2}\big) \mathbf{r} \end{array}\end{split}
with qw≥0q_w \geq 0 for θ∈[−π,π]\theta \in [-\pi, \pi]; qwq_w is the scalar part, while qv\mathbf{q}_v is the vector part, and θ\theta is the rotation about the axis r\mathbf{r}. We know the rotation matrix of a given unit quaternion is:
R(q)=(qw2−qvTqv)I3+2qvqvT+2qw⌊qv⌋×\mathbf{R}(\mathbf{q}) = (q_w^2 - \mathbf{q}_v^T\mathbf{q}_v)\mathbf{I}_3 + 2\mathbf{q}_v\mathbf{q}_v^T + 2 q_w\lfloor\mathbf{q}_v\rfloor_\times
where I3\mathbf{I}_3 is the 3×33\times 3 identity matrix, and ⌊qv⌋×\lfloor\mathbf{q}_v\rfloor_\times is the [skew-symmetric matrix](https://en.wikipedia.org/wiki/Skew-symmetric_matrix) of the vector part. Solving the equation above for the scalar and vector parts we get the unitary quaternion:
qw=12R00+R11+R22+1qv=[12sgn(R21−R12)R00−R11−R22+112sgn(R02−R20)R11−R22−R00+112sgn(R10−R01)R22−R00−R11+1]\begin{split}\begin{array}{rcl} q_w &=& \frac{1}{2} \sqrt{R_{00} + R_{11} + R_{22} + 1} \\ \mathbf{q}_v &=& \begin{bmatrix} \frac{1}{2} \mathrm{sgn}(R_{21} - R_{12}) \sqrt{R_{00} - R_{11} - R_{22} + 1} \\ \frac{1}{2} \mathrm{sgn}(R_{02} - R_{20}) \sqrt{R_{11} - R_{22} - R_{00} + 1} \\ \frac{1}{2} \mathrm{sgn}(R_{10} - R_{01}) \sqrt{R_{22} - R_{00} - R_{11} + 1} \end{bmatrix} \end{array}\end{split}
where sgn\mathrm{sgn} is the sign function:
sgn(x)={−1 x<01 otherwise\begin{split}\mathrm{sgn}(x) = \left\{ \begin{array}{ll} \mathrm{-1} & \: x < 0 \\ \mathrm{1} & \: \mathrm{otherwise} \end{array} \right.\end{split}
Parameters: : **dcm** (*numpy.ndarray*) – 3-by-3 or N-by-3-by-3 Direction Cosine Matrix (or Matrices). Returns: : **q** – 4-dimensional or N-by-4 Quaternion array. Return type: : numpy.ndarray