feat: add ESP32 BTS7960 motor controller with web interface
- Implement MotorController class with PWM speed control (0-100%) - Add bidirectional control (forward/reverse) via LEDC PWM at 20kHz - Include optional current sensing and stall detection - Create responsive web UI with speed slider and direction buttons - Configure WiFi with static IP (10.81.2.185) - Use built-in WebServer library for ESP32 Arduino 3.x compatibility
This commit is contained in:
42
include/config.h
Normal file
42
include/config.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
// WiFi Configuration
|
||||
#define WIFI_SSID "tami"
|
||||
#define WIFI_PASSWORD ""
|
||||
|
||||
// Static IP Configuration
|
||||
#define STATIC_IP IPAddress(10, 81, 2, 185)
|
||||
#define GATEWAY IPAddress(10, 81, 2, 1)
|
||||
#define SUBNET IPAddress(255, 255, 255, 0)
|
||||
#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 L_IS_PIN 35 // Left Current Sense (ADC input only)
|
||||
|
||||
// PWM Configuration
|
||||
#define PWM_FREQ 20000 // 20kHz - reduces motor noise
|
||||
#define PWM_RESOLUTION 8 // 8-bit resolution (0-255)
|
||||
#define PWM_CHANNEL_R 0 // LEDC channel for RPWM
|
||||
#define PWM_CHANNEL_L 1 // LEDC channel for LPWM
|
||||
|
||||
// Current Sense Configuration
|
||||
// BTS7960 current sense ratio: 8500:1 (kilo-amps)
|
||||
// With 1kΩ resistor on IS pin: V = I_motor / 8500
|
||||
// ESP32 ADC: 12-bit (0-4095), 0-3.3V
|
||||
#define CURRENT_SENSE_RATIO 8500.0f // Amps to sense current ratio
|
||||
#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 STALL_CURRENT_THRESHOLD 5.0f // Current (amps) that indicates stall
|
||||
#define STALL_DETECT_TIME_MS 500 // Time to confirm stall (ms)
|
||||
|
||||
// Web Server
|
||||
#define HTTP_PORT 80
|
||||
|
||||
#endif
|
||||
41
include/motor.h
Normal file
41
include/motor.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef MOTOR_H
|
||||
#define MOTOR_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
|
||||
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 stall detection
|
||||
|
||||
int getSpeed();
|
||||
int getDirection();
|
||||
float getCurrentRight(); // Current in amps (forward direction)
|
||||
float getCurrentLeft(); // Current in amps (reverse direction)
|
||||
float getCurrentActive(); // Current from active direction
|
||||
bool isStalled(); // True if stall detected
|
||||
|
||||
// Callback for stall events
|
||||
void setStallCallback(void (*callback)(float current));
|
||||
|
||||
private:
|
||||
int _speed = 0;
|
||||
int _direction = 0;
|
||||
float _currentRight = 0;
|
||||
float _currentLeft = 0;
|
||||
bool _stalled = false;
|
||||
unsigned long _stallStartTime = 0;
|
||||
void (*_stallCallback)(float current) = nullptr;
|
||||
|
||||
void applyMotorState();
|
||||
float readCurrentSense(int pin);
|
||||
void checkStall();
|
||||
};
|
||||
|
||||
extern MotorController motor;
|
||||
|
||||
#endif
|
||||
7
include/webserver.h
Normal file
7
include/webserver.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef WEBSERVER_H
|
||||
#define WEBSERVER_H
|
||||
|
||||
void setupWebServer();
|
||||
void handleWebServer();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user