This streamlines manual dual-motor operation by using spring-centered signed sliders and synchronized drive modes while keeping live current monitoring in the UI and API.
54 lines
1.4 KiB
C++
54 lines
1.4 KiB
C++
#ifndef MOTOR_H
|
|
#define MOTOR_H
|
|
|
|
#include <Arduino.h>
|
|
#include "config.h"
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
// Static flag to track if enable pins are already configured
|
|
static bool _enablePinsConfigured;
|
|
|
|
void applyMotorState();
|
|
float readCurrentSense(int pin);
|
|
void calibrateCurrentOffset();
|
|
};
|
|
|
|
// Two motor controller instances
|
|
extern MotorController motor1;
|
|
extern MotorController motor2;
|
|
|
|
// Legacy alias for backwards compatibility
|
|
#define motor motor1
|
|
|
|
#endif
|