My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

endstops.h 5.2KB

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