Abstract
Three-dimensional attitude estimation for unmanned aerial vehicles is usually based on the combination of magnetometer, accelerometer, and gyroscope (MARG). But MARG sensor can be easily affected by various disturbances, for example, vibration, external magnetic interference, and gyro drift. Optical flow sensor has the ability to extract motion information from image sequence, and thus, it is potential to augment three-dimensional attitude estimation for unmanned aerial vehicles. But the major problem is that the optical flow can be caused by both translational and rotational movements, which are difficult to be distinguished from each other. To solve the above problems, this article uses a gated recurrent unit neural network to implement data fusion for MARG and optical flow sensors, so as to enhance the accuracy of three-dimensional attitude estimation for unmanned aerial vehicles. The proposed algorithm can effectively make use of the attitude information contained in the optical flow measurements and can also achieve multi-sensor fusion for attitude estimation without explicit mathematical model. Compared with the commonly used extended Kalman filter algorithm for attitude estimation, the proposed algorithm shows higher accuracy in the flight test of quad-rotor unmanned aerial vehicles.
Keywords
Introduction
Attitude estimation is the premise to ensure stable and controllable flight for unmanned aerial vehicles (UAV).1–3 In recent years, attitude and heading reference systems (AHRS) are widely adopted in UAV flight control. 4 The typical configuration of AHRS includes magnetometer, accelerometer, and gyroscope (MARG). 5 With the continuous development of micro-electro-mechanical system (MEMS) technology, MARG-based AHRS provides a low-cost solution for UAV attitude estimation.
MARG-based AHRS has the advantages of low power consumption and compact size. Theories and techniques for MARG-based AHRS are also well developed. The ellipsoid fitting (also called scalar checking) and multi-position methods can help to calibrate and compensate MARG sensor errors,6–8 while Kalman filtering4,5 and complementary filtering algorithms 9 can be used to implement data fusion for MARG sensor. But still, the dynamic accuracy, robustness, and redundancy of AHRS needs further enhancement, since MARG-based three-dimensional (3D) attitude estimation is sensitive to external disturbances, for example, motional acceleration and magnetic interference.
Optical flow sensor can detect the movements of its carrier by processing the sequence of images and extract the differences between every two adjacent frames. 10 At present, optical flow sensor has also been applied to UAV flight control. With the aid of optical flow sensor, UAV can achieve tracking flight and spot hover with higher accuracy.11–13 Besides, optical flow measurement can also be used for autonomous obstacle avoidance of UAV. 14 Moreover, optical flow measurement also contains attitude information.15–20 Nevertheless, the attitude information contained in optical flow measurement is mixed with the translational movements in most cases, and thus, it is not easy to be fully utilized.21,22
Therefore, when using optical flow sensor to augment MARG-based attitude estimation, one of the key issues is to distinguish the angular motion and translational motion that contained in optical flow measurement from each other. In most applications of optical flow sensors on UAV, the angular motion component of optical flow is eliminated with the aid of attitude information provided by IMU, and thus, only the translational motion component of optical flow is utilized. 15 However, to make use of the attitude information contained in optical flow, the translational motion component of optical flow must be compensated using additional position and/or velocity information, for example, position/velocity provided by global navigation satellite system (GNSS) or inertial navigation system (INS).
There are three main purposes of the research presented in this paper: (a) to extract the attitude information contained in optical flow measurement; (b) to implement multi-sensor fusion, including MARG and optical flow sensors; and (c) to compensate sensor errors that probably exists. To achieve the above three goals, a gated recurrent unit (GRU) neural network is used for data fusion between MARG and optical flow sensors, in order to improve the performance of AHRS under dynamic conditions. As a special brunch of artificial neural network, GRU is able to draw time-related information from time series signals, and thus, it can help to extract attitude information from optical flow measurement, without the need of complex mathematical models and additional position/velocity information.
The rest of this article is organized as follows. In section “Theoretical basis,” the theoretical basis will be briefly reviewed, including the principles of attitude estimation, data fusion, and optical flow sensor. Then in section “Attitude estimation algorithm based on GRU,” the design of GRU-based attitude estimation algorithm will be introduced, which incorporates MARG and optical flow sensors. In section “Experiment,” the proposed GRU-based algorithm will be evaluated through experiments on a quad-rotor UAV, and its performance will be compared with that of extended Kalman filter (EKF). After discussions of the experiment results in section “Discussion”, conclusions will be drawn in the sixth section.
Theoretical basis
Attitude representation and estimation
The 3D attitude of UAV can be defined by the relationship between the body frame (coordinate system that fixed to UAV) and the reference frame (coordinate system that used as the reference for navigation, e.g. North-East-Down reference frame). The transformation from one coordinate system to another can be decomposed into three consecutive rotations around the three coordinate axes, namely the Euler angles. In the case of UAV, these three Euler angles are called yaw (or heading), pitch, and roll, respectively. Besides, quaternion and direction cosine matrix (DCM) are also commonly used for UAV attitude representation. 23
In traditional INS, especially the strap-down INS (SINS), the 3D attitude are calculated according to the angular velocity measured by gyroscope, and thus, the long-term attitude accuracy is affected by gyro drift.4,24 In MARG-based AHRS, however, 3D attitude estimation mainly relies on the gravity vector
Kalman filter (KF) is widely used for data fusion in MARG-based AHRS. It is worth mentioning that the mathematical relationship between attitude quaternion and vector observations (i.e. the gravity vector
Optical flow sensor
Optical flow is the instantaneous velocity of the pixel motion on the imaging plane, which is caused by a spatially moving object. It can be calculated according to the correlation between the previous frame and the current frame in the image sequence. In another word, it contains the motion information of the object between every two adjacent frames. Based on the above principle, optical flow sensor can detect the movement of the UAV relative to the ground, as shown in Figure 1.

Geometric relationship of optical flow sensor: (a) optical flow caused by horizontal velocity, (b) optical flow caused by rotation, and (c) optical flow sensor on UAV with both translational and rotational movements.
As shown in Figure 1(a) and (b), both the horizontal speed and rotation can generate corresponding optical flow. Therefore, in the case that optical flow sensor is attached to UAV and pointed to the ground, as shown in Figure 1(c), the optical flow measurement
According to equation (1), the optical flow measurement
Attitude estimation algorithm based on GRU
GRU neural network for attitude estimation
GRU is a special version of recurrent neural network (RNN).27–29 RNN is a type of neural network that used to process sequence data. To better process sequence information, RNN has local feedback loop in its hidden layer, in order to recognize the relationship between the current input and the previous ones.29,30 GRU also has local feedback, but the feedback is controlled by two gates, namely the reset gate and the update gate. In other words, GRU can be viewed as a gated RNN. GRU has achieved great success in natural language processing and speech conversion. The schematic diagram of GRU-based attitude estimation is shown in Figure 2.

Schematic diagram of GRU neural network.
As shown in Figure 2, the GRU-based neural network takes the measurements from four different sensors as its inputs, namely the gravity vector
At the kth time step, the reset gate
Then, the hidden state
Furthermore, the hidden state
As can be seen in equation (5), the update gate works as a complementary multiplexer, in which larger value of
In most studies of GRU, the sigmoid function is used for the activation function in equations (2) and (3), which is defined as
As stated above, GRU can deal with time series signals, and it is also able to approximate nonlinear functions. Therefore, it can be used to solve the problem of data fusion between MARG and optical flow sensors for UAV attitude estimation. After sufficient training, GRU neural network can effectively utilize the measurements of MARG and optical flow sensors for attitude estimation, without the need of rigorous mathematical modeling.
GRU neural network training
The training process of GRU network is to determine the parameters in both the hidden layer and the output layer, including
First of all, the cost function for GRU training is defined as equation (7), in which
Meanwhile, all the parameters to be estimated can be rearranged as a column vector
Then, a hybrid algorithm is used for the optimization problem in equation (8), which is a combination of the commonly used Levenberg–Marquardt (L-M) method and extreme learning technique. The L-M method relies on the Jacobian matrix
(a) Derivatives for
(b) Derivatives for
(c) Derivatives for
(d) Derivatives for
It is noteworthy that the relationship between
Initialization: iteration number
Construct vector
Calculate the hidden state series
Calculate the output series
Update the parameter vector
Update the hidden state series
Update the output series
If
If
Experiment
Raw data acquisition
PX4FLOW optical flow sensor is used in the experimental evaluation of the proposed algorithm, accompanied with PIXHAWK2.4.8 autopilot system. PX4FLOW has the original resolution of 752 × 480 pixels, and it uses a fourfold grading and clipping algorithm to calculate the optical flow. This sensor is connected to PIXHAWK2.4.8 autopilot and attached to the quadcopter platform. Meanwhile, the quadcopter platform also integrates magnetometer, accelerometer, and gyroscope. After debugging, the quadcopter can fly stably in fixed height and hover modes, as shown in Figure 3. The PIXHAWK platform is equipped with 168 MHz Cortex M4 F CPU to process flight data. All the raw data, including the measurements from MARG and optical flow sensors, are recorded in the flight log.

Apparatus for raw data acquisition and flight test: (a) quadcopter platform in flight, (b) PIXHAWK autopilot, and (c) PX4FLOW optical flow sensor module.
Four datasets are collected using the apparatus shown in Figure 3, and they are denoted as Dataset 1, 2, 3, and 4, respectively. Figure 4 shows the trajectories of UAV during acquiring these four datasets. Dataset 1 is used for GRU training, while the other three datasets are used to evaluate the performance of GRU-based attitude estimation and compare it to EKF. Each dataset contains the raw data from magnetometer, accelerometer, gyroscope, and optical flow sensor, at the output rate of 50 Hz. Figure 5 illustrates the data in Dataset 1.

UAV trajectories during raw data acquisition: (a) Dataset 1, (b) Dataset 2, (c) Dataset 3, and (d) Dataset 4.

Raw data in Dataset 1 from different sensors: (a) gyroscope, (b) accelerometer, (c) magnetometer, and (d) optical flow.
GRU training
The GRU neural network is trained using Dataset 1 with different numbers of hidden layer dimensions. The training results are evaluated in terms of root mean square error (RMSE), which can be calculated by equation (23)
In equation (23),
Figure 6 shows the RMSE and training time of GRU versus different hidden layer dimensions. The training is carried out on Intel Core i5-4590 CPU at 3.50 GHz with 8 GB RAM. It can be seen from Figure 6 that the relationship between GRU training time and hidden layer dimension is almost linear. Besides, the best performance can be achieved when hidden layer dimension is set to 15 (i.e. 15 GRUs in the hidden layer). The RMSE of roll, pitch, and yaw angles are 1.51°, 2.07°, and 0.87°, respectively. Furthermore, Figure 7 shows the estimated 3D attitude compared with the true values (including roll, pitch, and yaw) when the dimension of hidden layer is 15. Compared to the attitude algorithms based on Elman neural network31,32 the GRU-based attitude estimation method proposed in this article shows higher accuracy. Therefore, it is a feasible approach for data fusion.

RMSE of Euler angles after training and training time of GRU versus hidden layer dimension of GRU.

GRU-based attitude estimation results of Dataset 1 after training: (a) roll angle, (b) pitch angle, and (c) yaw angle.
GRU validating
After GRU training, the performance of GRU-based attitude estimation is further evaluated using Datasets 2, 3, and 4. Figure 8 shows the 3D attitude estimated by the proposed GRU-based algorithm in terms of Euler angles, as well as that of traditional EKF algorithm, compared to the ground truth from PIXHAWK flight logs. The root mean square error (RMSE) is still used to evaluate the overall performance for both GRU and EKF, and the results are listed in Table 1. It is worth mentioning that the proposed GRU-based algorithm is evaluated in two cases, that is, working with and without optical flow sensor, while EKF performs typical MARG-based attitude estimation and does not make use of optical flow sensor. According to the results in Table 1, the performance of GRU-based algorithm is better than that of EKF, especially when incorporating optical flow sensor.

Attitude estimation of different algorithms in the flight test: (a) Dataset 2, roll angle; (b) Dataset 2, pitch angle; (c) Dataset 2, roll angle; (d) Dataset 3, roll angle; (e) Dataset 3, pitch angle; (f) Dataset 3, yaw angle; (g) Dataset 4, roll angle; (h) Dataset 4, pitch angle; and (i) Dataset 4, yaw angle.
RMSE of GRU- and EKF-based attitude algorithms.
To compare the computational complexity of GRU and EKF, the average execution time of both algorithms on STM32F103 micro-controller is listed in Table 2. The hidden layer dimension of GRU is still set to 15. It can be seen that GRU-based algorithm needs shorter execution time than EKF, either with or without the aid of optical flow.
Execution time of GRU and EKF on micro-controller.
Discussion
The experimental results evidently prove the feasibility of the proposed GRU-based attitude estimation algorithm. According to Figures 7 and 8, the proposed algorithm can effectively solve the problems of sensor data fusion and error compensation in attitude estimation of the quadcopter.
Furthermore, as can be seen in Table 1, the performance of the proposed GRU-based algorithm is comparable to that of EKF, in the case that optical flow sensor is unused. When the measurement of optical flow sensor is adopted, GRU-based algorithm can achieve higher attitude accuracy. Consequently, the proposed GRU-based algorithm is able to make better use of the 3D attitude information in the measurements of MARG and optical flow sensors.
It is worth mentioning again that the proposed algorithm can implement data fusion for MARG and optical flow sensors, without considering explicit mathematical models of these sensors. It is the very point that the proposed algorithm differs essentially from the commonly used KF-based data fusion technique.
Conclusion
In this article, a 3D attitude estimation algorithm based on GRU network is proposed, which make use of MARG and optical flow sensors. Experimental results on a quadcopter show that the proposed algorithm can provide reliable attitude estimation according to the measurements of MARG and optical flow sensor. Compared with commonly used EKF-based attitude estimation, the proposed algorithm can further enhance 3D attitude accuracy by extracting the attitude information from the optical flow measurement, with no need to redesign the attitude algorithm according to complex mathematical equations. Therefore, it is a novel and feasible approach for the development of AHRS and UAV flight control.
Footnotes
Handling Editor: Lyudmila Mihaylova
Declaration of conflicting interests
The author(s) declared no potential conflicts of interest with respect to the research, authorship, and/or publication of this article.
Funding
The author(s) disclosed receipt of the following financial support for the research, authorship, and/or publication of this article: The work reported in this article was funded by the National Natural Science Foundation of China (grant numbers 61603107 and 41761087).
