Skip to content

Commit 4b2ba49

Browse files
authored
Merge pull request #5 from FoamyGuy/master
adding compass example
2 parents c94a2e3 + d23fc6e commit 4b2ba49

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

examples/lis2mdl_calibrate.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
15+
def calibrate():
16+
start_time = time.monotonic()
17+
18+
# Update the high and low extremes
19+
while time.monotonic() - start_time < 10.0:
20+
magval = magnetometer.magnetic
21+
print("Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(*magval))
22+
for i, axis in enumerate(magval):
23+
hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis)
24+
hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis)
25+
print("Calibration complete:")
26+
print("hardiron_calibration =", hardiron_calibration)
27+
28+
29+
print("Prepare to calibrate! Twist the magnetometer around in 3D in...")
30+
print("3...")
31+
time.sleep(1)
32+
print("2...")
33+
time.sleep(1)
34+
print("1...")
35+
time.sleep(1)
36+
37+
calibrate()

examples/lis2mdl_compass.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
""" Display compass heading data from a calibrated magnetometer """
2+
3+
import time
4+
import math
5+
import board
6+
import busio
7+
import adafruit_lis2mdl
8+
9+
i2c = busio.I2C(board.SCL, board.SDA)
10+
sensor = adafruit_lis2mdl.LIS2MDL(i2c)
11+
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+
#
15+
# To get these values run the lis2mdl_calibrate.py script on your device.
16+
# Twist the device around in 3D space while it calibrates. It will print
17+
# some calibration values like these:
18+
# ...
19+
# Calibrating - X: -46.62, Y: -22.33, Z: -16.94 uT
20+
# ...
21+
# Calibration complete:
22+
# hardiron_calibration = [[-63.5487, 33.0313], [-40.5145, 53.8293], [-43.7153, 55.5101]]
23+
#
24+
# You need t copy your own value for hardiron_calibration from the output and paste it
25+
# into this script here:
26+
hardiron_calibration = [[-61.4879, 34.4782], [-43.6714, 53.5662], [-40.7337, 52.4554]]
27+
28+
29+
# This will take the magnetometer values, adjust them with the calibrations
30+
# and return a new array with the XYZ values ranging from -100 to 100
31+
def normalize(_magvals):
32+
ret = [0, 0, 0]
33+
for i, axis in enumerate(_magvals):
34+
minv, maxv = hardiron_calibration[i]
35+
axis = min(max(minv, axis), maxv) # keep within min/max calibration
36+
ret[i] = (axis - minv) * 200 / (maxv - minv) + -100
37+
return ret
38+
39+
40+
while True:
41+
magvals = sensor.magnetic
42+
normvals = normalize(magvals)
43+
print("magnetometer: %s -> %s" % (magvals, normvals))
44+
45+
# we will only use X and Y for the compass calculations, so hold it level!
46+
compass_heading = int(math.atan2(normvals[1], normvals[0]) * 180.0 / math.pi)
47+
# compass_heading is between -180 and +180 since atan2 returns -pi to +pi
48+
# this translates it to be between 0 and 360
49+
compass_heading += 180
50+
51+
print("Heading:", compass_heading)
52+
time.sleep(0.1)

0 commit comments

Comments
 (0)