Quaternions and Attitude Representation
1 Attitude Representation
The attitude of a rigid body belongs to the configuration space known as Special Orthogonal group and is represented in most general terms as rotation matrix. The space can, however, be parameterized in terms of fewer parameters. The advantages and disadvantages of some important representations used are summarized below:

Rotation matrices: This is the most general form for representing attitude of a body, also called special orthogonal group group, is the space of matrices satisfying some constraints. Since it uses nine numbers to represent three angular degrees of freedom, there are six independent constraints on the matrix elements. Each column(and row) is unit vector, which gives us 3 constraints and the columns (and rows) are orthogonal to each other, yielding another 3 constraints. The translation () and rotation together are represented as Special Euclidean group . The 6 constraints are larger compared to all other parameterizations mentioned below. They are therefore computationally more expensive than them. However, these have the advantage that they have no singularities or ambiguities such as double cover in attitude space in their representation as the rotation matrix is uniquely determined for a given configuration.

Euler angles: These are more intuitive and easy to interpret physically. However, they suffer from singularity in mathematical formulations such as in RollPitchYaw, also known as gimbal lock. At Pitch angle of , it is unable to differentiate between Yaw and Roll degrees of freedom. There may also be a discontinuity when moving the angles in the parameter space, say, at . One of the Euler angles change suddenly in response to small change in rotation.

Unit Quaternions: Owing to parametrization by 4 variables compared to 3 in Euler angles, these do not have singularity problem. Also, any rotation sequence can be represented by a continuous quaternion trajectory and do not suffer any discontinuity like Euler angles. These are also computationally more efficient than Euler angles since computation of sines and cosines is more expensive than simple operator on numbers. However, they possess the ambiguity of dual covering, i.e., the quaternions and represent the same quaternion. It is also easier to interpolate between rotations and to chain rigid transformations.

Axis/angle representation: It parameterizes the rotation by a unit vector and a rotation about it by angle . Since unit vector has a norm constraint, three parameters are required for representation, 2 for unit vector and 1 for rotation angle. The representation is not unique and an additional rotation of gives the same rotation matrix. However it has many advantages too. The representation is minimal and does not require any constraints on parameters such as unit modulus of quaternions used to represent rotations. It is easer to understand the pose (example a twist about y axis) and the derivatives of rotation matrix R with respect to rotation angle can easily be computed.
2 Problem with Euler Angles
Although Euler angles are widely used because of their minimalistic (3 parameters for 3 Degrees of Freedom) and intuitive physical representation, they exhibit a phenomenon known as Gimbal Lock which results in a singularity in the attitude representation. Note that the Euler angles are relative sequential rotations (See video on GuerrilaCG Youtube channel: Euler (gimbal lock) Explained: https://www.youtube.com/watch?v=zc8b2Jo7mno ). For example in a 123 rotation, a single roll angle will change the orientation of pitch and yaw axis but in case of a single pitch angle rotation, the roll axis remains intact while yaw axis changes its orientation with the pitch rotation. When the pitch is , the roll and yaw axis becomes the same and system can rotate only about 2 axis in space at that particular instant. Therefore, when the axis of two out of the three gimbals are driven parallel to each other in a configuration, there is a loss of degree of freedom and the dimension of the attitude space reduces to 2. The gimbal is still free to move(there is no actual mechanical lock as the name suggests) however, in order to move along the third missing axis in this configuration, the body will have to move simultaneously along two gimbals axis and may exhibit unfamiliar motions when such a situation is encountered physically. There are many ways in which people deal with this problem while still using Euler angles however, that treatment is not a topic of these notes.
2.1 Mathematical Implication
Mathematically the formulation of attitude space in terms of Euler angles can cause a singularity and demand …. commands. Consider a rigid body with its angular velocity represented by the orthogonal vector as per the convention. The Euler rates are not orthogonal as they are defined in different frame of references and, for a 321 rotation, can be related to angular velocity as follows:
(1)  
(2) 
The determinant of the above matrix becomes zero at and there the dimension of the space reduces to 2. The matrix can be inverted to obtain Euler rates in terms of angular velocity as:
(3) 
At the singularity point, the matrix elements are not defined as and terms go to .
3 Quaternions
Quaternions can be considered as 4 component extended complex number of the form
(4) 
where in , is called the real part and is called the imaginary part of the quaternion. Some concepts and definitions used in Quaternion theory are described below[1]:

A right handed notation follows the following rules:
(5) (6) (7) (8) 
The conjugate of is defined as
(9) 
Norm is defined as
(10) 
The quaternion product is denoted as and can be obtained using the rules of Eqs. 25.
(11) (12) (13) (14) (15) (16) 
(17) (18) 
Inverse of a quaternion is defined as :
(19) This is similar to inverse in complex numbers:
(20)
4 Quaternion: Intuitive Representation
A unit quaternion () can be used to represent a rotation in space. This set of 4 dimensional vectors with unit modulus are said to belong to set [4]. Since its modulus is one, ( is also the notation for conjugate of quaternion which is more commonly used in case quaternion elements are complex numbers. Since rotation quaternions have real entries, ). Henceforth, all the quaternions mentioned will be unit quaternions unless stated otherwise.
Euler’s Rotation Theorem
Any rotation or sequence of rotations of a rigid body or coordinate system about a fixed point is equivalent to a single rotation by a given angle Î¸ about a fixed axis (called Euler axis) that runs through the fixed point.
Quaternions can be very easily correlated to the axis angle representation of attitude. Any attitude of a rigid body can be defined by stating an axis in 3D with unit vector , and a rotation about that axis, . A rotation quaternion can be represented in terms of these two as:
(21) 
As verification, consider the well known rotation formula studied in dynamics course.
(22) 
where is the rotation about the axis of rotation
Now consider the equation
(23) 
Simplifying the above, we get
(24)  
(25) 
If we now choose as , , then becomes:
(26) 
This is same as the known rotation formula.
4.1 Extension of complex numbers
It is well known that multiplying a complex numbers (=) by results in amplification of its magnitude by times and change in orientation by an angle (product = ). See Fig.1 for an example. Now if we were to multiply with a unit modulus complex number, i.e. , then we can perform pure rotation. Quaternions can be seen just to be an extension of this mathematical construction and the constraint of unit modulus to higher dimension.
5 Quaternion Conventions
Quaternions are represented in many ways and have different interpretations. For example, one can consider rotating frames or rotating vectors or a left handed or right handed operations(Note that the usual vector cross product is based on right handed system, however it can also be defined in a left handed system). Two most commonly used notations are Hamilton and JPL conventions, both of which considers rotating frame[2]. The one used in these notes above and which will be used in subsequent theory is the Hamilton convention. Some differences between two are:
Quaternion Type  Hamilton  JPL 

1. Component order  
2. Algebra  ij=k(right handed)  ij=k(left handed) 
3. Default notation  Local to Global  Global to local 
where stands for local frame and stands for global frame. Therefore given the sequence of operation , we go from local frame to global frame in Hamilton notation and vice versa in JPL when going from left hand side to right hand side in an equation.
6 Quaternion Derivatives
Quaternion derivatives are often used to represent angular velocity or to integrate the system dynamics. This section will state and derive some important relations which are very common in literature.
6.1 First Derivative
The quaternion rates are given in terms of angular velocity expressed in global frame () or body frame ().
(27)  
(28)  
(29)  
(30) 
A simple derivation for the quaternion rate is done using the first principle[2].
(31)  
(32)  
(33)  
(34)  
(35) 
Note that in Hamilton notation, local disturbances or rotations(i.e., with respect to current configuration) are postmultiplied as done above since we move from local to global frames when going left. Thus, the change in angle represents the angular velocity in body frame. Similarly if one were to add global disturbances(i.e., with respect to glbal coordinate system and not the current body configuration), they would be premultiplied and not postmultiplied. This can be better understood by following example. Consider the following two sets of consecutive rotations:

Case a: Rotation about by , then rotation about (new axis) by

Case b: Rotation about by . then rotation about (original axis) by
Consider the vector in the final frame of reference. We desire to obtain this vector in the original frame of reference. From Fig.2 it is clear that Case a should yield as the answer and Case b should yield as the answer.
Let us denote first rotation quaternion by and the second by . In the first case, since we are rotating with respect to local frame of reference, this local perturbation is post multiplied, i.e., . Whereas, in the second case, we are rotating again with respect to global frame of reference, therefore we will be premultiplying, i.e., .
Let denote the quaternion corresponding to position vector in global frame and denote the quaternion corresponding to position vector in local frame. Another derivation for the derivative formula is as follows[1]:
(36)  
(37)  
(38)  
(39) 
Now from definition of quaternion product,
(40)  
(41) 
where in last step scalar part becomes zero, which can be obtained by differentiating , and the vector part is defined as for convenience.
Therefore, we get
(42)  
(43) 
Note that it can be easily shown from above expressions and using that which implies that is perpendicular to .
Now for pure rotation, where is the angular velocity and is perpendicular to . Therefore, we have
(44)  
(45)  
(46) 
Now the angular velocity in body frame is given by and hence Eq.(46) becomes
(47) 
It is very convenient to have matrix notations for all the operations mentioned above. Let and be defined by:
(48)  
(49) 
The angular velocities can then also be written in terms of G and E as follows:
(50)  
(51)  
(52)  
(53) 
It can easily be shown that following also holds:
(54) 
Note that . Therefore . Similarly .
6.2 Second Derivative
We will now derive relationship between quaternion and angular acceleration. Differentiating , we get
(55)  
(56)  
(57)  
(58)  
(59) 
One can similarly work out the derivative in terms of world frame angular velocity .
7 Rotation Matrix
The rotation matrix R which transforms to can be written as[1]:
(60) 
The R matrix is obtained as:
(61) 
This can easily be seen as: .
The rotation matrix derivative can then easily be obtained as[1]
(62)  
(63) 
where is the skew symmetric matrix corresponding to the angular velocity vector.
8 Problems with Quaternions and other attitude representations
8.1 Quaternions
Since there are two quaternions:

Ambiguities in representing an attitude: The constraint of unit modulus of quaternions restrict the quaternions to a sphere of unit radius in 4 dimensions, known as the threesphere which is the set of unitvectors in . This three sphere doublecovers the attitude configuration of the special orthogonal group, SO(3).

Single physical attitude of a rigid body may yield two different control inputs, which causes inconsistency in the resulting control system. A specific choice between two quaternions generates discontinuity that makes the resulting control system sensitive to noise and disturbances.
8.2 General Representation
We first need to understand an important fact related to attitude stabilization. It has been proved mathematically[4] that there cannot exist a smooth controller on the configuration space (such as or ) that is globally asymptotically stable.
Theorem: The geometry of attitude space precludes the existence of continuous time invariant control that achieves global asymptotic stability.
The proof of this theorem is a little involved with consideration of topological obstructions and configuration manifold which is out of scope of these notes. Details can be found in Reference [4]. Note that even the geometric controllers which work on directly instead of any parameterizations have also been shown to be almost globally asymptotically stable, such as in [5], where a two dimensional manifold is excluded from the three dimensional in the region of attraction. In case of quaternions or Euler angles it is easy to note that since and and and represent the same attitude, there exists more than one equilibrium point and thus one cannot even talk about global stability. One can filter the algorithm for these cases but then the controller becomes discontinuous (nonsmooth) and hence the above theorem is not violated.
Unwinding Phenomenon: The body may rest arbitrarily close to the desired attitude yet rotate through large angles before coming to rest in the desired attitude[4].
Consider a body rotating about a single axis in a plane with angular velocity with angular position . We can design a control torque to stabilize the system dynamics as:
(64)  
(65)  
(66)  
(67) 
It may seem that is globally asymptotically stable point but values of that differ by integral values of represent the same point in configuration space and hence it is not well defined in the state space. If we start at , then instead of going shorter way of anticlockwise, the controller will take it clockwise.
9 Quaternion Error Dynamics
The quaternion error is defined as the relative rotation between the desired rotation and actual rotation. In order to better understand the inverse multiplication operation, we take example of rotation matrices. Suppose the rotation matrices corresponding to two consecutive relative rotations are and . The net rotation matrix is then given by . Now if and were to be given, then can be found by multiplying both sides by inverse of .
(68)  
(69) 
Now suppose takes us from body to global frame and takes us from desired to global frame. Then (error rotation matrix) takes us from body to desired frame. Hence the error rotation matrix can be found out by:
(70) 
The quaternions behave in a similar way to the rotation matrices. If the body attitude quaternion is and the desired body attitude is then the the error quaternion is defined as
(71) 
As verification, note that
(72)  
(73)  
(74)  
(76)  
(78)  
This is just one way to define the error quaternion and authors may define this quaternion in different ways.
9.1 Quaternion Error dynamics: First Order
Closed loop systems compute a feedback based not only on the error of involved quantities but also many times based on the rate of change of error. For this purpose, we derive here the expressions for quaternion error derivatives. It is to be noted that subtraction between two quantities has a meaning(or is consistent) only if they are expressed in same frame. Hence, suppose we are given a history of desired attitudes and we obtain the angular velocity from it. This operation will return us the derivative in the desired frame of references at each instant. To compare it to actual angular velocity, defined in the body frame, appropriate transformations have to be made which will be become clear below. Note that these transformations arise naturally since the physical quantities have to be consistent in their arithmetic.
From Eq.(72), we can obtain the time derivative of the error quaternion in the following way
(79)  
(80)  
(81)  
(82)  
(85)  
(86)  
(87)  
(88) 
where is the desired angular velocity quaternion and stands for desired quaternion frame and denotes body frame.
Now, according to , in
q: B to G frame, : D to G frame : B to D frame
(89) 
Denoting the angular velocity error as , the above equation can be written in a similar form as Eq.(35)
(90) 
10 Quaternion to Euler Angle Conversion
Euler angles can be defined in many ways depending on the order of rotation and are still in popular use due to their simple physical interpretations. However it can be tricky to convert between quaternions and the Euler angles and this section gives a method to derive this conversion. The formula derived can be verified in a number of resources[7].
Let XYZ represent the rotation first about X axis by , then about new Y axis by and finally about new Z axis by . Then the rotation matrix converting a vector from final frame (x,y,z) to original frame(X,Y,Z) is given by
(91)  
(92)  
(93) 
We have already derived the rotation matrix with quaternions which is
(94) 
Comparing the above two matrices element by element, we can find Euler angles from the quaternions:
(95)  
(96)  
(97) 
To obtain quaternion in terms of Euler Angles, we can solve the above system of equations. However, it can be tedious to solve. Instead, we can directly construct quaternion from the knowledge of Euler sequence. For the above sequence, just like we proceeded while deriving the quaternion derivative, the local rotation is compounded to the right. We first represent the quaternions for three rotations as
(98) 
Now compounding the rotations as before, i.e., postmultiplying the local rotations, we get
(99)  
(100) 
The above expressions can be verified with the NASA documentation[7]. The reader can also refer to this document for transformations of all 12 Euler angle combinations.
Disclaimer
These notes on quaternions are by no means complete and exhaustive. They only describe some fundamental details and do not go into much depth. They serve only to introduce the quaternions and coherently state their properties so that the reader can now readily refer to other literatures and understand the subtlety involved in the use of quaternions due to the existence of multiple conventions. Interested readers may refer to the references given below and to the vast literature accessible on Internet for greater detail.
Refernences
[1]Graf, Basile. ”Quaternions and dynamics.” arXiv preprint arXiv:0811.2889 (2008)
[2]Sola, Joan. ”Quaternion kinematics for the errorstate KF.” Laboratoire dâAnalyse et dâArchitecture des SystemesCentre national de la recherche scientifique (LAASCNRS), Toulouse, France, Tech. Rep (2012)
[3]Diebel, James. ”Representing attitude: Euler angles, unit quaternions, and rotation vectors.” Matrix 58.1516 (2006): 135
[4]Bhat, Sanjay P., and Dennis S. Bernstein. ”A topological obstruction to continuous global stabilization of rotational motion and the unwinding phenomenon.” Systems & Control Letters 39.1 (2000): 6370
[5]Lee, Taeyoung, Melvin Leok, and N. Harris McClamroch. ”Control of complex maneuvers for a quadrotor UAV using geometric methods on SE (3).” arXiv preprint arXiv:1003.2005 (2010)
[6]Choset, Howie M. Principles of robot motion: theory, algorithms, and implementation. MIT press, 2005.
[7]Henderson, D. M. ”Euler angles, quaternions, and transformation matricesâWorking relationships.” NASA TM74839, JSC12960 (1977).