From 80ce8ff63d993e97186a7530f52407605b88399e Mon Sep 17 00:00:00 2001 From: devdesk Date: Thu, 5 Feb 2026 18:23:10 +0200 Subject: [PATCH] Fix EMA chasing spike during detection window Freeze EMA updates when any spike candidate is detected (delta > threshold), not just after stall is confirmed. This prevents EMA from rising to meet the spike before the confirmation time elapses. --- src/motor.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/motor.cpp b/src/motor.cpp index a89555b..46fed8c 100644 --- a/src/motor.cpp +++ b/src/motor.cpp @@ -347,13 +347,16 @@ void MotorController::checkStall() { // Calculate delta from average float delta = activeCurrent - _currentEMA; - // Update EMA (only when not in stall to prevent average rising during stall) - if (!_stalled) { + // Check if current spike exceeds delta threshold + bool spikeDetected = (delta > STALL_DELTA_THRESHOLD); + + // Update EMA only when no spike is being investigated + // This prevents EMA from "chasing" up to the spike during detection window + if (!spikeDetected && !_stalled) { _currentEMA = (STALL_EMA_ALPHA * activeCurrent) + ((1.0f - STALL_EMA_ALPHA) * _currentEMA); } - // Check if current spike exceeds delta threshold - if (delta > STALL_DELTA_THRESHOLD) { + if (spikeDetected) { if (_stallStartTime == 0) { // Start timing potential stall _stallStartTime = now;