Replace stall/homing flow with signed slider drive control
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.
This commit is contained in:
115
src/main.cpp
115
src/main.cpp
@@ -4,118 +4,6 @@
|
||||
#include "motor.h"
|
||||
#include "webserver.h"
|
||||
|
||||
// Coordinated movement state
|
||||
enum CoordinatedState {
|
||||
COORD_IDLE = 0,
|
||||
COORD_MOVING_REV = 1, // Homing - moving to reverse end
|
||||
COORD_MOVING_FWD = 2 // Moving to forward end
|
||||
};
|
||||
|
||||
// Global coordinated movement state
|
||||
static CoordinatedState coordState = COORD_IDLE;
|
||||
static bool motor1Done = false;
|
||||
static bool motor2Done = false;
|
||||
static int coordSpeed = 50; // Default coordinated movement speed
|
||||
|
||||
// Get current coordinated movement state
|
||||
int getCoordinatedState() {
|
||||
return (int)coordState;
|
||||
}
|
||||
|
||||
// Check if coordinated movement is complete
|
||||
bool isCoordinatedMovementDone() {
|
||||
return motor1Done && motor2Done;
|
||||
}
|
||||
|
||||
// Called when motor 1 stall is detected
|
||||
void onMotor1Stall() {
|
||||
Serial.println("Motor1 stall callback triggered - stopping motor!");
|
||||
motor1.stop();
|
||||
|
||||
// Mark motor1 as done if in coordinated movement
|
||||
if (coordState != COORD_IDLE) {
|
||||
motor1Done = true;
|
||||
Serial.println("Motor1 reached end position (stall)");
|
||||
|
||||
if (isCoordinatedMovementDone()) {
|
||||
Serial.println("Coordinated movement complete - both motors at end position");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Called when motor 2 stall is detected
|
||||
void onMotor2Stall() {
|
||||
Serial.println("Motor2 stall callback triggered - stopping motor!");
|
||||
motor2.stop();
|
||||
|
||||
// Mark motor2 as done if in coordinated movement
|
||||
if (coordState != COORD_IDLE) {
|
||||
motor2Done = true;
|
||||
Serial.println("Motor2 reached end position (stall)");
|
||||
|
||||
if (isCoordinatedMovementDone()) {
|
||||
Serial.println("Coordinated movement complete - both motors at end position");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start coordinated movement in specified direction
|
||||
// dir: -1 = reverse (homing), 1 = forward
|
||||
void startCoordinatedMovement(int dir, int speed) {
|
||||
coordSpeed = speed;
|
||||
motor1Done = false;
|
||||
motor2Done = false;
|
||||
|
||||
if (dir < 0) {
|
||||
coordState = COORD_MOVING_REV;
|
||||
Serial.printf("Starting coordinated REVERSE movement at %d%% speed\n", speed);
|
||||
} else {
|
||||
coordState = COORD_MOVING_FWD;
|
||||
Serial.printf("Starting coordinated FORWARD movement at %d%% speed\n", speed);
|
||||
}
|
||||
|
||||
// Start both motors
|
||||
motor1.setSpeed(speed);
|
||||
motor1.setDirection(dir);
|
||||
|
||||
motor2.setSpeed(speed);
|
||||
motor2.setDirection(dir);
|
||||
}
|
||||
|
||||
// Toggle coordinated movement (for button press)
|
||||
// Returns: new state (0=idle, 1=moving_rev, 2=moving_fwd)
|
||||
int toggleCoordinatedMovement(int speed) {
|
||||
if (coordState == COORD_IDLE) {
|
||||
// First press: start moving reverse (homing)
|
||||
startCoordinatedMovement(-1, speed);
|
||||
return COORD_MOVING_REV;
|
||||
} else if (coordState == COORD_MOVING_REV) {
|
||||
// If currently moving rev or completed rev, switch to forward
|
||||
// Stop any still-moving motors first
|
||||
if (!motor1Done) motor1.stop();
|
||||
if (!motor2Done) motor2.stop();
|
||||
startCoordinatedMovement(1, speed);
|
||||
return COORD_MOVING_FWD;
|
||||
} else {
|
||||
// Currently moving fwd or completed fwd, switch to reverse
|
||||
// Stop any still-moving motors first
|
||||
if (!motor1Done) motor1.stop();
|
||||
if (!motor2Done) motor2.stop();
|
||||
startCoordinatedMovement(-1, speed);
|
||||
return COORD_MOVING_REV;
|
||||
}
|
||||
}
|
||||
|
||||
// Stop coordinated movement
|
||||
void stopCoordinatedMovement() {
|
||||
coordState = COORD_IDLE;
|
||||
motor1Done = false;
|
||||
motor2Done = false;
|
||||
motor1.stop();
|
||||
motor2.stop();
|
||||
Serial.println("Coordinated movement stopped");
|
||||
}
|
||||
|
||||
void setupWiFi() {
|
||||
Serial.println("Connecting to WiFi...");
|
||||
|
||||
@@ -156,10 +44,7 @@ void setup() {
|
||||
|
||||
// Initialize motor controllers
|
||||
motor1.begin();
|
||||
motor1.setStallCallback(onMotor1Stall);
|
||||
|
||||
motor2.begin();
|
||||
motor2.setStallCallback(onMotor2Stall);
|
||||
|
||||
// Connect to WiFi
|
||||
setupWiFi();
|
||||
|
||||
Reference in New Issue
Block a user