Skip to content

Commit 488ba5b

Browse files
committed
lis2mdl : fix magic number errors.
1 parent d1f20c1 commit 488ba5b

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

lib/lis2mdl/exemple/magnet_test.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
from lis2mdl.const import *
55
import math
66

7+
# Définition des constantes pour remplacer les valeurs magiques
8+
MAGNETIC_FIELD_MIN = 5.0
9+
MAGNETIC_FIELD_MAX = 200.0
10+
TEMP_MIN = -100.0
11+
TEMP_MAX = 150.0
12+
CENTER_TOLERANCE = 0.2
13+
ROUND_TOLERANCE = 1.4
14+
CENTER_TOLERANCE_3D = 0.3
15+
ROUND_TOLERANCE_3D = 1.6
16+
ANGLE_DIFF_MIN = 14.0
17+
ANGLE_DIFF_MAX = 20.0
18+
ANGLE_DIFF_WRAP_MIN = 340.0
19+
ANGLE_DIFF_WRAP_MAX = 346.0
20+
SPAN_MIN = 300.0
21+
FILTER_DIFF_MAX = 90.0
22+
723

824
def _bits(v, hi, lo):
925
m = (1 << (hi - lo + 1)) - 1
@@ -254,10 +270,10 @@ def test_reads(dev):
254270
# MAGNITUDE
255271
B = dev.magnitude_uT()
256272
print(
257-
f"magnitude_uT: |B|={B:.1f} µT (Earth ~2565 µT). =>",
258-
"OK" if 5.0 <= B <= 200.0 else "FAIL",
273+
f"magnitude_uT: |B|={B:.1f} µT (Earth ~25-65 µT). =>",
274+
"OK" if MAGNETIC_FIELD_MIN <= B <= MAGNETIC_FIELD_MAX else "FAIL",
259275
)
260-
ok &= 5.0 <= B <= 200.0 # wide, since local disturbances are possible
276+
ok &= MAGNETIC_FIELD_MIN <= B <= MAGNETIC_FIELD_MAX # wide, since local disturbances are possible
261277

262278
# CALIBRATION NORM
263279
xc, yc, zc = dev.read_magnet_calibrated_norm()
@@ -276,8 +292,8 @@ def test_reads(dev):
276292
ok_temp = (
277293
isinstance(t1, float)
278294
and isinstance(t2, float)
279-
and (-100.0 < t1 < 150.0)
280-
and (-100.0 < t2 < 150.0)
295+
and (TEMP_MIN < t1 < TEMP_MAX)
296+
and (TEMP_MIN < t2 < TEMP_MAX)
281297
)
282298
print("Temp check =>", "OK" if ok_temp else "FAIL")
283299
ok &= ok_temp
@@ -331,8 +347,8 @@ def test_calibrate_2d(dev):
331347
)
332348
print("r_std_xy =", "{:.3f}".format(q["r_std_xy"]), " (smaller = better)")
333349

334-
ok_center = abs(q["mean_xy"][0]) < 0.2 and abs(q["mean_xy"][1]) < 0.2
335-
ok_round = q["anisotropy_xy"] < 1.4 # realistic tolerances
350+
ok_center = abs(q["mean_xy"][0]) < CENTER_TOLERANCE and abs(q["mean_xy"][1]) < CENTER_TOLERANCE
351+
ok_round = q["anisotropy_xy"] < ROUND_TOLERANCE # realistic tolerances
336352
print("=> Center close to 0 :", "OK" if ok_center else "WARN")
337353
print("=> Circle ≈ round :", "OK" if ok_round else "WARN")
338354
return ok_center and ok_round
@@ -362,8 +378,8 @@ def test_calibrate_3d(dev):
362378
"{:.3f}".format(q["r_std_xy"]),
363379
)
364380

365-
ok_center = abs(q["mean_xy"][0]) < 0.3 and abs(q["mean_xy"][1]) < 0.3
366-
ok_round = q["anisotropy_xy"] < 1.6
381+
ok_center = abs(q["mean_xy"][0]) < CENTER_TOLERANCE_3D and abs(q["mean_xy"][1]) < CENTER_TOLERANCE_3D
382+
ok_round = q["anisotropy_xy"] < ROUND_TOLERANCE_3D
367383
print("=> Center close to 0 :", "OK" if ok_center else "WARN")
368384
print("=> Circle ≈ round :", "OK" if ok_round else "WARN")
369385
return ok_center and ok_round
@@ -425,7 +441,7 @@ def test_heading_offset_declination(dev):
425441
# difference mod 360
426442
diff = (a1 - a0) % 360.0
427443
# accept ~17° ±3° (due to noise/quantization/filtering)
428-
ok = (14.0 <= diff <= 20.0) or (340.0 <= diff <= 346.0) # wrap
444+
ok = (ANGLE_DIFF_MIN <= diff <= ANGLE_DIFF_MAX) or (ANGLE_DIFF_WRAP_MIN <= diff <= ANGLE_DIFF_WRAP_MAX) # wrap
429445
print(
430446
f"angle0={a0:.2f}°, angle1={a1:.2f}°, diff≈{diff:.2f}° =>",
431447
"OK" if ok else "FAIL",
@@ -456,7 +472,7 @@ def test_heading_span_turn(dev, duration_ms=6000, step_ms=50):
456472
if span < 0:
457473
span += 360.0
458474
print(f"min={minA:.1f}°, max={maxA:.1f}°, span≈{span:.1f}°")
459-
ok = span > 300.0 # we expect almost 360° for a full turn
475+
ok = span > SPAN_MIN # we expect almost 360° for a full turn
460476
print("SPAN =>", "OK" if ok else "WARN (do a more complete/slower turn)")
461477
return ok
462478

@@ -483,7 +499,7 @@ def test_heading_filter_wrap(dev):
483499
outs.append(out)
484500
print(f"in={ang:>3}° -> out_filt={out:>6.2f}°")
485501
# Check that the output is monotonically increasing (no jump around ~180°)
486-
ok = all((outs[i] - outs[i - 1]) % 360.0 < 90.0 for i in range(1, len(outs)))
502+
ok = all((outs[i] - outs[i - 1]) % 360.0 < FILTER_DIFF_MAX for i in range(1, len(outs)))
487503
print("Wrap-safe filter =>", "OK" if ok else "FAIL")
488504
return ok
489505

lib/lis2mdl/lis2mdl/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def use_spi_4wire(self, enable: bool):
133133
self.setReg(reg, LIS2MDL_CFG_REG_C)
134134

135135
# --- Compass: heading offset & declination (software) ---
136-
_heading_offset_deg = 0.0
137-
_declination_deg = 0.0
136+
_heading_offset_deg = 0.0 # user setting: align your physical 0°
137+
_declination_deg = 0.0 # true north vs magnetic north
138138

139139
def set_heading_offset(self, deg: float):
140140
self._heading_offset_deg = float(deg)

0 commit comments

Comments
 (0)