2.12 Nonsingular Coordinates and Quaternions
The Euler angles provide a convenient way to parameterize the orientation of a rigid body. However, the equations of motion derived for them have singularities. Though we can avoid the singularities by using other Eulerlike combinations with different singularities, this kludge is not very satisfying.

We know from Euler’s theorem that any orientation can be reached with a single rotation. Specifying this rotation requires specifying its axis and the angle of rotation.

One possibility: Use latitude and longitude to define the axis. But this becomes undefined in the case of zero rotation

Another possibility: Use a vector whose direction is that of the axis vector and whose length defines the amount of rotation. In this case, rotation by $2\pi$ is equal to no rotation at all. Every rotation can be represented by a countably infinite number of vectors, each with a length of $\theta + 2M\pi$ for an integer $M$. This makes it problematic to invert the mapping from other representations of rotation.

A fix for the above point is to scale the vector by sine of half of the rotation angle. With this choice a rotation by zero angle will have the same orientation vector as a rotation by $2\pi$. But there is still another problem: rotations by $\theta$ and $2\pi  \theta$ are not distinguished. That is, a vector may represent both the “shortrotation” and the “long rotation” for the same orientation.

We can solve this by keeping track of the cosine of half the angle of rotation (though in reality we just need the sign of the cosine). Wrapping this all up into 4tuples gives us Hamilton’s quaternions.
For a rotation by angle $\theta$ about axis $\hat{n}$, the components of a quaternion defining the rotation are:
This is a unit quaternion as the sum of squares of its components is equal to one. The first element is called the “real part”, $r$, and the other three elements can form a tuple $v$ called the “imaginary part”. The angle of rotation can be obtained by computing $\theta = \arctan(v, r)$, and the axis direction is $v/v$.
 The rotation represented by a quaternion is not changed by reversing the sign of all its components. This is because changing the sign of v reverses the axis but does not change the angle and changing the sign of the first component changes the angle $\theta$ to $2\pi − \theta$, so the actual rotation is unchanged.
DCM from Quaternion
Given the four elements of a quaternion, we can do compute the corresponding rotation matrix. First we get the angle $\theta$ and axis, $\hat{n}$ when given a quaternion. We rotate by angle $\theta$ about the $z$ axis and then transform this rotation by a rotation to the axis specified by the colatitude ($\varphi$) and longitude ($\lambda$) of the axis $\hat{n}$:
where $\varphi = \arccos(\hat{n_z})$ and $\lambda = \arctan(\hat{n}_y, \hat{n}_x)$.
(defn myangleaxis>rotationmatrix [theta n]
(let [nx (ref n 0)
ny (ref n 1)
nz (ref n 2)
colatitude (acos nz)
longitude (atan ny nx)]
(* (rotatezmatrix longitude)
(rotateymatrix colatitude)
(rotatezmatrix theta)
(transpose (rotateymatrix colatitude))
(transpose (rotatezmatrix longitude)))))
(rendermd (myangleaxis>rotationmatrix (/ pi 4) (up 0 0 1)))
(defn quaternion>angleaxis [q]
(let [v (quat/threevector q)
sintheta (sqrt (dotproduct v v))
theta (* 2 (atan sintheta (quat/realpart q)))
axis (/ v sintheta)]
(up theta axis)))
;; Quaternion to DCM
(defn quaternion>DCM [q]
(let [aa (quaternion>angleaxis q)
theta (ref aa 0)
n (ref aa 1)]
(angleaxis>rotationmatrix theta n)))
;; 30 degrees about z axis
(render (quaternion>DCM (quat/make (cos (/ (/ pi 6) 2)) 0 0 (sin (/ (/ pi 6) 2)))))
The matrix terms here are all divided by the magnitude of $q$ which can be ignored since we use unit quaternions. Once this is removed, we get the following form for the rotation matrix from a quaternion $(q_0, q_1, q_2, q_3)$:
(defn quaternion>rotationmatrix [q]
(let [qvec (quat/>vector q)
q0 (ref qvec 0)
q1 (ref qvec 1)
q2 (ref qvec 2)
q3 (ref qvec 3)
m2
(+ (expt q0 2) (expt q1 2)
(expt q2 2) (expt q3 2))]
(/ (matrixbyrows
(list ( (+ (expt q0 2) (expt q1 2))
(+ (expt q2 2) (expt q3 2)))
(* 2 ( (* q1 q2) (* q0 q3)))
(* 2 (+ (* q1 q3) (* q0 q2))))
(list (* 2 (+ (* q1 q2) (* q0 q3)))
( (+ (expt q0 2) (expt q2 2))
(+ (expt q1 2) (expt q3 2)))
(* 2 ( (* q2 q3) (* q0 q1))))
(list (* 2 ( (* q1 q3) (* q0 q2)))
(* 2 (+ (* q2 q3) (* q0 q1)))
( (+ (expt q0 2) (expt q3 2))
(+ (expt q1 2) (expt q2 2)))))
m2)))
;; This function can be further used to compute the components of angular velocity using M>omegabody
(rendermat
((rigid/M>omegabody
(compose quaternion>rotationmatrix quat/make))
(up 't
(up 'q_0 'q_1 'q_2 'q_3)
(up 'qdot_0 'qdot_1 'qdot_2 'qdot_3))))
Ignoring the denominator, the quaternion is independent of the scale of the quaternion. And since it is a fucntion of time, the rates of $q$ are also independent of scale. This can be further simplified by representing the products in the numerator as a matrix product. Define the following matrices:
The angular velocity components can now be written as :
where $\mathscr{q}$ is a column vector of the components of $q$. The antisymmetric matrices $\mathbf{i}$, $\mathbf{j}$ and $\mathbf{k}$ have the interesting properties that make it the basis vectors for the quaternions:
where $\mathbf{1}$ is the identity matrix.
Composition of rotations
An easy way to compose ttwo rotations would be to convert ehm to DCMs and multiply them. However this results in a very messy results where each component is scaled by a factor of $qp$ (which is equal to $1$ for unit quaternions). Eliminating this scale factor, we get the following form for composition of quaternions
This can be restated as:
where $r_0$ is the scalar part of the result, $v_r$ is the vector part of the result and $v_p$ and $v_q$ are the vector parts of the original quaternions. This can be considered to be the product of the two quaternions.