A compass is a navigational instrument that measures directions in a frame of reference that is stationary relative to the surface of the earth.
We can use magnetometer to build a compass.
Magnetic distortions can be categorized as two types—hard iron and soft iron effects. Hard iron distortions arise from permanent magnets and magnetized iron or steel on the compass platform. These distortions will remain constant and in a fixed location relative to the compass for all heading orientations. Hard iron effects add a constant magnitude field component along each axes of the sensor output. The soft iron distortion arises from the interaction of the earth’s magnetic field and any magnetically soft material surrounding the compass.
This Python script implements a simple Magnetometer calibration routine to estimate soft and hard iron correction values.
The magnetomer calibration consist of collecting raw values from the magnetometer placing it in varios orientation.
Then the raw data collected are computed by one of the two script provided.
The math of the computation scripts is out of this document.
* "ellipsoid fit" by Yury Petrov - http://www.mathworks.com/matlabcentral/fileexchange/24693-ellipsoid-fit
* "magneto" by http://www.sailboatinstruments.blogspot.com - http://sites.google.com/site/sailboatinstruments1/home
The bias obtained will correct hard iron errors, the scale factor will correct soft iron errors.
To obtain values, run this python script and follow the instructions.
A processing script is also provided to collect raw values.
Once you obtain those values you can get calibrated data by applying this formula:
xt_raw = x_raw - offsetx;
yt_raw = y_raw - offsety;
zt_raw = z_raw - offsetz;
x_calibrated = scalefactor_x * xt_raw + scalefactor_x * yt_raw + scalefactor_x * zt_raw;
y_calibrated = scalefactor_y * xt_raw + scalefactor_y * yt_raw + scalefactor_y * zt_raw;
z_calibrated = scalefactor_z * xt_raw + scalefactor_z * yt_raw + scalefactor_z * zt_raw;
Once you obtain raw values using the script provided, to compute offset and
scale factor values, you can use the matlab script or magneto12 program provided.
On the microcontroller side you have to setup a function that print out to UART
raw values read from your chip.
Given 2 bytes (int16_t) variables for every axis, output the LSB and then MSB
byte ((uint8_t)(int16_t>>0) + (uint8_t)(int16_t>>8)), follow by a '\n' char.
Snippets are provided for AVR Atmega and Arduino, but it can be setup for other micro too.
- read risk disclaimer
- excuse my bad english