# Estimator¶

## Introduction¶

The estimator is used to calculate an estimate of the attitude and angular velocity of the multirotor. It is assumed that the flight controller is mounted rigidly to the body of the aircraft (perhaps with dampening material to remove vibrations from the motors), such that measurements of the onboard IMU are consistent with the motion of the aircraft.

Due to the limited computational power on the embedded processor, and to calculate attitude estimates at speeds up to 8000Hz, a simple complementary filter is used, rather than an extended Kalman filter. In practice, this method works extremely well, and it used widely throughout commercially available autopilots. There are a variety of complementary filters, but the general theory is the same. A complementary filter is a method to fuse the measurements from a gyroscope, accelerometer and sometimes magnetometer to produce an estimate of the attitude of the MAV.

## Complementary Filtering¶

The idea behind complementary filtering is to try to get the "best of both worlds" of gyros and accelerometers. Gyros are very accurate in short spaces of time, but they are subject to drift. Accelerometers don't drift in the long scheme of things, but they are noisy as the MAV moves about. So, to solve these problems, the complementary filter primarily propagates states using gyroscope measurements, but then corrects drift with the accelerometer. In a general sense, it is like taking a high-pass filtered version of gyroscope measurements, and a low-pass filtered version of accelerometers, and fusing the two together in a manner that results in an estimate that is stable over time, but also able to handle quick transient motions.

## Attitude Representation¶

There are a number of ways to represent the attitude of a MAV. Often, attitude is represented in terms of the euler angles yaw, pitch and roll, but it can also be represented in other ways, such as rotation matrices, and quaternions.

### Euler Angles¶

Euler angles represent rotations about three different axes, usually, the z, y, and x axes in that order. This method is often the most easy for users to understand and interpret, but it is by far the least computationally efficient. To propagate euler angles, the following kinematics are employed:

Note the large number of trigonometric functions associated with this propagation. In a complementary filter, this will be evaluated at every measurement, and the non-linear coupling between \omega and the attitude becomes very expensive, particularly on embedded processors.

Another shortcoming of euler angles is known as "gimbal lock". Gimbal lock occurs at the "singularity" of the euler angle representation, or pitched directly up or down. The problem occurs because there is more than one way to represent this particular rotation. There are some steps one can take to handle these issues, but it is a fundamental problem associated with using euler angles, and motivates the other attitude representations.

### Rotation Matrix¶

Rotation matrices, are often used in attitude estimation, because they do not suffer from gimbal lock, are quickly converted to and from euler angles, and because of their simple kinematics.

where \lfloor\omega\rfloor is the skew-symmetric matrix of \omega, and is related to calculating the cross product.

This propagation step is linear with respect to the angular rates, which simplifies calculation significantly.

A rotation matrix from the inertial frame to body frame can be constructed from euler angles via the following formula:

and converting back to euler angles is done via the following;

### Quaternions¶

Quaternions are a number system which extends complex numbers. They have four elements, commonly known as w, x, y, and z. The last three elements can be though of as describing an axis, \beta about which a rotation occurred, while the first element, w can be though of as describing the amount of rotation \alpha about that axis. (see eq~\ref{eq:euler_to_axis_angle}). While this may seem straight-forward, quaternions are normalized so that they can form a group. (That is, a quaternion multiplied by a quaternion is a quaternion), so they end up being really difficult for a human being to interpret just by looking at the values. However, they provide some amazing computational efficiencies, most of which comes from the following special mathematics associated with quaternions.

First, just the definition of a quaternion:

and second, some formulas to convert to and from euler angles.

The quaternion group is "closed" under the following operation, termed quaternion multiplication.

To take the "inverse" of a quaternion is simply to multiply s or v by -1

and to find the difference between two quaternions, just quaternion multiply the inverse of one quaternion with the other.

However, the most important aspect of quaternions is the way we propagate dynamics.

where q_\omega is the pure quaternion created from angular rates.

What this means is that, like rotation matrices, quaternion dynamics are *linear* with respect to angular rates, as opposed to euler angles, which are non-linear, and they take less computation than rotation matrices because they have fewer terms. Casey et al. [Casey2013] performed a study comparing all three of the above representations, and found that complementary filters using an euler angle representation took **12 times** longer to compute on average than a quaternion-based filter. Quaternions were also about 20% more efficient when compared with rotation matrices. For these reasons, ROSflight uses quaternions in its filter.

## Derivation¶

ROSflight implements the quaternion-based passive "Mahony" filter as described in this paper. In particular, we implement equation 47 from that paper, which also estimates gyroscope biases. A Lyuapanov stability analysis is performed in that paper, in which it is shown that all states and biases, except heading, are globally asymptotically stable given an accelerometer measurement and gyroscope. The above reference also describes how a magnetometer can be integrated in a similar method to the accelerometer. That portion of the filter is ommitted here due to the unreliable nature of magnetometers onboard modern small UAS.

### Passive Complementary Filter¶

The original filter propagates per the following dynamics:

where q_{\omega} is the pure quaternion formed from a composite \omega_{comp} which consists of the estimated angular rates, \bar{\omega}, the estimated gyroscope biases, \hat{b}, and an adjustment term, calculated from accelerometer measurements, \omega_{acc}. k_p and k_Is are constant gains used in determining the dynamics of the filter. $$ \begin{equation} \begin{aligned} q_{\omega} &= \textrm{p}\left(\bar{\omega} - \hat{b} + k_P\wacc\right) \ &= \textrm{p}\left( \omega_{comp} \right) \end{aligned} \label{eq:q_omega} \tag{2} \end{equation} $$

\wacc can be described as the error in the attitude as predicted by the accelerometer. To calculate \wacc the quaternion describing the rotation between the accelerometer estimate and the inertial frame, \qmeas, is first calculated

Next, the quaternion error between the estimate \qhat and the accelerometer measure \qmeas is calculated. \begin{equation} \qtilde = \qmeas^{-1} \otimes \qhat = \begin{bmatrix} \tilde{s}\ \vtilde \end{bmatrix} \end{equation}

Finally, \qmeas is converted back into a 3-vector per the method described in eq. 47a of Mahony2007. \begin{equation} \wmeas = 2\tilde{s}\vtilde \end{equation}

Although the formulation in Eq.\eqref{eq:traditional_prop} is more concise, in implementation, q_{\omega} is never actually formed, instead the composite \omega_{comp} vector of Eq.\eqref{eq:q_omega} is directly substituted into equation\eqref{eq:mat_exponential}. Gyroscope bias propagation is performed using standard euler integration, because they are assumed to be nearly constant.

### Modifications to Original Passive Filter¶

There have been a few modifications to the passive filter described in Mahony2007, consisting primarily of contributions from Casey2013. Firstly, rather than simply taking gyroscope measurements directly as an estimate of \omega, a quadratic polynomial is used to approximate the true angular rate from gyroscope measurements to reduce error. In Casey2013, this process was shown to reduce RMS error by more than 1,000 times. There are additional steps associated with performing this calculation, but the benefit in accuracy more than compensates for the extra calculation time. The equation to perform this calculation is shown in Eq.\eqref{eq:quad_approx}.

where \omega(t_{n-x}) is the gyroscope measurement of the angular rate x measurements previous.

The second modification is in the way that the attitude is propagated after finding \dot{\hat{q}}. Instead of performing the standard euler integration

we use an approximation of the matrix exponential. The matrix exponential arises out of the solution to the differential equation \dot{x} = Ax, namely $$ \begin{equation} x(t) = e^{At} x(0) \end{equation} $$

and the discrete-time equivalent $$ \begin{equation} x(t_{n+1}) = e^{hA}(t_n) \end{equation} $$

This discrete-time matrix exponential can be approximated by first expanding the matrix exponential into the infinite series

and then grouping odd and even terms from the infinite series into two sinusoids, and results in the following equation for the propagation of the quaternion dynamics

where \lfloor\w\rfloor_4 is the 4x4 skew-symmetric matrix formed from \w

## Tuning¶

The filter can be tuned with the two gains k_P and k_I. Upon initialization, k_P and k_i are set very high, so as to quickly cause the filter to converge upon approprate values. After a few seconds, they are both reduced by a factor of 10, to a value chosen through manual tuning. A high k_P will cause sensitivity to transient accelerometer errors, while a small k_P will cause sensitivity to gyroscope drift. A high k_I will cause biases to wander unnecessarily, while a low k_I will result in slow convergence upon accurate gyroscope bias estimates. These parameters generally do not need to be modified from the default values.

## Implementation¶

The entire filter is implemented in float-based quaternion calculations. Even though the STM32F10x microprocessor does not contain a floating-point unit, the entire filter has been timed to take about 370\mus. The extra steps of quadratic integration and matrix exponential propagation can be ommited for a 20\mus and 90\mus reduction in speed, respectively. Even with these functions, however, this is sufficiently short to run at well over 1000Hz, which is the update rate of the MPU6050 on the naze32.

Control is performed according to euler angle estimates, and to reduce the computational load of converting from quaternion to euler angles (See Equation\eqref{eq:euler_from_quat}), a lookup table approximation of atan2 and asin are used. The Invensense MPU6050 has a 16-bit ADC and an accelerometer and gyro onboard. The accelerometer, when scaled to \pm4g, has a resolution of 0.002394 m/s^2. The lookup table method used to approximate atan2 and asin in the actual implementation is accurate to \pm 0.001 rad. Given the accuracy of the accelerometer, use of this lookup table implementation is justfied. The C-code implementation of the estimator can be found in the file `src/estimator.c.`