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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://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. enum EndstopEnum : char {
  29. X_MIN, Y_MIN, Z_MIN, Z_MIN_PROBE,
  30. X_MAX, Y_MAX, Z_MAX,
  31. X2_MIN, X2_MAX,
  32. Y2_MIN, Y2_MAX,
  33. Z2_MIN, Z2_MAX,
  34. Z3_MIN, Z3_MAX
  35. };
  36. class Endstops {
  37. public:
  38. #if HAS_EXTRA_ENDSTOPS
  39. typedef uint16_t esbits_t;
  40. #if ENABLED(X_DUAL_ENDSTOPS)
  41. static float x2_endstop_adj;
  42. #endif
  43. #if ENABLED(Y_DUAL_ENDSTOPS)
  44. static float y2_endstop_adj;
  45. #endif
  46. #if Z_MULTI_ENDSTOPS
  47. static float z2_endstop_adj;
  48. #endif
  49. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  50. static float z3_endstop_adj;
  51. #endif
  52. #else
  53. typedef uint8_t esbits_t;
  54. #endif
  55. private:
  56. static bool enabled, enabled_globally;
  57. static esbits_t live_state;
  58. static volatile uint8_t hit_state; // Use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT index
  59. #if ENDSTOP_NOISE_THRESHOLD
  60. static esbits_t validated_live_state;
  61. static uint8_t endstop_poll_count; // Countdown from threshold for polling
  62. #endif
  63. public:
  64. Endstops() {};
  65. /**
  66. * Initialize the endstop pins
  67. */
  68. static void init();
  69. /**
  70. * Are endstops or the probe set to abort the move?
  71. */
  72. FORCE_INLINE static bool abort_enabled() {
  73. return (enabled
  74. #if HAS_BED_PROBE
  75. || z_probe_enabled
  76. #endif
  77. );
  78. }
  79. static inline bool global_enabled() { return enabled_globally; }
  80. /**
  81. * Periodic call to poll endstops if required. Called from temperature ISR
  82. */
  83. static void poll();
  84. /**
  85. * Update endstops bits from the pins. Apply filtering to get a verified state.
  86. * If abort_enabled() and moving towards a triggered switch, abort the current move.
  87. * Called from ISR contexts.
  88. */
  89. static void update();
  90. /**
  91. * Get Endstop hit state.
  92. */
  93. FORCE_INLINE static uint8_t trigger_state() { return hit_state; }
  94. /**
  95. * Get current endstops state
  96. */
  97. FORCE_INLINE static esbits_t state() {
  98. return
  99. #if ENDSTOP_NOISE_THRESHOLD
  100. validated_live_state
  101. #else
  102. live_state
  103. #endif
  104. ;
  105. }
  106. /**
  107. * Report endstop hits to serial. Called from loop().
  108. */
  109. static void event_handler();
  110. /**
  111. * Report endstop states in response to M119
  112. */
  113. static void report_states();
  114. // Enable / disable endstop checking globally
  115. static void enable_globally(const bool onoff=true);
  116. // Enable / disable endstop checking
  117. static void enable(const bool onoff=true);
  118. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  119. static void not_homing();
  120. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  121. // If the last move failed to trigger an endstop, call kill
  122. static void validate_homing_move();
  123. #else
  124. FORCE_INLINE static void validate_homing_move() { hit_on_purpose(); }
  125. #endif
  126. // Clear endstops (i.e., they were hit intentionally) to suppress the report
  127. FORCE_INLINE static void hit_on_purpose() { hit_state = 0; }
  128. // Enable / disable endstop z-probe checking
  129. #if HAS_BED_PROBE
  130. static volatile bool z_probe_enabled;
  131. static void enable_z_probe(const bool onoff=true);
  132. #endif
  133. static void resync();
  134. // Debugging of endstops
  135. #if ENABLED(PINS_DEBUGGING)
  136. static bool monitor_flag;
  137. static void monitor();
  138. static void run_monitor();
  139. #endif
  140. #if ENABLED(SPI_ENDSTOPS)
  141. typedef struct {
  142. union {
  143. bool any;
  144. struct { bool x:1, y:1, z:1; };
  145. };
  146. } tmc_spi_homing_t;
  147. static tmc_spi_homing_t tmc_spi_homing;
  148. static void clear_endstop_state();
  149. static bool tmc_spi_homing_check();
  150. #endif
  151. };
  152. extern Endstops endstops;
  153. /**
  154. * A class to save and change the endstop state,
  155. * then restore it when it goes out of scope.
  156. */
  157. class TemporaryGlobalEndstopsState {
  158. bool saved;
  159. public:
  160. TemporaryGlobalEndstopsState(const bool enable) : saved(endstops.global_enabled()) {
  161. endstops.enable_globally(enable);
  162. }
  163. ~TemporaryGlobalEndstopsState() { endstops.enable_globally(saved); }
  164. };