# GNSS Based Low-Cost Magnetometer Calibration
## 2 Magnetometer Calibration Based on GNSS
### Calibration Options  
Magnetometer calibration based on knowledge of the motion vector from GNSS is only possible under certain conditions. As described above, both systems have specific error sources. Suppose we want to calibrate the data based on the knowledge of the path with a certain error from GNSS. In that case, it is necessary to travel the path in the order of hundreds of meters to km so that the error of locating one point is negligible. At the same time, it is necessary to change the path so that it is not always only in one direction. We also need to know the orientation of the sensor for the direction vector. Finally, we need to know the exact values of the magnetic field in the given place and altitude (inclination, declination, magnetic field strength). We can obtain these values based on freely available data or implemented libraries. The direction of the magnetic field vector can be seen in Figure 2.  
Based on the knowledge of the path from GNSS, we can calculate the direction vector of movement in the Cartesian coordinate system in Equation (7). The intensity and direction (inclination and declination) of the magnetic field should be known for the given area in which the vehicle is located (vector \(M\)). Based on this knowledge, one may determine the direction of the earth's magnetic field vector with respect to the local coordinates of the car. We assume the vehicle moves along a straight path during the calculation. From the motion vector, we calculate the rotation of the global coordinates in Equation (8). Subsequently, we rotate vector B of the known magnetic field of the earth so that we get the exact direction of the magnetic field vector in the local coordinates of the vehicle in Equations (9)-(11).  
\[GPS(lan,lon,\ alt)\rightarrow[x,y,z] \tag{7}\]  
\[S_{n}=G_{n}-G_{n-1}\rightarrow\psi=atan2(y,x) \tag{8}\]  
Figure 2: Magnetic field vector.  
\[B=R\cdot M_{n} \tag{9}\]  
\[B=\begin{bmatrix}cos\psi&sin\psi&0\\ -sin\psi&cos\psi&0\\ 0&0&1\end{bmatrix}\begin{bmatrix}B_{r}\ cos\beta\ cos\alpha\\ B_{r}\ cos\beta\ sin\alpha\\ B_{r}\ sin\beta\end{bmatrix} \tag{10}\]  
\[B=B_{r}\begin{bmatrix}cos\psi\ cos\beta\ cos\alpha+sin\psi\ cos\beta\ sin\alpha\\ -sin\psi\ cos\beta\ cos\alpha+cos\psi\ cos\beta\ sin\alpha\\ sin\beta\end{bmatrix} \tag{11}\]  
At the same time, we obtain values from the magnetometer \(M\) (\(m_{x}\), \(m_{y}\), \(m_{z}\)). By subtracting the given vectors, we get the sensor error \(E\) (\(e_{x}\), \(e_{y}\), \(e_{z}\)) in Equations (12) and (13). During the calibration phase, we obtain an error vector for each point. After representing the error vectors, we should get a cloud of points that are close to each other. From the data, we need to calculate the gain and bias calibration constants or the calibration matrix. We can obtain the calibration constants of the magnetometer only after passing a certain curved (not straight) path. The error vector is smaller as the speed increases. The algorithm is ineffective when the vehicle is not moving.  
\[E=M-B \tag{12}\]  
\[\begin{bmatrix}e_{x}\\ e_{y}\\ e_{z}\end{bmatrix}=\begin{bmatrix}m_{x}\\ m_{y}\\ m_{z}\end{bmatrix}-\begin{bmatrix}b_{x}\\ b_{y}\\ b_{z}\end{bmatrix} \tag{13}\]