- Refactor MotorController to parameterized class with MotorPins struct - Add motor1 and motor2 instances with shared enable pins (GPIO 14, 27) - Motor 2 uses GPIO 32/33 for PWM, GPIO 36/39 for current sense - Update web UI with side-by-side dual motor control panels - Add per-motor API endpoints (/motor1/*, /motor2/*) - Add emergency stop button for both motors - Legacy endpoints map to motor1 for backwards compatibility - Update readme and AGENTS.md documentation
69 lines
1.8 KiB
C++
69 lines
1.8 KiB
C++
#ifndef MOTOR_H
|
|
#define MOTOR_H
|
|
|
|
#include <Arduino.h>
|
|
#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
|