5

I want to have a "compass heading" (I want to know the angle upon the north) using a magnetometer. I have seen on several tutorials that first, I need to calibrate it. When I looked up on how to do it I saw graphics comparisons of magnetometer value with and without calibration.

Here are the links I used : link_one and link_two

Both links shows that an uncalibrated magnetometer should display several clusters on the graph as bellow :

This is the graph that I should have for an uncalibrated magnetometer

And calibrated mangnetometer should have both 3-axis on the same point around zero like this :

This is the graph that I should have after the calibration


I am using the Adafruit LSM9DS1. I tried to get the same graphics with the following python code :

def save_mag_values():
    f = open("magnetometer.csv","w")
    for i in range(10000):
        value = sensor.magnetic
        f.write(",".join(map(str,value)))
        f.write("\n")

Then I use the following gnuplot command to print :

gnuplot> plot "magnetometer.csv" using 1:2 title "XY" pointsize 2 pointtype 7, \
              "magnetometer.csv" using 1:3 title "XZ" pointsize 2 pointtype 7, \
              "magnetometer.csv" using 2:3 title "YZ" pointsize 2 pointtype 7

As it's written in the tutorial I just slowly move the sensor and after 1 min I print the values. Here is what I have for the uncalibrated magnetometer : My graph of non calibrated magnetometer

As you can see, the global shape is not a circle and I don't know why. I tried to calibrate it and here is what I have :

This is my graph with calibration

Can anyone tell me what I did wrong and why can't I have "circle shape" values like it should be ? Or Does the shape have to be perfect circle like I see on tutorials or mine are fine ? Thanks

MelKoutch
  • 180
  • 1
  • 12
  • If you are asking why the magnetometer readings cluster on one side of the pattern, I cannot help. If you are asking how to make the aspect ratio of the plot coordinates 1:1, the gnuplot command is `set size ratio -1` – Ethan Mar 27 '19 at 19:08
  • I am wondering if the shape I have are normals or not. It should be a circle and clearly it is not and I don't know why. Thanks for the gnuplot command but I think the problem doesn't come from it. – MelKoutch Mar 28 '19 at 07:51
  • Are you sure your magnetometer is functioning properly? – Jack Aidley Mar 29 '19 at 13:46
  • 1
    I tested this experiment on two different sensors (both lsm9ds1 from adafruit) and I get approximatly the same result. I don't know if they are both not working or if the problem may comes from the environment (I mean like magnetic field or something) or from the code I used. – MelKoutch Mar 29 '19 at 15:19
  • Are you sure you're moving it on all three axis (x, y, z) when capturing sample data? It looks as if you've not got enough diverse sample data from a glance. – RMass Mar 29 '19 at 22:14

1 Answers1

1

If your magnetometer is working (not magnetically damaged) and if you capture readings by rotating magnetometer 360 degrees along 3 axes, then your plot should show something like this(worst case scenario).

enter image description here

In ideal case, readings should be in 3 concentric circles overlapping in the same boundary.

In practical cases, due to soft and hard iron biases, surrounding magnetic material influences, readings will become elliptical and their centers will shift away as show in above figure.

Calibration simply brings them to common center and make elliptical readings circular.

Couple of years back, I have ordered several magnetometers. I spent a lot of time debugging code and later found that all of them were damaged during transit. I also wrote a blog post regarding calibration of magnetometer.

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
  • Thanks for your answer and your link !! I will try this out with another type or sensor (an other brand). Thanks again !! – MelKoutch Apr 04 '19 at 17:09