Browse Source

TMC SPI Endstops and Improved Sensorless Homing (#14044)

teemuatlut 4 years ago
parent
commit
d4974ea719

+ 17
- 7
Marlin/Configuration_adv.h View File

@@ -1851,17 +1851,25 @@
1851 1851
   #define E5_HYBRID_THRESHOLD     30
1852 1852
 
1853 1853
   /**
1854
+   * Use StallGuard2 to home / probe X, Y, Z.
1855
+   *
1854 1856
    * TMC2130, TMC2160, TMC2209, TMC2660, TMC5130, and TMC5160 only
1855
-   * Use StallGuard2 to sense an obstacle and trigger an endstop.
1856 1857
    * Connect the stepper driver's DIAG1 pin to the X/Y endstop pin.
1857 1858
    * X, Y, and Z homing will always be done in spreadCycle mode.
1858 1859
    *
1859
-   * X/Y/Z_STALL_SENSITIVITY is used for tuning the trigger sensitivity.
1860
-   * Higher values make the system LESS sensitive.
1861
-   * Lower value make the system MORE sensitive.
1862
-   * Too low values can lead to false positives, while too high values will collide the axis without triggering.
1863
-   * It is advised to set X/Y/Z_HOME_BUMP_MM to 0.
1864
-   * M914 X/Y/Z to live tune the setting
1860
+   * X/Y/Z_STALL_SENSITIVITY is used to tune the trigger sensitivity.
1861
+   * Use M914 X Y Z to live-adjust the sensitivity.
1862
+   *  Higher: LESS sensitive. (Too high => failure to trigger)
1863
+   *   Lower: MORE sensitive. (Too low  => false positives)
1864
+   *
1865
+   * It is recommended to set [XYZ]_HOME_BUMP_MM to 0.
1866
+   *
1867
+   * SPI_ENDSTOPS  *** Beta feature! *** TMC2130 Only ***
1868
+   * Poll the driver through SPI to determine load when homing.
1869
+   * Removes the need for a wire from DIAG1 to an endstop pin.
1870
+   *
1871
+   * IMPROVE_HOMING_RELIABILITY tunes acceleration and jerk when
1872
+   * homing and adds a guard period for endstop triggering.
1865 1873
    */
1866 1874
   //#define SENSORLESS_HOMING // StallGuard capable drivers only
1867 1875
 
@@ -1878,6 +1886,8 @@
1878 1886
     #define X_STALL_SENSITIVITY  8
1879 1887
     #define Y_STALL_SENSITIVITY  8
1880 1888
     //#define Z_STALL_SENSITIVITY  8
1889
+    //#define SPI_ENDSTOPS              // TMC2130 only
1890
+    //#define IMPROVE_HOMING_RELIABILITY
1881 1891
   #endif
1882 1892
 
1883 1893
   /**

+ 7
- 0
Marlin/src/Marlin.cpp View File

@@ -659,6 +659,13 @@ void idle(
659 659
     bool no_stepper_sleep/*=false*/
660 660
   #endif
661 661
 ) {
662
+
663
+  #if ENABLED(SPI_ENDSTOPS)
664
+    if (endstops.tmc_spi_homing.any && ELAPSED(millis(), sg_guard_period))
665
+      for (uint8_t i = 4; i--;) // Read SGT 4 times per idle loop
666
+        if (endstops.tmc_spi_homing_check()) break;
667
+  #endif
668
+
662 669
   #if ENABLED(MAX7219_DEBUG)
663 670
     max7219.idle_tasks();
664 671
   #endif

+ 52
- 1
Marlin/src/feature/tmc_util.h View File

@@ -140,6 +140,9 @@ class TMCMarlin : public TMC, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
140 140
           this->stored.homing_thrs = sgt_val;
141 141
         #endif
142 142
       }
143
+      #if ENABLED(SPI_ENDSTOPS)
144
+        bool test_stall_status();
145
+      #endif
143 146
     #endif
144 147
 
145 148
     #if HAS_LCD_MENU
@@ -355,9 +358,22 @@ void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z
355 358
  * Defined here because of limitations with templates and headers.
356 359
  */
357 360
 #if USE_SENSORLESS
361
+
358 362
   // Track enabled status of stealthChop and only re-enable where applicable
359 363
   struct sensorless_t { bool x, y, z, x2, y2, z2, z3; };
360 364
 
365
+  #if ENABLED(IMPROVE_HOMING_RELIABILITY)
366
+    extern millis_t sg_guard_period;
367
+    constexpr uint16_t default_sg_guard_duration = 400;
368
+
369
+    struct slow_homing_t {
370
+      struct { uint32_t x, y; } acceleration;
371
+      #if HAS_CLASSIC_JERK
372
+        struct { float x, y; } jerk;
373
+      #endif
374
+    };
375
+  #endif
376
+
361 377
   bool tmc_enable_stallguard(TMC2130Stepper &st);
362 378
   void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
363 379
 
@@ -366,7 +382,42 @@ void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z
366 382
 
367 383
   bool tmc_enable_stallguard(TMC2660Stepper);
368 384
   void tmc_disable_stallguard(TMC2660Stepper, const bool);
369
-#endif
385
+
386
+  #if ENABLED(SPI_ENDSTOPS)
387
+
388
+    template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
389
+    bool TMCMarlin<TMC, AXIS_LETTER, DRIVER_ID, AXIS_ID>::test_stall_status() {
390
+      uint16_t sg_result = 0;
391
+
392
+      this->switchCSpin(LOW);
393
+
394
+      if (this->TMC_SW_SPI != nullptr) {
395
+        this->TMC_SW_SPI->transfer(TMC2130_n::DRV_STATUS_t::address);
396
+        this->TMC_SW_SPI->transfer16(0);
397
+        // We only care about the last 10 bits
398
+        sg_result = this->TMC_SW_SPI->transfer(0);
399
+        sg_result <<= 8;
400
+        sg_result |= this->TMC_SW_SPI->transfer(0);
401
+      }
402
+      else {
403
+        SPI.beginTransaction(SPISettings(16000000/8, MSBFIRST, SPI_MODE3));
404
+        // Read DRV_STATUS
405
+        SPI.transfer(TMC2130_n::DRV_STATUS_t::address);
406
+        SPI.transfer16(0);
407
+        // We only care about the last 10 bits
408
+        sg_result = SPI.transfer(0);
409
+        sg_result <<= 8;
410
+        sg_result |= SPI.transfer(0);
411
+        SPI.endTransaction();
412
+      }
413
+      this->switchCSpin(HIGH);
414
+
415
+      return (sg_result & 0x3FF) == 0;
416
+    }
417
+
418
+  #endif // SPI_ENDSTOPS
419
+
420
+#endif // USE_SENSORLESS
370 421
 
371 422
 #if TMC_HAS_SPI
372 423
   void tmc_init_cs_pins();

+ 45
- 9
Marlin/src/gcode/calibrate/G28.cpp View File

@@ -78,15 +78,19 @@
78 78
                 fr_mm_s = _MIN(homing_feedrate(X_AXIS), homing_feedrate(Y_AXIS)) * SQRT(sq(mlratio) + 1.0);
79 79
 
80 80
     #if ENABLED(SENSORLESS_HOMING)
81
-      sensorless_t stealth_states { false };
82
-      stealth_states.x = tmc_enable_stallguard(stepperX);
83
-      stealth_states.y = tmc_enable_stallguard(stepperY);
84
-      #if AXIS_HAS_STALLGUARD(X2)
85
-        stealth_states.x2 = tmc_enable_stallguard(stepperX2);
86
-      #endif
87
-      #if AXIS_HAS_STALLGUARD(Y2)
88
-        stealth_states.y2 = tmc_enable_stallguard(stepperY2);
89
-      #endif
81
+      sensorless_t stealth_states {
82
+          tmc_enable_stallguard(stepperX)
83
+        , tmc_enable_stallguard(stepperY)
84
+        , false
85
+        , false
86
+          #if AXIS_HAS_STALLGUARD(X2)
87
+            || tmc_enable_stallguard(stepperX2)
88
+          #endif
89
+        , false
90
+          #if AXIS_HAS_STALLGUARD(Y2)
91
+            || tmc_enable_stallguard(stepperY2)
92
+          #endif
93
+      };
90 94
     #endif
91 95
 
92 96
     do_blocking_move_to_xy(1.5 * mlx * x_axis_home_dir, 1.5 * mly * home_dir(Y_AXIS), fr_mm_s);
@@ -229,6 +233,22 @@ void GcodeSuite::G28(const bool always_home_all) {
229 233
     workspace_plane = PLANE_XY;
230 234
   #endif
231 235
 
236
+  #if ENABLED(IMPROVE_HOMING_RELIABILITY)
237
+    slow_homing_t slow_homing { 0 };
238
+    slow_homing.acceleration.x = planner.settings.max_acceleration_mm_per_s2[X_AXIS];
239
+    slow_homing.acceleration.y = planner.settings.max_acceleration_mm_per_s2[Y_AXIS];
240
+    planner.settings.max_acceleration_mm_per_s2[X_AXIS] = 100;
241
+    planner.settings.max_acceleration_mm_per_s2[Y_AXIS] = 100;
242
+    #if HAS_CLASSIC_JERK
243
+      slow_homing.jerk.x = planner.max_jerk[X_AXIS];
244
+      slow_homing.jerk.y = planner.max_jerk[Y_AXIS];
245
+      planner.max_jerk[X_AXIS] = 0;
246
+      planner.max_jerk[Y_AXIS] = 0;
247
+    #endif
248
+
249
+    planner.reset_acceleration_rates();
250
+  #endif
251
+
232 252
   // Always home with tool 0 active
233 253
   #if HOTENDS > 1
234 254
     #if DISABLED(DELTA) || ENABLED(DELTA_HOME_TO_SAFE_ZONE)
@@ -393,6 +413,11 @@ void GcodeSuite::G28(const bool always_home_all) {
393 413
 
394 414
   endstops.not_homing();
395 415
 
416
+  // Clear endstop state for polled stallGuard endstops
417
+  #if ENABLED(SPI_ENDSTOPS)
418
+    endstops.clear_endstop_state();
419
+  #endif
420
+
396 421
   #if BOTH(DELTA, DELTA_HOME_TO_SAFE_ZONE)
397 422
     // move to a height where we can use the full xy-area
398 423
     do_blocking_move_to_z(delta_clip_start_height);
@@ -414,6 +439,17 @@ void GcodeSuite::G28(const bool always_home_all) {
414 439
     tool_change(old_tool_index, NO_FETCH);
415 440
   #endif
416 441
 
442
+  #if ENABLED(IMPROVE_HOMING_RELIABILITY)
443
+    planner.settings.max_acceleration_mm_per_s2[X_AXIS] = slow_homing.acceleration.x;
444
+    planner.settings.max_acceleration_mm_per_s2[Y_AXIS] = slow_homing.acceleration.y;
445
+    #if HAS_CLASSIC_JERK
446
+      planner.max_jerk[X_AXIS] = slow_homing.jerk.x;
447
+      planner.max_jerk[Y_AXIS] = slow_homing.jerk.y;
448
+    #endif
449
+
450
+    planner.reset_acceleration_rates();
451
+  #endif
452
+
417 453
   ui.refresh();
418 454
 
419 455
   report_current_position();

+ 5
- 0
Marlin/src/inc/Conditionals_post.h View File

@@ -920,6 +920,11 @@
920 920
   #define X_SENSORLESS (AXIS_HAS_STALLGUARD(X) && defined(X_STALL_SENSITIVITY))
921 921
   #define Y_SENSORLESS (AXIS_HAS_STALLGUARD(Y) && defined(Y_STALL_SENSITIVITY))
922 922
   #define Z_SENSORLESS (AXIS_HAS_STALLGUARD(Z) && defined(Z_STALL_SENSITIVITY))
923
+  #if ENABLED(SPI_ENDSTOPS)
924
+    #define X_SPI_SENSORLESS X_SENSORLESS
925
+    #define Y_SPI_SENSORLESS Y_SENSORLESS
926
+    #define Z_SPI_SENSORLESS Z_SENSORLESS
927
+  #endif
923 928
 #endif
924 929
 
925 930
 // Endstops and bed probe

+ 5
- 4
Marlin/src/module/delta.cpp View File

@@ -227,10 +227,11 @@ void home_delta() {
227 227
 
228 228
   // Disable stealthChop if used. Enable diag1 pin on driver.
229 229
   #if ENABLED(SENSORLESS_HOMING)
230
-    sensorless_t stealth_states { false };
231
-    stealth_states.x = tmc_enable_stallguard(stepperX);
232
-    stealth_states.y = tmc_enable_stallguard(stepperY);
233
-    stealth_states.z = tmc_enable_stallguard(stepperZ);
230
+    sensorless_t stealth_states {
231
+      tmc_enable_stallguard(stepperX),
232
+      tmc_enable_stallguard(stepperY),
233
+      tmc_enable_stallguard(stepperZ)
234
+    };
234 235
   #endif
235 236
 
236 237
   // Move all carriages together linearly until an endstop is hit.

+ 56
- 6
Marlin/src/module/endstops.cpp View File

@@ -76,6 +76,13 @@ Endstops::esbits_t Endstops::live_state = 0;
76 76
   float Endstops::z3_endstop_adj;
77 77
 #endif
78 78
 
79
+#if ENABLED(SPI_ENDSTOPS)
80
+  Endstops::tmc_spi_homing_t Endstops::tmc_spi_homing; // = 0
81
+#endif
82
+#if ENABLED(IMPROVE_HOMING_RELIABILITY)
83
+  millis_t sg_guard_period; // = 0
84
+#endif
85
+
79 86
 /**
80 87
  * Class and Instance Methods
81 88
  */
@@ -699,7 +706,7 @@ void Endstops::update() {
699 706
   // Now, we must signal, after validation, if an endstop limit is pressed or not
700 707
   if (stepper.axis_is_moving(X_AXIS)) {
701 708
     if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
702
-      #if HAS_X_MIN
709
+      #if HAS_X_MIN || (X_SPI_SENSORLESS && X_HOME_DIR < 0)
703 710
         #if ENABLED(X_DUAL_ENDSTOPS)
704 711
           PROCESS_DUAL_ENDSTOP(X, X2, MIN);
705 712
         #else
@@ -708,7 +715,7 @@ void Endstops::update() {
708 715
       #endif
709 716
     }
710 717
     else { // +direction
711
-      #if HAS_X_MAX
718
+      #if HAS_X_MAX || (X_SPI_SENSORLESS && X_HOME_DIR > 0)
712 719
         #if ENABLED(X_DUAL_ENDSTOPS)
713 720
           PROCESS_DUAL_ENDSTOP(X, X2, MAX);
714 721
         #else
@@ -720,7 +727,7 @@ void Endstops::update() {
720 727
 
721 728
   if (stepper.axis_is_moving(Y_AXIS)) {
722 729
     if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
723
-      #if HAS_Y_MIN
730
+      #if HAS_Y_MIN || (Y_SPI_SENSORLESS && Y_HOME_DIR < 0)
724 731
         #if ENABLED(Y_DUAL_ENDSTOPS)
725 732
           PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
726 733
         #else
@@ -729,7 +736,7 @@ void Endstops::update() {
729 736
       #endif
730 737
     }
731 738
     else { // +direction
732
-      #if HAS_Y_MAX
739
+      #if HAS_Y_MAX || (Y_SPI_SENSORLESS && Y_HOME_DIR > 0)
733 740
         #if ENABLED(Y_DUAL_ENDSTOPS)
734 741
           PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
735 742
         #else
@@ -741,7 +748,7 @@ void Endstops::update() {
741 748
 
742 749
   if (stepper.axis_is_moving(Z_AXIS)) {
743 750
     if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
744
-      #if HAS_Z_MIN
751
+      #if HAS_Z_MIN || (Z_SPI_SENSORLESS && Z_HOME_DIR < 0)
745 752
         #if ENABLED(Z_TRIPLE_ENDSTOPS)
746 753
           PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MIN);
747 754
         #elif ENABLED(Z_DUAL_ENDSTOPS)
@@ -763,7 +770,7 @@ void Endstops::update() {
763 770
       #endif
764 771
     }
765 772
     else { // Z +direction. Gantry up, bed down.
766
-      #if HAS_Z_MAX
773
+      #if HAS_Z_MAX || (Z_SPI_SENSORLESS && Z_HOME_DIR > 0)
767 774
         #if ENABLED(Z_TRIPLE_ENDSTOPS)
768 775
           PROCESS_TRIPLE_ENDSTOP(Z, Z2, Z3, MAX);
769 776
         #elif ENABLED(Z_DUAL_ENDSTOPS)
@@ -778,6 +785,49 @@ void Endstops::update() {
778 785
   }
779 786
 } // Endstops::update()
780 787
 
788
+#if ENABLED(SPI_ENDSTOPS)
789
+
790
+  #define X_STOP (X_HOME_DIR < 0 ? X_MIN : X_MAX)
791
+  #define Y_STOP (Y_HOME_DIR < 0 ? Y_MIN : Y_MAX)
792
+  #define Z_STOP (Z_HOME_DIR < 0 ? Z_MIN : Z_MAX)
793
+
794
+  bool Endstops::tmc_spi_homing_check() {
795
+    bool hit = false;
796
+    #if X_SPI_SENSORLESS
797
+      if (tmc_spi_homing.x && stepperX.test_stall_status()) {
798
+        SBI(live_state, X_STOP);
799
+        hit = true;
800
+      }
801
+    #endif
802
+    #if Y_SPI_SENSORLESS
803
+      if (tmc_spi_homing.y && stepperY.test_stall_status()) {
804
+        SBI(live_state, Y_STOP);
805
+        hit = true;
806
+      }
807
+    #endif
808
+    #if Z_SPI_SENSORLESS
809
+      if (tmc_spi_homing.z && stepperZ.test_stall_status()) {
810
+        SBI(live_state, Z_STOP);
811
+        hit = true;
812
+      }
813
+    #endif
814
+    return hit;
815
+  }
816
+
817
+  void Endstops::clear_endstop_state() {
818
+    #if X_SPI_SENSORLESS
819
+      CBI(live_state, X_STOP);
820
+    #endif
821
+    #if Y_SPI_SENSORLESS
822
+      CBI(live_state, Y_STOP);
823
+    #endif
824
+    #if Z_SPI_SENSORLESS
825
+      CBI(live_state, Z_STOP);
826
+    #endif
827
+  }
828
+
829
+#endif // SPI_ENDSTOPS
830
+
781 831
 #if ENABLED(PINS_DEBUGGING)
782 832
 
783 833
   bool Endstops::monitor_flag = false;

+ 12
- 0
Marlin/src/module/endstops.h View File

@@ -161,6 +161,18 @@ class Endstops {
161 161
       static void monitor();
162 162
       static void run_monitor();
163 163
     #endif
164
+
165
+    #if ENABLED(SPI_ENDSTOPS)
166
+      typedef struct {
167
+        union {
168
+          bool any;
169
+          struct { bool x:1, y:1, z:1; };
170
+        };
171
+      } tmc_spi_homing_t;
172
+      static tmc_spi_homing_t tmc_spi_homing;
173
+      static void clear_endstop_state();
174
+      static bool tmc_spi_homing_check();
175
+    #endif
164 176
 };
165 177
 
166 178
 extern Endstops endstops;

+ 52
- 2
Marlin/src/module/motion.cpp View File

@@ -1121,6 +1121,26 @@ float get_homing_bump_feedrate(const AxisEnum axis) {
1121 1121
           break;
1122 1122
       #endif
1123 1123
     }
1124
+
1125
+    #if ENABLED(SPI_ENDSTOPS)
1126
+      switch (axis) {
1127
+        #if X_SPI_SENSORLESS
1128
+          case X_AXIS: endstops.tmc_spi_homing.x = true; break;
1129
+        #endif
1130
+        #if Y_SPI_SENSORLESS
1131
+          case Y_AXIS: endstops.tmc_spi_homing.y = true; break;
1132
+        #endif
1133
+        #if Z_SPI_SENSORLESS
1134
+          case Z_AXIS: endstops.tmc_spi_homing.z = true; break;
1135
+        #endif
1136
+        default: break;
1137
+      }
1138
+    #endif
1139
+
1140
+    #if ENABLED(IMPROVE_HOMING_RELIABILITY)
1141
+      sg_guard_period = millis() + default_sg_guard_duration;
1142
+    #endif
1143
+
1124 1144
     return stealth_states;
1125 1145
   }
1126 1146
 
@@ -1170,6 +1190,21 @@ float get_homing_bump_feedrate(const AxisEnum axis) {
1170 1190
           break;
1171 1191
       #endif
1172 1192
     }
1193
+
1194
+    #if ENABLED(SPI_ENDSTOPS)
1195
+      switch (axis) {
1196
+        #if X_SPI_SENSORLESS
1197
+          case X_AXIS: endstops.tmc_spi_homing.x = false; break;
1198
+        #endif
1199
+        #if Y_SPI_SENSORLESS
1200
+          case Y_AXIS: endstops.tmc_spi_homing.y = false; break;
1201
+        #endif
1202
+        #if Z_SPI_SENSORLESS
1203
+          case Z_AXIS: endstops.tmc_spi_homing.z = false; break;
1204
+        #endif
1205
+        default: break;
1206
+      }
1207
+    #endif
1173 1208
   }
1174 1209
 
1175 1210
 #endif // SENSORLESS_HOMING
@@ -1383,9 +1418,24 @@ void homeaxis(const AxisEnum axis) {
1383 1418
     // Only Z homing (with probe) is permitted
1384 1419
     if (axis != Z_AXIS) { BUZZ(100, 880); return; }
1385 1420
   #else
1386
-    #define CAN_HOME(A) \
1421
+    #define _CAN_HOME(A) \
1387 1422
       (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
1388
-    if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
1423
+    #if X_SPI_SENSORLESS
1424
+      #define CAN_HOME_X true
1425
+    #else
1426
+      #define CAN_HOME_X _CAN_HOME(X)
1427
+    #endif
1428
+    #if Y_SPI_SENSORLESS
1429
+      #define CAN_HOME_Y true
1430
+    #else
1431
+      #define CAN_HOME_Y _CAN_HOME(Y)
1432
+    #endif
1433
+    #if Z_SPI_SENSORLESS
1434
+      #define CAN_HOME_Z true
1435
+    #else
1436
+      #define CAN_HOME_Z _CAN_HOME(Z)
1437
+    #endif
1438
+    if (!CAN_HOME_X && !CAN_HOME_Y && !CAN_HOME_Z) return;
1389 1439
   #endif
1390 1440
 
1391 1441
   if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");

Loading…
Cancel
Save