Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions lib/steami_config/examples/calibrate_temperature.py
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
"""Calibrate all temperature sensors against WSEN-HIDS reference.

This example reads each sensor one at a time to stay within RAM
limits on the STM32WB55. Calibration offsets are stored in the
persistent config zone and survive power cycles.
Reads the WSEN-HIDS sensor as reference, then computes an offset for
each other sensor. Calibration offsets are stored in the persistent
config zone and survive power cycles.

Note: this example assumes the drivers are frozen into the firmware.
Use ``make firmware && make deploy`` to build a firmware with the
latest drivers before running this script.
"""

import gc
import sys
from time import sleep_ms

from daplink_flash.device import DaplinkFlash
from daplink_flash import DaplinkFlash
from hts221 import HTS221
from ism330dl import ISM330DL
from lis2mdl import LIS2MDL
from machine import I2C
from steami_config.device import SteamiConfig
from wsen_hids.device import WSEN_HIDS
from steami_config import SteamiConfig
from wsen_hids import WSEN_HIDS
from wsen_pads import WSEN_PADS

i2c = I2C(1)
flash = DaplinkFlash(i2c)
config = SteamiConfig(flash)
config.load()

# Read reference temperature from WSEN-HIDS (most accurate at ambient)
ref_temp = WSEN_HIDS(i2c).temperature()
ref = WSEN_HIDS(i2c)
ref_temp = ref.temperature()
print("Reference (WSEN-HIDS): {:.2f} C".format(ref_temp))
config.set_temperature_calibration("wsen_hids", gain=1.0, offset=0.0)
del WSEN_HIDS
sys.modules.pop("wsen_hids.device", None)
gc.collect()

# Calibrate each sensor one at a time to save RAM
SENSORS = [
("hts221", "hts221.device", "HTS221", "temperature"),
("wsen_pads", "wsen_pads.device", "WSEN_PADS", "temperature"),
("lis2mdl", "lis2mdl.device", "LIS2MDL", "temperature"),
("ism330dl", "ism330dl.device", "ISM330DL", "temperature"),
# Calibrate each sensor
sensors = [
("hts221", HTS221(i2c)),
("wsen_pads", WSEN_PADS(i2c)),
("lis2mdl", LIS2MDL(i2c)),
("ism330dl", ISM330DL(i2c)),
]
Comment on lines +34 to 40
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensors is built with already-constructed driver instances. This means if any sensor constructor raises (device missing / wrong address / I2C error), the script aborts before calibrating any sensors, and it also keeps all sensor instances alive at once. Consider storing (name, DriverClass) (or a small factory) and instantiating each sensor inside the loop (and del it after) so one failure doesn’t prevent calibrating the others and to keep RAM use bounded.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged. With frozen drivers, all instances share the same ROM code so RAM impact is minimal. We'll keep the current approach (all instantiated upfront) and revisit if hardware tests show RAM issues (#175).


for config_name, module, class_name, method in SENSORS:
mod = __import__(module, None, None, [class_name])
cls = getattr(mod, class_name)
sensor = cls(i2c)
if config_name == "ism330dl":
for name, sensor in sensors:
if name in ("ism330dl", "lis2mdl"):
sleep_ms(200)
raw = getattr(sensor, method)()
raw = sensor.temperature()
offset = ref_temp - raw
config.set_temperature_calibration(config_name, gain=1.0, offset=offset)
print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(config_name, raw, offset))
del sensor, cls, mod
sys.modules.pop(module, None)
gc.collect()
config.set_temperature_calibration(name, gain=1.0, offset=offset)
print(" {:10s}: {:6.2f} C -> offset {:+.2f}".format(name, raw, offset))

config.save()
print("\nCalibration saved.")

# Verify by reloading
gc.collect()
# Verify by reloading with fresh sensor instances (simulates a reboot)
config2 = SteamiConfig(flash)
config2.load()

verify_sensors = [
("hts221", HTS221(i2c)),
("wsen_pads", WSEN_PADS(i2c)),
("lis2mdl", LIS2MDL(i2c)),
("ism330dl", ISM330DL(i2c)),
]

print("\nVerification (after reload):")
for config_name, module, class_name, method in SENSORS:
mod = __import__(module, None, None, [class_name])
cls = getattr(mod, class_name)
sensor = cls(i2c)
if config_name == "ism330dl":
for name, sensor in verify_sensors:
if name in ("ism330dl", "lis2mdl"):
sleep_ms(200)
config2.apply_temperature_calibration(sensor)
print(" {:10s}: {:6.2f} C".format(config_name, getattr(sensor, method)()))
del sensor, cls, mod
sys.modules.pop(module, None)
gc.collect()
print(" {:10s}: {:6.2f} C".format(name, sensor.temperature()))
Loading