Implement proper stepper idling. Should close #3
This commit is contained in:
+30
-5
@@ -15,6 +15,7 @@
|
||||
* HOME1 → Home motor 1 (Right)
|
||||
* HOME2 → Home motor 2 (Left)
|
||||
* HOMEALL → Home both motors simultaneously
|
||||
* DISABLE → Disable steppers (un-hold) and require rehoming
|
||||
* POS → Request current positions (triggers NOTIFY)
|
||||
*
|
||||
* BLE Status Characteristic (NOTIFY):
|
||||
@@ -72,6 +73,11 @@ bool bleConnected = false;
|
||||
unsigned long lastNotify = 0;
|
||||
const unsigned long NOTIFY_INTERVAL_MS = 200;
|
||||
|
||||
// ─── Idle timeout tracker ─────────────────────────────────────────
|
||||
unsigned long lastMotorActive = 0;
|
||||
const unsigned long IDLE_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes
|
||||
bool motorsDisabled = true; // Assume disabled at startup until moved
|
||||
|
||||
// ─── Forward declarations ─────────────────────────────────────────
|
||||
void parseCommand(const String &cmd);
|
||||
void sendPosition();
|
||||
@@ -193,6 +199,14 @@ void parseCommand(const String &cmd) {
|
||||
sendPosition();
|
||||
return;
|
||||
}
|
||||
// DISABLE
|
||||
if (cmd == "DISABLE") {
|
||||
stepper1.disableOutputs();
|
||||
stepper2.disableOutputs();
|
||||
motorsDisabled = true;
|
||||
Serial.println("[SYS] Steppers manually disabled.");
|
||||
return;
|
||||
}
|
||||
// POS – explicit position request
|
||||
if (cmd == "POS") {
|
||||
sendPosition();
|
||||
@@ -280,6 +294,22 @@ void loop() {
|
||||
stepper1.run();
|
||||
stepper2.run();
|
||||
|
||||
bool moving = stepper1.isRunning() || stepper2.isRunning() || homingState != HOME_IDLE;
|
||||
|
||||
if (moving) {
|
||||
lastMotorActive = millis();
|
||||
motorsDisabled = false;
|
||||
} else if (!motorsDisabled && (millis() - lastMotorActive > IDLE_TIMEOUT_MS)) {
|
||||
stepper1.disableOutputs();
|
||||
stepper2.disableOutputs();
|
||||
motorsDisabled = true;
|
||||
Serial.println("[SYS] Idle timeout reached. Steppers disabled.");
|
||||
if (bleConnected && pStatusChar) {
|
||||
pStatusChar->setValue("SYS:IDLE_TIMEOUT");
|
||||
pStatusChar->notify();
|
||||
}
|
||||
}
|
||||
|
||||
if (homingState == HOME_ALL_PHASE1) {
|
||||
if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0) {
|
||||
stepper1.setCurrentPosition(1024);
|
||||
@@ -291,8 +321,6 @@ void loop() {
|
||||
}
|
||||
} else if (homingState == HOME_ALL_PHASE2) {
|
||||
if (stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0) {
|
||||
stepper1.disableOutputs();
|
||||
stepper2.disableOutputs();
|
||||
stepper1.setMaxSpeed(currentMaxSpeed);
|
||||
stepper2.setMaxSpeed(currentMaxSpeed);
|
||||
stepper1.setAcceleration(currentAccel);
|
||||
@@ -309,7 +337,6 @@ void loop() {
|
||||
}
|
||||
} else if (homingState == HOME_1_PHASE2) {
|
||||
if (stepper1.distanceToGo() == 0) {
|
||||
stepper1.disableOutputs();
|
||||
stepper1.setMaxSpeed(currentMaxSpeed);
|
||||
stepper1.setAcceleration(currentAccel);
|
||||
homingState = HOME_IDLE;
|
||||
@@ -322,7 +349,6 @@ void loop() {
|
||||
}
|
||||
} else if (homingState == HOME_2_PHASE2) {
|
||||
if (stepper2.distanceToGo() == 0) {
|
||||
stepper2.disableOutputs();
|
||||
stepper2.setMaxSpeed(currentMaxSpeed);
|
||||
stepper2.setAcceleration(currentAccel);
|
||||
homingState = HOME_IDLE;
|
||||
@@ -332,7 +358,6 @@ void loop() {
|
||||
// Periodic position notify while motors are moving
|
||||
if (bleConnected) {
|
||||
unsigned long now = millis();
|
||||
bool moving = stepper1.isRunning() || stepper2.isRunning() || homingState != HOME_IDLE;
|
||||
if (moving && (now - lastNotify >= NOTIFY_INTERVAL_MS)) {
|
||||
lastNotify = now;
|
||||
sendPosition();
|
||||
|
||||
Reference in New Issue
Block a user