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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. _ES_ITEM(HAS_U_MIN, U_MIN)
  45. _ES_ITEM(HAS_U_MAX, U_MAX)
  46. _ES_ITEM(HAS_V_MIN, V_MIN)
  47. _ES_ITEM(HAS_V_MAX, V_MAX)
  48. _ES_ITEM(HAS_W_MIN, W_MIN)
  49. _ES_ITEM(HAS_W_MAX, W_MAX)
  50. // Extra Endstops for XYZ
  51. #if ENABLED(X_DUAL_ENDSTOPS)
  52. _ES_ITEM(HAS_X_MIN, X2_MIN)
  53. _ES_ITEM(HAS_X_MAX, X2_MAX)
  54. #endif
  55. #if ENABLED(Y_DUAL_ENDSTOPS)
  56. _ES_ITEM(HAS_Y_MIN, Y2_MIN)
  57. _ES_ITEM(HAS_Y_MAX, Y2_MAX)
  58. #endif
  59. #if ENABLED(Z_MULTI_ENDSTOPS)
  60. _ES_ITEM(HAS_Z_MIN, Z2_MIN)
  61. _ES_ITEM(HAS_Z_MAX, Z2_MAX)
  62. #if NUM_Z_STEPPERS >= 3
  63. _ES_ITEM(HAS_Z_MIN, Z3_MIN)
  64. _ES_ITEM(HAS_Z_MAX, Z3_MAX)
  65. #endif
  66. #if NUM_Z_STEPPERS >= 4
  67. _ES_ITEM(HAS_Z_MIN, Z4_MIN)
  68. _ES_ITEM(HAS_Z_MAX, Z4_MAX)
  69. #endif
  70. #endif
  71. // Bed Probe state is distinct or shared with Z_MIN (i.e., when the probe is the only Z endstop)
  72. #if !HAS_DELTA_SENSORLESS_PROBING
  73. _ES_ITEM(HAS_BED_PROBE, Z_MIN_PROBE IF_DISABLED(USES_Z_MIN_PROBE_PIN, = Z_MIN))
  74. #endif
  75. // The total number of states
  76. NUM_ENDSTOP_STATES
  77. // Endstops can be either MIN or MAX but not both
  78. #if HAS_X_MIN || HAS_X_MAX
  79. , X_ENDSTOP = TERN(X_HOME_TO_MAX, X_MAX, X_MIN)
  80. #endif
  81. #if HAS_Y_MIN || HAS_Y_MAX
  82. , Y_ENDSTOP = TERN(Y_HOME_TO_MAX, Y_MAX, Y_MIN)
  83. #endif
  84. #if HAS_Z_MIN || HAS_Z_MAX || HOMING_Z_WITH_PROBE
  85. , Z_ENDSTOP = TERN(Z_HOME_TO_MAX, Z_MAX, TERN(HOMING_Z_WITH_PROBE, Z_MIN_PROBE, Z_MIN))
  86. #endif
  87. #if HAS_I_MIN || HAS_I_MAX
  88. , I_ENDSTOP = TERN(I_HOME_TO_MAX, I_MAX, I_MIN)
  89. #endif
  90. #if HAS_J_MIN || HAS_J_MAX
  91. , J_ENDSTOP = TERN(J_HOME_TO_MAX, J_MAX, J_MIN)
  92. #endif
  93. #if HAS_K_MIN || HAS_K_MAX
  94. , K_ENDSTOP = TERN(K_HOME_TO_MAX, K_MAX, K_MIN)
  95. #endif
  96. };
  97. #undef __ES_ITEM
  98. #undef _ES_ITEM
  99. class Endstops {
  100. public:
  101. typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
  102. #if ENABLED(X_DUAL_ENDSTOPS)
  103. static float x2_endstop_adj;
  104. #endif
  105. #if ENABLED(Y_DUAL_ENDSTOPS)
  106. static float y2_endstop_adj;
  107. #endif
  108. #if ENABLED(Z_MULTI_ENDSTOPS)
  109. static float z2_endstop_adj;
  110. #endif
  111. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPERS >= 3
  112. static float z3_endstop_adj;
  113. #endif
  114. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPERS >= 4
  115. static float z4_endstop_adj;
  116. #endif
  117. private:
  118. static bool enabled, enabled_globally;
  119. static endstop_mask_t live_state;
  120. static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
  121. #if ENDSTOP_NOISE_THRESHOLD
  122. static endstop_mask_t validated_live_state;
  123. static uint8_t endstop_poll_count; // Countdown from threshold for polling
  124. #endif
  125. public:
  126. Endstops() {};
  127. /**
  128. * Initialize the endstop pins
  129. */
  130. static void init();
  131. /**
  132. * Are endstops or the probe set to abort the move?
  133. */
  134. FORCE_INLINE static bool abort_enabled() {
  135. return enabled || TERN0(HAS_BED_PROBE, z_probe_enabled);
  136. }
  137. static bool global_enabled() { return enabled_globally; }
  138. /**
  139. * Periodic call to poll endstops if required. Called from temperature ISR
  140. */
  141. static void poll();
  142. /**
  143. * Update endstops bits from the pins. Apply filtering to get a verified state.
  144. * If abort_enabled() and moving towards a triggered switch, abort the current move.
  145. * Called from ISR contexts.
  146. */
  147. static void update();
  148. #if ENABLED(BD_SENSOR)
  149. static bool bdp_state;
  150. static void bdp_state_update(const bool z_state) { bdp_state = z_state; }
  151. #endif
  152. /**
  153. * Get Endstop hit state.
  154. */
  155. FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
  156. /**
  157. * Get current endstops state
  158. */
  159. FORCE_INLINE static endstop_mask_t state() {
  160. return
  161. #if ENDSTOP_NOISE_THRESHOLD
  162. validated_live_state
  163. #else
  164. live_state
  165. #endif
  166. ;
  167. }
  168. static bool probe_switch_activated() {
  169. return (true
  170. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  171. && READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE
  172. #endif
  173. );
  174. }
  175. /**
  176. * Report endstop hits to serial. Called from loop().
  177. */
  178. static void event_handler();
  179. /**
  180. * Report endstop states in response to M119
  181. */
  182. static void report_states();
  183. // Enable / disable endstop checking globally
  184. static void enable_globally(const bool onoff=true);
  185. // Enable / disable endstop checking
  186. static void enable(const bool onoff=true);
  187. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  188. static void not_homing();
  189. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  190. // If the last move failed to trigger an endstop, call kill
  191. static void validate_homing_move();
  192. #else
  193. FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
  194. #endif
  195. // Clear endstops (i.e., they were hit intentionally) to suppress the report
  196. FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
  197. // Enable / disable endstop z-probe checking
  198. #if HAS_BED_PROBE
  199. static volatile bool z_probe_enabled;
  200. static void enable_z_probe(const bool onoff=true);
  201. #endif
  202. static void resync();
  203. // Debugging of endstops
  204. #if ENABLED(PINS_DEBUGGING)
  205. static bool monitor_flag;
  206. static void monitor();
  207. static void run_monitor();
  208. #endif
  209. #if ENABLED(SPI_ENDSTOPS)
  210. typedef struct {
  211. union {
  212. bool any;
  213. struct { bool NUM_AXIS_LIST(x:1, y:1, z:1, i:1, j:1, k:1); };
  214. };
  215. } tmc_spi_homing_t;
  216. static tmc_spi_homing_t tmc_spi_homing;
  217. static void clear_endstop_state();
  218. static bool tmc_spi_homing_check();
  219. #endif
  220. public:
  221. // Basic functions for Sensorless Homing
  222. #if USE_SENSORLESS
  223. static void set_homing_current(const bool onoff);
  224. #endif
  225. };
  226. extern Endstops endstops;
  227. /**
  228. * A class to save and change the endstop state,
  229. * then restore it when it goes out of scope.
  230. */
  231. class TemporaryGlobalEndstopsState {
  232. bool saved;
  233. public:
  234. TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
  235. endstops.enable_globally(enable);
  236. }
  237. ~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
  238. };