|
| 1 | +""" |
| 2 | +Interactive hard-iron calibration. |
| 3 | +Ask the user to slowly rotate the board flat on a table. |
| 4 | +Uses calibrate_minmax_2d() to compute offsets, then displays before/after heading quality with calibrate_quality(). |
| 5 | +Demonstrates the full calibration workflow. |
| 6 | +""" |
| 7 | +from time import sleep_ms |
| 8 | + |
| 9 | +from lis2mdl import LIS2MDL |
| 10 | +from machine import I2C |
| 11 | + |
| 12 | + |
| 13 | +def print_quality(title, quality): |
| 14 | + print(title) |
| 15 | + print(" mean_xy = ({:.3f}, {:.3f})".format(quality["mean_xy"][0], quality["mean_xy"][1])) |
| 16 | + print(" mean_z = {:.3f}".format(quality["mean_z"])) |
| 17 | + print(" std_xy = ({:.3f}, {:.3f})".format(quality["std_xy"][0], quality["std_xy"][1])) |
| 18 | + print(" std_z = {:.3f}".format(quality["std_z"])) |
| 19 | + print(" r_mean_xy = {:.3f}".format(quality["r_mean_xy"])) |
| 20 | + print(" r_std_xy = {:.3f}".format(quality["r_std_xy"])) |
| 21 | + print(" anisotropy_xy = {:.3f}".format(quality["anisotropy_xy"])) |
| 22 | + print() |
| 23 | + |
| 24 | + |
| 25 | +i2c = I2C(1) |
| 26 | +mag = LIS2MDL(i2c) |
| 27 | + |
| 28 | +print("2D hard-iron calibration example") |
| 29 | +print("Keep the board flat on a table.") |
| 30 | +print("Slowly rotate it through full circles.") |
| 31 | +print() |
| 32 | + |
| 33 | +print("Checking heading quality before calibration...") |
| 34 | +quality_before = mag.calibrate_quality(samples_check=120, delay_ms=15) |
| 35 | +print_quality("Before calibration:", quality_before) |
| 36 | + |
| 37 | +print("Starting 2D calibration now...") |
| 38 | +mag.calibrate_minmax_2d(samples=300, delay_ms=20) |
| 39 | + |
| 40 | +print("Calibration values:") |
| 41 | +print( |
| 42 | + " x_off={:.2f} y_off={:.2f} x_scale={:.2f} y_scale={:.2f}".format( |
| 43 | + mag.x_off, mag.y_off, mag.x_scale, mag.y_scale |
| 44 | + ) |
| 45 | +) |
| 46 | +print() |
| 47 | + |
| 48 | +print("Checking heading quality after calibration...") |
| 49 | +quality_after = mag.calibrate_quality(samples_check=120, delay_ms=15) |
| 50 | +print_quality("After calibration:", quality_after) |
| 51 | + |
| 52 | +print("Live heading preview after calibration") |
| 53 | +print("Press Ctrl+C to stop.") |
| 54 | +print() |
| 55 | + |
| 56 | +while True: |
| 57 | + heading = mag.heading_flat_only() |
| 58 | + direction = mag.direction_label(heading) |
| 59 | + print("Heading: {:7.2f} deg Direction: {}".format(heading, direction)) |
| 60 | + sleep_ms(200) |
0 commit comments