-
Notifications
You must be signed in to change notification settings - Fork 1
fix(steami_config): Simplify calibrate_temperature for frozen drivers. #249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)), | ||
| ] | ||
|
|
||
| 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())) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sensorsis 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 (anddelit after) so one failure doesn’t prevent calibrating the others and to keep RAM use bounded.There was a problem hiding this comment.
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).