scale_reader.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Scale reader wrapper with stability detection and calibration."""
  2. import logging
  3. import time
  4. from collections import deque
  5. logger = logging.getLogger(__name__)
  6. MOVING_AVG_SIZE = 5
  7. class ScaleReader:
  8. def __init__(self, tare_offset: int = 0, calibration_factor: float = 1.0):
  9. from spoolbuddy.scale_diag import NAU7802
  10. self._scale = NAU7802()
  11. self._tare_offset = tare_offset
  12. self._calibration_factor = calibration_factor
  13. self._samples: deque[float] = deque(maxlen=MOVING_AVG_SIZE)
  14. self._stability_history: deque[tuple[float, float]] = deque(maxlen=20)
  15. self._ok = False
  16. self._last_raw = 0
  17. try:
  18. self._scale.init()
  19. self._ok = True
  20. logger.info("Scale initialized (tare=%d, cal=%.6f)", tare_offset, calibration_factor)
  21. except Exception as e:
  22. logger.error("Scale init failed: %s", e)
  23. @property
  24. def ok(self) -> bool:
  25. return self._ok
  26. @property
  27. def last_raw(self) -> int:
  28. return self._last_raw
  29. def close(self):
  30. try:
  31. self._scale.close()
  32. except Exception:
  33. pass
  34. def update_calibration(self, tare_offset: int, calibration_factor: float):
  35. self._tare_offset = tare_offset
  36. self._calibration_factor = calibration_factor
  37. logger.info("Calibration updated: tare=%d, factor=%.6f", tare_offset, calibration_factor)
  38. def tare(self):
  39. """Set current raw reading as tare offset."""
  40. if self._last_raw:
  41. self._tare_offset = self._last_raw
  42. self._samples.clear()
  43. self._stability_history.clear()
  44. logger.info("Tared at raw=%d", self._tare_offset)
  45. return self._tare_offset
  46. def read(self) -> tuple[float, bool, int] | None:
  47. """Read current weight. Returns (grams, stable, raw_adc) or None."""
  48. try:
  49. if not self._scale.data_ready():
  50. return None
  51. raw = self._scale.read_raw()
  52. self._last_raw = raw
  53. self._ok = True
  54. grams = (raw - self._tare_offset) * self._calibration_factor
  55. self._samples.append(grams)
  56. # Moving average
  57. avg_grams = sum(self._samples) / len(self._samples)
  58. # Stability: track readings over time
  59. now = time.monotonic()
  60. self._stability_history.append((now, avg_grams))
  61. # Stable if all readings within 1s window are within 2g of each other
  62. stable = False
  63. if len(self._stability_history) >= 5:
  64. cutoff = now - 1.0
  65. recent = [g for t, g in self._stability_history if t >= cutoff]
  66. if len(recent) >= 3:
  67. spread = max(recent) - min(recent)
  68. stable = spread < 2.0
  69. return round(avg_grams, 1), stable, raw
  70. except Exception as e:
  71. logger.debug("Scale read error: %s", e)
  72. self._ok = False
  73. return None