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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. _ES_ITEM(HAS_X_MIN, X_MIN)
  32. _ES_ITEM(HAS_X_MAX, X_MAX)
  33. _ES_ITEM(HAS_Y_MIN, Y_MIN)
  34. _ES_ITEM(HAS_Y_MAX, Y_MAX)
  35. _ES_ITEM(HAS_Z_MIN, Z_MIN)
  36. _ES_ITEM(HAS_Z_MAX, Z_MAX)
  37. #if ENABLED(X_DUAL_ENDSTOPS)
  38. _ES_ITEM(HAS_X_MIN, X2_MIN)
  39. _ES_ITEM(HAS_X_MAX, X2_MAX)
  40. #endif
  41. #if ENABLED(Y_DUAL_ENDSTOPS)
  42. _ES_ITEM(HAS_Y_MIN, Y2_MIN)
  43. _ES_ITEM(HAS_Y_MAX, Y2_MAX)
  44. #endif
  45. #if ENABLED(Z_MULTI_ENDSTOPS)
  46. _ES_ITEM(HAS_Z_MIN, Z2_MIN)
  47. _ES_ITEM(HAS_Z_MAX, Z2_MAX)
  48. #if NUM_Z_STEPPER_DRIVERS >= 3
  49. _ES_ITEM(HAS_Z_MIN, Z3_MIN)
  50. _ES_ITEM(HAS_Z_MAX, Z3_MAX)
  51. #endif
  52. #if NUM_Z_STEPPER_DRIVERS >= 4
  53. _ES_ITEM(HAS_Z_MIN, Z4_MIN)
  54. _ES_ITEM(HAS_Z_MAX, Z4_MAX)
  55. #endif
  56. #endif
  57. _ES_ITEM(HAS_Z_MIN_PROBE_PIN, Z_MIN_PROBE)
  58. NUM_ENDSTOP_STATES
  59. };
  60. #define X_ENDSTOP TERN(X_HOME_TO_MAX, X_MAX, X_MIN)
  61. #define Y_ENDSTOP TERN(Y_HOME_TO_MAX, Y_MAX, Y_MIN)
  62. #define Z_ENDSTOP TERN(Z_HOME_TO_MAX, Z_MAX, TERN(HOMING_Z_WITH_PROBE, Z_MIN_PROBE, Z_MIN))
  63. #undef __ES_ITEM
  64. #undef _ES_ITEM
  65. class Endstops {
  66. public:
  67. typedef IF<(NUM_ENDSTOP_STATES > 8), uint16_t, uint8_t>::type endstop_mask_t;
  68. #if ENABLED(X_DUAL_ENDSTOPS)
  69. static float x2_endstop_adj;
  70. #endif
  71. #if ENABLED(Y_DUAL_ENDSTOPS)
  72. static float y2_endstop_adj;
  73. #endif
  74. #if ENABLED(Z_MULTI_ENDSTOPS)
  75. static float z2_endstop_adj;
  76. #endif
  77. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 3
  78. static float z3_endstop_adj;
  79. #endif
  80. #if ENABLED(Z_MULTI_ENDSTOPS) && NUM_Z_STEPPER_DRIVERS >= 4
  81. static float z4_endstop_adj;
  82. #endif
  83. private:
  84. static bool enabled, enabled_globally;
  85. static endstop_mask_t live_state;
  86. static volatile endstop_mask_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
  87. #if ENDSTOP_NOISE_THRESHOLD
  88. static endstop_mask_t validated_live_state;
  89. static uint8_t endstop_poll_count; // Countdown from threshold for polling
  90. #endif
  91. public:
  92. Endstops() {};
  93. /**
  94. * Initialize the endstop pins
  95. */
  96. static void init();
  97. /**
  98. * Are endstops or the probe set to abort the move?
  99. */
  100. FORCE_INLINE static bool abort_enabled() {
  101. return enabled || TERN0(HAS_BED_PROBE, z_probe_enabled);
  102. }
  103. static inline bool global_enabled() { return enabled_globally; }
  104. /**
  105. * Periodic call to poll endstops if required. Called from temperature ISR
  106. */
  107. static void poll();
  108. /**
  109. * Update endstops bits from the pins. Apply filtering to get a verified state.
  110. * If abort_enabled() and moving towards a triggered switch, abort the current move.
  111. * Called from ISR contexts.
  112. */
  113. static void update();
  114. /**
  115. * Get Endstop hit state.
  116. */
  117. FORCE_INLINE static endstop_mask_t trigger_state() { return hit_state; }
  118. /**
  119. * Get current endstops state
  120. */
  121. FORCE_INLINE static endstop_mask_t state() {
  122. return
  123. #if ENDSTOP_NOISE_THRESHOLD
  124. validated_live_state
  125. #else
  126. live_state
  127. #endif
  128. ;
  129. }
  130. static inline bool probe_switch_activated() {
  131. return (true
  132. #if ENABLED(PROBE_ACTIVATION_SWITCH)
  133. && READ(PROBE_ACTIVATION_SWITCH_PIN) == PROBE_ACTIVATION_SWITCH_STATE
  134. #endif
  135. );
  136. }
  137. /**
  138. * Report endstop hits to serial. Called from loop().
  139. */
  140. static void event_handler();
  141. /**
  142. * Report endstop states in response to M119
  143. */
  144. static void report_states();
  145. // Enable / disable endstop checking globally
  146. static void enable_globally(const bool onoff=true);
  147. // Enable / disable endstop checking
  148. static void enable(const bool onoff=true);
  149. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  150. static void not_homing();
  151. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  152. // If the last move failed to trigger an endstop, call kill
  153. static void validate_homing_move();
  154. #else
  155. FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
  156. #endif
  157. // Clear endstops (i.e., they were hit intentionally) to suppress the report
  158. FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
  159. // Enable / disable endstop z-probe checking
  160. #if HAS_BED_PROBE
  161. static volatile bool z_probe_enabled;
  162. static void enable_z_probe(const bool onoff=true);
  163. #endif
  164. static void resync();
  165. // Debugging of endstops
  166. #if ENABLED(PINS_DEBUGGING)
  167. static bool monitor_flag;
  168. static void monitor();
  169. static void run_monitor();
  170. #endif
  171. #if ENABLED(SPI_ENDSTOPS)
  172. typedef struct {
  173. union {
  174. bool any;
  175. struct { bool x:1, y:1, z:1; };
  176. };
  177. } tmc_spi_homing_t;
  178. static tmc_spi_homing_t tmc_spi_homing;
  179. static void clear_endstop_state();
  180. static bool tmc_spi_homing_check();
  181. #endif
  182. };
  183. extern Endstops endstops;
  184. /**
  185. * A class to save and change the endstop state,
  186. * then restore it when it goes out of scope.
  187. */
  188. class TemporaryGlobalEndstopsState {
  189. bool saved;
  190. public:
  191. TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
  192. endstops.enable_globally(enable);
  193. }
  194. ~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
  195. };