LL Algo

1) Preface

In this post we face the problem to solve the differential equation of quaternions in order to find the attitude of an aircraft without having the "gimbal lock" problem. We will first develop the numerical solution of the differential equation with the LL Algorithm (Local Linearization), than we will find a workable solution of the algorithm analyzing how a function of matrix can be expressed, and finally we will find the numerical algorithm to solve the entire problem.

2) LL (Local Linearization) Algo

We now re-write what already discovered previously relating to the calculation of the attitude of an aircraft, without having the problem to encounter the “gimbal lock” in the condition of theta = 90°. The differential equation to be solved in order to calculate the components of the quaternion describing the attitude is:

(q0˙q1˙q2˙q3˙)=12(0PQRP0RQQR0PRQP0)(q0q1q2q3)   1)

The Local Linearization Algorithm (LL Algo) is proposed to solve this differential equation.

In order to follow this chapter a basic knowledge of matrices, functions of matrices, differential equations, functions of multiple variables e their development in the Taylor series is required. Not all the development will be described, but I believe it will be sufficient to the reader who wants to go in a deeper detail. This algorithm is taken by the NASA technical note TN D-7347 “Development and application of a local linearization algorithm for the integration of quaternion rate equations in real-time flight simulation problems”, December 1973 (Ref a). In that paper the 4X4 matrix in equation 1) is quiet different , coming from another approach for the solution of the attitude problem. Instead to use quaternions as I did in this post,they proposed to use Cayley-Klein parameters. The two methods, even producing two different systems of differential equations, lead to the same result, of course. I personally have a preference for the quaternion approach, that seems to me more elegant and direct.

Let’s start approaching the problem in very general terms considering the following expression:

X¯˙=F¯(X¯,t)  where

X¯(t)   is the n-dimensional vector representing system states and

F¯(X¯,t)   is the n-dimensional vector composed of general nonlinear time-varying functions of the state vector X¯ at time t.

Let ΔX¯(t) a small perturbation around Xk¯=X(Tk) defined as

ΔX¯(t)=X¯Xk¯ where tktt(k+1)     2)

and let Δt be a small perturbation in time around tkdefined as

Δt=ttk

Therefore from 2)

ΔX¯˙(t)=X¯˙=F¯(X¯,t)

Let’s develop the F¯(X¯,t) around the initial point ( Xk¯ , tk)until the first order (from here the term “Local Linearization”)

ΔX¯˙(t)=F¯(Xk¯,tk)+F¯(Xk¯,tk)X¯ΔX¯+F¯(Xk¯,tk)tΔt+    3)

In order to simplify the notation let’s make the following assumptions

Ak¯=F¯(Xk¯,tk)X¯     Bk¯=F¯(xk,tk)     Ck¯=F¯(Xk¯,tk)t

ΔX¯˙(t)=y¯˙(t)     ΔX¯(t)=y¯(t)     Δt=ttk

where

Ak¯=F¯(Xk¯,tk)X¯

is a matrix whose generic component (i,j) is

aij=Fi¯(Xk¯,tk)xj    where xj is the jth element of vector X¯

Expression 3) becomes:

y¯˙=Bk¯+Ak¯y¯+Ck¯(ttk)   and therefore

y¯˙Ak¯y¯=Bk¯Ck¯tk+Ck¯t    4)

The relation 4) is a linear first order differential equation. As known from the general theory, the solution of such an equation is found summing up the solution of the homogeneous equation to a particular solution of the equation.

The homogeneous equation associated to 4) is

y¯˙Ak¯y¯=0   whom solution is of type

yh¯(t)=k¯exp(Ak¯t)    5)

where k is a arbitrary constant vector depending from boundary condition.

A particular solution of 4) is of type

yp¯(t)=M¯t+N¯     6)

In order to identify the the constant vectors M¯ and N¯ let’s substitute general solution 6) in 4)

M¯Ak¯M¯tAk¯N¯=Bk¯Ck¯tk+Ck¯t    7)

Matching both terms in t

Ak¯M¯=Ck¯    and therefore

M¯=Ak1¯Ck¯     8)

Matching the other terms leads to

Ak¯N¯=Bk¯Ck¯tkM¯     N¯=Ak1¯(Bk¯Ck¯tkM¯)   and finally

N¯=Ak1¯Bk¯+Ak1¯Ck¯tkAk2¯Ck¯    9)

considering 6), 8) and 9) a particular solution of differential equation 4) is

yp¯(t)=Ak1¯Ck¯tAk1¯Bk¯+Ak1¯Ck¯tkAk2¯Ck¯    10)

According to the general rule, the solution of 4) is the sum of 5) and 10), that is

y¯(t)=yh¯(t)+yp¯(t)=k¯exp(Ak¯t)Ak1¯Ck¯(ttk)Ak1¯Bk¯Ak2¯Ck¯   11)

in order to find the arbitrary constant k, let’s consider that for t=tk ,(see 2))

y¯=ΔX¯=X¯(t)Xk¯=0

in this special case 11) becomes

k¯exp(Ak¯tk)Ak1¯Bk¯Ak2¯Ck¯=0    and therefore

k¯=exp(Ak¯tk)(Ak1¯Bk¯+Ak2¯Ck¯)

Under this assumption solution 11) becomes

y¯(t)=exp(Ak¯(ttk))((Ak1¯Bk¯+Ak2¯Ck¯))Ak1¯Ck¯(ttk)Ak1¯Bk¯Ak2¯Ck¯   12)

let’ remember that

y(t)=ΔX(t)=X(t)Xk    and therefore

y(tk+1)=X(tk+1)Xk

relation 12) becomes

X¯(tk+1)=X¯(tk)+exp(Ak¯h)((A¯k1Bk¯+Ak¯2Ck¯))A¯k1Ck¯hA¯k1Bk¯A¯k2Ck¯

where h = tk+1 – tk, represents the integration step

giving to Bk¯ and Ck¯ their values and after some simple elaboration

X¯(tk+1)=X¯(tk)+A¯k1(exp(Ak¯h)I)F¯(xk,tk)+A¯k2(exp(Ak¯h)IAk¯h)F¯(Xk¯,tk)t

where I is the identity matrix. Finally, assuming

Pk¯=A¯k1((exp(Ak¯h)I))      13)

Qk¯=Ak¯2(exp(Ak¯h)IAk¯h)     14)

X¯(tk+1)=X¯(tk)+Pk¯F¯(Xk¯,tk)+Qk¯F¯(Xk¯,tk)t    15)

Now let’s use 15) in order to solve the particular differential equation:

X¯˙=F¯(x¯,t)=A(t)X¯    16)

It is quit obvious that:

F¯(Xk¯,tk)t=A˙(tk)Xk¯=Ak˙Xk¯     17)

The solution 15) applied to problem 16) and considering17) becomes

X¯(k+1)=Xk¯+Pk¯F¯(Xk¯,tk)+Qk¯Ak˙Xk¯    18)

Remembering the values of Pk¯ and Qk¯ from 13) and 14) and after some elaboration we arrive at the following result

X¯(k+1)=Ak1exp(Akh)AkXk¯+Qk¯Ak˙Xk¯    19)

where h = tk+1 – tk, represents the integration step

We need now to improve expression 19) to a form more manageable. In order to do that let’s consider the term

Ak1exp(Akh)Ak        20)

and let’s remember some matrix properties that can be found in any good book on the argument (for example Matrix Theory by Gantmacher, Chelsey publishing Company, New York)

Let matrix B be the contragredient transformation of matrix A by means of matrix T: by definition

B=T1AT in this case we have that

f(B)=T1f(A)T      21

let’s apply the same transformation to Ak

B=T1AkT       22)

and let’s specify as transformation matrix T=Ak

Relation 22) becomes

B=Ak1AkAk=Ak and thus, applying property 21)

exp(Bh)=Ak1exp(Akh)Ak=exp(Akh)

expression 19) now becomes

X¯(k+1)=exp(Akh)Xk¯+Qk¯Ak˙Xk¯    23)

Let’s use 23) to solve differential equation 16) in case that

A(t)=12(0PQRP0RQQR0PRQP0)     24)

where P, Q, R are the angular velocities of a body around its X-Y-Z reference system

This differential equation is identical to differential equation 1)

It is easy to show that

det(A)=116[P2+Q2+R2]2

assuming

P2+Q2+R2=ω2   previous expression becomes

det(A)=116ω4   where ω is the modulus of the overall angular velocity

To apply solution 23) we need to understand the meaning of the expression

exp(Ak * h)

and to do that we need to introduce briefly in the next paragraph, the concept of “function of matrix (for a complete and exhaustive treatment, see specialized books/articles on matrix theory)

3) Function of Matrices

Let’s start with some definition, let Anxn be a square matrix:

definition 1

Let Ψ(λ) be a polynomial in λ : it is defined minimal polynomial for the matrix A if the following conditions are met:

a) Ψ(A)=0

b) among all the polynomials satisfying condition 1) it is the polynomial with minimal rank

c) Coefficient of maximum rank in λ is equal 1 (it is a monic polynomial)

definition 2

The characteristic polynomial of A is defined as the determinant of the matrix

c(λ)=det(λIA)

definition 3

A principal minor of order k of a square matrix A is the determinant of the matrix obtained by A deleting n-k row and columns of the same order

It shall be proved that the characteristic polynomial of A is

c(λ)=λnc1λn1+c2λn2+..........+(1n)cn

where cr are the sum of all the principal minors of order r.

Let’s now explain a procedure in order to find the minimal polynomial.

Let’s consider the characteristic polynomial c(λ) of a square matrix A. Due to the theorem of Hamilton-Cayley we can affirm that:

c(A)=0

In general c(λ) is not necessarily the minimum polynomial, and in general c(λ) shall be divided by a polynomial D(λ) in order to obtain the minimal polynomial Ψ(λ) .

Let’s define now how to find D(λ) and let’s define the following polynomial:

δ(λ,μ)=c(μ)c(λ)μλ and let’s consider the matrix

B(λ)=δ(λ,A)

The greatest common divisor (gcd) of all the elements of B(λ) is D(λ)

Dividing c(λ) by D(λ) gives us the minimal polynomial Ψ(λ) (we omit the demonstration).

Let now λ1 and λ2 be for example the two solution of Ψ(λ)=0 (the fact that they are only two does not influence the generality of the following statements). It shall be demonstrated that:

f(A)=f(λ1)Z1+f(λ2)Z2    25)

where Z1and Z2 (and in general Zi) are the “constituent matrices” of A and they are independent from the particular function f(). They can be found assuming some particular simple functions as we will see later.

Let’s now apply the procedure just explained in order to give a meaning to the expression

exp(A * h)

where A is given by 24).

Let’s first calculate the characteristic polynomial of matrix 24)

c(λ)=λ4c1λ3+c2λ2c3λ+c4    26)

where:

c1 is the sum of all the principal minor of order 1

c2 is the sum of all the principal minor of order 2

c3 is the sum of all the principal minor of order 3

c4 is the sum of all the principal minor of order 4

In practise:

c1 is the sum of all the elements of the matrix diagonal and thus

c1=0

c2=|0P2P20|+|0Q2Q20|+|0R2R20|+|0R2R20|+|0Q2Q20|+|0P2P20|=ω22

c3=|0P2Q2P20R2Q2R20|+|0P2R2P20Q2R2Q20|+|0Q2R2Q20P2R2P20|+|0R2Q2R20P2Q2P20|=0

C4=det(A)=116[P2+Q2+R2]2=ω416

The characteristic polynomial 26) becomes:

c(λ)=λ4+ω22λ2+ω416=(λ2+ω24)2     27)

Let’s continue to apply the procedure just explained and let’s calculate

δ(λ,μ)=c(μ)c(λ)μλ

δ(λ,μ)=(μ2+ω24)2(λ2+ω24)2μλ

after some simplification the expression becomes:

δ(λ,μ)=μ3+μ2λ+μ(λ2+ω22)+(ω22λ+λ3)

Let’s calculate the matrix B(λ)=δ(λ,A)

B(λ)=δ(λ,A)=A3+A2λ+A(λ2+ω22)+(ω22λ+λ3)I   28)

with simple calculations we obtain

A2=ω24I  29)  and thus   A3=ω24A  30)

Replacing 29) and 30) in 28) and after some calculation we obtained

B(λ)=A(λ2+ω22)+(ω24+λ2)λI   and finally

B(λ)=(ω24+λ2)(A+λI)

(ω24+λ2) is the greatest common divisor (gcd) of all the elements of B(λ) and thus as explained before

D(λ)=(ω24+λ2)     31)

The minimal polynomial of matrix A is the ratio between the characteristic polynomial 27) and 31)

Ψ(λ)=(λ2+ω24)2(ω24+λ2)=(ω24+λ2)

The two solution of Ψ(λ)=0 are   λ1=jω2  and  λ2=jω2

As already explained any function f(A) can be expressed as

f(A)=f(jω2)Z1+f(jω2)Z2     32)

Where Z1 and Z2 are independent from f(). In order to find them let’s calculate f(λ)=1

expression 32) becomes

I = Z1 + Z2    33)

Let’s now assume f(λ)=λ+jω2

expression 32) now becomes

A+jω2I=jωZ2  and thus   Z2=1jωA+I2   34)

and from 33) and 34)

Z1=IZ2=I21jωA    35)

We are now in the position to apply expression 25) considering the function we want to use:

eAh=ejω2h(I21jωA)+ejω2h(I2+1jωA)

eAh=I2(ejω2h+ejω2h)+2Aω(ejω2hejω2h)2j   and finally

eAh=Icos(ωh2)+2Aωsin(ωh2)    36)

Now we have given a meaning to expression exp(A*h) we can recall the algo 23) to arrive to the final result

4) The final Algo

Taking into account the result achieved in 36) we can now recall 23) where we use A instead of AK given by 24)

X¯(k+1)=eAhXk¯+Qk¯A˙Xk¯ that can be written as

X¯(k+1)=(eAh+Qk¯A˙)Xk¯

X¯(k+1)=MXk¯   where    M=eAh+Qk¯A˙

assuming   ρ=ωh2   and considering 36) and 14)

M=Icos(ρ)+2Aωsin(ρ)+A¯2(Icos(ρ)+2Aωsin(ρ)IA¯h)A˙

Knowing that    A2=ω24I   and thus    A2=4ω2I

M=Icos(ρ)+2Aωsin(ρ)+4ω2(1cos(ρ))A˙+4ω2(h2ωsin(ρ))AA˙   37)

Now let’s calculate the matrix   AA˙

AA˙=12(0PQRP0RQQR0PRQP0)12(0P˙Q˙R˙P˙0R˙Q˙Q˙R˙0P˙R˙Q˙P˙0)

AA˙=14((PP˙+QQ˙+RR˙)(QR˙RQ˙)(RP˙PR˙)(PQ˙QP˙)(RQ˙QR˙)(PP˙+QQ˙+RR˙)(QP˙PQ˙)(RP˙PR˙)(PR˙RP˙)(PQ˙QP˙)(PP˙+QQ˙+RR˙)RQ˙QR˙(QP˙PQ˙)(PR˙RP˙)(QR˙RQ˙)(PP˙+QQ˙+RR˙)) 38)

The single components of matrix M in 37) are based of the 4 following expressions:

H=cos(ρ)1ω2(h2ωsin(ρ))(PP˙+QQ˙+RR˙)      38 a)

K=Pωsin(ρ)+2ω2(1cos(ρ))P˙1ω2(h2ωsin(ρ))(QR˙RQ˙)   38 b)

J=Qωsin(ρ)+2ω2(1cos(ρ))Q˙+1ω2(h2ωsin(ρ))(PR˙RP˙)    38 c)

G=Rωsin(ρ)2ω2(1cos(ρ))R˙+1ω2(h2ωsin(ρ))(PQ˙QP˙)   38 d)

Assuming the following notation as in reference a):

C1=cos(ρ)C4=4ω2(h2ωsin(ρ))

C'2=sin(ρ)ωC'3=2ω2(1cos(ρ))

A'=(PP˙+QQ˙+RR˙)4B'=(PQ˙QP˙)4

C'=(RP˙PR˙)4    D'=(QR˙RQ˙)4

expression 38 a),38 b),38 c), and 38 d) assume the following aspect:

H=C1+C4A'

G=C2'RC3'R˙+C4B'

J=C2'Q+C3'Q˙C4C'

K=C2'P+C3'P˙C4D'

The algo 23) can be written now as:

X¯(k+1)=M¯Xk¯

where M¯=(HKJGKHGJJGHKGJKH)   39

making the previous expression explicit leads to:

(q0(k+1)q1(k+1)q2(k+1)q3(k+1))=(HKJGKHGJJGHKGJKH)(q0kq1kq2kq3k)     40)

and more:

q0(k+1)=q0kHq1kKq2kJq3kG

q1(k+1)=q0kK+q1kHq2kGq3kJ

q2(k+1)=q0kJ+q1kG+q2kH+q3kK

q3(k+1)=q0kG+q1kJq2kK+q3kH

It’s evident now the advantage of this approach which leads to a very simple solution of a problem apparently complicated. The big advantage is the absence of the condition with θ=±90° (gimbal lock condition) which leads to an undefined equation like in case of traditional approach.

Two more notation:

  1. this algorithm shall be used taking into account good programming rules like for examples avoiding overflow condition while attempting to divide one number for a quantity very close to 0. It is the case when calculating the 4 quantities H,G,J,K where the variable ω act as a divisor. This means that when the aircraft is flying without maneuvering (ω almost equal 0), without proper protection, there is a risk of overflow.

  2. Equation 40) is similar to the equation explained in paper a). However some of the components of the matrix 39) has a different collocation. It is interesting to note that matrix 39) becomes exactly equal to matrix Mk in paper a), just inverting the second and forth columns and rows. Thin means swapping the quaternion components q1 and q3, as clearly declared at pag 6 of ref a). This is due to a complete different mathematical approach : while in paper a), for the description of the rotation of a body, the Cayley-Cleyn complex matrices have been used (explained in ref B), in this post we used the quaternion approach, as explained in a previous post. This approach leads in my opinion to the clearest and most elegant solution of the problem

5) Code snippet

A simple implementation in C/C++ code is proposed. This routine is supposed to be called cyclically from a main procedure: first run should be with ilong and ilat = 0 to setup all the variable, after that ilong and ilat shall be set to 1: a typical value for dt is 20 ms (50 Hz), that means that to run a 10 seconds long simulation we need to iterate for 500 times the routine. Writing the main procedure is trivial and it is omitted. The routines start setting the quaternion component starting from some initial condition of the Euler angles psi, theta and phi; and at the and of the run a new calculation of psi, theta and phi using the new values of the quaternion components has been made.

#include "quat.h"  /* variable definitions */

#define limit(a) if ( a > 1.) a = 1.; else { if ( a < -1.) a = -1.;}

void Quat(void)

{

if( ilong == 0 && ilat == 0 ) { / no integration, initial condition setting */

/* Initial condition setting (conversion degrees to Radiant*/

fi_2 = (fio*D2R)/2.;

teta_2 =(thetao* D2R )/2.;

psi_2=(psio*D2R)/2.;

costeta0=cosf(teta_2);

sinteta0=sinf(teta_2);

cosfi0=cosf(fi_2);

sinfi0=sinf(fi_2);

cospsi0=cosf(psi_2);

sinpsi0=sinf(psi_2);

/*set initial condition for the 4 quaternion components*/

q1=cospsi0*costeta0*cosfi0+sinpsi0*sinteta0*sinfi0;

q2=sinpsi0*costeta0*cosfi0-cospsi0*sinteta0*sinfi0;

q3=cospsi0*sinteta0*cosfi0+sinpsi0*costeta0*sinfi0;

q4=cospsi0*costeta0*sinfi0-sinpsi0*sinteta0*cosfi0;

}

/* LL integration algo */

/*for variables definition see paper at reference 1)*/

omega2 = p*p + q*q + r*r;

omega = sqrtf ( omega2 );

/* avoid overflow condition*/

if(omega2 < .0000000001) { omega2 = .0000000001; omega= .00001; }

rhok= .5 * omega * dt;

c1 = cosf(rhok);

c2 = sinf(rhok) / omega;

c3 = ( 1. - c1 ) * 2. / omega2;

c4 = ( - 2. * c2 + dt ) / omega2;

h = - ( p * pdot + q * qdot + r * rdot) * c4 + c1;

g = - c2 * p - c3 * pdot + c4 * ( r * qdot - q * rdot );

j = c2 * q + c3 * qdot + c4 * ( r * pdot - p * rdot );

k = c2 * r + c3 * rdot + c4 * ( p * qdot - q * pdot );

q1n = h * q1 + g * q2 - j * q3 - k * q4;

q2n = - g * q1 + h * q2 - k * q3 + j * q4;

q3n = j * q1 + k * q2 + h * q3 + g * q4;

q4= k * q1 - j * q2 - g * q3 + h * q4;

/* Normalization */

qenne = 1. / sqrtf ( q1n*q1n + q2n*q2n + q3n*q3n + q4*q4 );

q1 = q1n * qenne;

q2 = q2n * qenne;

q3 = q3n * qenne;

q4 = q4* qenne;

/* direction cosine calculation */

q12=q1*q1;

q22=q2*q2;

q32=q3*q3;

q42=q4*q4;

c11=q12+q22-q32-q42;

c12=2.*(q1*q4+q2*q3);

c13=2.*(q2*q4-q1*q3);

c21=2.*(q2*q3-q1*q4);

c22=q12-q22+q32-q42;

c23=2.*(q3*q4+q1*q2);

c31=2.*(q2*q4+q1*q3);

c32=2.*(q3*q4-q1*q2);

c33=q12-q22-q32+q42;

/* limiting direction cosine between minus plus 1*/

limit(c11);

limit(c12);

limit(c13);

limit(c21);

limit(c22);

limit(c23);

limit(c31);

limit(c32);

limit(c33);

/*Euler angles Teta,Fi,Psi( radiants )*/

theta=-asinf(c13);

stheta=-c13;

ctheta=cosf(theta);

phi=atan2f(c23,c33);

sphi=sinf(phi);

cphi=cosf(phi);

psi=atan2f(c12,c11);

spsi=sinf(psi);

cpsi=cosf(psi);

}

6) Reference

  1. NASA technical note TN D-7347 “Development and application of a local linearization algorithm for the integration of quaternion rate equations in real-time flight simulation problems”, December 1973.

  2. Robinson,Alfred C.: On the Use of Quaternionsin Simulation of Rigid-Body Motion. WADC Tech. Rep. 58-17, U.S. Air Force, Dec. 1958. (Available from DDC as AD 234 422.