- Add STALL_THRESHOLD (8A) and STALL_CONFIRM_SAMPLES (3) config - Add 500ms stabilization delay after direction change to prevent false positives - Add stall warning banner in web UI - Stop motor and pingpong when stall detected Algorithm: If active current > 8A for 3 consecutive samples (300ms), and motor has been running for at least 500ms, trigger stall callback.
76 lines
2.1 KiB
C++
76 lines
2.1 KiB
C++
#ifndef MOTOR_H
|
|
#define MOTOR_H
|
|
|
|
#include <Arduino.h>
|
|
#include "config.h"
|
|
|
|
// Stall callback function type
|
|
typedef void (*StallCallback)();
|
|
|
|
class MotorController {
|
|
public:
|
|
void begin();
|
|
void setSpeed(int speed); // 0-100 percentage
|
|
void setDirection(int dir); // -1=reverse, 0=stop, 1=forward
|
|
void stop();
|
|
void update(); // Call in loop() for current monitoring
|
|
|
|
int getSpeed();
|
|
int getDirection();
|
|
float getCurrentRight(); // Current in amps (forward direction)
|
|
float getCurrentLeft(); // Current in amps (reverse direction)
|
|
float getCurrentActive(); // Current from active direction
|
|
|
|
// Stall detection
|
|
bool isStalled();
|
|
void setStallCallback(StallCallback callback);
|
|
void resetStallDetection();
|
|
|
|
// Pingpong mode (time-based only)
|
|
void startPingpong(int speed, int timeMs, int speedRandomPercent, int timeRandomPercent);
|
|
void stopPingpong();
|
|
bool isPingpongActive();
|
|
int getPingpongSpeed();
|
|
int getPingpongTime();
|
|
int getPingpongSpeedRandom();
|
|
int getPingpongTimeRandom();
|
|
|
|
private:
|
|
int _speed = 0;
|
|
int _direction = 0;
|
|
float _currentRight = 0;
|
|
float _currentLeft = 0;
|
|
|
|
// ADC zero-current offset (calibrated at startup)
|
|
int _adcOffsetRight = 0;
|
|
int _adcOffsetLeft = 0;
|
|
|
|
// Pingpong state
|
|
bool _pingpongActive = false;
|
|
int _pingpongBaseSpeed = 50;
|
|
int _pingpongBaseTime = 2000;
|
|
int _pingpongSpeedRandomPercent = 0;
|
|
int _pingpongTimeRandomPercent = 0;
|
|
int _pingpongCurrentSpeed = 50;
|
|
int _pingpongCurrentTime = 2000;
|
|
unsigned long _pingpongLastSwitch = 0;
|
|
int _pingpongDirection = 1;
|
|
|
|
// Stall detection state
|
|
bool _stalled = false;
|
|
int _stallConfirmCount = 0;
|
|
StallCallback _stallCallback = nullptr;
|
|
unsigned long _lastDirectionChangeTime = 0;
|
|
|
|
void applyMotorState();
|
|
float readCurrentSense(int pin);
|
|
void calibrateCurrentOffset();
|
|
void updatePingpong();
|
|
int applyRandomness(int baseValue, int randomPercent);
|
|
void checkStall();
|
|
};
|
|
|
|
extern MotorController motor;
|
|
|
|
#endif
|