# GNSS Based Low-Cost Magnetometer Calibration
## 1 Introduction  
The application area of the magnetometer is broad. However, the Earth's magnetic field is weak, and its measurement is often distorted due to ferromagnetic materials in the surroundings. Compasses on ships have also had to be calibrated and compensated since iron ships began to be made. Their calibration is ensured using two iron balls on the sides. The magnetic compass should be calibrated every time before use. There are a wide variety of different approaches to calibrating a magnetometer. Overall, there are two groups of methods: offline and online calibration. With offline calibration, we get calibration data that we do not change afterward; with online calibration, we continuously get current calibration data [1]. Such an approach is better because the disturbing influences around the sensor can change (e.g., electrical current flowing through the conductor around the sensor, movement of the ferromagnetic material in the surroundings). However, online calibration is not always possible. Finally, during offline calibration, it is necessary to rotate the sensor in all directions. The most straightforward approach to obtain calibration constants is to get minima and maxima in individual axes after rotation. Due to the rotation, we get a cloud of points that look like an ellipsoid. The average value from the min and max will determine the calibration value of the offset in the given axis. However, such an approach is trivial. We only get offset values. A more common approach is the search for an optimal ellipsoid fitting the cloud of points which determines the calibration constants. Such an approach is described in several articles [2, 3, 4, 5]. Some researchers solved the search for ellipsoids via maximum likelihood (ML) estimation [6]. Online calibration is mostly based on theKalman filter and its modifications [7; 8] or through non-linear programming [1]. For online calibration, the recursive least square (RLS) estimation and ML estimation methods can be used [9]. Such an approach is also suitable for other three-axis sensors. Several algorithms have been developed based on the discontinuity between the axes of different sensors (accelerometer and gyroscope) [10; 11; 12]. It is possible to calibrate the magnetometer based on the gyroscope and accelerometer. Authors in Reference [13] calibrated the magnetometer and gyroscope to each other, and the error of the magnetometer was reduced to 0.5\({}^{\circ}\) while the magnetometer and the gyroscope were mounted on the same platform [13]. We can also use neural networks to calibrate the magnetometer using a gyroscope, but such an approach is computationally demanding [14]. Rotating vehicles such as cars, ships, or airplanes is difficult. There are magnetometer calibration approaches that calibrate based on level rotation [15]. Such an approach has also proven to be very effective. The magnetometer in small drones can be calibrated based on flight data. The intensity of the Earth's magnetic field determining the direction of the magnetic north is affected and deformed by the current of the motors. We can compensate for this error and calibrate the magnetometer based on knowledge of the position from the flight data. The method can be implemented both online and offline [16]. Another option is to use the position data from several GNSS receivers, which allows for determining the vehicle's position and rotation [17; 18]. GNSS receivers must know the carrier phase. Such an approach has proven to be very effective, but it requires several GNSS receivers that are at least a meter apart. In our approach, we want to take a closer look at the possibility of calibrating the magnetometer based on the knowledge of the motion vector from GNSS while the vehicle is moving. At each new position, we gain information about the direction vector of the vehicle and its position. Based on this, along with known local declination, we can accurately calculate the direction of the Earth's magnetic field vector.