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

endstops.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * endstops.cpp - A singleton object to manage endstops
  24. */
  25. #include "Marlin.h"
  26. #include "cardreader.h"
  27. #include "endstops.h"
  28. #include "temperature.h"
  29. #include "stepper.h"
  30. #include "ultralcd.h"
  31. // TEST_ENDSTOP: test the old and the current status of an endstop
  32. #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits & old_endstop_bits, ENDSTOP))
  33. Endstops endstops;
  34. Endstops::Endstops() {
  35. enable_globally(ENABLED(ENDSTOPS_ONLY_FOR_HOMING));
  36. enable(true);
  37. #if ENABLED(HAS_Z_MIN_PROBE)
  38. enable_z_probe(false);
  39. #endif
  40. } // Endstops::Endstops
  41. void Endstops::init() {
  42. #if HAS_X_MIN
  43. SET_INPUT(X_MIN_PIN);
  44. #if ENABLED(ENDSTOPPULLUP_XMIN)
  45. WRITE(X_MIN_PIN,HIGH);
  46. #endif
  47. #endif
  48. #if HAS_Y_MIN
  49. SET_INPUT(Y_MIN_PIN);
  50. #if ENABLED(ENDSTOPPULLUP_YMIN)
  51. WRITE(Y_MIN_PIN,HIGH);
  52. #endif
  53. #endif
  54. #if HAS_Z_MIN
  55. SET_INPUT(Z_MIN_PIN);
  56. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  57. WRITE(Z_MIN_PIN,HIGH);
  58. #endif
  59. #endif
  60. #if HAS_Z2_MIN
  61. SET_INPUT(Z2_MIN_PIN);
  62. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  63. WRITE(Z2_MIN_PIN,HIGH);
  64. #endif
  65. #endif
  66. #if HAS_X_MAX
  67. SET_INPUT(X_MAX_PIN);
  68. #if ENABLED(ENDSTOPPULLUP_XMAX)
  69. WRITE(X_MAX_PIN,HIGH);
  70. #endif
  71. #endif
  72. #if HAS_Y_MAX
  73. SET_INPUT(Y_MAX_PIN);
  74. #if ENABLED(ENDSTOPPULLUP_YMAX)
  75. WRITE(Y_MAX_PIN,HIGH);
  76. #endif
  77. #endif
  78. #if HAS_Z_MAX
  79. SET_INPUT(Z_MAX_PIN);
  80. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  81. WRITE(Z_MAX_PIN,HIGH);
  82. #endif
  83. #endif
  84. #if HAS_Z2_MAX
  85. SET_INPUT(Z2_MAX_PIN);
  86. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  87. WRITE(Z2_MAX_PIN,HIGH);
  88. #endif
  89. #endif
  90. #if HAS_Z_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
  91. SET_INPUT(Z_MIN_PROBE_PIN);
  92. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  93. WRITE(Z_MIN_PROBE_PIN,HIGH);
  94. #endif
  95. #endif
  96. } // Endstops::init
  97. void Endstops::report_state() {
  98. if (endstop_hit_bits) {
  99. #if ENABLED(ULTRA_LCD)
  100. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  101. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  102. #else
  103. #define _SET_STOP_CHAR(A,C) ;
  104. #endif
  105. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  106. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
  107. _SET_STOP_CHAR(A,C); }while(0)
  108. #define _ENDSTOP_HIT_TEST(A,C) \
  109. if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
  110. _ENDSTOP_HIT_ECHO(A,C)
  111. SERIAL_ECHO_START;
  112. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  113. _ENDSTOP_HIT_TEST(X, 'X');
  114. _ENDSTOP_HIT_TEST(Y, 'Y');
  115. _ENDSTOP_HIT_TEST(Z, 'Z');
  116. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  117. #define P_AXIS Z_AXIS
  118. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  119. #endif
  120. SERIAL_EOL;
  121. #if ENABLED(ULTRA_LCD)
  122. char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
  123. sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  124. lcd_setstatus(msg);
  125. #endif
  126. hit_on_purpose();
  127. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  128. if (stepper.abort_on_endstop_hit) {
  129. card.sdprinting = false;
  130. card.closefile();
  131. stepper.quick_stop();
  132. disable_all_heaters(); // switch off all heaters.
  133. }
  134. #endif
  135. }
  136. } // Endstops::report_state
  137. void Endstops::M119() {
  138. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  139. #if HAS_X_MIN
  140. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  141. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  142. #endif
  143. #if HAS_X_MAX
  144. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  145. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  146. #endif
  147. #if HAS_Y_MIN
  148. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  149. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  150. #endif
  151. #if HAS_Y_MAX
  152. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  153. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  154. #endif
  155. #if HAS_Z_MIN
  156. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  157. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  158. #endif
  159. #if HAS_Z_MAX
  160. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  161. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  162. #endif
  163. #if HAS_Z2_MAX
  164. SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
  165. SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  166. #endif
  167. #if HAS_Z_PROBE
  168. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  169. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  170. #endif
  171. } // Endstops::M119
  172. #if ENABLED(Z_DUAL_ENDSTOPS)
  173. // Pass the result of the endstop test
  174. void Endstops::test_dual_z_endstops(EndstopEnum es1, EndstopEnum es2) {
  175. byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
  176. if (stepper.current_block->steps[Z_AXIS] > 0) {
  177. stepper.endstop_triggered(Z_AXIS);
  178. SBI(endstop_hit_bits, Z_MIN);
  179. if (!stepper.performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  180. stepper.kill_current_block();
  181. }
  182. }
  183. #endif
  184. // Check endstops - Called from ISR!
  185. void Endstops::update() {
  186. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  187. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  188. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  189. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  190. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  191. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  192. // COPY_BIT: copy the value of COPY_BIT to BIT in bits
  193. #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
  194. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  195. UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
  196. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
  197. _ENDSTOP_HIT(AXIS); \
  198. stepper.endstop_triggered(_AXIS(AXIS)); \
  199. } \
  200. } while(0)
  201. #if ENABLED(COREXY) || ENABLED(COREXZ)
  202. // Head direction in -X axis for CoreXY and CoreXZ bots.
  203. // If Delta1 == -Delta2, the movement is only in Y or Z axis
  204. if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(A_AXIS) == stepper.motor_direction(CORE_AXIS_2))) {
  205. if (stepper.motor_direction(X_HEAD))
  206. #else
  207. if (stepper.motor_direction(X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  208. #endif
  209. { // -direction
  210. #if ENABLED(DUAL_X_CARRIAGE)
  211. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  212. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == -1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  213. #endif
  214. {
  215. #if HAS_X_MIN
  216. UPDATE_ENDSTOP(X, MIN);
  217. #endif
  218. }
  219. }
  220. else { // +direction
  221. #if ENABLED(DUAL_X_CARRIAGE)
  222. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  223. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == 1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  224. #endif
  225. {
  226. #if HAS_X_MAX
  227. UPDATE_ENDSTOP(X, MAX);
  228. #endif
  229. }
  230. }
  231. #if ENABLED(COREXY) || ENABLED(COREXZ)
  232. }
  233. #endif
  234. #if ENABLED(COREXY)
  235. // Head direction in -Y axis for CoreXY bots.
  236. // If DeltaX == DeltaY, the movement is only in X axis
  237. if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[B_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(B_AXIS))) {
  238. if (stepper.motor_direction(Y_HEAD))
  239. #else
  240. if (stepper.motor_direction(Y_AXIS)) // -direction
  241. #endif
  242. { // -direction
  243. #if HAS_Y_MIN
  244. UPDATE_ENDSTOP(Y, MIN);
  245. #endif
  246. }
  247. else { // +direction
  248. #if HAS_Y_MAX
  249. UPDATE_ENDSTOP(Y, MAX);
  250. #endif
  251. }
  252. #if ENABLED(COREXY)
  253. }
  254. #endif
  255. #if ENABLED(COREXZ)
  256. // Head direction in -Z axis for CoreXZ bots.
  257. // If DeltaX == DeltaZ, the movement is only in X axis
  258. if ((stepper.current_block->steps[A_AXIS] != stepper.current_block->steps[C_AXIS]) || (stepper.motor_direction(A_AXIS) != stepper.motor_direction(C_AXIS))) {
  259. if (stepper.motor_direction(Z_HEAD))
  260. #else
  261. if (stepper.motor_direction(Z_AXIS))
  262. #endif
  263. { // z -direction
  264. #if HAS_Z_MIN
  265. #if ENABLED(Z_DUAL_ENDSTOPS)
  266. UPDATE_ENDSTOP_BIT(Z, MIN);
  267. #if HAS_Z2_MIN
  268. UPDATE_ENDSTOP_BIT(Z2, MIN);
  269. #else
  270. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  271. #endif
  272. test_dual_z_endstops(Z_MIN, Z2_MIN);
  273. #else // !Z_DUAL_ENDSTOPS
  274. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
  275. if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
  276. #else
  277. UPDATE_ENDSTOP(Z, MIN);
  278. #endif
  279. #endif // !Z_DUAL_ENDSTOPS
  280. #endif // HAS_Z_MIN
  281. #if ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) && ENABLED(HAS_Z_MIN_PROBE)
  282. if (z_probe_enabled) {
  283. UPDATE_ENDSTOP(Z, MIN_PROBE);
  284. if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
  285. }
  286. #endif
  287. }
  288. else { // z +direction
  289. #if HAS_Z_MAX
  290. #if ENABLED(Z_DUAL_ENDSTOPS)
  291. UPDATE_ENDSTOP_BIT(Z, MAX);
  292. #if HAS_Z2_MAX
  293. UPDATE_ENDSTOP_BIT(Z2, MAX);
  294. #else
  295. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  296. #endif
  297. test_dual_z_endstops(Z_MAX, Z2_MAX);
  298. #else // !Z_DUAL_ENDSTOPS
  299. UPDATE_ENDSTOP(Z, MAX);
  300. #endif // !Z_DUAL_ENDSTOPS
  301. #endif // Z_MAX_PIN
  302. }
  303. #if ENABLED(COREXZ)
  304. }
  305. #endif
  306. old_endstop_bits = current_endstop_bits;
  307. } // Endstops::update()