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

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