Attitude Calculation

Preface

The simulation of the aircraft dynamic is the first step and probably one of the most important in order to have the right fidelity in a full mission simulator. In this post we face the problem to determine the attitude of the aircraft once the angular velocities and accelerations are know

Aircraft attitude calculation

As stated in other posts, we define attitude of an aircraft its angular position respect to some reference system. In the post Euler Angles” we define a proper reference system and the three attitude angles (psi, theta and phi) defining the angular position of the aircraft at some time. In the same post we find a relationship between the Euler angular velocities psidot, thetadot and fidot and the instantaneous components P, Q, R of the overall angular velocity. Let’s recall that result (16a, 16b and 16c in the mentioned post):

ϕ˙=P+tan(θ)sin(ϕ) Q+tan(θ)cos(ϕ) R

θ˙=cos(ϕ) Qsin(ϕ) R

ψ˙=sin(ϕ)cos(θ) Q+cos(ϕ)cos(θ) R

We posed also the question: is it possible with a proper integration process (an algorithm) applied to the previous relations, to determine ϕ,θ,ψ ? Theoretically the answer is affirmative, but looking at the expressions we discover that the condition for θ=π2 makes two of the three expressions undefined. This condition (true also for θ=π2 ) is often called “gimbal lock” condition, from the analogous difficulties to drive a gimbal when approaching that condition. So we arrived to the conclusion to look for another approach: to do that we introduced the Euler Parameters.

In the Euler Angles” post we also defined a standard reference system to use in the definition of ϕ,θ,ψ and we calculate the transformation matrix to pass from the reference system to the actual angular position of the aircraft. We recall that result:

Mψθϕ=(cos(θ)cos(ψ)cos(θ)sin(ψ)sin(θ)sin(ψ)cos(ϕ)+sin(ϕ)sin(θ)cos(ψ)cos(ψ)cos(ϕ)+sin(ϕ)sin(θ)sin(ψ)sin(ϕ)cos(θ)sin(ψ)sin(ϕ)+cos(ϕ)sin(θ)cos(ψ)cos(ψ)sin(ϕ)+cos(ϕ)sin(θ)sin(ψ)cos(ϕ)cos(θ))  1)

We added the consideration that, if we are able to know 1) in some other way, the calculation of ϕ,θ,ψ becomes very easy applying the following relations:

θ=arcsin(m13) ϕ=arctan(m23m33) ψ=arctan(m12m11)  2)

being mij the generic element of 1).

Euler Parameters

In this Euler Parameters post we find another way to express the rotation matrix 1). We defined in a proper reference system, an axis (described by a vector forming with the reference system axis the three angles α β and γ) where to apply a rotation of the angle μ. According to Euler's rotation theorem , choosing the right α β γ and μ, we are able to have the same transformation described by 1), as a function of α β γ and μ instead of ϕ,θ,ψ .

We recall here the related transformation matrix:

T = (a12+a22a32a422(a1a4+a2a3)2(a2a4a1a3)2(a2a3a1a4)a12a22+a32a422(a3a4+a1a2)2(a2a4+a1a3)2(a3a4a1a2)a12a22a32+a42)  3)

where

a1=cos(μ2)a2=sin(μ2)cosαa3=sin(μ2)cosβa4=sin(μ2)cosγ

are named Euler Parameters. It is quite obvious that if matrix 3) and matrix 1) represents the same transformation, each corresponding member of each matrix must be equal, and this, applying relation 2) to matrix 3), allows to find easily the attitude angles, provide that there is a viable way to calculate a1, a2, a3 and a4. We will shortly demonstrate that it is the case.

Quaternion

In the post Quaternion we introduced this amazing mathematical entity. This is a three dimensional extension of a complex number. It is formed by a real part and an imaginary 3D vector part. The general form of a quaternion is:

q=q0+iq1+jq2+kq3

where i, j, k are three unitary pure imaginary number (in analogy with i, j, k versors of a real three dimensional reference system). In the same post we discovered that in case the quaternion was a versor, of the form

q=cosμ2+icosαsinμ2+jcosβsinμ2+kcosγsinμ2

the following relation:

V'=q~Vq   4)

allows to calculate V’ in a reference system derived by the one where V is defined, rotating it by an angle μ around an axis (vector) which forms with the X, Y and Z axis the three angles α β γ. In fact relation 4) can be written as (see related post Quaternion)

(V'xV'yV'z)=((q02+q12q22q32)(2q0q3+2q1q2)(2q1q32q0q2)(2q1q22q0q3)(q02q12+q22q32)(2q0q1+2q2q3)(2q0q2+2q1q3)(2q2q3q0q1)(q02q12q22+q32))(VxVyVz)  5)

It is evident that the matrix transformation in 5) is identical of the matrix transformation 3) when we pose

q0=a1=cosμ2q1=a2=cosαsinμ2

q2=a3=cosβsinμ2q3=a4=cosγsinμ2

We have here established the equivalence between the Euler parameters and the quaternion components.

So, summarizing, if we know the quaternion components, we know the Euler parameters, and if we know the Euler parameters we know matrix 1), and if we know matrix 1), using relations 2), we know the attitude angles. Let’s orient now our effort in knowing the quaternion components in a viable way.

In the post Quaternion we face the problem of quaternion differentiation, and we found out that the derivative of a quaternion is:

q˙=12q(t)ω¯  6)

where

ω¯=ìωx+jωy+kωz=ìP+jQ+kR

is the pure imaginary quaternion representing the overall angular velocity.

In the same post we developed relation 6) arriving to the result:

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

If we are able, knowing the three components of the instantaneous angular velocity (P, Q, R), to solve the system of differential equations 7), we know the components of the quaternion, and thus the matrix 1) and finally the attitude angles. In the same post we developed an easy way to calculate the component of a quaternion able to rotate the reference system once the three attitude angles ϕ,θ,ψ are given. This is very useful for initializing a quaternion knowing the attitude angles.

The Local Linearization Algorithm

To solve system 7) we make use of an efficient integration algorithm explained in detail in the post LL Algo, the local linearization algorithm. This post is a pure mathematical treatment and cannot be easily summarized: it implies knowledge in differential equation solution, Taylor series expansion of function of multiple variables, matrices and function of matrices. The integration process is suppose to be done numerically on a digital computer, adopting a proper integration step (in flight simulation typical integration frequency are form 25 to 100 Hz with corresponding integration step varying from 40 to 10 msec). We here report simply the results of the post LL Algo:

q0(k+1)=q0kHq1kKq2kJq3kG

q1(k+1)=q0kK+q1kHq2kGq3kJ

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

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

At each step the quaternion components are calculated as a linear combination of the quaternion components at the previous step, using particular coefficients which are functions of P, Q, R and their derivatives. In the same post we report also a code snippet in C/C++ where the LL Algo, in its essential core (excluding initialization of parameters, condition checking etc) is about 100 lines of code, so very compact, with minimal CPU load and above all, “gimbal lock” condition free, solving completely the problem. Nevertheless this result comes from a rather complex path that I wanted to explain in detail, hoping that it will be useful to someone. Often these algorithms are already proposed in the libraries of many tools used in scientific computing, but it is also true that these tools are normally incompatible with real-time applications so their detailed knowledge can be useful.