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.cpp 11KB

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