Calibration of 3-Axis Low-Cost Magnetometer Using the Least Square Ellipsoid Fitting Algorithm

: This paper presents a calibration method for low-cost 3-axis magnetometers using the least square ellipsoid fitting algorithm. The aim of the calibration process is to reduce noise and mitigate the effects of magnetic interferences and instrumentation errors, thereby enhancing the accuracy and reliability of magnetometer measurements. By collecting data while moving the sensor in arbitrary directions, the calibration parameters are estimated


Introduction
Magnetometers play a vital role in a wide range of applications, including navigation systems, robotics, geophysics, and motion capture. However, to ensure accurate and reliable measurements, magnetometers require calibration to correct various errors such as bias, scale factor, misalignment, and non-orthogonality. In recent years, significant research efforts have been directed toward developing calibration methods for magnetometers, particularly those with a 3-axis configuration.
Andel et al. [1] propose a calibration method for low-cost magnetometers that utilizes Global Navigation Satellite System (GNSS) data. By incorporating GNSS-derived position and velocity errors, the authors effectively correct the magnetometer measurements, thereby improving their accuracy. This approach is particularly useful in applications where accurate positioning is crucial, such as navigation systems and robotics. Cao et al. [2] present a real-time calibration method specifically designed for magnetometers embedded in inertial navigation systems. Their approach employs the Recursive Least Squares/Maximum Likelihood (RLS/ML) algorithm, which enables accurate and efficient calibration. By continuously updating the calibration parameters in real-time, the overall performance of the navigation system can be significantly enhanced. Hu et al. [3] introduce an automatic method for calculating the zero offset of magnetometers using the interplanetary magnetic field. By analyzing variations in the interplanetary magnetic field obtained from spacecraft, the authors estimate the magnetometer's zero offsets. This approach eliminates the need for external references and provides a self-contained calibration technique, making it suitable for space missions and remote sensing applications. Li et al. [4] propose a calibration method specifically designed for strapdown magnetic vector measurement systems. By fitting magnetic field measurements to a plane model in different orientations, they estimate errors and improve the reliability of magnetometer measurements. This approach is particularly useful in applications where precise attitude estimation is required, such as autonomous vehicles and unmanned aerial systems. Ru et al. [5] provide an overview of the current status and future trends in MEMS inertial sensor calibration technology. While not directly focused on magnetometers, their insights into calibration techniques and challenges for achieving accurate and reliable sensor measurements are valuable. The authors discuss the calibration of accelerometers and gyroscopes, providing a broader context for magnetometer calibration studies. Renaudin et al. [6] present a comprehensive calibration method for triaxial magnetometers. Their approach addresses various errors, including scale factor, misalignment, and non-orthogonality errors. By combining calibration models and optimization techniques, they achieve accurate calibration and precise magnetic field measurements. This method is applicable to a wide range of triaxial magnetometers used in diverse applications such as geomagnetic mapping and attitude determination. Styp-Rekowski et al. [7] take a machine learning-based approach to calibrating magnetometers on the GOCE satellite platform. By utilizing support vector regression and random forests, they model and correct errors, resulting in improved calibration accuracy. This approach demonstrates the potential of machine learning techniques in magnetometer calibration and provides insights into the integration of artificial intelligence methods with sensor calibration. Abosekeen et al. [8] conduct a comparative analysis of MEMS-based magnetometers in magnetic-based Relative Incremental Slip Sensor (RISS) systems. By examining accuracy, precision, and reliability, their findings aid in selecting and calibrating magnetometers for RISS applications. This study contributes to the understanding of the performance characteristics of MEMS-based magnetometers in specific application contexts, helping researchers and engineers make informed decisions regarding sensor selection and calibration. Brigante et al. [9] discuss the miniaturization of MEMS-based wearable motion capture systems. Although not directly related to calibration, their insights into design, calibration, and performance considerations are valuable for miniaturized MEMS sensor applications. This study highlights the importance of considering calibration requirements during the design phase of wearable systems to ensure accurate and reliable motion capture data. Coulin et al. [10] propose a method for online magnetometer calibration in indoor environments for magnetic field-based Simultaneous Localization and Mapping (SLAM). By estimating magnetometer errors in real-time during SLAM operations, their approach enhances the accuracy of magnetic field-based SLAM systems. This study addresses the practical challenges of online calibration in dynamic environments and contributes to the development of robust SLAM algorithms. Chen et al. [11] introduce a novel calibration method for triaxial magnetometers. Their expanded error model and two-step total least square algorithm effectively estimate and correct bias, scale factor, and non-orthogonality errors, leading to improved calibration accuracy. This study provides an advanced calibration technique that can be applied to triaxial magnetometers used in various applications, including robotics, geophysics, and navigation systems. Ouni and Landry [12] focus on calibrating low-cost magnetometers using the Particle Swarm Optimization (PSO) algorithm. Their PSO-based calibration method successfully estimates bias and scale factor errors, enhancing the calibration accuracy of these devices. This approach offers a computationally efficient solution for calibrating low-cost magnetometers, making it suitable for resource-constrained applications. Pylvänäinen [13] discusses the automatic and adaptive calibration of 3D field sensors, including magnetometers. Through an iterative model-based approach, their method adaptively updates calibration parameters, ultimately improving the accuracy and reliability of 3D field sensor measurements. This study highlights the importance of continuous calibration in maintaining the performance of field sensors over time, especially in dynamic environments where the calibration parameters may change. Yan et al. [14] propose a calibration approach for MEMS-based magnetometers in Autonomous Underwater Vehicle (AUV) navigation systems. By utilizing the Earth's magnetic field as a reference, their method estimates magnetometer errors using field measurements collected during AUV operations. This study addresses the unique challenges of underwater navigation and provides a calibration solution specifically tailored to AUV applications. Wahdan et al. [15] present a fast and autonomous magnetometer calibration technique for portable navigation devices in vehicles. Leveraging the Earth's magnetic field during vehicle movements, their approach achieves accurate calibration, enhancing the reliability of magnetometer measurements. This study contributes to the development of calibration techniques suitable for real-time applications, such as navigation systems in vehicles. Micro Electromechanical Systems (MEMS) are widely used in navigation and motion capture systems because of their affordable price and their high sampling frequency. Nonetheless, the outputs of the MEMS-IMUs, especially the magnetometers, are affected by the presence of sensor noise and by external magnetic interferences. Hence, the parameters of the magnetic distortions should be precisely estimated to reduce the drift of the measured data. In this paper, a real-time magnetometer calibration method is performed based on the estimation of two calibration parameters: the magnetic disturbances and the instrumentation errors. The errors need to be removed from the measured data to obtain an estimation of the calibrated data. This method does not require any additional equipment for measuring the data, in fact, the data is being collected while moving the sensor by the hand in arbitrary directions. This method was simulated and developed on MATLAB and integrated into the intelligent IMU Sensor (IIS). The algorithm consists of the magnetometer's field measurements fitting to an ellipsoid surface. Thus, the Least Square Fitting algorithm was used for this purpose. The obtained results proved that the Ellipsoid Fitting Algorithm calibrated data fits the surface of an ellipsoid unlike the original magnetometer's data. [6]

Magnetometer Error Models
In this part, the model of error for a real magnetometer is described. The model consists of two main parts, the first part is called magnetic interferences, and the second one is defined as instrumentation error. Each part has its own error parameters. Table 1 demonstrates the parameters of error for defining the main model of the magnetometer. [6]

Magnetic Interferences
The local magnetic field measured by the magnetometers is disturbed by the presence of ferromagnetic materials and electromagnetic systems. These errors are presented as the hard and soft iron errors. The soft iron errors are created from the presence of the ferromagnetic materials that generate complex magnetic fields in the surrounding of the magnetometer. The soft iron error is modeled by the following matrix: [6] = The hard iron errors are generated by permanent magnets and magnetic hysteresis. This error is equivalent to a bias in the measured data and it is represented by the following vector: [6] =

Instrumentation Errors
The instrumentation errors vary from one magnetometer to another and they are not variable. The first instrumentation error is defined as the scale factor error. It represents a constant of proportionality which relies on the input to the output. The scale factor error is modeled by the following matrix: [6] = ( ) The matrix represented the misalignment errors are represented as follows: The vectors of the matrix N represent the direction of the magnetometer axis according to the sensor frame. A bias is introduced by the sensor offset in the output data. This bias is modeled by the following vector: [6]

Modeling of the Measured Data
The outputs of the magnetometers are affected by the combination of the magnetic interferes and the instrumentation errors. The expression of the measured data is given as follow: [6] ℎ % = ( ℎ + ) + # + !
Where h represents the ideal data that is free from the magnetic disturbances and the instrumentation errors. ! represents the Gaussian white noise. The equation can be rewritten into a more simplified equation which is expressed as follow: [6] ℎ % = ℎ + + ! Where, A is a 3⨯3 matrix that represents a combination of the scale factor, misalignment and soft iron interference. On the other hand, b is a vector that represents a combination of the bias. [6]

Calibration Algorithm
From the equation (8) it can be seen that the procedure of magnetometer calibration is reduced for finding an estimation of the calibration parameters A and b. In case we neglect the Gaussian noise, the calibration equation will become: [6,16] ℎ = (ℎ % − ) If we suppose that the measured data are recorded by an ideal tri-axis magnetometer in an environment which is free from any magnetic distortion, their norm should be equal to the norm of the earth magnetic field: [6] ) % − ‖ℎ‖ = ) % − ℎ ℎ = 0 Replacing the equation (11) in the equation (10), gives us the following equation: [6] (ℎ % − ) (ℎ % − ) -) % = 0 The equation (12) can be rewritten into a quadratic form as follow: [6] ℎ % ,ℎ % + -ℎ % + . = 0 Where , = To find the values of Q, u, and k we use the least square ellipsoid fitting algorithm which fits a set of points into a surface of an ellipsoid. In this paper, the points represent the measured data of the magnetometer. This algorithm gives us as an output of the algebraic ellipsoid description (the nine elements of the equation of the ellipsoid) and the geometric characteristic of the ellipsoid (center, radius, principle axis). The following figure shows the block scheme of the ellipsoid fitting algorithm. [6]  The vector v contains the nine coefficients of the quadratic equation S of the ellipsoid. The quadratic equation is given by: [6] : The nine coefficients are used to calculate the matrix Q and the vector u. The expression of the matrix Q and the vector u is given as follow: [16] , 5 6 5 7 6 7 3 (18) From the estimated values of Q and u, it can be determined that the values of A and b can be calculated from the following equations: ( , - ; After obtaining the values of A and b, we replace in the equation (10) to obtain the calibrated data. [16]

Online Calibration Method
The module which is utilized for the project needs to perform the calibration algorithm in real-time. As a result of that, the system needs the real-time method without any interfere or delay in performance of module. In this part, the method for performing the calibration method in real-time is designed and the results of real-time experiments are shown in the next part of this paper.
The system consists of timing block, sample recorder block and threshold parameters. The threshold parameter is defined as a sampling amount in which the sampling block will stop the storing data. It can be defined at each amount more than 1000 in according to the fact that, for accurate calibration at least 1000 sample for 10 seconds with sampling frequency of 100 Hz is needed. All the samples will be recorded in vectors in sampling recorder data and after the threshold time, the output of online calibration block will demonstrate the calibrated data. Block diagram of online calibration method is illustrated in the figure 2.

Experimental Results
The original magnetometer data are distributed into an arbitrary shape due to the presence of the instrumentation errors and the magnetic disturbance. As an evaluation of the project, a MATLAB based algorithm is designed to test the calibration of real magnetometer. For that, the low-cost module MPU-9250 set to send its magnetometer's output in serial to record in the system. Table 2 illustrates the specification the MPU-9250. In this study the test was conducted in the room temperature with the magnetometer range set to ±500°/s. After sending the data with sampling frequency of 100 Hz, the data is recorded with serial reader software.  The calculated parameters of calibration A and b from the least square ellipsoid fitting algorithm are shown in Table 3. As described in Table 3, b is a vector with elements of total bias in the x, y, and z axis of the magnetometer which should be subtracted from the real raw data. After estimating the parameters of calibration with the proposed method, the experiment was performed for testing the online calibration design, the test was done in real-time, and a sampling time of 0.01 second with Simulink-based design.
As is obvious in Figure 3, the plots clearly illustrate the behavior of the magnetometer's output over time regarding X, Y and Z axis. Prior to the threshold sample time of 10 seconds, the output is uncalibrated, exhibiting erratic and inconsistent values. This uncalibrated phase is denoted by a noticeable lack of stability and accuracy in the magnetometer readings. However, beyond the 10-second mark, a significant change is observed in the magnetometer's output. The plot indicates a distinct shift in the behaviour, with the output becoming calibrated. The magnetometer readings now exhibit enhanced precision and reliability, as indicated by a more consistent and accurate representation of the measured magnetic field. The calibration process has effectively compensated for the initial inaccuracies in the magnetometer's output. Figure 4 presents the uncalibrated magnetometer data, depicting the raw measurements obtained prior to the calibration process. In contrast, Figure 5 exhibits the outcomes subsequent to the removal of specific parameters from the original magnetometer readings. Notably, the application of these calibration techniques has resulted in a significant improvement in data accuracy.  Analyzing Figure 5 reveals that approximately 90% of the red data points are aligned with and conform to the surface of a sphere. This correspondence between the calibrated measurements and the spherical shape indicates a calibration accuracy of 90%. This substantial level of precision is a testament to the effectiveness of the employed calibration methodology. Furthermore, the calibrated data points are distributed along the surface of an ellipsoid. The radius of this ellipsoid corresponds to the magnitude of the local magnetic Earth field, representing the strength and orientation of the magnetic field at the measurement location. By conforming to the ellipsoid's surface, the calibrated data accurately captures the characteristics of the magnetic field, allowing for more reliable analysis and interpretation of the magnetometer measurements.
Overall, the calibration process showcased in Figure 5 demonstrates a remarkable enhancement in the accuracy and reliability of the magnetometer data. These calibrated measurements provide a solid foundation for conducting further analysis, modeling, or applications that require precise understanding and utilization of the local magnetic field.

Conclusion
In conclusion, this paper presented a calibration method for 3-axis low-cost magnetometers using the least square ellipsoid fitting algorithm. The calibration process aimed to reduce noise and mitigate the effects of magnetic interferences and instrumentation errors, ultimately enhancing the accuracy and reliability of magnetometer measurements. By collecting data while moving the sensor in arbitrary directions, the calibration parameters were estimated, including magnetic disturbances (soft iron and hard iron effects) and instrumental errors (scale factor, nonorthogonality, and bias). The measured data were modelled as a combination of these errors, and the calibration parameters were obtained by solving a quadratic form equation using the least square ellipsoid fitting algorithm. The results demonstrated that the calibrated data exhibited a better fit to the surface of an ellipsoid compared to the original magnetometer data, indicating the effectiveness of the calibration method. By accurately estimating and correcting the error parameters, the calibrated magnetometer data can be utilized in various applications, such as motion capture, navigation systems, robotics, and geophysics. The proposed calibration method offers several advantages, including its simplicity and cost-effectiveness, as it only requires the data collected by the magnetometer and a computer for processing. Furthermore, the real-time capability of the algorithm makes it suitable for applications that require continuous calibration, ensuring accurate and reliable measurements over time. The integration of the calibration method into the intelligent IMU Sensor (IIS) further enhances its practicality and applicability in real-world scenarios. It is important to note that while this paper focused on low-cost magnetometers, the principles and techniques presented can be extended to other types of magnetometers with a 3-axis configuration. Additionally, future research could explore the integration of machine learning algorithms or advanced optimization techniques to further improve the calibration accuracy and robustness. Overall, the proposed calibration method using the least square ellipsoid fitting algorithm provides a valuable contribution to the field of magnetometer calibration with 90% accuracy in magnetometer calibration of module MPU-9250, enabling more accurate and reliable measurements for a wide range of applications.