From d079e4f22c7d65c3a1104cb7f33cb0a26aacce14 Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Tue, 26 Sep 2023 21:19:57 -0700 Subject: [PATCH 1/4] add calibrator file to examples --- examples/lis3mdl_calibrator.py | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 examples/lis3mdl_calibrator.py diff --git a/examples/lis3mdl_calibrator.py b/examples/lis3mdl_calibrator.py new file mode 100755 index 0000000..394ceb0 --- /dev/null +++ b/examples/lis3mdl_calibrator.py @@ -0,0 +1,78 @@ +# SPDX-FileCopyrightText: 2023 Cedar Grove Maker Studios +# SPDX-License-Identifier: MIT + +""" +lis3mdl_calibrator.py is a simple CircuitPython calibrator example for +the LIS3MDL magnetometer. The calibrator measures the minimum and +maximum values for each axis as the sensor is moved. The values are +captured over a fixed number of samples. A middle-of-the-range +calibration offset value is calculated and reported after all samples +are collected. + +The sensor needs to be moved during the collection period in a manner +that exercises the entire range of each axis. A series of overlapping +figure-eight patterns is recommended. + +This code was derived from the Blinka 9dof_calibration.py code in the +'Adafruit SensorLab - Magnetometer Calibration' learning guide. The +guide code was authored by Melissa LeBlanc-Williams (c)2020. +""" + +import time +import board +import busio +from adafruit_lis3mdl import LIS3MDL + +SAMPLE_SIZE = 2000 + +i2c = busio.I2C(board.SCL, board.SDA) +magnetometer = LIS3MDL(i2c) + +while True: + print("=" * 40) + print("LIS3MDL MAGNETOMETER CALIBRATION") + print(" Move the sensor through a series of") + print(" overlapping figure-eight patterns") + print(f" for approximately {SAMPLE_SIZE/100:.0f} seconds \n") + + print(" countdown to start:", end=" ") + for i in range(5, -1, -1): + print(i, end=" ") + time.sleep(1) + print("MOVE the sensor...") + print(" >progress<") + print(" ", end="") + + # Initialize the min/max values + mag_x, mag_y, mag_z = magnetometer.magnetic + min_x = max_x = mag_x + min_y = max_y = mag_y + min_z = max_z = mag_z + + for i in range(SAMPLE_SIZE): + # Capture the samples and show the progress + if not (i % (SAMPLE_SIZE / 10)): + print("*", end="") + + mag_x, mag_y, mag_z = magnetometer.magnetic + + min_x = min(min_x, mag_x) + min_y = min(min_y, mag_y) + min_z = min(min_z, mag_z) + + max_x = max(max_x, mag_x) + max_y = max(max_y, mag_y) + max_z = max(max_z, mag_z) + + time.sleep(0.01) + + # Calculate the middle of the min/max range + offset_x = (max_x + min_x) / 2 + offset_y = (max_y + min_y) / 2 + offset_z = (max_z + min_z) / 2 + + print( + f"\n\n Final Calibration: X:{offset_x:6.2f} Y:{offset_y:6.2f} Z:{offset_z:6.2f} uT\n" + ) + + time.sleep(5) From df4a87af8b832addf1a97da38b08621f357c4a61 Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Wed, 27 Sep 2023 09:43:32 -0700 Subject: [PATCH 2/4] expand progress bar; update docs --- examples/lis3mdl_calibrator.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/examples/lis3mdl_calibrator.py b/examples/lis3mdl_calibrator.py index 394ceb0..12579ef 100755 --- a/examples/lis3mdl_calibrator.py +++ b/examples/lis3mdl_calibrator.py @@ -2,20 +2,23 @@ # SPDX-License-Identifier: MIT """ -lis3mdl_calibrator.py is a simple CircuitPython calibrator example for -the LIS3MDL magnetometer. The calibrator measures the minimum and -maximum values for each axis as the sensor is moved. The values are -captured over a fixed number of samples. A middle-of-the-range -calibration offset value is calculated and reported after all samples -are collected. - -The sensor needs to be moved during the collection period in a manner +'lis3mdl_calibrator.py' is a simple CircuitPython calibrator example for +the LIS3MDL magnetometer. The resultant offset values can be used to +compensate for 'hard iron' effects, static magnetic fields, or to orient +the sensor with the earth's magnetic field for use as a compass. + +The calibrator measures the minimum and maximum values for each axis as +the sensor is moved. The values are captured over a fixed number of +samples. A middle-of-the-range calibration offset value is calculated +and reported after all samples are collected. + +The sensor needs to be tumbled during the collection period in a manner that exercises the entire range of each axis. A series of overlapping figure-eight patterns is recommended. -This code was derived from the Blinka 9dof_calibration.py code in the -'Adafruit SensorLab - Magnetometer Calibration' learning guide. The -guide code was authored by Melissa LeBlanc-Williams (c)2020. +This code was derived from the '9dof_calibration.py' Blinka code +authored by Melissa LeBlanc-Williams for the 'Adafruit SensorLab - +Magnetometer Calibration' learning guide (c)2020. """ import time @@ -31,7 +34,7 @@ while True: print("=" * 40) print("LIS3MDL MAGNETOMETER CALIBRATION") - print(" Move the sensor through a series of") + print(" Tumble the sensor through a series of") print(" overlapping figure-eight patterns") print(f" for approximately {SAMPLE_SIZE/100:.0f} seconds \n") @@ -39,8 +42,8 @@ for i in range(5, -1, -1): print(i, end=" ") time.sleep(1) - print("MOVE the sensor...") - print(" >progress<") + print("\n MOVE the sensor...") + print(" > progress <") print(" ", end="") # Initialize the min/max values @@ -51,7 +54,7 @@ for i in range(SAMPLE_SIZE): # Capture the samples and show the progress - if not (i % (SAMPLE_SIZE / 10)): + if not (i % (SAMPLE_SIZE / 20)): print("*", end="") mag_x, mag_y, mag_z = magnetometer.magnetic From 7a6134f9a58ccdbdaab21f220c573478f772b22c Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Wed, 27 Sep 2023 14:30:36 -0700 Subject: [PATCH 3/4] fix trailing space error --- examples/lis3mdl_calibrator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lis3mdl_calibrator.py b/examples/lis3mdl_calibrator.py index 12579ef..cd7e701 100755 --- a/examples/lis3mdl_calibrator.py +++ b/examples/lis3mdl_calibrator.py @@ -10,7 +10,7 @@ The calibrator measures the minimum and maximum values for each axis as the sensor is moved. The values are captured over a fixed number of samples. A middle-of-the-range calibration offset value is calculated -and reported after all samples are collected. +and reported after all samples are collected. The sensor needs to be tumbled during the collection period in a manner that exercises the entire range of each axis. A series of overlapping From 6fed7c0161f503fb9e5339223b1d7110c3dd45d9 Mon Sep 17 00:00:00 2001 From: Cedar Grove Maker Studios Date: Wed, 27 Sep 2023 14:33:35 -0700 Subject: [PATCH 4/4] correct pylint error --- examples/lis3mdl_calibrator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/lis3mdl_calibrator.py b/examples/lis3mdl_calibrator.py index cd7e701..7006bec 100755 --- a/examples/lis3mdl_calibrator.py +++ b/examples/lis3mdl_calibrator.py @@ -54,7 +54,7 @@ for i in range(SAMPLE_SIZE): # Capture the samples and show the progress - if not (i % (SAMPLE_SIZE / 20)): + if not i % (SAMPLE_SIZE / 20): print("*", end="") mag_x, mag_y, mag_z = magnetometer.magnetic