# A Real-Time GNSS/PDR Navigation System for Mobile Devices
## 2 Materials and Methods
### Methods
#### 2.2.1 Sensor Fusion for Device Orientation Estimation  
As it is known, each inertial and magnetic sensor measures a specific physical quantity, but can also be used to derive other information such as the ones related to velocity, orientation, position. Due to the errors that are always present in the measurement, the sensor's information has to be manipulated (e.g., direct integration of the accelerometer data to estimate the velocity) and the cumulative error on the derived information grows with time. A technique that is used to obtain a more accurate information from the one which is obtained by sensors is the fusion. The aim of the sensor fusion technique for the orientation estimation is to obtain a combined information and to define a stable AHRS. The latter allows to represent the sensor data, originally expressed in the sensor body frame, in a reference frame which permits to represent the sensor orientation on the Earth surface, namely the North-East-Down (NED) frame for this work. The operating systems of Android or iOS devices commonly provide information on the orientation as a function of the Euler angles. In this paper, representing the orientation via unitary quaternion was preferred to Euler angles in order to avoid the gimbal lock problem. When dealing with Euler angles, the gimbal lock phenomenon occurs when one of the sensor axes, used to estimate the angles, aligns with the gravity vector causing a loss of information; this scenario can easily occur when the phone is in the pocket. As a result a Madgwick filter was then implemented to merge the accelerometer, gyroscope and magnetometer data. This filter, reported first time in 2010 by [PERSON]. [PERSON] [10], represents an efficient way to fuse data for estimating the orientation with respect to a reference fixed navigation frame <=> using quaternion representation. It uses a gradient-descent algorithm to compute the direction of the gyroscope measurement error as a quaternion derivative [11]. The main idea behind the Madgwick filter is to evaluate the orientation from the angular velocity of the gyroscope and correct the resulting bias by evaluating the orientation from homogeneous fields, such as Earth magnetic and gravitational fields, and finally perform a compensation of magnetic distortion. It is possible to consider the quaternion representation for the rate of change of the Earth frame relative to the sensor frame as  
\[\overset{S}{E}\dot{\mathbf{q}}_{t}=\frac{1}{2}\overset{S}{E}\dot{\mathbf{q}}_{ t-1}\otimes^{S}\boldsymbol{\omega}_{t}, \tag{1}\]  
where the \(\overset{\cdot}{\cdot}\) accent denotes a normalized vector of the unit length, \({}^{S}\boldsymbol{\omega}_{t}= \begin{bmatrix}0&{}^{S}\omega_{t,x}&{}^{S} \omega_{t,y}&{}^{S}\omega_{t,z}\end{bmatrix} \) is the quaternion representation of the angular rate elements at time \(t\) from gyroscope expressed in \([rad/s]\) referred to sensor frame, and the symbol \(\otimes\) represents the quaternion product that can be determined using the Hamilton rule:  
\[\mathbf{a}\otimes\mathbf{b}= \begin{bmatrix}a_{1}&a_{2}&a_{3}&a_{4}\end{bmatrix}  \otimes \begin{bmatrix}b_{1}&b_{2}&b_{3}&b_{4}\end{bmatrix} = \begin{bmatrix}a_{ 1}b_{1}-a_{2}b_{2}-a_{3}b_{3}-a_{4}b_{4}\\ a_{1}b_{2}+a_{2}b_{1}+a_{3}b_{4}-a_{4}b_{3}\\ a_{1}b_{3}-a_{2}b_{4}+a_{3}b_{1}+a_{4}b_{2}\\ a_{1}b_{4}+a_{2}b_{3}-a_{3}b_{2}+a_{4}b_{1}\end{bmatrix} .\]The quaternion that expresses the orientation of the Earth frame relative to the sensor frame considering only the gyroscope data can be calculated through direct integration as follows:  
\[\overset{S}{E}\mathbf{q}_{t}=\frac{1}{2}\overset{S}{E}\hat{\mathbf{q}}_{t-1} \otimes^{S}\mathbf{\omega}_{t}T_{s}, \tag{2}\]  
where \(T_{s}\) is the sample time. It is necessary to obtain the orientation from the accelerometer and the magnetometer to compensate for the errors that are made in the integration of the gyro bias. The attitude estimation from the accelerometer or magnetometer is done by using a gradient descent algorithm to solve the following minimization problem.