# Maths - Quaternion Arithmetic - Forum Discussion

 By: Martin Baker - martinbaker RE: Quaternions and Aircraft Roll Demand Cont   2005-10-22 07:36 Hi Anthony,    I think one important issue is to minimise conversions between euler angles and quaternions or matrices because these conversions are costly in terms of performance and unstable around the singularities. Doing these conversions every time you go through the control loop would be horrible.    We have to use quaternions or matrices when we want to combine rotations (we can only add euler angles if we are only changing one of pitch, roll or yaw or if the angles are very small) otherwise its probably best to stay in quaternions.    So can you do the whole control loop in quaternions? Since adding angles is equivalent to multiplying quaternions then I suspect that finding the error would be equivalent to finding the angle difference which would be equivalent to dividing quaternions which is equivalent to multiplying by the conjugate.    Martin

 By: Prospero999 - prospero999 RE: Quaternions and Aircraft Roll Demand Control*   2005-10-22 08:07 Hi Martin,    Excellent - that last paragraph particularly was what I think I need! I will try to implement it and I'll get back to you.    Many thanks,    Anthony

 By: Prospero999 - prospero999 RE: Quaternions and Aircraft Roll Demand Control*   2005-10-22 10:52 Hi Martin,    Well, I'm trying, but it's not quite right yet...    Two things:    1) When you say, "Finding the error would be equivalent to finding the angle difference which would be equivalent to dividing quaternions which is equivalent to multiplying by the conjugate."    Do you mean is equivalent to multipling by the inverse rather than by the conjugate? It's just that I read up on dividing quaternions, and they seemed to say inverse rather than conjugate...    2) Once the "difference" quarternion has been correctly calculated (if only!), you seem to imply that elements of this quarternion can be used "directly" to feed the autopilot with the roll angle error (i.e. without converting to matrix form and then extracting it from the matrix). How would I do this?    Your help most appreciated!    Anthony

 By: Martin Baker - martinbaker RE: Quaternions and Aircraft Roll Demand Cont   2005-10-23 07:56 > Do you mean is equivalent to multipling by the inverse rather than by the conjugate? It's just that I read up on dividing  > quaternions, and they seemed to say inverse rather than conjugate...     Yes, I should make that clearer on the website, the conjugate is the multiplicative inverse for quaternions.    > 2) Once the "difference" quarternion has been correctly calculated (if only!), you seem to imply that elements of this  > quarternion can be used "directly" to feed the autopilot with the roll angle error (i.e. without converting to matrix form  > and then extracting it from the matrix). How would I do this?     Since I don't know about your application I can only suggest general principles which may not be applicable to what you are doing.  I was thinking that you had some control loop which, in a 2D case would be something like this:  error angle = actual angle - required angle    The equivalent is 3D might be:  error quaternion = actual quaternion * conj(required quaternion)    However, re-reading your message you are looking for the 'roll angle error' so perhaps you have to extract out the roll angle every time? but that does look horrible so its much better to formulate the problem in terms of quaternions or matrices if you can.    Martin

 By: Prospero999 - prospero999 RE: Quaternions and Aircraft Roll Demand Control*   2005-10-23 09:41 Many, many thanks Martin and Andy.    Lots to think about there. I need a while to let it all sink in!    Andy, by coincidence, I've been in correspondence with Sandy Barbour and Austin about related matters on the X-Plane.org forums. I've actually been putting off using Sandy's plugin SDK, though I mean eventually to use it, and so far have stuck with VB and UDP since my C++ is, well, shall we say rough at best?;) I have so many questions for you, but I'll try to think of framing them in a semi-intelligent way before emailing you!    Cheers and thanks again,    Anthony

 By: Prospero999 - prospero999 RE: Quaternions and Aircraft Roll Demand Control*   2005-10-24 03:32 Hi chaps,    I'm still having a problem with using the quaternion division method to determine my roll angle error.    I'm almost there, but something odd is going on.    Here's the setup:    actual attitude = w1, x1, y1, z1  desired attitude = w2, x2, y2, z2    So, to determine the error, we do the "quaternion equivalent" of "w2 minus w1" which is  w2 mutiplied by the conjugate of w1:    w3 = w2 * cw1 - x2 * cx1 - y2 * cy1 - z2 * cz1  x3 = x2 * cw1 + w2 * cx1 + y2 * cz1 - z2 * cy1  y3 = w2 * cy1 - x2 * cz1 + y2 * cw1 + z2 * cx1  z3 = w2 * cz1 + x2 * cy1 - y2 * cx1 + z2 * cw1    So I try this, and the resulting quaternion (w3, x3, y3, z3) isn't correct!    But here's the odd thing: when I do "w1 minus w2" (i.e. w1 mutiplied by the conjugate of w2)  using the following lines of code:    w3 = w1 * cw2 - x1 * cx2 - y1 * cy2 - z1 * cz2  x3 = x1 * cw2 + w1 * cx2 + y1 * cz2 - z1 * cy2  y3 = w1 * cy2 - x1 * cz2 + y1 * cw2 + z1 * cx2  z3 = w1 * cz2 + x1 * cy2 - y1 * cx2 + z1 * cw2    ...I get the right error angles, but they are all the wrong sign. Which is I guess exactly what  they should be...    So how come w2 multiplied by the conjugate of w1 doesn't seem to work...? It *should*  give me same numbers with the signs flipped, surely?    Agh! I'm in Quaternion Hell! Any help much appreciated!    Anthony

 By: Andy Goldstein - andygoldstein RE: Quaternions and Aircraft Roll Demand Cont   2005-10-24 06:58 Anthony,    Reverse the order of multiplication and you'll get the right answer. You didn't give a formal expression of the quaternion product and I can't afford the time right now to reverse-compute it from your algebra. (Sorry - I know how tedious this stuff is. I've spent may hours cranking through multiplications.) However...    Going back to basic quaternion theory, the way you rotate a vector P using quaternion Q is    QP' = Q x QP x Q*    where QP is the quaternion (0,P), Q* is the conjugate of Q and "x" denotes quaternion multiplication. To compose rotations, say Q1 and Q2 in that order, the math becomes    QP' = Q2 x (Q1 x QP x Q1*) x Q2*    Quaternion multiplication is associative and reversing the order yields you the conjugate, i.e.    (Q1* x Q2*) = (Q2 x Q1)*    So you can conclude that composing the rotations Q1 and Q2 (in that order) is Q2 x Q1. Relating my notation back to yours, the math you want is    (conjugate of w1) x (w2)    - Andy

 By: Martin Baker - martinbaker RE: Quaternions and Aircraft Roll Demand Cont   2005-10-24 07:46 Just to amplify what Andy said some people find it helpful to think of quaternions in terms of their relationship with the axis-angle representation:  https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/notations/axisAngle/    With axis-angle reversing the angle reverses the direction of rotation.  And reversing the direction of axis reverses the direction of rotation.  And reversing the axis and the angle leaves the direction of rotation unchanged.    similarly with quaternions, so,  If w + i x + j y + k z rotates from A to B  Then w - i x - j y - k z rotates from B to A  And -w + i x + j y + k z rotates from B to A  And -w - i x - j y - k z rotates from A to B    (the last two don't apply to fundamental particles which have to rotate by 720 degrees to get back to where they started - When you get to the stage where quaternions are too simple for you its worth reading about quantum mechanics!).    But for the sort of rotations we are talking about w + i x + j y + k z and -w - i x - j y - k z represent the same rotation.    If the axis-angle way of looking at it doesn't help, ignore this message, different people like to think about quaternions in different ways.    Martin

 By: Nobody/Anonymous - nobody RE: Quaternions and Aircraft Roll Demand Control*   2005-10-24 11:26 Thanks chaps,    But something is still wrong. I must be bloody stupid. I am truly in Quaternion Hell.    Andy, you said I should swap the order of multiplication, so when I originally said...    "So, to determine the error, we do the "quaternion equivalent" of "w2 minus w1" which is   w2 mutiplied by the conjugate of w1:     w3 = w2 * cw1 - x2 * cx1 - y2 * cy1 - z2 * cz1   x3 = x2 * cw1 + w2 * cx1 + y2 * cz1 - z2 * cy1   y3 = w2 * cy1 - x2 * cz1 + y2 * cw1 + z2 * cx1   z3 = w2 * cz1 + x2 * cy1 - y2 * cx1 + z2 * cw1"    ...should I actually be doing the following?:    w3 = cw1 * w2 - cx1 * x2 - cy1 * y2 - cz1 * z2   x3 = cx1 * w2 + cw1 * x2 + cy1 * z2 - cz1 * y2   y3 = cw1 * y2 - cx1 * z2 + cy1 * w2 + cz1 * x2   z3 = cw1 * z2 + cx1 * y2 - cy1 * x2 + cz1 * w2    I tried this and it doesn't come out right:( Should it be right? If so, there's something wrong elsewhere!!!    (By the way, when I use w, x, y and z, I mean that w is the "angle" component of the quaternion and x, y and z form the "vector" component.)    (And cw1, cx1, cy1 and cz1 is the conjugate).     Thanks both of you,    Anthony

 By: Martin Baker - martinbaker RE: Quaternions and Aircraft Roll Demand Cont   2005-10-26 08:18 Perhaps by analogy with the 2D case, we could use:    error angle = actual angle - required angle     Or we could use:    error angle = required angle - actual angle     These are the same except it reverses which direction of error is considered positive and which is considered negative.    In the 3D case addition is replaced with multiplication and multiplication by -1 is replaced by conjugate.    So the equivalent is 3D might be:     error quaternion = actual quaternion * conj(required quaternion)     or    error quaternion = required quaternion * conj(actual quaternion)       depending on which direction of error we consider 'positive'.  One result would be the conjugate of the other (which could be proved by the equation Andy gives)    Martin

 By: Andy Goldstein - andygoldstein RE: Quaternions and Aircraft Roll Demand Cont   2005-10-26 12:15 The answer to the above two possibilities is "probably neither". The reason has to do with frame of reference.    Anthony, you haven't said so, but I'm going to make a guess that you want your "difference angle" (i.e., the correction quaternion) expressed in the frame of reference of the airplane. (After all, that's how control inputs are applied - ailerons apply a roll along the *plane's* roll axis.) With that in mind, let's look very carefully at the rotation quaternions we're dealing with:    Q1 = the plane's actual attitude, expressed in the external frame of reference.  Q2 = the plane's desired attitude, expressed in the external frame of reference.  Q3 = the difference, expressed in the plane's frame of reference.    Expressed formally, the problem is defined as    Q2 = Q1 x Q3    The reason for this ordering is that  (1) Q3 is expressed in the plane's frame of reference, and therefore must be applied first, and  (2) referring to my preceding post, rotations are composed by ordering the quaternions right to left.    This equation is solved for Q3 simply by pre-multiplying by Q1* (conjugate of Q1), yielding    Q1* x Q2 = Q1* x (Q1 x Q3) = (Q1* x Q1) x Q3) = Q3    Anthony, this matches the second of your computations in your last post. If that's the one you tried and you're not getting the right answer, then something is still wrong. First step would be to check your frames of reference again. If it's still not working for you I'll have to throw some code together quick and see if the actual reuslts match my theory...    - Andy

 By: Prospero999 - prospero999 RE: Quaternions and Aircraft Roll Demand Control*   2005-10-28 07:12 Thanks guys,    I'm taking a few days to absorb this.    Cheers,    Anthony