Skip to content

feat(spoolbuddy): scale auto-calibration and manual recalibrate button - #3071

Closed
khaosdoctor wants to merge 2 commits into
maziggy:mainfrom
khaosdoctor:fork-main
Closed

feat(spoolbuddy): scale auto-calibration and manual recalibrate button#3071
khaosdoctor wants to merge 2 commits into
maziggy:mainfrom
khaosdoctor:fork-main

Conversation

@khaosdoctor

Copy link
Copy Markdown

Description

Adds automatic calibration for the NAU7802 + load cell scale to correct thermal and mechanical drift, plus a manual recalibration button in the SpoolBuddy settings UI.

Related Issue

N/A

Documentation

Pick one:

  • No docs update required — reason: SpoolBuddy scale feature, no user-facing config changes

Type of Change

  • New feature (non-breaking change that adds functionality)
  • Test addition or update

Changes Made

  • Auto-zero detection: when the scale reads within ±5g and is stable for 10s (with 60s cooldown), runs NAU7802 system offset calibration (mode=2) to re-tare the scale automatically
  • Periodic AFE recalibration: runs internal offset calibration (mode=0) every 6 hours to compensate for thermal drift, safe to run with a spool on the scale
  • Manual recalibrate button: new "Recalibrate Scale" button in SpoolBuddy settings (scale tab) that triggers both calibrations via the existing diagnostic queue/poll infrastructure and displays results in a terminal-style modal
  • Tests: added test coverage for auto-zero detection, calibrate_zero, and AFE recalibration in scale_reader

Files changed

  • spoolbuddy/daemon/scale_reader.py — auto-zero detection loop, calibrate_zero(), recalibrate_afe() methods
  • spoolbuddy/daemon/main.pyrun_recalibrate_diag command handler
  • spoolbuddy/tests/test_scale_reader.py — new test file (176 lines)
  • backend/app/api/routes/spoolbuddy.py — added "recalibrate" to diagnostic validation
  • frontend/src/api/client.ts — extended diagnostic type unions
  • frontend/src/components/SpoolBuddySettings.tsx — recalibrate button + modal
  • frontend/src/components/spoolbuddy/DiagnosticModal.tsx — recalibrate type + title

Testing

  • I have tested this on my local machine
  • I have tested with my printer model: A1 Mini (SpoolBuddy with NAU7802 + load cell)

Checklist

  • My code follows the project's coding style
  • My changes generate no new warnings
  • I have tested my changes thoroughly

Additional Notes

All 90 daemon tests pass. The auto-zero only triggers when the scale is genuinely empty and stable, with a 60s cooldown to avoid rapid re-calibration loops. The periodic internal recal (mode=0) is safe with a spool on the scale since it only recalibrates the ADC's internal analog front-end.

…drift

Auto-zero uses NAU7802 system offset calibration (mode=2) when scale is
empty and stable for 10s. Periodic internal cal (mode=0) every 6h.
New "Recalibrate Scale" button in the scale tab runs calibrate_zero + recalibrate_afe
directly from the UI, reusing the existing diagnostic queue/poll infrastructure.
@khaosdoctor
khaosdoctor requested a review from maziggy as a code owner September 9, 2026 23:05
Copilot AI lite review requested due to automatic review settings September 9, 2026 23:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new recalibration diagnostic currently relies on private ScaleReader internals and reports potentially misleading success/messaging, and there are unprofessional/unclear comment markers that should be cleaned up.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds scale drift-mitigation and user-triggered calibration flows for SpoolBuddy by introducing auto-zero detection, periodic NAU7802 AFE recalibration, and a new “recalibrate” diagnostic surfaced in the UI and API.

Changes:

  • Implemented auto-zero detection and zero-point calibration logic in the scale reader, plus periodic AFE recalibration.
  • Added a new recalibrate diagnostic command handler in the daemon and wired it through backend validation + frontend client/modal UI.
  • Added test coverage for auto-zero detection, calibrate_zero(), and AFE recalibration behavior.
File summaries
File Description
spoolbuddy/daemon/scale_reader.py Adds auto-zero state machine, calibrate_zero(), and periodic recalibrate_afe() support.
spoolbuddy/daemon/main.py Runs auto-zero + periodic recal in the scale poll loop and adds run_recalibrate_diag handler.
spoolbuddy/tests/test_scale_reader.py New unit tests covering auto-zero detection, zero calibration, and AFE recalibration.
backend/app/api/routes/spoolbuddy.py Allows recalibrate as a diagnostic type for queue/result endpoints.
frontend/src/api/client.ts Extends diagnostics type unions to include recalibrate.
frontend/src/components/SpoolBuddySettings.tsx Adds “Recalibrate Scale” button and opens diagnostic modal.
frontend/src/components/spoolbuddy/DiagnosticModal.tsx Adds recalibrate type and title handling.
Review details

Suppressed comments (2)

spoolbuddy/daemon/main.py:354

  • calibrate_zero() returns a tare offset even when it falls back to software adjustment after a hardware calibration failure, so reporting "System offset cal: OK" here can be misleading. Also, the else branch (skipped (not pending)) is effectively unreachable because _auto_zero_pending is set immediately before calling calibrate_zero(). Consider using neutral wording that reflects the fallback behavior and removing the dead branch.
                        lines.append(f"    System offset cal: OK (new tare={new_tare})")
                    else:
                        lines.append("    System offset cal: skipped (not pending)")

spoolbuddy/daemon/main.py:372

  • The diagnostic success flag only reflects the internal offset calibration result (afe_ok) and ignores the outcome of the zero calibration step, so the UI may show overall success even if step [2] fails/throws. Track a separate zero_ok (even if just new_tare is not None) and combine them for the final success/exit_code.
                    success = afe_ok
  • Files reviewed: 7/7 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread spoolbuddy/daemon/main.py
Comment on lines +346 to +347
scale._auto_zero_pending = True
scale._pending_zero_avg = 0.0
@khaosdoctor khaosdoctor closed this Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants