added proper kinematics

This commit is contained in:
PROFERIS - Mi³osz Stocki
2026-07-06 15:23:35 +02:00
parent 67b4eda6aa
commit ee8a2d97ed
5 changed files with 810 additions and 100 deletions
+96 -1
View File
@@ -50,6 +50,20 @@
#define DEFAULT_SPEED 600.0f // steps/sec
#define DEFAULT_ACCEL 100.0f // steps/sec²
// ─── Homing configuration ─────────────────────────────────────────
// TUNE these after first hardware test.
// SEEK_DIR: +1 = CW (positive steps), -1 = CCW toward stop
// SEEK_STEPS: must exceed maximum arm travel (~240° = 1365 steps)
// OP_OFFSET: steps from the mechanical stop to the operating home
// (derived from original PositionControl.cpp homing offsets)
#define M1_HOME_DIR (-1) // ← TUNE: flip to +1 if M1 moves wrong way
#define M2_HOME_DIR (+1) // ← TUNE: flip to -1 if M2 moves wrong way
#define HOME_SEEK_STEPS 1600 // ← TUNE: > max travel; 1600 ≈ 281° of a rev
#define HOME_SPEED 180.0f // ← TUNE: slow seek to avoid missing steps
#define HOME_ACCEL 50.0f // ← TUNE: gentle accel during seek
#define M1_OP_OFFSET 550 // ← TUNE: steps from M1 stop → operating home
#define M2_OP_OFFSET -530 // ← TUNE: steps from M2 stop → operating home
// ─── BLE UUIDs ────────────────────────────────────────────────────
#define SERVICE_UUID "a0b1c2d3-e4f5-6789-abcd-ef0123456700"
#define CMD_UUID "a0b1c2d3-e4f5-6789-abcd-ef0123456701"
@@ -73,6 +87,7 @@ const unsigned long NOTIFY_INTERVAL_MS = 200;
// ─── Forward declarations ─────────────────────────────────────────
void parseCommand(const String& cmd);
void sendPosition();
void runHoming();
// ─── BLE Server Callbacks ─────────────────────────────────────────
class ServerCallbacks : public BLEServerCallbacks {
@@ -130,7 +145,13 @@ void parseCommand(const String& cmd) {
Serial.printf("[CFG] Acceleration → %.0f steps/sec²\n", acc);
return;
}
// HOME1
// HOME full stall-seek homing sequence
if (cmd == "HOME") {
Serial.println("[HOME] Received HOME command");
runHoming();
return;
}
// HOME1 / HOME2 manual zero (soft zero, no motion)
if (cmd == "HOME1") {
stepper1.setCurrentPosition(0);
Serial.println("[S1] Zeroed");
@@ -152,6 +173,80 @@ void parseCommand(const String& cmd) {
Serial.printf("[CMD] Unknown command: '%s'\n", cmd.c_str());
}
// ─── Stall-based homing sequence ─────────────────────────────────
/**
* Drives each motor slowly into its mechanical hard stop (the arm
* folds against the motor housing). The 28BYJ-48 stalls harmlessly
* for a fraction of a second at the stop. After both stops are
* found, the arm backs off to the operating home position and both
* step counters are zeroed.
*
* This is a BLOCKING call — BLE is not serviced during homing
* (~15-20 s total). The browser shows "Homing…" until the
* HOMED notify arrives.
*
* Tune M1_HOME_DIR, M2_HOME_DIR, HOME_SEEK_STEPS, and OP_OFFSETs
* after the first hardware test.
*/
void runHoming() {
Serial.println("[HOME] Starting homing sequence…");
// Reduce speed for the seek phase
stepper1.setMaxSpeed(HOME_SPEED);
stepper1.setAcceleration(HOME_ACCEL);
stepper2.setMaxSpeed(HOME_SPEED);
stepper2.setAcceleration(HOME_ACCEL);
// ── Seek M1 to its mechanical stop ───────────────────────────
Serial.println("[HOME] Seeking M1 stop…");
stepper1.move(M1_HOME_DIR * HOME_SEEK_STEPS);
while (stepper1.isRunning()) {
stepper1.run();
stepper2.run(); // keep M2 coils energised during M1 seek
}
stepper1.setCurrentPosition(0);
Serial.println("[HOME] M1 stop found → zeroed");
delay(200); // brief pause before M2 seek
// ── Seek M2 to its mechanical stop ───────────────────────────
Serial.println("[HOME] Seeking M2 stop…");
stepper2.move(M2_HOME_DIR * HOME_SEEK_STEPS);
while (stepper2.isRunning()) {
stepper1.run();
stepper2.run();
}
stepper2.setCurrentPosition(0);
Serial.println("[HOME] M2 stop found → zeroed");
delay(200);
// ── Move to operating home (restore normal speed) ─────────────
stepper1.setMaxSpeed(DEFAULT_SPEED);
stepper1.setAcceleration(DEFAULT_ACCEL);
stepper2.setMaxSpeed(DEFAULT_SPEED);
stepper2.setAcceleration(DEFAULT_ACCEL);
Serial.println("[HOME] Moving to operating home…");
stepper1.moveTo(M1_OP_OFFSET);
stepper2.moveTo(M2_OP_OFFSET);
while (stepper1.isRunning() || stepper2.isRunning()) {
stepper1.run();
stepper2.run();
}
// Declare operating home as the new step zero
stepper1.setCurrentPosition(0);
stepper2.setCurrentPosition(0);
Serial.println("[HOME] Homing complete — at operating home (0, 0)");
// Notify the browser so it can sync its step counters
if (bleConnected && pStatusChar) {
pStatusChar->setValue("HOMED");
pStatusChar->notify();
}
delay(50);
sendPosition(); // immediately follow with P:0,0
}
// ─── Send current positions via BLE NOTIFY ────────────────────────
void sendPosition() {
if (!bleConnected || !pStatusChar) return;