|
1 | 1 | """Calibrate all temperature sensors against WSEN-HIDS reference. |
2 | 2 |
|
3 | | -This example reads each sensor one at a time to stay within RAM |
4 | | -limits on the STM32WB55. Calibration offsets are stored in the |
5 | | -persistent config zone and survive power cycles. |
| 3 | +Reads the WSEN-HIDS sensor as reference, then computes an offset for |
| 4 | +each other sensor. Calibration offsets are stored in the persistent |
| 5 | +config zone and survive power cycles. |
| 6 | +
|
| 7 | +Note: this example assumes the drivers are frozen into the firmware. |
| 8 | +Use ``make firmware && make deploy`` to build a firmware with the |
| 9 | +latest drivers before running this script. |
6 | 10 | """ |
7 | 11 |
|
8 | | -import gc |
9 | | -import sys |
10 | 12 | from time import sleep_ms |
11 | 13 |
|
12 | | -from daplink_flash.device import DaplinkFlash |
| 14 | +from daplink_flash import DaplinkFlash |
| 15 | +from hts221 import HTS221 |
| 16 | +from ism330dl import ISM330DL |
| 17 | +from lis2mdl import LIS2MDL |
13 | 18 | from machine import I2C |
14 | | -from steami_config.device import SteamiConfig |
15 | | -from wsen_hids.device import WSEN_HIDS |
| 19 | +from steami_config import SteamiConfig |
| 20 | +from wsen_hids import WSEN_HIDS |
| 21 | +from wsen_pads import WSEN_PADS |
16 | 22 |
|
17 | 23 | i2c = I2C(1) |
18 | 24 | flash = DaplinkFlash(i2c) |
19 | 25 | config = SteamiConfig(flash) |
20 | 26 | config.load() |
21 | 27 |
|
22 | 28 | # Read reference temperature from WSEN-HIDS (most accurate at ambient) |
23 | | -ref_temp = WSEN_HIDS(i2c).temperature() |
| 29 | +ref = WSEN_HIDS(i2c) |
| 30 | +ref_temp = ref.temperature() |
24 | 31 | print("Reference (WSEN-HIDS): {:.2f} C".format(ref_temp)) |
25 | 32 | config.set_temperature_calibration("wsen_hids", gain=1.0, offset=0.0) |
26 | | -del WSEN_HIDS |
27 | | -sys.modules.pop("wsen_hids.device", None) |
28 | | -gc.collect() |
29 | 33 |
|
30 | | -# Calibrate each sensor one at a time to save RAM |
31 | | -SENSORS = [ |
32 | | - ("hts221", "hts221.device", "HTS221", "temperature"), |
33 | | - ("wsen_pads", "wsen_pads.device", "WSEN_PADS", "temperature"), |
34 | | - ("lis2mdl", "lis2mdl.device", "LIS2MDL", "temperature"), |
35 | | - ("ism330dl", "ism330dl.device", "ISM330DL", "temperature"), |
| 34 | +# Calibrate each sensor |
| 35 | +sensors = [ |
| 36 | + ("hts221", HTS221(i2c)), |
| 37 | + ("wsen_pads", WSEN_PADS(i2c)), |
| 38 | + ("lis2mdl", LIS2MDL(i2c)), |
| 39 | + ("ism330dl", ISM330DL(i2c)), |
36 | 40 | ] |
37 | 41 |
|
38 | | -for config_name, module, class_name, method in SENSORS: |
39 | | - mod = __import__(module, None, None, [class_name]) |
40 | | - cls = getattr(mod, class_name) |
41 | | - sensor = cls(i2c) |
42 | | - if config_name == "ism330dl": |
| 42 | +for name, sensor in sensors: |
| 43 | + if name in ("ism330dl", "lis2mdl"): |
43 | 44 | sleep_ms(200) |
44 | | - raw = getattr(sensor, method)() |
| 45 | + raw = sensor.temperature() |
45 | 46 | offset = ref_temp - raw |
46 | | - config.set_temperature_calibration(config_name, gain=1.0, offset=offset) |
47 | | - print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(config_name, raw, offset)) |
48 | | - del sensor, cls, mod |
49 | | - sys.modules.pop(module, None) |
50 | | - gc.collect() |
| 47 | + config.set_temperature_calibration(name, gain=1.0, offset=offset) |
| 48 | + print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(name, raw, offset)) |
51 | 49 |
|
52 | 50 | config.save() |
53 | 51 | print("\nCalibration saved.") |
54 | 52 |
|
55 | | -# Verify by reloading |
56 | | -gc.collect() |
| 53 | +# Verify by reloading with fresh sensor instances (simulates a reboot) |
57 | 54 | config2 = SteamiConfig(flash) |
58 | 55 | config2.load() |
59 | 56 |
|
| 57 | +verify_sensors = [ |
| 58 | + ("hts221", HTS221(i2c)), |
| 59 | + ("wsen_pads", WSEN_PADS(i2c)), |
| 60 | + ("lis2mdl", LIS2MDL(i2c)), |
| 61 | + ("ism330dl", ISM330DL(i2c)), |
| 62 | +] |
| 63 | + |
60 | 64 | print("\nVerification (after reload):") |
61 | | -for config_name, module, class_name, method in SENSORS: |
62 | | - mod = __import__(module, None, None, [class_name]) |
63 | | - cls = getattr(mod, class_name) |
64 | | - sensor = cls(i2c) |
65 | | - if config_name == "ism330dl": |
| 65 | +for name, sensor in verify_sensors: |
| 66 | + if name in ("ism330dl", "lis2mdl"): |
66 | 67 | sleep_ms(200) |
67 | 68 | config2.apply_temperature_calibration(sensor) |
68 | | - print(" {:10s}: {:6.2f} C".format(config_name, getattr(sensor, method)())) |
69 | | - del sensor, cls, mod |
70 | | - sys.modules.pop(module, None) |
71 | | - gc.collect() |
| 69 | + print(" {:10s}: {:6.2f} C".format(name, sensor.temperature())) |
0 commit comments