CMM-22 / a2

CMM Assignment 2 - Kinematic walking controller
1 stars 0 forks source link

How to use a quaternion #5

Open ghost opened 2 years ago

ghost commented 2 years ago

So for many years, each time a student asks any prof about quaternions, the answer ends up being something among the lines of 'this is a complicated subject outside the scope of the course, so we won't get into it'. Now we seemingly need to use a heading quaternion in Exercise 3, but I have no idea what to do with it. Any explanation would be much appreciated!

eastskykang commented 2 years ago

We will revisit more details of unit quaternion in following lectures very soon.

TL;DR:

Please note you don't have to know the details of unit quaternion for this assignment. However, if you are not familiar with the notion of quaternion, you may want to learn the definition, and basics of unit quaternion, since this is a fundamental notion that you have to know if you want to study further (or research) toward computer graphics or robotics domain.

In short, unit quaternion is one way of representing 3D rotational transformation (or 3D orientation of a coordinate system). Its real part is cos (0.5 * theta) and its imaginary part is sin(0.5 * theta) * v where (theta, v) is an angle of rotation and an axis of rotation (in an unit vector) respectively; for the axis vector [x, y, z], we define v := xi + yj + zk.

For instance, let's say you rotate some vector around x axis by 1 rad. The corresponding quaternion is q = [cos(0.5), sin(0.5), 0, 0, 0] or more specifically, cos(0.5) + sin(0.5) i + 0 j + 0 k

Then you can use this quaternion q to rotate a 3D vector v around x axis by 1 rad with some special mathematical operation (option 1 is using matrix operation, option 2 is using complex number multiplication. see the one of the references below.)

If you want some materials to learn more about quaternion. I recommend these amazing videos from 3brown1blue:

Other references are here


For now, in our code base, you can just use quaternion as the same way you use a rotation matrix.

For instance, let's say Eigen quaternion qAB is equivalent to a rotational matrix R_{A}{B} wherev_A = R_{A}{B} v_B; v_A denotes a 3D vector v expressed in frame A, and v_B denotes v expressed in frame B .

Then, in cpp code, you can use quaternion as follows

V3D v_A = qAB * v_B

Please note that this is not just a simple matrix-vector or vector-vector multiplication but rather a special operation for rotating a vector with an unit quaternion that is already implemented and overloaded by cpp * operator in Eigen library. I hope this helps.

eastskykang commented 2 years ago

By the way, you can use getRotationQuaternion function as follows to convert heading angle into quaternion as I already left in the code.

Quaternion heading = getRotationQuaternion(headingAngle, V3D(0, 1, 0));
ghost commented 2 years ago

Thank you so much Dongho for the comprehensive explanation and all the references, it is much appreciated :D It's all clear now!