Rotations using quaternions

From Robotics
Jump to: navigation, search
← Back: Multiplication of quaternions Overview: Quaternions Next: Composition of rotations
Quaternion-rpy.png

Usually rotations are defined by 3 angles, either Euler or Roll-Pitch-Yaw angles. So three successive rotations around three different axes lead to a combined rotation, that can be described by a single rotation matrix. Such a combined rotation is equal to a rotation around a certain axis in three-dimensional space about a certain angle. This is shown in the figure on the right. The vector \vec{\mathbf{r}} is rotated such that it results in \vec{\mathbf{r}}'. This could be done by rotating the vector around the z-, the y- and the x-axis successively or by just rotating it around the rotation axis \vec{\mathbf{u}} by \phi.

Mathematical description

First the rotation axis \vec{\mathbf{u}} has to be defined by a unit vector. So it holds:


|\vec{\mathbf{u}}|= \sqrt{u_x^2+u_y^2+u_z^2} = 1 \qquad \Rightarrow \qquad {u_x^2+u_y^2+u_z^2} = 1

Then a rotation about \vec{\mathbf{u}} by the angle \phi can be represented by a quaternion e like follows:


\begin{align}
e(\vec{\mathbf{u}},\phi) 
&= e_0 + \vec{\mathbf{e}} \\
&= e_0+e_1i+e_2j+e_3k \\
&= \cos{\frac{\phi}{2}} + \sin{\frac{\phi}{2}}\vec{\mathbf{u}} \\
&= \cos{\frac{\phi}{2}} + \sin{\frac{\phi}{2}}(u_xi+u_yj+u_zk) \\
&= \cos{\frac{\phi}{2}} + \sin{\frac{\phi}{2}}u_xi + \sin{\frac{\phi}{2}}u_yj + \sin{\frac{\phi}{2}}u_zk
\end{align}

So the scalar part e_0 is defined as the cosine of \frac{\phi}{2} and the vector part consisting of e_1, e_2 and e_3 equals \vec{\mathbf{u}} multiplied by the sine of \frac{\phi}{2}.

That the resulting quaternion e is a unit quaternion can be proven as follows:


\begin{align}
|e| &= \sqrt{e_0^2+e_1^2+e_2^2+e_3^2} \\
 &= \sqrt{\cos^2{\frac{\phi}{2}} + \sin^2{\frac{\phi}{2}}u_x^2 + \sin^2{\frac{\phi}{2}}u_y^2 + \sin^2{\frac{\phi}{2}}u_z^2 } \\
 &= \sqrt{\cos^2{\frac{\phi}{2}} + \sin^2{\frac{\phi}{2}}\cdot(u_x^2 + u_y^2 + u_z^2)} \\
 &= \sqrt{\cos^2{\frac{\phi}{2}} + \sin^2{\frac{\phi}{2}}\cdot1}  = \sqrt{\cos^2{\frac{\phi}{2}} + \sin^2{\frac{\phi}{2}}} = \sqrt{1} = 1
\end{align}

So if a quaternion e=(e_0,e_1,e_2,e_3) is given, the angle \phi and the rotation axis \vec{\mathbf{u}} can be computed as follows:


\begin{align}
\phi &= 2\cdot \arccos e_0 \\
\vec{\mathbf{u}} &= \frac{1}{\sin{\frac{\phi}{2}}}\left[\begin{array}{c}e_1\\e_2\\e_3\end{array}\right]
\end{align}

Mathematical solving of a quaternion rotation

Assume a quaternion e(\vec{\mathbf{u}},\phi) = (e_0,e_1,e_2,e_3) describing a rotation and a vector \vec{\mathbf{r}} that should be rotated. To be able to apply the rotation, \vec{\mathbf{r}} has to be presented as a quaternion. So the pure quaternion r is defined with scalar part equal to 0 and \vec{\mathbf{r}} as vector part:


r = 0 \oplus \vec{\mathbf{r}}

The same is done for the resulting vector \vec{\mathbf{r'}} of the rotation:


r' = 0 \oplus \vec{\mathbf{r'}}

The rotation is then applied by the following quaternion multiplication:


r' = e(\vec{\mathbf{u}},\phi)\ r \ e^*(\vec{\mathbf{u}},\phi)

Following the rules for quaternion multiplication, this equation leads to


r' = e \ r \ e^* = 0 \ \oplus \ 
\left[\begin{array}{ccc}
e_0^2+e_1^2-e_2^2-e_3^2 & 2(e_1e_2-e_0e_3) & 2(e_0e_2+e_1e_3) \\
2(e_0e_3+e_1e_2) & e_0^2-e_1^2+e_2^2-e_3^2 & 2(e_2e_3-e_0e_1) \\
2(e_1e_3-e_0e_2) & 2(e_0e_1+e_2e_3) & e_0^2-e_1^2-e_2^2+e_3^2
\end{array}\right] 
\left[\begin{array}{ccc}
r_1 \\
r_2 \\
r_3
\end{array}\right] 
= 0 \ \oplus \ \mathbf{R_e}\vec{\mathbf{r}}

This shows, that the result of the multiplication is again a pure quaternion describing the rotated vector \vec{\mathbf{r}}'. The corresponding rotation matrix describing the rotation obtained by the quaternion can easily be computed using the equation for \mathbf{R_e}.

How several rotations can be composed in one quaternion is explained in the next article Composition of rotations.

Example: Quaternion rotation and rotation matrices
Quatexample.png

An object is shifted 1 unit on the x-axis. So its position vector and the corresponding quaternion are defined as follows:


\vec{\mathbf{p}} = \left[\begin{array}{c}1\\0\\0\end{array}\right], \qquad
p = 0 \ \oplus \ \vec{\mathbf{p}} = (0,1,0,0)

Assume the object or the vector, respectively, is rotated corresponding to the Roll-Pitch-Yaw notation like shown in the figure on the right. So the first rotation is applied around the z-axis by a yaw angle of 45^\circ (shown in blue). Then it is rotated around the y-axis by a pitch angle of 90^\circ (shown in green). The roll angle is defined as 135^\circ around the x-axis (shown in red). Multiplying the three rotation matrices leads to the following combined rotation matrix:


\begin{align}
\mathbf{R}_{rpy} &= 
\left[\begin{array}{ccc}
1 & 0 & 0 \\
0 & \cos{45^\circ} & -\sin{45^\circ} \\
0 & \sin{45^\circ} & \cos{45^\circ}
\end{array}\right]
\left[\begin{array}{ccc}
\cos{90^\circ} & 0 & \sin{90^\circ} \\
0 & 1 & 0 \\
-\sin{90^\circ} & 0 & \cos{90^\circ}
\end{array}\right]
\left[\begin{array}{ccc}
\cos{135^\circ} & -\sin{135^\circ} & 0 \\
\sin{135^\circ} & \cos{135^\circ} & 0 \\
0 & 0 & 1
\end{array}\right] 
\\ &=
\left[\begin{array}{ccc}
1 & 0 & 0 \\
0 & \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \\
0 & \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}}
\end{array}\right]
\left[\begin{array}{ccc}
0 & 0 & 1 \\
0 & 1 & 0 \\
-1 & 0 & 0
\end{array}\right]
\left[\begin{array}{ccc}
-\frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} & 0 \\
\frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} & 0 \\
0 & 0 & 1
\end{array}\right]
\\ &=
\left[\begin{array}{ccc}
0 & 0 & 1 \\
0 & -1 & 0 \\
1 & 0 & 0
\end{array}\right]
\end{align}

The resulting vector \vec{\mathbf{p'}} of the rotation, that is also shown in the figure on the right, then is:


\begin{align}
\vec{\mathbf{p'}} = \mathbf{R}_{rpy}\vec{\mathbf{p}} = \left[\begin{array}{ccc}
0 & 0 & 1 \\
0 & -1 & 0 \\
1 & 0 & 0
\end{array}\right]
\left[\begin{array}{c}1\\0\\0\end{array}\right] = \left[\begin{array}{c}0\\0\\1\end{array}\right]
\end{align}

Regarding the figure it can easily be seen, that a rotation around the diagonal \vec{\mathbf{u}} of the xz-plane by 180^\circ results in the same vector \vec{\mathbf{p'}}. The corresponding diagonal vector of length 1 is \vec{\mathbf{u}}= (\frac{1}{\sqrt{2}},0,\frac{1}{\sqrt{2}})^T. So this rotation can be described by a quaternion e as follows:


\begin{align}
e(\vec{\mathbf{u}},180^\circ) &= \cos{\frac{180^\circ}{2}}+\sin{\frac{180^\circ}{2}}\vec{\mathbf{u}} = 
\cos{\frac{180^\circ}{2}}+\sin{\frac{180^\circ}{2}}\left[\begin{array}{c}\frac{1}{\sqrt{2}}\\ 0\\\frac{1}{\sqrt{2}}\end{array}\right]=
0 + \frac{1}{\sqrt{2}}i + 0\ j + \frac{1}{\sqrt{2}}k \\ 
&= (0,\frac{1}{\sqrt{2}},0,\frac{1}{\sqrt{2}})
\end{align}

Using the quaternion rotation equation e\ p \ e^* and solving it then leads to:


\begin{align}
p' = e \ p \ e^* &= 0 \ \oplus \ \mathbf{R_e}\vec{\mathbf{p}} 
\\ &=  0 \ \oplus \ 
\left[\begin{array}{ccc}
e_0^2+e_1^2-e_2^2-e_3^2 & 2(e_1e_2-e_0e_3) & 2(e_0e_2+e_1e_3) \\
2(e_0e_3+e_1e_2) & e_0^2-e_1^2+e_2^2-e_3^2 & 2(e_2e_3-e_0e_1) \\
2(e_1e_3-e_0e_2) & 2(e_0e_1+e_2e_3) & e_0^2-e_1^2-e_2^2+e_3^2
\end{array}\right] 
\left[\begin{array}{c}
p_1 \\
p_2 \\
p_3
\end{array}\right] 
\\ &=  0 \ \oplus \ 
\left[\begin{array}{ccc}
0^2+\frac{1}{\sqrt{2}}^2-0^2-\frac{1}{\sqrt{2}}^2 & 2(\frac{1}{\sqrt{2}}\cdot 0-0\cdot \frac{1}{\sqrt{2}}) & 2(0 \cdot 0+\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}) \\
2(0\cdot \frac{1}{\sqrt{2}}+\frac{1}{\sqrt{2}} \cdot 0) & 0^2-\frac{1}{\sqrt{2}}^2+0^2-\frac{1}{\sqrt{2}}^2 & 2(0 \cdot \frac{1}{\sqrt{2}}-0 \cdot \frac{1}{\sqrt{2}}) \\
2(\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}-0 \cdot 0) & 2(0 \cdot \frac{1}{\sqrt{2}}+0 \cdot \frac{1}{\sqrt{2}}) & 0^2-\frac{1}{\sqrt{2}}^2-0^2+\frac{1}{\sqrt{2}}^2
\end{array}\right] 
\left[\begin{array}{c}
p_1 \\
p_2 \\
p_3
\end{array}\right]
\\ &=  0 \ \oplus \ 
\left[\begin{array}{ccc}
0+\frac{1}{2}-0-\frac{1}{2} & 2\cdot 0 & 2(0 +\frac{1}{2}) \\
2\cdot 0 & 0-\frac{1}{2}+0-\frac{1}{2} & 2 \cdot 0 \\
2(\frac{1}{2}-0) & 2 \cdot 0 & 0-\frac{1}{2}-0+\frac{1}{2}
\end{array}\right] 
\left[\begin{array}{c}
p_1 \\
p_2 \\
p_3
\end{array}\right] 
\\ &=  0 \ \oplus \ 
\left[\begin{array}{ccc}
0 & 0 & 1 \\
0 & -1 & 0 \\
1 & 0 & 0
\end{array}\right] 
\left[\begin{array}{c}
p_1 \\
p_2 \\
p_3
\end{array}\right] 
\end{align}

So the rotation matrices for the rotation done following the Roll-Pitch-Yaw rule and the corresponding rotation using a quaternion lead to the same rotation matrices \mathbf{R}_{rpy} and \mathbf{R}_e and so to the same vector \vec{\mathbf{p'}}.

The applet at the bottom of this page can be used to reconstruct the example situation and to test other combinations of roll, pitch and yaw angle and the corresponding quaternions.

Applet

The following three-dimensional applet helps you to understand the relation between Roll-Pitch-Yaw angles and a quaternion. The initial position of the object can be set using the sliders for x, y and z. Then the object can be rotated by defining the roll, pitch and yaw angles. The most intuitive way is to start with the yaw angle, because this one is applied first. Then the object is rotated aroud the y-axis by the pitch angle followed by a rotation around the x-axis by the roll angle. The quaternion describing the same rotation is shown dynamically and the corresponding angle \alpha and the rotation axis \vec{\mathbf{u}} are presented. The rotation axis and the rotational path are visualized on the left side. After pressing the Show Quaternion Rotation button, the rotation of the object around \vec{\mathbf{u}} gets animated.