My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

endstops.cpp 13KB

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