Skip to content

adding compass example #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/lis2mdl_calibrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
""" Calibrate the magnetometer and print out the hard-iron calibrations """

import time
import board
import busio
import adafruit_lis2mdl

i2c = busio.I2C(board.SCL, board.SDA)
magnetometer = adafruit_lis2mdl.LIS2MDL(i2c)

# calibration for magnetometer X (min, max), Y and Z
hardiron_calibration = [[1000, -1000], [1000, -1000], [1000, -1000]]


def calibrate():
start_time = time.monotonic()

# Update the high and low extremes
while time.monotonic() - start_time < 10.0:
magval = magnetometer.magnetic
print("Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(*magval))
for i, axis in enumerate(magval):
hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis)
hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis)
print("Calibration complete:")
print("hardiron_calibration =", hardiron_calibration)


print("Prepare to calibrate! Twist the magnetometer around in 3D in...")
print("3...")
time.sleep(1)
print("2...")
time.sleep(1)
print("1...")
time.sleep(1)

calibrate()
52 changes: 52 additions & 0 deletions examples/lis2mdl_compass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
""" Display compass heading data from a calibrated magnetometer """

import time
import math
import board
import busio
import adafruit_lis2mdl

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

# You will need the calibration values from your magnetometer calibration
# these values are in uT and are in X, Y, Z order (min and max values).
#
# To get these values run the lis2mdl_calibrate.py script on your device.
# Twist the device around in 3D space while it calibrates. It will print
# some calibration values like these:
# ...
# Calibrating - X: -46.62, Y: -22.33, Z: -16.94 uT
# ...
# Calibration complete:
# hardiron_calibration = [[-63.5487, 33.0313], [-40.5145, 53.8293], [-43.7153, 55.5101]]
#
# You need t copy your own value for hardiron_calibration from the output and paste it
# into this script here:
hardiron_calibration = [[-61.4879, 34.4782], [-43.6714, 53.5662], [-40.7337, 52.4554]]


# This will take the magnetometer values, adjust them with the calibrations
# and return a new array with the XYZ values ranging from -100 to 100
def normalize(_magvals):
ret = [0, 0, 0]
for i, axis in enumerate(_magvals):
minv, maxv = hardiron_calibration[i]
axis = min(max(minv, axis), maxv) # keep within min/max calibration
ret[i] = (axis - minv) * 200 / (maxv - minv) + -100
return ret


while True:
magvals = sensor.magnetic
normvals = normalize(magvals)
print("magnetometer: %s -> %s" % (magvals, normvals))

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

print("Heading:", compass_heading)
time.sleep(0.1)