Skip to content

Commit 1f6bf6b

Browse files
committed
adding calibration script for compass example and update compass to use calibration values.
1 parent 62f1412 commit 1f6bf6b

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

examples/lis2mdl_calibrate.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
""" Calibrate the magnetometer and print out the hard-iron calibrations """
2+
3+
import time
4+
import board
5+
import busio
6+
import adafruit_lis2mdl
7+
8+
i2c = busio.I2C(board.SCL, board.SDA)
9+
magnetometer = adafruit_lis2mdl.LIS2MDL(i2c)
10+
11+
# calibration for magnetometer X (min, max), Y and Z
12+
hardiron_calibration = [[1000, -1000], [1000, -1000], [1000, -1000]]
13+
14+
def calibrate():
15+
start_time = time.monotonic()
16+
17+
# Update the high and low extremes
18+
while time.monotonic() - start_time < 10.0:
19+
magval = magnetometer.magnetic
20+
print('Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT'.format(*magval))
21+
for i, axis in enumerate(magval):
22+
hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis)
23+
hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis)
24+
print("Calibration complete:")
25+
print("hardiron_calibration =", hardiron_calibration)
26+
27+
print("Prepare to calibrate! Twist the magnetometer around in 3D in...")
28+
print("3...")
29+
time.sleep(1)
30+
print("2...")
31+
time.sleep(1)
32+
print("1...")
33+
time.sleep(1)
34+
35+
calibrate()

examples/lis2mdl_compass.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
1-
""" Display compass heading data five times per second """
1+
""" Display compass heading data from a calibrated magnetometer """
2+
23
import time
3-
from math import atan2, degrees
4+
import math
45
import board
56
import busio
67
import adafruit_lis2mdl
78

89
i2c = busio.I2C(board.SCL, board.SDA)
910
sensor = adafruit_lis2mdl.LIS2MDL(i2c)
1011

12+
# You will need the calibration values from your magnetometer calibration
13+
# these values are in uT and are in X, Y, Z order (min and max values)
14+
hardiron_calibration = [[-61.4879, 34.4782], [-43.6714, 53.5662], [-40.7337, 52.4554]]
1115

12-
def vector_2_degrees(x, y):
13-
angle = degrees(atan2(y, x))
14-
if angle < 0:
15-
angle += 360
16-
return angle
16+
# This will take the magnetometer values, adjust them with the calibraitons
17+
# and return a new array with the XYZ values ranging from -100 to 100
18+
def normalize(magvals):
19+
ret = [0, 0, 0]
20+
for i, axis in enumerate(magvals):
21+
minv, maxv = hardiron_calibration[i]
22+
axis = min(max(minv, axis), maxv) # keep within min/max calibration
23+
ret[i] =(axis - minv) * 200 / (maxv - minv) + -100
24+
return ret
1725

1826

19-
def get_heading(_sensor):
20-
magnet_x, magnet_y, _ = _sensor.magnetic
21-
return vector_2_degrees(magnet_x, magnet_y)
27+
while True:
28+
magvals = sensor.magnetic
29+
normvals = normalize(magvals)
30+
print("magnetometer: %s -> %s" % (magvals, normvals))
2231

32+
# we will only use X and Y for the compass calculations, so hold it level!
33+
compass_heading = int(math.atan2(normvals[1], normvals[0]) * 180.0 / math.pi)
34+
# compass_heading is between -180 and +180 since atan2 returns -pi to +pi
35+
# this translates it to be between 0 and 360
36+
compass_heading += 180
2337

24-
while True:
25-
print("heading: {:.2f} degrees".format(get_heading(sensor)))
26-
time.sleep(0.2)
38+
print("Heading:", compass_heading)
39+
time.sleep(0.1)

0 commit comments

Comments
 (0)