#ifndef MOTOR_H #define MOTOR_H #include #include "config.h" // Stall callback function type typedef void (*StallCallback)(); class MotorController { public: // Constructor with pin configuration and motor name MotorController(const MotorPins& pins, const char* name); 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 const char* getName(); // Get motor name for logging // Stall detection bool isStalled(); void setStallCallback(StallCallback callback); void resetStallDetection(); private: MotorPins _pins; const char* _name; 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; // Stall detection state bool _stalled = false; int _stallConfirmCount = 0; StallCallback _stallCallback = nullptr; unsigned long _lastDirectionChangeTime = 0; // Static flag to track if enable pins are already configured static bool _enablePinsConfigured; void applyMotorState(); float readCurrentSense(int pin); void calibrateCurrentOffset(); void checkStall(); }; // Two motor controller instances extern MotorController motor1; extern MotorController motor2; // Legacy alias for backwards compatibility #define motor motor1 #endif