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:
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:
where
is the n-dimensional vector representing system states and
is the n-dimensional vector composed of general nonlinear time-varying functions of the state vector at time t.
Let a small perturbation around defined as
where 2)
and let Δt be a small perturbation in time around tkdefined as
Therefore from 2)
Let’s develop the around the initial point ( , tk)until the first order (from here the term “Local Linearization”)
3)
In order to simplify the notation let’s make the following assumptions
where
is a matrix whose generic component (i,j) is
where xj is the jth element of vector
Expression 3) becomes:
and therefore
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
whom solution is of type
5)
where k is a arbitrary constant vector depending from boundary condition.
A particular solution of 4) is of type
6)
In order to identify the the constant vectors and let’s substitute general solution 6) in 4)
7)
Matching both terms in t
and therefore
8)
Matching the other terms leads to
and finally
9)
considering 6), 8) and 9) a particular solution of differential equation 4) is
10)
According to the general rule, the solution of 4) is the sum of 5) and 10), that is
11)
in order to find the arbitrary constant k, let’s consider that for t=tk ,(see 2))
in this special case 11) becomes
and therefore
Under this assumption solution 11) becomes
12)
let’ remember that
and therefore
relation 12) becomes
where h = tk+1 – tk, represents the integration step
giving to and their values and after some simple elaboration
where I is the identity matrix. Finally, assuming
13)
14)
15)
Now let’s use 15) in order to solve the particular differential equation:
16)
It is quit obvious that:
17)
The solution 15) applied to problem 16) and considering17) becomes
18)
Remembering the values of and from 13) and 14) and after some elaboration we arrive at the following result
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
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
in this case we have that
21
let’s apply the same transformation to
22)
and let’s specify as transformation matrix
Relation 22) becomes
and thus, applying property 21)
expression 19) now becomes
23)
Let’s use 23) to solve differential equation 16) in case that
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
assuming
previous expression becomes
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)
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
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
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 of a square matrix A. Due to the theorem of Hamilton-Cayley we can affirm that:
In general is not necessarily the minimum polynomial, and in general shall be divided by a polynomial in order to obtain the minimal polynomial .
Let’s define now how to find and let’s define the following polynomial:
and let’s consider the matrix
The greatest common divisor (gcd) of all the elements of is
Dividing by gives us the minimal polynomial (we omit the demonstration).
Let now and be for example the two solution of (the fact that they are only two does not influence the generality of the following statements). It shall be demonstrated that:
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)
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
The characteristic polynomial 26) becomes:
27)
Let’s continue to apply the procedure just explained and let’s calculate
after some simplification the expression becomes:
Let’s calculate the matrix
28)
with simple calculations we obtain
29) and thus 30)
Replacing 29) and 30) in 28) and after some calculation we obtained
and finally
is the greatest common divisor (gcd) of all the elements of and thus as explained before
31)
The minimal polynomial of matrix A is the ratio between the characteristic polynomial 27) and 31)
The two solution of are and
As already explained any function f(A) can be expressed as
32)
Where Z1 and Z2 are independent from f(). In order to find them let’s calculate
expression 32) becomes
I = Z1 + Z2 33)
Let’s now assume
expression 32) now becomes
and thus 34)
and from 33) and 34)
35)
We are now in the position to apply expression 25) considering the function we want to use:
and finally
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)
that can be written as
where
assuming and considering 36) and 14)
Knowing that and thus
37)
Now let’s calculate the matrix
38)
The single components of matrix M in 37) are based of the 4 following expressions:
38 a)
38 b)
38 c)
38 d)
Assuming the following notation as in reference a):
expression 38 a),38 b),38 c), and 38 d) assume the following aspect:
The algo 23) can be written now as:
where 39
making the previous expression explicit leads to:
40)
and more:
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 (gimbal lock condition) which leads to an undefined equation like in case of traditional approach.
Two more notation:
-
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.
-
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