## 2 Methods  
### Magnetometer Error Calibration  
Manufacturing errors inevitably occur in the production of a magnetometer sensor. These errors are generally defined as magnetometer error, existing in the form of scaling error, offset error, and non-orthogonality error [9; 10]. 1. Scaling error. Scaling error denotes the difference in sensitivity of each axis due to the different characteristics of the internal electronic devices. The scaling error matrix \(\mathbf{k}_{sf}\) can be modeled as \[\mathbf{k}_{sf}=diag[k_{1},k_{2},k_{3}]\] (1) where \(k_{1},k_{2},k_{3}\) denote the axial scaling errors of the magnetometer respectively. 2. Offset error. Offset error denotes the deviation between magnetometer's output and true value, which can be modeled as \[\mathbf{h}_{b}=\left[h_{b}^{x},h_{b}^{y},h_{b}^{z}\right]^{T}\] (2) where \(h_{b}^{x},h_{b}^{y},h_{b}^{z}\) denote the axial offset errors of the magnetometer respectively. 3. Non-orthogonality error. The orthogonality between three axes of the magnetometer cannot be guaranteed due to manufacturing precision limitations, thus resulting in non-orthogonal error, as illustrated in Figure 1. As is shown, \(o-xyz\) denotes the ideal tri-axial magnetometer while \(o^{\prime}-x^{\prime}y^{\prime}z^{\prime}\) denotes the non-orthogonal magnetometer. The non-orthogonality error matrix \(\mathbf{k}_{nor}\) can be modeled as  
\[\mathbf{k}_{nor}=\left[ \begin{array}{ccc}coscos\phi&cos\phi sin\phi&sin\phi\\ 0&cos\psi&sin\psi\\ 0&0&1\end{array} \right] \tag{3}\]where \(\phi,\varphi,\psi\) denote the error angles. The comprehensive mathematical model for output error of the magnetometer interfered by different error sources can be expressed as  
\[\mathbf{h}_{m}=\mathbf{k}_{sf}\mathbf{k}_{nor}\mathbf{h}_{g}+\mathbf{h}_{b} \tag{4}\]  
where \(\mathbf{h}_{m}\) denotes the measured data of the magnetometer, \(\mathbf{h}_{g}\) denotes the true geomagnetic field. According to Equation (4), the calibration model of magnetometer error can be expressed as  
\[\mathbf{h}_{g}=\mathbf{k}(\mathbf{h}_{m}-\mathbf{h}_{b}) \tag{5}\]  
where \(\mathbf{k}=\mathbf{k}_{nor}^{-1}\mathbf{k}_{sf}^{-1}\). We can get  
\[\left|\mathbf{\mu}_{g}\right|^{2}=\mathbf{h}_{g}^{T}\mathbf{h}_{g}=(\mathbf{h}_{m}-\mathbf{h}_{b} )^{T}\mathbf{k}^{T}\mathbf{k}(\mathbf{h}_{m}-\mathbf{h}_{b}) \tag{6}\]  
then  
\[\frac{(\mathbf{h}_{m}-\mathbf{h}_{b})^{T}\mathbf{k}^{T}\mathbf{k}(\mathbf{h}_{m}-\mathbf{h}_{b})}{ \left\|\mathbf{h}_{g}\right\|^{2}}=1 \tag{7}\]  
In areas with stable magnetic field, the magnitude of the geomagnetic field remains constant within a short period, so as the magnetometer rotates, its measuring locus will form a sphere with a radius equal to the magnitude of the local geomagnetic field [11]. However, due to the magnetometer error, the sphere is distorted into an ellipsoid [12]. The equation of quadric surface is  
\[F(\rho,\sigma)=\rho^{T}\sigma=\alpha x^{2}+by^{2}+cz^{2}+2 dxy+2 exz+2 fyz+2 px+2 qy+2 rz+g=0 \tag{8}\]  
where \(\rho=[a,b,c,d,e,f,p,q,r,g]^{T}\), \(\sigma=[x^{2},y^{2},z^{2},2 xy,2 xz,2 yz,2x,2y,2z,1]^{T}\). Let \(v=[H_{m}^{x\ 2},H_{m}^{y\ 2},H_{m}^{z\ 2},2H_{m}^{x}H_{m}^{y\ 2},H_{m}^{x\ 2}H_{m}^{x\ 2},H_{m}^{y\ 2}H_{m}^{x\ 2},2H_{m}^{y},2H_{m}^{z\ 2},1]^{T}\),\(F(\rho,v)\) denotes the distance from the measuring point \(\mathbf{h}_{m}=\left[H_{m}^{x},H_{m}^{y},H_{m}^{z}\right]^{T}\) to the quadric surface \(F(\rho,\sigma)=0\). Therefore, the parameter \(\rho\) can be solved by fitting based on the minimum value of \(F(\rho,v)\). That is,  
\[\sum_{i=1}^{n}\left\|F(\rho,v_{i})\right\|^{2}=min \tag{9}\]  
Figure 1: Diagram of the non-orthogonality error. The quadric surface has multiple forms such as ellipsoid, cylinder, parabola, etc. In the mathematical sense, to ensure that the fitted quadric surface is an ellipsoid, it is required to add the following constraints in the fitting process. \[I_{1}\
eq 0,\quad I_{2}>0,\quad I_{1}\cdot I_{3}>0,\quad I_{4}<0\]  
where \(I_{1}=a+b+c\), \(I_{2}=\left| \begin{array}{cc}b&f\\ f&c\end{array} \right|+\left| \begin{array}{cc}c&e\\ e&a\end{array}