Magnetometers can detect the orientation with respect to the Earth's magnetic field. Basically, they are 3D compasses. We are using FXOS8700 3-Axis accelerometer and magnetometer (by [Adafruit](https://www.adafruit.com/product/3463)). Since magnetometers have to measure quite small magnetic fields, it is easy to be affected from other sources.
Some magnetometer errors, can be:
1. Hard iron distortions caused by external magnetic fields (high current wires, magnets)
2. Soft iron distortions that come from paramagnetic materials or the Earth's magnetic field.
3. Temperature & acceleration dependence
## Method
In this way, magnetometer calibration is essential. Following a simple matrix multiplication calculation, using the raw data, the hard-iron correction data and the matrix with the soft iron, scale factor & misalingment corrections, we can estimate the calibrated values.
If we plot the uncalibrated values we have an ellipsoid, while the calibrated values give us a circle. Thus, the method we described above is called ellipsoid fitting.
XY,YZ & XZ-graph: Calibrated data shape a better-centered circle, while raw data shape an ellipsoid that is shifted. With the ellipsoid fitting method, we corrected the data to fit in a perfect sphere.