Fix stall oscillation loop in pingpong mode

When pingpong detected a stall and switched direction, only _stalled
and _stallStartTime were reset, leaving _stallCandidateCount and
_motorStartTime unchanged. This caused motor inrush current after
direction change to immediately trigger another stall, creating an
infinite oscillation loop.

Now calls resetStallDetection() which properly resets all stall state
including triggering the STALL_STABILIZE_MS grace period to ignore
inrush current.
This commit is contained in:
devdesk
2026-02-05 20:49:59 +02:00
parent 568339367e
commit 241d1ae457
4 changed files with 93 additions and 21 deletions

View File

@@ -12,13 +12,14 @@
#define DNS IPAddress(10, 81, 2, 1)
// BTS7960 Pin Definitions
#define RPWM_PIN 25 // Right PWM (Forward)
#define LPWM_PIN 26 // Left PWM (Reverse)
#define R_EN_PIN 27 // Right Enable
#define L_EN_PIN 14 // Left Enable
#define R_IS_PIN 34 // Right Current Sense (ADC input only)
#define LPWM_PIN 26 // Left PWM (Reverse)
#define L_IS_PIN 35 // Left Current Sense (ADC input only)
#define R_EN_PIN 27 // Right Enable
#define RPWM_PIN 25 // Right PWM (Forward)
#define R_IS_PIN 34 // Right Current Sense (ADC input only)
// PWM Configuration
#define PWM_FREQ 20000 // 20kHz - reduces motor noise
#define PWM_RESOLUTION 8 // 8-bit resolution (0-255)
@@ -34,16 +35,25 @@
#define SENSE_RESISTOR 1000.0f // 1kΩ sense resistor (ohms)
#define ADC_MAX 4095.0f // 12-bit ADC max value
#define ADC_VREF 3.3f // ADC reference voltage
#define CURRENT_CALIBRATION 0.33f // Calibration factor (measured current / reported current)
#define CURRENT_CALIBRATION 1.0f // Calibration factor (measured current / reported current)
// Stall Detection Configuration (delta-based)
// Detects sudden current spikes above the rolling average
#define STALL_DELTA_THRESHOLD 2.0f // Current spike above average that indicates stall (amps)
#define STALL_DELTA_THRESHOLD 1.2f // Current spike above average that indicates stall (amps)
#define STALL_EMA_ALPHA 0.1f // EMA smoothing factor (0.1 = slow, 0.5 = fast response)
#define STALL_STABILIZE_MS 500 // Ignore stalls for this long after motor starts/changes
#define STALL_CONFIRM_MS 100 // Current must exceed threshold for this long to confirm stall
#define STALL_EMA_BASELINE 2.0f // Expected running current baseline for EMA seeding (amps)
#define STALL_STABILIZE_MS 300 // Ignore stalls for this long after motor starts/changes
#define STALL_CONFIRM_MS 50 // Current must exceed threshold for this long to confirm stall
#define DISABLE_STALL_DETECT false // Set to true to disable stall detection
// Under-current stall detection: motor commanded but drawing no current
#define STALL_UNDERCURRENT_THRESHOLD 0.5f // If current below this for too long = stall (amps)
#define STALL_UNDERCURRENT_MS 1000 // Under-current must persist this long to confirm stall
// Repeated spike stall detection: catches oscillating stall (driver protection cycling)
#define STALL_CANDIDATE_COUNT 5 // Number of stall candidates in window to confirm stall
#define STALL_CANDIDATE_WINDOW_MS 2000 // Time window for counting stall candidates
// Web Server
#define HTTP_PORT 80

View File

@@ -50,6 +50,13 @@ private:
unsigned long _motorStartTime = 0; // When motor started (for stabilization period)
bool _emaInitialized = false; // EMA needs seeding on first reading
// Under-current stall detection
unsigned long _undercurrentStartTime = 0; // When under-current condition started
// Repeated spike detection (oscillating stall pattern)
int _stallCandidateCount = 0; // Number of stall candidates in current window
unsigned long _stallCandidateWindowStart = 0; // When current counting window started
// Pingpong state
bool _pingpongActive = false;
int _pingpongBaseSpeed = 50;