# Indoor Positioning Integrating PDR/Geomagnetic Positioning Based on the Genetic-Particle Filter
## 2 Materials and Methods
### Pedestrian Dead-reckoning (PDR) Module
#### 2.1.1. Heading Angle Estimation  
Estimating heading angle is important for the PDR method to calculate position coordinates. In general, the heading angle can be measured by using the gyroscope. But there are many integration calculations that will make the measured heading angle inaccurate. In our research, the Mahony complementary filter Mahony (2016) is implemented on the smartphone and used to compute the heading angle. It should be clear that the MCF algorithm mainly utilizes the gyroscope data to calculate the attitude of carrier, but the accelerometer and magnetometer will correct the gyroscope data so that the estimation error will not accumulate. Generally, the whole producer of the MCF algorithm contains error compensation calculation and gyroscope data correction. The gyroscope data will be expressed as the quaternion vector, which represents current attitude prediction. Assuming that the quaternion vector is \(\mathbf{Q}=[q_{0}\ q_{1}\ q_{2}\ q_{3}]^{T}\), it is defined as follows:  
\[\left\{ \begin{array}{l}q_{0}=\cos(\beta/2)\\ q_{1}=\omega_{x}^{\prime}\sin(\beta/2)\\ q_{2}=\omega_{y}^{\prime}\sin(\beta/2)\\ q_{3}=\omega_{z}^{\prime}\sin(\beta/2)\\ \end{array} \right. \tag{2}\]  
where \(\beta\) is the rotation angle, \(\omega_{x}^{\prime}\), \(\omega_{y}^{\prime}\) and \(\omega_{z}^{\prime}\) are the normalized instantaneous gyroscope data. Then, in the error compensation calculation stage, two error items will be calculated by using the accelerometer and magnetometer data. If the gyroscope error correction is \(\mathbf{e}=\left[ \begin{array}{ccc}e_{x}&e_{y}&e_{z}\\ \end{array} \right]^{T}\), it can be defined as:  
\[\mathbf{e}=\mathbf{e}_{a}+\mathbf{e}_{m} \tag{3}\]  
where \(\mathbf{e}_{a}\) and \(\mathbf{e}_{m}\) are the error correction items calculated by using the accelerometer and magnetometer data, respectively. They can be expressed as \(\mathbf{e}_{a}=\left[ \begin{array}{ccc}e_{ax}&e_{ay}&e_{az}\\ \end{array} \right]^{T}\) and \(\mathbf{e}_{m}=\left[ \begin{array}{ccc}e_{mx}&e_{my}&e_{mz}\\ \end{array} \right]^{T}\). Their calculation methods are as follows:  
\[\left\{ \begin{array}{l}\mathbf{e}_{a}=(C_{n}^{b}\bullet g_{a})\times\mathbf{a}\\ \mathbf{e}_{m}=(C_{n}^{b}\bullet b_{m})\times\mathbf{m}\\ \end{array} \right. \tag{4}\]  
where \(\mathbf{g}_{a}\) is the gravity vector in the geographic coordinate system, \(\mathbf{g}_{a}=\left[ \begin{array}{ccc}0&0&g\\ \end{array} \right]^{T}\), \(b_{m}\) is the geomagnetic vector when \(x\)-axis of device points to the north, \(\mathbf{b}_{m}=\left[ \begin{array}{ccc}b_{mx}&0&b_{mz}\\ \end{array} \right]^{T}\); \(\mathbf{a}\) and \(\mathbf{m}\) are the normalized measured accelerometer and magnetometer data; \(C_{n}^{b}\) is the rotation matrix from the geographic coordinate system to the carrier coordinate system and it will be described in Section 2.2; \"\(\times\)\" represents the vector cross product, respectively. After getting the error correction, the corrected gyroscope data can be calculated as:  
\[\mathbf{\omega}=\mathbf{\omega}_{g}+K_{P}\mathbf{e}+K_{I}\int\mathbf{e} \tag{5}\]  
where \(\mathbf{\omega}_{g}=\left[ \begin{array}{ccc}\omega_{gx}&\omega_{gy}&\omega_{gz }\\ \end{array} \right]^{T}\) and \(\mathbf{\omega}=\left[ \begin{array}{ccc}\omega_{x}&\omega_{y}&\omega_{z}\\ \end{array} \right]^{T}\) are the normalized gyroscope raw data and the corrected gyroscope data, respectively; \(K_{P}\) and \(K_{I}\) are the error control items, which are 2.0 and 0.001 based on our test, respectively. After getting the corrected gyroscope data, we should substitute \(\mathbf{\omega}\) into the quaternion differential equation and solve this equation by using the first-order Runge-Kutta method Runge andKutta (1962) to obtain the quaternion update equation as Equation (6). The quaternion values can be calculated by this equation:\[\left\{