My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

endstops.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * endstops.h - manages endstops
  25. */
  26. #include "../inc/MarlinConfig.h"
  27. #include <stdint.h>
  28. #define __ES_ITEM(N) N,
  29. #define _ES_ITEM(K,N) TERN_(K,DEFER4(__ES_ITEM)(N))
  30. enum EndstopEnum : char {
  31. // Common XYZ (ABC) endstops. Defined according to USE_[XYZ](MIN|MAX)_PLUG settings.
  32. _ES_ITEM(HAS_X_MIN, X_MIN)
  33. _ES_ITEM(HAS_X_MAX, X_MAX)
  34. _ES_ITEM(HAS_Y_MIN, Y_MIN)
  35. _ES_ITEM(HAS_Y_MAX, Y_MAX)
  36. _ES_ITEM(HAS_Z_MIN, Z_MIN)
  37. _ES_ITEM(HAS_Z_MAX, Z_MAX)
  38. _ES_ITEM(HAS_I_MIN, I_MIN)
  39. _ES_ITEM(HAS_I_MAX, I_MAX)
  40. _ES_ITEM(HAS_J_MIN, J_MIN)
  41. _ES_ITEM(HAS_J_MAX, J_MAX)
  42. _ES_ITEM(HAS_K_MIN, K_MIN)
  43. _ES_ITEM(HAS_K_MAX, K_MAX)
  44. // Extra Endstops for XYZ
  45. #if ENABLED(X_DUAL_ENDSTOPS)
  46. _ES_ITEM(HAS_X_MIN, X2_MIN)
  47. _ES_ITEM(HAS_X_MAX, X2_MAX)
  48. #endif
  49. #if ENABLED(Y_DUAL_ENDSTOPS)
  50. _ES_ITEM(HAS_Y_MIN, Y2_MIN)
  51. _ES_ITEM(HAS_Y_MAX, Y2_MAX)
  52. #endif
  53. #if ENABLED(Z_MULTI_ENDSTOPS)
  54. _ES_ITEM(HAS_Z_MIN, Z2_MIN)
  55. _ES_ITEM(HAS_Z_MAX, Z2_MAX)
  56. #if NUM_Z_STEPPER_DRIVERS >= 3
  57. _ES_ITEM(HAS_Z_MIN, Z3_MIN)
  58. _ES_ITEM(HAS_Z_MAX, Z3_MAX)
  59. #endif
  60. #if NUM_Z_STEPPER_DRIVERS >= 4
  61. _ES_ITEM(HAS_Z_MIN, Z4_MIN)
  62. _ES_ITEM(HAS_Z_MAX, Z4_MAX)
  63. #endif
  64. #endif
  65. // Bed Probe state is distinct or shared with Z_MIN (i.e., when the probe is the only Z endstop)
  66. #if !HAS_DELTA_SENSORLESS_PROBING
  67. _ES_ITEM(HAS_BED_PROBE, Z_MIN_PROBE IF_DISABLED(USES_Z_MIN_PROBE_PIN, = Z_MIN))
  68. #endif
  69. // The total number of states
  70. NUM_ENDSTOP_STATES
  71. // Endstops can be either MIN or MAX but not both
  72. #if HAS_X_MIN || HAS_X_MAX
  73. , X_ENDSTOP = TERN(X_HOME_TO_MAX, X_MAX, X_MIN)
  74. #endif
  75. #if HAS_Y_MIN || HAS_Y_MAX
  76. , Y_ENDSTOP = TERN(Y_HOME_TO_MAX, Y_MAX, Y_MIN)
  77. #endif
  78. #if HAS_Z_MIN || HAS_Z_MAX || HOMING_Z_WITH_PROBE
  79. , Z_ENDSTOP = TERN(Z_HOME_TO_MAX, Z_MAX, TERN(HOMING_Z_WITH_PROBE, Z_MIN_PROBE, Z_MIN))
  80. #endif
  81. #if HAS_I_MIN || HAS_I_MAX
  82. , I_ENDSTOP = TERN(I_HOME_TO_MAX, I_MAX, I_MIN)
  83. #endif
  84. #if HAS_J_MIN || HAS_J_MAX
  85. , J_ENDSTOP = TERN(J_HOME_TO_MAX, J_MAX, J_MIN)
  86. #endif
  87. #if HAS_K_MIN || HAS_K_MAX
  88. , K_ENDSTOP = TERN(K_HOME_TO_MAX, K_MAX, K_MIN)
  89. #endif
  90. };
  91. #undef __ES_ITEM
  92. #undef _ES_ITEM
  93. class Endstops {
  94. public:
  95. typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
  96. #if ENABLED(X_DUAL_ENDSTOPS)
  97. static float x2_endstop_adj;
  98. #endif
  99. #if ENABLED(Y_DUAL_ENDSTOPS)
  100. static float y2_endstop_adj;
  101. #endif
  102. #if ENABLED(Z_MULTI_ENDSTOPS)
  103. static float z2_endstop_adj;
  104. #endif
  105. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
  106. static float z3_endstop_adj;
  107. #endif
  108. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
  109. static float z4_endstop_adj;
  110. #endif
  111. private:
  112. static bool enabled, enabled_globally;
  113. static endstop_mask_t live_state;
  114. static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
  115. #if ENDSTOP_NOISE_THRESHOLD
  116. static endstop_mask_t validated_live_state;
  117. static uint8_t endstop_poll_count; // Countdown from threshold for polling
  118. #endif
  119. public:
  120. Endstops() {};
  121. /**
  122. * Initialize the endstop pins
  123. */
  124. static void init();
  125. /**
  126. * Are endstops or the probe set to abort the move?
  127. */
  128. FORCE_INLINE static bool abort_enabled() {
  129. return enabled || TERN0(HAS_BED_PROBE, z_probe_enabled);
  130. }
  131. static bool global_enabled() { return enabled_globally; }
  132. /**
  133. * Periodic call to poll endstops if required. Called from temperature ISR
  134. */
  135. static void poll();
  136. /**
  137. * Update endstops bits from the pins. Apply filtering to get a verified state.
  138. * If abort_enabled() and moving towards a triggered switch, abort the current move.
  139. * Called from ISR contexts.
  140. */
  141. static void update();
  142. /**
  143. * Get Endstop hit state.
  144. */
  145. FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
  146. /**
  147. * Get current endstops state
  148. */
  149. FORCE_INLINE static endstop_mask_t state() {
  150. return
  151. #if ENDSTOP_NOISE_THRESHOLD
  152. validated_live_state
  153. #else
  154. live_state
  155. #endif
  156. ;
  157. }
  158. static bool probe_switch_activated() {
  159. return (true
  160. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  161. && READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE
  162. #endif
  163. );
  164. }
  165. /**
  166. * Report endstop hits to serial. Called from loop().
  167. */
  168. static void event_handler();
  169. /**
  170. * Report endstop states in response to M119
  171. */
  172. static void report_states();
  173. // Enable / disable endstop checking globally
  174. static void enable_globally(const bool onoff=true);
  175. // Enable / disable endstop checking
  176. static void enable(const bool onoff=true);
  177. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  178. static void not_homing();
  179. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  180. // If the last move failed to trigger an endstop, call kill
  181. static void validate_homing_move();
  182. #else
  183. FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
  184. #endif
  185. // Clear endstops (i.e., they were hit intentionally) to suppress the report
  186. FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
  187. // Enable / disable endstop z-probe checking
  188. #if HAS_BED_PROBE
  189. static volatile bool z_probe_enabled;
  190. static void enable_z_probe(const bool onoff=true);
  191. #endif
  192. static void resync();
  193. // Debugging of endstops
  194. #if ENABLED(PINS_DEBUGGING)
  195. static bool monitor_flag;
  196. static void monitor();
  197. static void run_monitor();
  198. #endif
  199. #if ENABLED(SPI_ENDSTOPS)
  200. typedef struct {
  201. union {
  202. bool any;
  203. struct { bool LINEAR_AXIS_LIST(x:1, y:1, z:1, i:1, j:1, k:1); };
  204. };
  205. } tmc_spi_homing_t;
  206. static tmc_spi_homing_t tmc_spi_homing;
  207. static void clear_endstop_state();
  208. static bool tmc_spi_homing_check();
  209. #endif
  210. };
  211. extern Endstops endstops;
  212. /**
  213. * A class to save and change the endstop state,
  214. * then restore it when it goes out of scope.
  215. */
  216. class TemporaryGlobalEndstopsState {
  217. bool saved;
  218. public:
  219. TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
  220. endstops.enable_globally(enable);
  221. }
  222. ~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
  223. };