endstops.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  32. #include "endstop_interrupts.h"
  33. #endif
  34. Endstops endstops;
  35. // public:
  36. bool Endstops::enabled, Endstops::enabled_globally; // Initialized by settings.load()
  37. volatile uint8_t Endstops::hit_state;
  38. Endstops::esbits_t Endstops::live_state = 0;
  39. #if ENABLED(ENDSTOP_NOISE_FILTER)
  40. Endstops::esbits_t Endstops::validated_live_state;
  41. uint8_t Endstops::endstop_poll_count;
  42. #endif
  43. #if HAS_BED_PROBE
  44. volatile bool Endstops::z_probe_enabled = false;
  45. #endif
  46. // Initialized by settings.load()
  47. #if ENABLED(X_DUAL_ENDSTOPS)
  48. float Endstops::x_endstop_adj;
  49. #endif
  50. #if ENABLED(Y_DUAL_ENDSTOPS)
  51. float Endstops::y_endstop_adj;
  52. #endif
  53. #if ENABLED(Z_DUAL_ENDSTOPS)
  54. float Endstops::z_endstop_adj;
  55. #endif
  56. /**
  57. * Class and Instance Methods
  58. */
  59. void Endstops::init() {
  60. #if HAS_X_MIN
  61. #if ENABLED(ENDSTOPPULLUP_XMIN)
  62. SET_INPUT_PULLUP(X_MIN_PIN);
  63. #else
  64. SET_INPUT(X_MIN_PIN);
  65. #endif
  66. #endif
  67. #if HAS_X2_MIN
  68. #if ENABLED(ENDSTOPPULLUP_XMIN)
  69. SET_INPUT_PULLUP(X2_MIN_PIN);
  70. #else
  71. SET_INPUT(X2_MIN_PIN);
  72. #endif
  73. #endif
  74. #if HAS_Y_MIN
  75. #if ENABLED(ENDSTOPPULLUP_YMIN)
  76. SET_INPUT_PULLUP(Y_MIN_PIN);
  77. #else
  78. SET_INPUT(Y_MIN_PIN);
  79. #endif
  80. #endif
  81. #if HAS_Y2_MIN
  82. #if ENABLED(ENDSTOPPULLUP_YMIN)
  83. SET_INPUT_PULLUP(Y2_MIN_PIN);
  84. #else
  85. SET_INPUT(Y2_MIN_PIN);
  86. #endif
  87. #endif
  88. #if HAS_Z_MIN
  89. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  90. SET_INPUT_PULLUP(Z_MIN_PIN);
  91. #else
  92. SET_INPUT(Z_MIN_PIN);
  93. #endif
  94. #endif
  95. #if HAS_Z2_MIN
  96. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  97. SET_INPUT_PULLUP(Z2_MIN_PIN);
  98. #else
  99. SET_INPUT(Z2_MIN_PIN);
  100. #endif
  101. #endif
  102. #if HAS_X_MAX
  103. #if ENABLED(ENDSTOPPULLUP_XMAX)
  104. SET_INPUT_PULLUP(X_MAX_PIN);
  105. #else
  106. SET_INPUT(X_MAX_PIN);
  107. #endif
  108. #endif
  109. #if HAS_X2_MAX
  110. #if ENABLED(ENDSTOPPULLUP_XMAX)
  111. SET_INPUT_PULLUP(X2_MAX_PIN);
  112. #else
  113. SET_INPUT(X2_MAX_PIN);
  114. #endif
  115. #endif
  116. #if HAS_Y_MAX
  117. #if ENABLED(ENDSTOPPULLUP_YMAX)
  118. SET_INPUT_PULLUP(Y_MAX_PIN);
  119. #else
  120. SET_INPUT(Y_MAX_PIN);
  121. #endif
  122. #endif
  123. #if HAS_Y2_MAX
  124. #if ENABLED(ENDSTOPPULLUP_YMAX)
  125. SET_INPUT_PULLUP(Y2_MAX_PIN);
  126. #else
  127. SET_INPUT(Y2_MAX_PIN);
  128. #endif
  129. #endif
  130. #if HAS_Z_MAX
  131. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  132. SET_INPUT_PULLUP(Z_MAX_PIN);
  133. #else
  134. SET_INPUT(Z_MAX_PIN);
  135. #endif
  136. #endif
  137. #if HAS_Z2_MAX
  138. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  139. SET_INPUT_PULLUP(Z2_MAX_PIN);
  140. #else
  141. SET_INPUT(Z2_MAX_PIN);
  142. #endif
  143. #endif
  144. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  145. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  146. SET_INPUT_PULLUP(Z_MIN_PROBE_PIN);
  147. #else
  148. SET_INPUT(Z_MIN_PROBE_PIN);
  149. #endif
  150. #endif
  151. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  152. setup_endstop_interrupts();
  153. #endif
  154. // Enable endstops
  155. enable_globally(
  156. #if ENABLED(ENDSTOPS_ALWAYS_ON_DEFAULT)
  157. true
  158. #else
  159. false
  160. #endif
  161. );
  162. } // Endstops::init
  163. // Called at ~1KHz from Temperature ISR: Poll endstop state if required
  164. void Endstops::poll() {
  165. #if ENABLED(PINS_DEBUGGING)
  166. run_monitor(); // report changes in endstop status
  167. #endif
  168. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) && ENABLED(ENDSTOP_NOISE_FILTER)
  169. if (endstop_poll_count) update();
  170. #elif DISABLED(ENDSTOP_INTERRUPTS_FEATURE) || ENABLED(ENDSTOP_NOISE_FILTER)
  171. update();
  172. #endif
  173. }
  174. void Endstops::enable_globally(const bool onoff) {
  175. enabled_globally = enabled = onoff;
  176. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  177. update();
  178. #endif
  179. }
  180. // Enable / disable endstop checking
  181. void Endstops::enable(const bool onoff) {
  182. enabled = onoff;
  183. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  184. update();
  185. #endif
  186. }
  187. // Disable / Enable endstops based on ENSTOPS_ONLY_FOR_HOMING and global enable
  188. void Endstops::not_homing() {
  189. enabled = enabled_globally;
  190. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  191. update();
  192. #endif
  193. }
  194. #if ENABLED(VALIDATE_HOMING_ENDSTOPS)
  195. // If the last move failed to trigger an endstop, call kill
  196. void Endstops::validate_homing_move() {
  197. if (trigger_state()) hit_on_purpose();
  198. else kill(PSTR(MSG_ERR_HOMING_FAILED));
  199. }
  200. #endif
  201. // Enable / disable endstop z-probe checking
  202. #if HAS_BED_PROBE
  203. void Endstops::enable_z_probe(const bool onoff) {
  204. z_probe_enabled = onoff;
  205. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  206. update();
  207. #endif
  208. }
  209. #endif
  210. #if ENABLED(PINS_DEBUGGING)
  211. void Endstops::run_monitor() {
  212. if (!monitor_flag) return;
  213. static uint8_t monitor_count = 16; // offset this check from the others
  214. monitor_count += _BV(1); // 15 Hz
  215. monitor_count &= 0x7F;
  216. if (!monitor_count) monitor(); // report changes in endstop status
  217. }
  218. #endif
  219. void Endstops::event_handler() {
  220. static uint8_t prev_hit_state; // = 0
  221. if (hit_state && hit_state != prev_hit_state) {
  222. #if ENABLED(ULTRA_LCD)
  223. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  224. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  225. #else
  226. #define _SET_STOP_CHAR(A,C) ;
  227. #endif
  228. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  229. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", planner.triggered_position_mm(_AXIS(A))); \
  230. _SET_STOP_CHAR(A,C); }while(0)
  231. #define _ENDSTOP_HIT_TEST(A,C) \
  232. if (TEST(hit_state, A ##_MIN) || TEST(hit_state, A ##_MAX)) \
  233. _ENDSTOP_HIT_ECHO(A,C)
  234. #define ENDSTOP_HIT_TEST_X() _ENDSTOP_HIT_TEST(X,'X')
  235. #define ENDSTOP_HIT_TEST_Y() _ENDSTOP_HIT_TEST(Y,'Y')
  236. #define ENDSTOP_HIT_TEST_Z() _ENDSTOP_HIT_TEST(Z,'Z')
  237. SERIAL_ECHO_START();
  238. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  239. ENDSTOP_HIT_TEST_X();
  240. ENDSTOP_HIT_TEST_Y();
  241. ENDSTOP_HIT_TEST_Z();
  242. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  243. #define P_AXIS Z_AXIS
  244. if (TEST(hit_state, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  245. #endif
  246. SERIAL_EOL();
  247. #if ENABLED(ULTRA_LCD)
  248. lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  249. #endif
  250. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  251. if (planner.abort_on_endstop_hit) {
  252. card.sdprinting = false;
  253. card.closefile();
  254. quickstop_stepper();
  255. thermalManager.disable_all_heaters(); // switch off all heaters.
  256. }
  257. #endif
  258. }
  259. prev_hit_state = hit_state;
  260. } // Endstops::report_state
  261. static void print_es_state(const bool is_hit, const char * const label=NULL) {
  262. if (label) serialprintPGM(label);
  263. SERIAL_PROTOCOLPGM(": ");
  264. serialprintPGM(is_hit ? PSTR(MSG_ENDSTOP_HIT) : PSTR(MSG_ENDSTOP_OPEN));
  265. SERIAL_EOL();
  266. }
  267. void _O2 Endstops::M119() {
  268. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  269. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(MSG_##S))
  270. #if HAS_X_MIN
  271. ES_REPORT(X_MIN);
  272. #endif
  273. #if HAS_X2_MIN
  274. ES_REPORT(X2_MIN);
  275. #endif
  276. #if HAS_X_MAX
  277. ES_REPORT(X_MAX);
  278. #endif
  279. #if HAS_X2_MAX
  280. ES_REPORT(X2_MAX);
  281. #endif
  282. #if HAS_Y_MIN
  283. ES_REPORT(Y_MIN);
  284. #endif
  285. #if HAS_Y2_MIN
  286. ES_REPORT(Y2_MIN);
  287. #endif
  288. #if HAS_Y_MAX
  289. ES_REPORT(Y_MAX);
  290. #endif
  291. #if HAS_Y2_MAX
  292. ES_REPORT(Y2_MAX);
  293. #endif
  294. #if HAS_Z_MIN
  295. ES_REPORT(Z_MIN);
  296. #endif
  297. #if HAS_Z2_MIN
  298. ES_REPORT(Z2_MIN);
  299. #endif
  300. #if HAS_Z_MAX
  301. ES_REPORT(Z_MAX);
  302. #endif
  303. #if HAS_Z2_MAX
  304. ES_REPORT(Z2_MAX);
  305. #endif
  306. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  307. print_es_state(READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING, PSTR(MSG_Z_PROBE));
  308. #endif
  309. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  310. #if NUM_RUNOUT_SENSORS == 1
  311. print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
  312. #else
  313. for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
  314. pin_t pin;
  315. switch (i) {
  316. default: continue;
  317. case 1: pin = FIL_RUNOUT_PIN; break;
  318. case 2: pin = FIL_RUNOUT2_PIN; break;
  319. #if NUM_RUNOUT_SENSORS > 2
  320. case 3: pin = FIL_RUNOUT3_PIN; break;
  321. #if NUM_RUNOUT_SENSORS > 3
  322. case 4: pin = FIL_RUNOUT4_PIN; break;
  323. #if NUM_RUNOUT_SENSORS > 4
  324. case 5: pin = FIL_RUNOUT5_PIN; break;
  325. #endif
  326. #endif
  327. #endif
  328. }
  329. SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  330. if (i > 1) { SERIAL_CHAR(' '); SERIAL_CHAR('0' + i); }
  331. print_es_state(digitalRead(pin) != FIL_RUNOUT_INVERTING);
  332. }
  333. #endif
  334. #endif
  335. } // Endstops::M119
  336. // The following routines are called from an ISR context. It could be the temperature ISR, the
  337. // endstop ISR or the Stepper ISR.
  338. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  339. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  340. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  341. // Check endstops - Could be called from Temperature ISR!
  342. void Endstops::update() {
  343. #if DISABLED(ENDSTOP_NOISE_FILTER)
  344. if (!abort_enabled()) return;
  345. #endif
  346. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  347. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  348. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  349. // If G38 command is active check Z_MIN_PROBE for ALL movement
  350. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  351. #endif
  352. // With Dual X, endstops are only checked in the homing direction for the active extruder
  353. #if ENABLED(DUAL_X_CARRIAGE)
  354. #define E0_ACTIVE stepper.movement_extruder() == 0
  355. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  356. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  357. #else
  358. #define X_MIN_TEST true
  359. #define X_MAX_TEST true
  360. #endif
  361. // Use HEAD for core axes, AXIS for others
  362. #if CORE_IS_XY || CORE_IS_XZ
  363. #define X_AXIS_HEAD X_HEAD
  364. #else
  365. #define X_AXIS_HEAD X_AXIS
  366. #endif
  367. #if CORE_IS_XY || CORE_IS_YZ
  368. #define Y_AXIS_HEAD Y_HEAD
  369. #else
  370. #define Y_AXIS_HEAD Y_AXIS
  371. #endif
  372. #if CORE_IS_XZ || CORE_IS_YZ
  373. #define Z_AXIS_HEAD Z_HEAD
  374. #else
  375. #define Z_AXIS_HEAD Z_AXIS
  376. #endif
  377. /**
  378. * Check and update endstops
  379. */
  380. #if HAS_X_MIN
  381. #if ENABLED(X_DUAL_ENDSTOPS)
  382. UPDATE_ENDSTOP_BIT(X, MIN);
  383. #if HAS_X2_MIN
  384. UPDATE_ENDSTOP_BIT(X2, MIN);
  385. #else
  386. COPY_LIVE_STATE(X_MIN, X2_MIN);
  387. #endif
  388. #else
  389. UPDATE_ENDSTOP_BIT(X, MIN);
  390. #endif
  391. #endif
  392. #if HAS_X_MAX
  393. #if ENABLED(X_DUAL_ENDSTOPS)
  394. UPDATE_ENDSTOP_BIT(X, MAX);
  395. #if HAS_X2_MAX
  396. UPDATE_ENDSTOP_BIT(X2, MAX);
  397. #else
  398. COPY_LIVE_STATE(X_MAX, X2_MAX);
  399. #endif
  400. #else
  401. UPDATE_ENDSTOP_BIT(X, MAX);
  402. #endif
  403. #endif
  404. #if HAS_Y_MIN
  405. #if ENABLED(Y_DUAL_ENDSTOPS)
  406. UPDATE_ENDSTOP_BIT(Y, MIN);
  407. #if HAS_Y2_MIN
  408. UPDATE_ENDSTOP_BIT(Y2, MIN);
  409. #else
  410. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  411. #endif
  412. #else
  413. UPDATE_ENDSTOP_BIT(Y, MIN);
  414. #endif
  415. #endif
  416. #if HAS_Y_MAX
  417. #if ENABLED(Y_DUAL_ENDSTOPS)
  418. UPDATE_ENDSTOP_BIT(Y, MAX);
  419. #if HAS_Y2_MAX
  420. UPDATE_ENDSTOP_BIT(Y2, MAX);
  421. #else
  422. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  423. #endif
  424. #else
  425. UPDATE_ENDSTOP_BIT(Y, MAX);
  426. #endif
  427. #endif
  428. #if HAS_Z_MIN
  429. #if ENABLED(Z_DUAL_ENDSTOPS)
  430. UPDATE_ENDSTOP_BIT(Z, MIN);
  431. #if HAS_Z2_MIN
  432. UPDATE_ENDSTOP_BIT(Z2, MIN);
  433. #else
  434. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  435. #endif
  436. #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  437. UPDATE_ENDSTOP_BIT(Z, MIN);
  438. #elif Z_HOME_DIR < 0
  439. UPDATE_ENDSTOP_BIT(Z, MIN);
  440. #endif
  441. #endif
  442. // When closing the gap check the enabled probe
  443. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  444. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  445. #endif
  446. #if HAS_Z_MAX
  447. // Check both Z dual endstops
  448. #if ENABLED(Z_DUAL_ENDSTOPS)
  449. UPDATE_ENDSTOP_BIT(Z, MAX);
  450. #if HAS_Z2_MAX
  451. UPDATE_ENDSTOP_BIT(Z2, MAX);
  452. #else
  453. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  454. #endif
  455. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  456. // If this pin isn't the bed probe it's the Z endstop
  457. UPDATE_ENDSTOP_BIT(Z, MAX);
  458. #endif
  459. #endif
  460. #if ENABLED(ENDSTOP_NOISE_FILTER)
  461. /**
  462. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  463. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  464. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  465. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  466. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  467. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  468. * still exist. The only way to reduce them further is to increase the number of samples.
  469. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  470. */
  471. static esbits_t old_live_state;
  472. if (old_live_state != live_state) {
  473. endstop_poll_count = 7;
  474. old_live_state = live_state;
  475. }
  476. else if (endstop_poll_count && !--endstop_poll_count)
  477. validated_live_state = live_state;
  478. if (!abort_enabled()) return;
  479. #endif
  480. // Test the current status of an endstop
  481. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  482. // Record endstop was hit
  483. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  484. // Call the endstop triggered routine for single endstops
  485. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  486. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  487. _ENDSTOP_HIT(AXIS, MINMAX); \
  488. planner.endstop_triggered(_AXIS(AXIS)); \
  489. } \
  490. }while(0)
  491. // Call the endstop triggered routine for dual endstops
  492. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  493. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
  494. if (dual_hit) { \
  495. _ENDSTOP_HIT(AXIS1, MINMAX); \
  496. /* if not performing home or if both endstops were trigged during homing... */ \
  497. if (!stepper.homing_dual_axis || dual_hit == 0b11) \
  498. planner.endstop_triggered(_AXIS(AXIS1)); \
  499. } \
  500. }while(0)
  501. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  502. // If G38 command is active check Z_MIN_PROBE for ALL movement
  503. if (G38_move) {
  504. if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
  505. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  506. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  507. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  508. G38_endstop_hit = true;
  509. }
  510. }
  511. #endif
  512. // Now, we must signal, after validation, if an endstop limit is pressed or not
  513. if (stepper.axis_is_moving(X_AXIS)) {
  514. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  515. #if HAS_X_MIN
  516. #if ENABLED(X_DUAL_ENDSTOPS)
  517. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  518. #else
  519. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  520. #endif
  521. #endif
  522. }
  523. else { // +direction
  524. #if HAS_X_MAX
  525. #if ENABLED(X_DUAL_ENDSTOPS)
  526. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  527. #else
  528. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  529. #endif
  530. #endif
  531. }
  532. }
  533. if (stepper.axis_is_moving(Y_AXIS)) {
  534. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  535. #if HAS_Y_MIN
  536. #if ENABLED(Y_DUAL_ENDSTOPS)
  537. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  538. #else
  539. PROCESS_ENDSTOP(Y, MIN);
  540. #endif
  541. #endif
  542. }
  543. else { // +direction
  544. #if HAS_Y_MAX
  545. #if ENABLED(Y_DUAL_ENDSTOPS)
  546. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  547. #else
  548. PROCESS_ENDSTOP(Y, MAX);
  549. #endif
  550. #endif
  551. }
  552. }
  553. if (stepper.axis_is_moving(Z_AXIS)) {
  554. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  555. #if HAS_Z_MIN
  556. #if ENABLED(Z_DUAL_ENDSTOPS)
  557. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  558. #else
  559. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  560. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  561. #elif ENABLED(Z_MIN_PROBE_ENDSTOP)
  562. if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  563. #else
  564. PROCESS_ENDSTOP(Z, MIN);
  565. #endif
  566. #endif
  567. #endif
  568. // When closing the gap check the enabled probe
  569. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  570. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  571. #endif
  572. }
  573. else { // Z +direction. Gantry up, bed down.
  574. #if HAS_Z_MAX
  575. #if ENABLED(Z_DUAL_ENDSTOPS)
  576. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  577. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  578. // If this pin is not hijacked for the bed probe
  579. // then it belongs to the Z endstop
  580. PROCESS_ENDSTOP(Z, MAX);
  581. #endif
  582. #endif
  583. }
  584. }
  585. } // Endstops::update()
  586. #if ENABLED(PINS_DEBUGGING)
  587. bool Endstops::monitor_flag = false;
  588. /**
  589. * monitors endstops & Z probe for changes
  590. *
  591. * If a change is detected then the LED is toggled and
  592. * a message is sent out the serial port
  593. *
  594. * Yes, we could miss a rapid back & forth change but
  595. * that won't matter because this is all manual.
  596. *
  597. */
  598. void Endstops::monitor() {
  599. static uint16_t old_live_state_local = 0;
  600. static uint8_t local_LED_status = 0;
  601. uint16_t live_state_local = 0;
  602. #if HAS_X_MIN
  603. if (READ(X_MIN_PIN)) SBI(live_state_local, X_MIN);
  604. #endif
  605. #if HAS_X_MAX
  606. if (READ(X_MAX_PIN)) SBI(live_state_local, X_MAX);
  607. #endif
  608. #if HAS_Y_MIN
  609. if (READ(Y_MIN_PIN)) SBI(live_state_local, Y_MIN);
  610. #endif
  611. #if HAS_Y_MAX
  612. if (READ(Y_MAX_PIN)) SBI(live_state_local, Y_MAX);
  613. #endif
  614. #if HAS_Z_MIN
  615. if (READ(Z_MIN_PIN)) SBI(live_state_local, Z_MIN);
  616. #endif
  617. #if HAS_Z_MAX
  618. if (READ(Z_MAX_PIN)) SBI(live_state_local, Z_MAX);
  619. #endif
  620. #if HAS_Z_MIN_PROBE_PIN
  621. if (READ(Z_MIN_PROBE_PIN)) SBI(live_state_local, Z_MIN_PROBE);
  622. #endif
  623. #if HAS_X2_MIN
  624. if (READ(X2_MIN_PIN)) SBI(live_state_local, X2_MIN);
  625. #endif
  626. #if HAS_X2_MAX
  627. if (READ(X2_MAX_PIN)) SBI(live_state_local, X2_MAX);
  628. #endif
  629. #if HAS_Y2_MIN
  630. if (READ(Y2_MIN_PIN)) SBI(live_state_local, Y2_MIN);
  631. #endif
  632. #if HAS_Y2_MAX
  633. if (READ(Y2_MAX_PIN)) SBI(live_state_local, Y2_MAX);
  634. #endif
  635. #if HAS_Z2_MIN
  636. if (READ(Z2_MIN_PIN)) SBI(live_state_local, Z2_MIN);
  637. #endif
  638. #if HAS_Z2_MAX
  639. if (READ(Z2_MAX_PIN)) SBI(live_state_local, Z2_MAX);
  640. #endif
  641. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  642. if (endstop_change) {
  643. #if HAS_X_MIN
  644. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", TEST(live_state_local, X_MIN));
  645. #endif
  646. #if HAS_X_MAX
  647. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", TEST(live_state_local, X_MAX));
  648. #endif
  649. #if HAS_Y_MIN
  650. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", TEST(live_state_local, Y_MIN));
  651. #endif
  652. #if HAS_Y_MAX
  653. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", TEST(live_state_local, Y_MAX));
  654. #endif
  655. #if HAS_Z_MIN
  656. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", TEST(live_state_local, Z_MIN));
  657. #endif
  658. #if HAS_Z_MAX
  659. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", TEST(live_state_local, Z_MAX));
  660. #endif
  661. #if HAS_Z_MIN_PROBE_PIN
  662. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", TEST(live_state_local, Z_MIN_PROBE));
  663. #endif
  664. #if HAS_X2_MIN
  665. if (TEST(endstop_change, X2_MIN)) SERIAL_PROTOCOLPAIR(" X2_MIN:", TEST(live_state_local, X2_MIN));
  666. #endif
  667. #if HAS_X2_MAX
  668. if (TEST(endstop_change, X2_MAX)) SERIAL_PROTOCOLPAIR(" X2_MAX:", TEST(live_state_local, X2_MAX));
  669. #endif
  670. #if HAS_Y2_MIN
  671. if (TEST(endstop_change, Y2_MIN)) SERIAL_PROTOCOLPAIR(" Y2_MIN:", TEST(live_state_local, Y2_MIN));
  672. #endif
  673. #if HAS_Y2_MAX
  674. if (TEST(endstop_change, Y2_MAX)) SERIAL_PROTOCOLPAIR(" Y2_MAX:", TEST(live_state_local, Y2_MAX));
  675. #endif
  676. #if HAS_Z2_MIN
  677. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", TEST(live_state_local, Z2_MIN));
  678. #endif
  679. #if HAS_Z2_MAX
  680. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", TEST(live_state_local, Z2_MAX));
  681. #endif
  682. SERIAL_PROTOCOLPGM("\n\n");
  683. analogWrite(LED_PIN, local_LED_status);
  684. local_LED_status ^= 255;
  685. old_live_state_local = live_state_local;
  686. }
  687. }
  688. #endif // PINS_DEBUGGING