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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. // public:
  35. bool Endstops::enabled = true,
  36. Endstops::enabled_globally =
  37. #if ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT)
  38. (true)
  39. #else
  40. (false)
  41. #endif
  42. ;
  43. volatile char Endstops::endstop_hit_bits; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
  44. #if ENABLED(Z_DUAL_ENDSTOPS)
  45. uint16_t
  46. #else
  47. byte
  48. #endif
  49. Endstops::current_endstop_bits = 0,
  50. Endstops::old_endstop_bits = 0;
  51. #if HAS_BED_PROBE
  52. volatile bool Endstops::z_probe_enabled = false;
  53. #endif
  54. /**
  55. * Class and Instance Methods
  56. */
  57. void Endstops::init() {
  58. #if HAS_X_MIN
  59. #if ENABLED(ENDSTOPPULLUP_XMIN)
  60. SET_INPUT_PULLUP(X_MIN_PIN);
  61. #else
  62. SET_INPUT(X_MIN_PIN);
  63. #endif
  64. #endif
  65. #if HAS_Y_MIN
  66. #if ENABLED(ENDSTOPPULLUP_YMIN)
  67. SET_INPUT_PULLUP(Y_MIN_PIN);
  68. #else
  69. SET_INPUT(Y_MIN_PIN);
  70. #endif
  71. #endif
  72. #if HAS_Z_MIN
  73. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  74. SET_INPUT_PULLUP(Z_MIN_PIN);
  75. #else
  76. SET_INPUT(Z_MIN_PIN);
  77. #endif
  78. #endif
  79. #if HAS_Z2_MIN
  80. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  81. SET_INPUT_PULLUP(Z2_MIN_PIN);
  82. #else
  83. SET_INPUT(Z2_MIN_PIN);
  84. #endif
  85. #endif
  86. #if HAS_X_MAX
  87. #if ENABLED(ENDSTOPPULLUP_XMAX)
  88. SET_INPUT_PULLUP(X_MAX_PIN);
  89. #else
  90. SET_INPUT(X_MAX_PIN);
  91. #endif
  92. #endif
  93. #if HAS_Y_MAX
  94. #if ENABLED(ENDSTOPPULLUP_YMAX)
  95. SET_INPUT_PULLUP(Y_MAX_PIN);
  96. #else
  97. SET_INPUT(Y_MAX_PIN);
  98. #endif
  99. #endif
  100. #if HAS_Z_MAX
  101. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  102. SET_INPUT_PULLUP(Z_MAX_PIN);
  103. #else
  104. SET_INPUT(Z_MAX_PIN);
  105. #endif
  106. #endif
  107. #if HAS_Z2_MAX
  108. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  109. SET_INPUT_PULLUP(Z2_MAX_PIN);
  110. #else
  111. SET_INPUT(Z2_MAX_PIN);
  112. #endif
  113. #endif
  114. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  115. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  116. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  117. #else
  118. SET_INPUT(Z_MIN_PROBE_PIN);
  119. #endif
  120. #endif
  121. } // Endstops::init
  122. void Endstops::report_state() {
  123. if (endstop_hit_bits) {
  124. #if ENABLED(ULTRA_LCD)
  125. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  126. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  127. #else
  128. #define _SET_STOP_CHAR(A,C) ;
  129. #endif
  130. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  131. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
  132. _SET_STOP_CHAR(A,C); }while(0)
  133. #define _ENDSTOP_HIT_TEST(A,C) \
  134. if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
  135. _ENDSTOP_HIT_ECHO(A,C)
  136. SERIAL_ECHO_START;
  137. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  138. _ENDSTOP_HIT_TEST(X, 'X');
  139. _ENDSTOP_HIT_TEST(Y, 'Y');
  140. _ENDSTOP_HIT_TEST(Z, 'Z');
  141. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  142. #define P_AXIS Z_AXIS
  143. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  144. #endif
  145. SERIAL_EOL;
  146. #if ENABLED(ULTRA_LCD)
  147. lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  148. #endif
  149. hit_on_purpose();
  150. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  151. if (stepper.abort_on_endstop_hit) {
  152. card.sdprinting = false;
  153. card.closefile();
  154. quickstop_stepper();
  155. thermalManager.disable_all_heaters(); // switch off all heaters.
  156. }
  157. #endif
  158. }
  159. } // Endstops::report_state
  160. void Endstops::M119() {
  161. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  162. #if HAS_X_MIN
  163. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  164. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  165. #endif
  166. #if HAS_X_MAX
  167. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  168. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  169. #endif
  170. #if HAS_Y_MIN
  171. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  172. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  173. #endif
  174. #if HAS_Y_MAX
  175. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  176. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  177. #endif
  178. #if HAS_Z_MIN
  179. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  180. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  181. #endif
  182. #if HAS_Z2_MIN
  183. SERIAL_PROTOCOLPGM(MSG_Z2_MIN);
  184. SERIAL_PROTOCOLLN(((READ(Z2_MIN_PIN)^Z2_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  185. #endif
  186. #if HAS_Z_MAX
  187. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  188. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  189. #endif
  190. #if HAS_Z2_MAX
  191. SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
  192. SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  193. #endif
  194. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  195. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  196. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  197. #endif
  198. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  199. SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  200. SERIAL_PROTOCOLLN(((READ(FIL_RUNOUT_PIN)^FIL_RUNOUT_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  201. #endif
  202. } // Endstops::M119
  203. #if ENABLED(Z_DUAL_ENDSTOPS)
  204. // Pass the result of the endstop test
  205. void Endstops::test_dual_z_endstops(const EndstopEnum es1, const EndstopEnum es2) {
  206. byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
  207. if (z_test && stepper.current_block->steps[Z_AXIS] > 0) {
  208. SBI(endstop_hit_bits, Z_MIN);
  209. if (!stepper.performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  210. stepper.kill_current_block();
  211. }
  212. }
  213. #endif
  214. // Check endstops - Called from ISR!
  215. void Endstops::update() {
  216. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  217. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  218. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  219. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  220. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  221. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  222. // COPY_BIT: copy the value of SRC_BIT to DST_BIT in DST
  223. #define COPY_BIT(DST, SRC_BIT, DST_BIT) SET_BIT(DST, DST_BIT, TEST(DST, SRC_BIT))
  224. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  225. UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
  226. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
  227. _ENDSTOP_HIT(AXIS); \
  228. stepper.endstop_triggered(_AXIS(AXIS)); \
  229. } \
  230. } while(0)
  231. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  232. // If G38 command is active check Z_MIN_PROBE for ALL movement
  233. if (G38_move) {
  234. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  235. if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
  236. if (stepper.current_block->steps[_AXIS(X)] > 0) { _ENDSTOP_HIT(X); stepper.endstop_triggered(_AXIS(X)); }
  237. else if (stepper.current_block->steps[_AXIS(Y)] > 0) { _ENDSTOP_HIT(Y); stepper.endstop_triggered(_AXIS(Y)); }
  238. else if (stepper.current_block->steps[_AXIS(Z)] > 0) { _ENDSTOP_HIT(Z); stepper.endstop_triggered(_AXIS(Z)); }
  239. G38_endstop_hit = true;
  240. }
  241. }
  242. #endif
  243. #if ENABLED(COREXY) || ENABLED(COREXZ)
  244. #define CORE_X_CMP ==
  245. #elif ENABLED(COREYX) || ENABLED(COREZX)
  246. #define CORE_X_CMP !=
  247. #endif
  248. /**
  249. * Head direction in -X axis for CoreXY and CoreXZ bots.
  250. *
  251. * If steps differ, both axes are moving.
  252. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z, handled below)
  253. * If DeltaA == DeltaB, the movement is only in the 1st axis (X)
  254. */
  255. #if CORE_IS_XY || CORE_IS_XZ
  256. if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
  257. || ( stepper.current_block->steps[CORE_AXIS_1] > 0
  258. && stepper.motor_direction(CORE_AXIS_1) CORE_X_CMP stepper.motor_direction(CORE_AXIS_2)
  259. )
  260. ) {
  261. if (stepper.motor_direction(X_HEAD))
  262. #else
  263. if (stepper.current_block->steps[X_AXIS] > 0)
  264. if (stepper.motor_direction(X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  265. #endif
  266. { // -direction
  267. #if ENABLED(DUAL_X_CARRIAGE)
  268. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  269. if ( (stepper.current_block->active_extruder == 0 && X_HOME_DIR < 0)
  270. || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR < 0)
  271. )
  272. #endif
  273. {
  274. #if HAS_X_MIN
  275. UPDATE_ENDSTOP(X, MIN);
  276. #endif
  277. }
  278. }
  279. else { // +direction
  280. #if ENABLED(DUAL_X_CARRIAGE)
  281. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  282. if ( (stepper.current_block->active_extruder == 0 && X_HOME_DIR > 0)
  283. || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR > 0)
  284. )
  285. #endif
  286. {
  287. #if HAS_X_MAX
  288. UPDATE_ENDSTOP(X, MAX);
  289. #endif
  290. }
  291. }
  292. #if CORE_IS_XY || CORE_IS_XZ
  293. }
  294. #endif
  295. // Handle swapped vs. typical Core axis order
  296. #if ENABLED(COREYX) || ENABLED(COREYZ)
  297. #define CORE_YZ_CMP ==
  298. #elif ENABLED(COREXY) || ENABLED(COREZY)
  299. #define CORE_YZ_CMP !=
  300. #endif
  301. #if CORE_IS_XY || CORE_IS_YZ
  302. /**
  303. * Head direction in -Y axis for CoreXY / CoreYZ bots.
  304. *
  305. * If steps differ, both axes are moving
  306. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y)
  307. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Y or Z)
  308. */
  309. if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
  310. || ( stepper.current_block->steps[CORE_AXIS_1] > 0
  311. && stepper.motor_direction(CORE_AXIS_1) CORE_YZ_CMP stepper.motor_direction(CORE_AXIS_2)
  312. )
  313. ) {
  314. if (stepper.motor_direction(Y_HEAD))
  315. #else
  316. if (stepper.current_block->steps[Y_AXIS] > 0)
  317. if (stepper.motor_direction(Y_AXIS)) // -direction
  318. #endif
  319. { // -direction
  320. #if HAS_Y_MIN
  321. UPDATE_ENDSTOP(Y, MIN);
  322. #endif
  323. }
  324. else { // +direction
  325. #if HAS_Y_MAX
  326. UPDATE_ENDSTOP(Y, MAX);
  327. #endif
  328. }
  329. #if CORE_IS_XY || CORE_IS_YZ
  330. }
  331. #endif
  332. #if ENABLED(COREZX) || ENABLED(COREZY)
  333. #define CORE_YZ_CMP ==
  334. #elif ENABLED(COREXZ) || ENABLED(COREYZ)
  335. #define CORE_YZ_CMP !=
  336. #endif
  337. #if CORE_IS_XZ || CORE_IS_YZ
  338. /**
  339. * Head direction in -Z axis for CoreXZ or CoreYZ bots.
  340. *
  341. * If steps differ, both axes are moving
  342. * If DeltaA == DeltaB, the movement is only in the 1st axis (X or Y, already handled above)
  343. * If DeltaA == -DeltaB, the movement is only in the 2nd axis (Z)
  344. */
  345. if (stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]
  346. || ( stepper.current_block->steps[CORE_AXIS_1] > 0
  347. && stepper.motor_direction(CORE_AXIS_1) CORE_YZ_CMP stepper.motor_direction(CORE_AXIS_2)
  348. )
  349. ) {
  350. if (stepper.motor_direction(Z_HEAD))
  351. #else
  352. if (stepper.current_block->steps[Z_AXIS] > 0)
  353. if (stepper.motor_direction(Z_AXIS))
  354. #endif
  355. { // Z -direction. Gantry down, bed up.
  356. #if HAS_Z_MIN
  357. #if ENABLED(Z_DUAL_ENDSTOPS)
  358. UPDATE_ENDSTOP_BIT(Z, MIN);
  359. #if HAS_Z2_MIN
  360. UPDATE_ENDSTOP_BIT(Z2, MIN);
  361. #else
  362. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  363. #endif
  364. test_dual_z_endstops(Z_MIN, Z2_MIN);
  365. #else // !Z_DUAL_ENDSTOPS
  366. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  367. if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
  368. #else
  369. UPDATE_ENDSTOP(Z, MIN);
  370. #endif
  371. #endif // !Z_DUAL_ENDSTOPS
  372. #endif // HAS_Z_MIN
  373. // When closing the gap check the enabled probe
  374. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  375. if (z_probe_enabled) {
  376. UPDATE_ENDSTOP(Z, MIN_PROBE);
  377. if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
  378. }
  379. #endif
  380. }
  381. else { // Z +direction. Gantry up, bed down.
  382. #if HAS_Z_MAX
  383. // Check both Z dual endstops
  384. #if ENABLED(Z_DUAL_ENDSTOPS)
  385. UPDATE_ENDSTOP_BIT(Z, MAX);
  386. #if HAS_Z2_MAX
  387. UPDATE_ENDSTOP_BIT(Z2, MAX);
  388. #else
  389. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  390. #endif
  391. test_dual_z_endstops(Z_MAX, Z2_MAX);
  392. // If this pin is not hijacked for the bed probe
  393. // then it belongs to the Z endstop
  394. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  395. UPDATE_ENDSTOP(Z, MAX);
  396. #endif // !Z_MIN_PROBE_PIN...
  397. #endif // Z_MAX_PIN
  398. }
  399. #if CORE_IS_XZ || CORE_IS_YZ
  400. }
  401. #endif
  402. old_endstop_bits = current_endstop_bits;
  403. } // Endstops::update()