A Tutorial on Euler Angles and Quaternions
Total Page:16
File Type:pdf, Size:1020Kb
A Tutorial on Euler Angles and Quaternions Moti Ben-Ari Department of Science Teaching Weizmann Institute of Science http://www.weizmann.ac.il/sci-tea/benari/ Version 2.0.1 c 2014–17 by Moti Ben-Ari. This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/ by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. Chapter 1 Introduction You sitting in an airplane at night, watching a movie displayed on the screen attached to the seat in front of you. The airplane gently banks to the left. You may feel the slight acceleration, but you won’t see any change in the position of the movie screen. Both you and the screen are in the same frame of reference, so unless you stand up or make another move, the position and orientation of the screen relative to your position and orientation won’t change. The same is not true with respect to your position and orientation relative to the frame of reference of the earth. The airplane is moving forward at a very high speed and the bank changes your orientation with respect to the earth. The transformation of coordinates (position and orientation) from one frame of reference is a fundamental operation in several areas: flight control of aircraft and rockets, move- ment of manipulators in robotics, and computer graphics. This tutorial introduces the mathematics of rotations using two formalisms: (1) Euler angles are the angles of rotation of a three-dimensional coordinate frame. A rotation of Euler angles is represented as a matrix of trigonometric functions of the angles. (2) Quaternions are an algebraic structure that extends the familiar concept of complex numbers. While quaternions are much less intuitive than angles, rotations defined by quaternions can be computed more efficiently and with more stability, and therefore are widely used. The tutorial assumes an elementary knowledge of trigonometry and matrices. The compu- tations will be given in great detail for two reasons. First, so that you can be convinced of the correctness of the formulas, and, second, so that you can learn how to do them yourselves, in case you come across a context that uses different definitions or notation. For a comprehensive presentation of quaternions using vector algebra, see: John Vince. Quaternions for Computer Graphics, 2011, Springer. 1 Chapter 2 Rotations in two dimensions We start by discussing rotations in two dimensions; the concepts and formulas should be familiar and a review will facilitate learning about rotations in three dimensions. 2.1 Cartesian and polar coordinates A vector or a position (the tip of the vector) in a two-dimensional space can be given either in cartesian coordinates (x, y) or in polar coordinates (r, f), relative to a frame of reference: x r y φ The formulas for transforming one representation to another are: x = r cos f y = r sin f q r = x2 + y2 1 y f = tan− . x 2.2 Rotating a vector Suppose now that the vector is rotated by the angle q: x0 y0 r x r y θ φ 2 The new vector’s polar coordinates are (r, f + q) and its cartesian coordinates can be computed using the trigonometric identities for the sum of two angles: x0 = r cos(f + q) = r(cos f cos q sin f sin q) − = (r cos f) cos q (r sin f) sin q − = x cos q y sin q, − y0 = r sin(f + q) = r(sin f cos q + cos f sin q) = (r sin f) cos q + (r cos f) sin q = y cos q + x sin q = x sin q + y cos q . The transformation of the position (x, y) to (x0, y0) caused by a rotation through the angle q can be expressed in matrix notation as: " # " #" # x cos q sin q x 0 = − . y0 sin q cos q y Example Let r = 1 and let both q and f be 30◦ so that: x = r cos f = cos f = cos q = cos 30◦ = p3/2 y = r sin f = sin f = sin q = sin 30◦ = 1/2 . We can now compute the values of x0 and y0: " # " #" # " # " # x p3/2 1/2 p3/2 (p3/2)2 (1/2)2 1/2 0 = − = − = . y 1/2 p3/2 1/2 1/2 p3/2 + 1/2 p3/2 p3/2 0 · · The result makes sense since f + q = 60◦. 2.3 A geometric derivation of the rotation matrix The rotation matrix can be derived geometrically. Rather than look at the vector, let us look at its x and y components and rotate them (counterclockwise) by q (Figure 2.1). The x- and y- components are rotated by the angle q so that the OAB becomes OA0B0. Construct two right triangles: (1) Drop the perpendicular from A0 to the x-axis to form the right triangle OA C; (b) Construct a line through A parallel to the x-axis and a line 4 0 0 through B parallel to the y-axis. They intersect at C to form the right triangle B A C . 0 0 4 0 0 0 Since A C is parallel to CO, C A O = A OC = q. OA B is a right angle and A B C 0 0 6 0 0 6 0 6 0 0 4 0 0 0 is a right triangle so 6 A0B0C0 = q. The sides of the two right triangles are labeled with 3 B0 = (x0, y0) θ y cos θ y B = (x, y) 90 θ C − A 0 y sin θ 0 sin θ x x θ O A x cos θ C Figure 2.1: Geometric derivation of the rotation matrix their lengths obtained by trigonometry. It is now easy to see that x0 is the length of OC minus the length of A0C0 and that y0 is the length of CA0 plus the length of C0B0: x0 = x cos q y sin q − y0 = x sin q + y cos q. 2.4 Rotating the frame of reference The effect of rotating a vector by q is the same as the effect obtained by rotation of the frame of reference. The left diagram below shows a vector in the frame of reference A. The right diagram shows the same vector after frame A is rotated q relative to a new frame B. (xA, yA), (xB, yB) yA (xA, yA) yB yA r xA r φ φ xA θ xB Given the coordinates (xA, yA) of the points in the frame A: xA = r cos f yA = r sin f, 4 what are its coordinates (xB, yB) in the frame B? The geometry of this diagram is the same as the geometry of the diagram in Section 2.2, so we can use the same equation: " # " #" # x cos q sin q x B = − A . (2.1) yB sin q cos q yA Example Consider the point (xA, yA) = (p3/2, 1/2) in frame A. What are the coordinates of the point in frame B? As before, the computation gives: " # " #" # " # x p3/2 1/2 p3/2 1/2 B = − = . yB 1/2 p3/2 1/2 p3/2 2.5 Multiple rotations Rotate the coordinate frame B by y relative to a new frame C. (xA, yA), (xB, yB), (xC, yC) xA yC yB r xB yA φ θ ψ xC If we know (xB, yB), we can multiply it by the rotation matrix for y to obtain (xC, yC). B Let us denote the rotation matrix from frame A to frame B by RA and the rotation matrix C A from frame B to frame C by RB. Given p , the coordinates of a point in frame A, its B B A C C C B coordinates in frame B are p = RA p , and p , its coordinates in frame C, are p = RB p . Combining the two equations we have: C C B C B A p = RB p = RB RA p . C C B Let RA = RB RA. This is the rotation matrix from A to C, so we can obtain the coordinates pC directly from pA by: C C A p = RA p . Example C B Let y = 30◦ so RB = RA. Then: " # " #" # " # x p3/2 1/2 1/2 0 C = − = . yC 1/2 p3/2 p3/2 1 5 From the drawing we see the vector lies on the yC-axis which corresponds to the coordi- nates (0, 1). Let us now compute: " p #" p # " p # C C B 3/2 1/2 3/2 1/2 1/2 3/2 RA = RB RA = − − = − . 1/2 p3/2 1/2 p3/2 p3/2 1/2 C C A The computation p = RA p is: " # " #" # " # x 1/2 p3/2 p3/2 0 C = − = , yC p3/2 1/2 1/2 1 the same result obtained in the two-stage computation. Example Consider the vector (1, 0) lying on the x-axis of frame A. Rotate A by 15◦ to frame B and then rotate frame B by 30◦ to frame C. Hopefully, the coordinates of the vector in frame C will be (p2/2, p2/2), because the vector makes an angle of 45◦ with the x-axis of frame C. The values of the trigonometric functions for 15◦ are: p6 + p2 p6 p2 cos 15◦ = , sin 15◦ − . 4 4 First rotate by 15◦: " # " p6+p2 p6 p2 #" # " p6+p2 # xB − 1 = 4 − 4 = 4 .