Browse Source

add interactive calibration

Install source as normal, enter REPL, `import calibrate`, move module
around, copy and paste values.
master
Brennen Bearnes 3 years ago
parent
commit
852a426725
2 changed files with 43 additions and 49 deletions
  1. +0
    -49
      limor_calibrated_recent.py
  2. +43
    -0
      src/calibrate.py

+ 0
- 49
limor_calibrated_recent.py View File

@ -1,49 +0,0 @@
import time
import math
import board
import busio
import adafruit_lsm9ds1
i2c = busio.I2C(board.SCL, board.SDA)
compass = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
mag_min = [-0.10206, 0.00238, -0.10458]
mag_max = [0.44926, 0.56938, 0.4501]
def calibrate_compass(compass):
mag_min = [1000, 1000, 1000]
mag_max = [-1000, -1000, -1000]
print("Magnetometer Calibration")
lastDisplayTime = time.monotonic()
while True:
x, y, z = compass.magnetometer
mag_vals = [x, y, z]
for i in range(3):
mag_min[i] = min(mag_min[i], mag_vals[i])
mag_max[i] = max(mag_max[i], mag_vals[i])
# display once every two seconds
if (time.monotonic() - lastDisplayTime >= 1):
print("Mag Min: ", mag_min, "& Max:", mag_max)
lastDisplayTime = time.monotonic();
#calibrate_compass(compass)
def map_range(x, in_min, in_max, out_min, out_max):
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
if out_min <= out_max:
return max(min(mapped, out_max), out_min)
return min(max(mapped, out_max), out_min)
while True:
x, y, z = compass.magnetometer
print("Uncalibrated: ", x, y, z, end="")
x = map_range(x, mag_min[0], mag_max[0], -1, 1)
y = map_range(y, mag_min[1], mag_max[1], -1, 1)
z = map_range(z, mag_min[2], mag_max[2], -1, 1)
print(" \tCalibrated: ", x, y, z)
compass_heading = (math.atan2(y, x) * 180) / math.pi
if compass_heading < 0:
compass_heading += 360
print("Heading: ", compass_heading)
time.sleep(0.5)

+ 43
- 0
src/calibrate.py View File

@ -0,0 +1,43 @@
import time
import math
import board
import busio
import adafruit_lsm9ds1
from glitterpos_util import map_range
i2c = busio.I2C(board.SCL, board.SDA)
compass = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
def calibrate_compass(compass):
mag_min = [1000, 1000, 1000]
mag_max = [-1000, -1000, -1000]
print("Magnetometer Calibration")
lastDisplayTime = time.monotonic()
while True:
x, y, z = compass.magnetometer
mag_vals = [x, y, z]
for i in range(3):
mag_min[i] = min(mag_min[i], mag_vals[i])
mag_max[i] = max(mag_max[i], mag_vals[i])
# Display once every three seconds:
if (time.monotonic() - lastDisplayTime >= 3):
print("Uncalibrated:", x, y, z)
cal_x = map_range(x, mag_min[0], mag_max[0], -1, 1)
cal_y = map_range(y, mag_min[1], mag_max[1], -1, 1)
cal_z = map_range(z, mag_min[2], mag_max[2], -1, 1)
print("Calibrated: ", cal_x, cal_y, cal_z)
print("MAG_MIN =", mag_min)
print("MAG_MAX =", mag_max)
compass_heading = (math.atan2(cal_y, cal_x) * 180) / math.pi
if compass_heading < 0:
compass_heading += 360
print("Heading: ", compass_heading)
print("----")
lastDisplayTime = time.monotonic();
calibrate_compass(compass)

Loading…
Cancel
Save