endstops.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. #if ENABLED(BLTOUCH)
  269. extern void _bltouch_set_SW_mode();
  270. _bltouch_set_SW_mode();
  271. #endif
  272. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  273. #define ES_REPORT(S) print_es_state(READ(S##_PIN) != S##_ENDSTOP_INVERTING, PSTR(MSG_##S))
  274. #if HAS_X_MIN
  275. ES_REPORT(X_MIN);
  276. #endif
  277. #if HAS_X2_MIN
  278. ES_REPORT(X2_MIN);
  279. #endif
  280. #if HAS_X_MAX
  281. ES_REPORT(X_MAX);
  282. #endif
  283. #if HAS_X2_MAX
  284. ES_REPORT(X2_MAX);
  285. #endif
  286. #if HAS_Y_MIN
  287. ES_REPORT(Y_MIN);
  288. #endif
  289. #if HAS_Y2_MIN
  290. ES_REPORT(Y2_MIN);
  291. #endif
  292. #if HAS_Y_MAX
  293. ES_REPORT(Y_MAX);
  294. #endif
  295. #if HAS_Y2_MAX
  296. ES_REPORT(Y2_MAX);
  297. #endif
  298. #if HAS_Z_MIN
  299. ES_REPORT(Z_MIN);
  300. #endif
  301. #if HAS_Z2_MIN
  302. ES_REPORT(Z2_MIN);
  303. #endif
  304. #if HAS_Z_MAX
  305. ES_REPORT(Z_MAX);
  306. #endif
  307. #if HAS_Z2_MAX
  308. ES_REPORT(Z2_MAX);
  309. #endif
  310. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  311. print_es_state(READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING, PSTR(MSG_Z_PROBE));
  312. #endif
  313. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  314. #if NUM_RUNOUT_SENSORS == 1
  315. print_es_state(READ(FIL_RUNOUT_PIN) != FIL_RUNOUT_INVERTING, PSTR(MSG_FILAMENT_RUNOUT_SENSOR));
  316. #else
  317. for (uint8_t i = 1; i <= NUM_RUNOUT_SENSORS; i++) {
  318. pin_t pin;
  319. switch (i) {
  320. default: continue;
  321. case 1: pin = FIL_RUNOUT_PIN; break;
  322. case 2: pin = FIL_RUNOUT2_PIN; break;
  323. #if NUM_RUNOUT_SENSORS > 2
  324. case 3: pin = FIL_RUNOUT3_PIN; break;
  325. #if NUM_RUNOUT_SENSORS > 3
  326. case 4: pin = FIL_RUNOUT4_PIN; break;
  327. #if NUM_RUNOUT_SENSORS > 4
  328. case 5: pin = FIL_RUNOUT5_PIN; break;
  329. #endif
  330. #endif
  331. #endif
  332. }
  333. SERIAL_PROTOCOLPGM(MSG_FILAMENT_RUNOUT_SENSOR);
  334. if (i > 1) { SERIAL_CHAR(' '); SERIAL_CHAR('0' + i); }
  335. print_es_state(digitalRead(pin) != FIL_RUNOUT_INVERTING);
  336. }
  337. #endif
  338. #endif
  339. #if ENABLED(BLTOUCH)
  340. extern void _bltouch_reset_SW_mode();
  341. _bltouch_reset_SW_mode();
  342. #endif
  343. } // Endstops::M119
  344. // The following routines are called from an ISR context. It could be the temperature ISR, the
  345. // endstop ISR or the Stepper ISR.
  346. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  347. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  348. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  349. // Check endstops - Could be called from Temperature ISR!
  350. void Endstops::update() {
  351. #if DISABLED(ENDSTOP_NOISE_FILTER)
  352. if (!abort_enabled()) return;
  353. #endif
  354. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT_TO(live_state, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  355. #define COPY_LIVE_STATE(SRC_BIT, DST_BIT) SET_BIT_TO(live_state, DST_BIT, TEST(live_state, SRC_BIT))
  356. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  357. // If G38 command is active check Z_MIN_PROBE for ALL movement
  358. if (G38_move) UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  359. #endif
  360. // With Dual X, endstops are only checked in the homing direction for the active extruder
  361. #if ENABLED(DUAL_X_CARRIAGE)
  362. #define E0_ACTIVE stepper.movement_extruder() == 0
  363. #define X_MIN_TEST ((X_HOME_DIR < 0 && E0_ACTIVE) || (X2_HOME_DIR < 0 && !E0_ACTIVE))
  364. #define X_MAX_TEST ((X_HOME_DIR > 0 && E0_ACTIVE) || (X2_HOME_DIR > 0 && !E0_ACTIVE))
  365. #else
  366. #define X_MIN_TEST true
  367. #define X_MAX_TEST true
  368. #endif
  369. // Use HEAD for core axes, AXIS for others
  370. #if CORE_IS_XY || CORE_IS_XZ
  371. #define X_AXIS_HEAD X_HEAD
  372. #else
  373. #define X_AXIS_HEAD X_AXIS
  374. #endif
  375. #if CORE_IS_XY || CORE_IS_YZ
  376. #define Y_AXIS_HEAD Y_HEAD
  377. #else
  378. #define Y_AXIS_HEAD Y_AXIS
  379. #endif
  380. #if CORE_IS_XZ || CORE_IS_YZ
  381. #define Z_AXIS_HEAD Z_HEAD
  382. #else
  383. #define Z_AXIS_HEAD Z_AXIS
  384. #endif
  385. /**
  386. * Check and update endstops
  387. */
  388. #if HAS_X_MIN
  389. #if ENABLED(X_DUAL_ENDSTOPS)
  390. UPDATE_ENDSTOP_BIT(X, MIN);
  391. #if HAS_X2_MIN
  392. UPDATE_ENDSTOP_BIT(X2, MIN);
  393. #else
  394. COPY_LIVE_STATE(X_MIN, X2_MIN);
  395. #endif
  396. #else
  397. UPDATE_ENDSTOP_BIT(X, MIN);
  398. #endif
  399. #endif
  400. #if HAS_X_MAX
  401. #if ENABLED(X_DUAL_ENDSTOPS)
  402. UPDATE_ENDSTOP_BIT(X, MAX);
  403. #if HAS_X2_MAX
  404. UPDATE_ENDSTOP_BIT(X2, MAX);
  405. #else
  406. COPY_LIVE_STATE(X_MAX, X2_MAX);
  407. #endif
  408. #else
  409. UPDATE_ENDSTOP_BIT(X, MAX);
  410. #endif
  411. #endif
  412. #if HAS_Y_MIN
  413. #if ENABLED(Y_DUAL_ENDSTOPS)
  414. UPDATE_ENDSTOP_BIT(Y, MIN);
  415. #if HAS_Y2_MIN
  416. UPDATE_ENDSTOP_BIT(Y2, MIN);
  417. #else
  418. COPY_LIVE_STATE(Y_MIN, Y2_MIN);
  419. #endif
  420. #else
  421. UPDATE_ENDSTOP_BIT(Y, MIN);
  422. #endif
  423. #endif
  424. #if HAS_Y_MAX
  425. #if ENABLED(Y_DUAL_ENDSTOPS)
  426. UPDATE_ENDSTOP_BIT(Y, MAX);
  427. #if HAS_Y2_MAX
  428. UPDATE_ENDSTOP_BIT(Y2, MAX);
  429. #else
  430. COPY_LIVE_STATE(Y_MAX, Y2_MAX);
  431. #endif
  432. #else
  433. UPDATE_ENDSTOP_BIT(Y, MAX);
  434. #endif
  435. #endif
  436. #if HAS_Z_MIN
  437. #if ENABLED(Z_DUAL_ENDSTOPS)
  438. UPDATE_ENDSTOP_BIT(Z, MIN);
  439. #if HAS_Z2_MIN
  440. UPDATE_ENDSTOP_BIT(Z2, MIN);
  441. #else
  442. COPY_LIVE_STATE(Z_MIN, Z2_MIN);
  443. #endif
  444. #elif ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  445. UPDATE_ENDSTOP_BIT(Z, MIN);
  446. #elif Z_HOME_DIR < 0
  447. UPDATE_ENDSTOP_BIT(Z, MIN);
  448. #endif
  449. #endif
  450. // When closing the gap check the enabled probe
  451. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  452. UPDATE_ENDSTOP_BIT(Z, MIN_PROBE);
  453. #endif
  454. #if HAS_Z_MAX
  455. // Check both Z dual endstops
  456. #if ENABLED(Z_DUAL_ENDSTOPS)
  457. UPDATE_ENDSTOP_BIT(Z, MAX);
  458. #if HAS_Z2_MAX
  459. UPDATE_ENDSTOP_BIT(Z2, MAX);
  460. #else
  461. COPY_LIVE_STATE(Z_MAX, Z2_MAX);
  462. #endif
  463. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  464. // If this pin isn't the bed probe it's the Z endstop
  465. UPDATE_ENDSTOP_BIT(Z, MAX);
  466. #endif
  467. #endif
  468. #if ENABLED(ENDSTOP_NOISE_FILTER)
  469. /**
  470. * Filtering out noise on endstops requires a delayed decision. Let's assume, due to noise,
  471. * that 50% of endstop signal samples are good and 50% are bad (assuming normal distribution
  472. * of random noise). Then the first sample has a 50% chance to be good or bad. The 2nd sample
  473. * also has a 50% chance to be good or bad. The chances of 2 samples both being bad becomes
  474. * 50% of 50%, or 25%. That was the previous implementation of Marlin endstop handling. It
  475. * reduces chances of bad readings in half, at the cost of 1 extra sample period, but chances
  476. * still exist. The only way to reduce them further is to increase the number of samples.
  477. * To reduce the chance to 1% (1/128th) requires 7 samples (adding 7ms of delay).
  478. */
  479. static esbits_t old_live_state;
  480. if (old_live_state != live_state) {
  481. endstop_poll_count = 7;
  482. old_live_state = live_state;
  483. }
  484. else if (endstop_poll_count && !--endstop_poll_count)
  485. validated_live_state = live_state;
  486. if (!abort_enabled()) return;
  487. #endif
  488. // Test the current status of an endstop
  489. #define TEST_ENDSTOP(ENDSTOP) (TEST(state(), ENDSTOP))
  490. // Record endstop was hit
  491. #define _ENDSTOP_HIT(AXIS, MINMAX) SBI(hit_state, _ENDSTOP(AXIS, MINMAX))
  492. // Call the endstop triggered routine for single endstops
  493. #define PROCESS_ENDSTOP(AXIS,MINMAX) do { \
  494. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX))) { \
  495. _ENDSTOP_HIT(AXIS, MINMAX); \
  496. planner.endstop_triggered(_AXIS(AXIS)); \
  497. } \
  498. }while(0)
  499. // Call the endstop triggered routine for dual endstops
  500. #define PROCESS_DUAL_ENDSTOP(AXIS1, AXIS2, MINMAX) do { \
  501. const byte dual_hit = TEST_ENDSTOP(_ENDSTOP(AXIS1, MINMAX)) | (TEST_ENDSTOP(_ENDSTOP(AXIS2, MINMAX)) << 1); \
  502. if (dual_hit) { \
  503. _ENDSTOP_HIT(AXIS1, MINMAX); \
  504. /* if not performing home or if both endstops were trigged during homing... */ \
  505. if (!stepper.homing_dual_axis || dual_hit == 0b11) \
  506. planner.endstop_triggered(_AXIS(AXIS1)); \
  507. } \
  508. }while(0)
  509. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN_PROBE) && !(CORE_IS_XY || CORE_IS_XZ)
  510. // If G38 command is active check Z_MIN_PROBE for ALL movement
  511. if (G38_move) {
  512. if (TEST_ENDSTOP(_ENDSTOP(Z, MIN_PROBE))) {
  513. if (stepper.axis_is_moving(X_AXIS)) { _ENDSTOP_HIT(X, MIN); planner.endstop_triggered(X_AXIS); }
  514. else if (stepper.axis_is_moving(Y_AXIS)) { _ENDSTOP_HIT(Y, MIN); planner.endstop_triggered(Y_AXIS); }
  515. else if (stepper.axis_is_moving(Z_AXIS)) { _ENDSTOP_HIT(Z, MIN); planner.endstop_triggered(Z_AXIS); }
  516. G38_endstop_hit = true;
  517. }
  518. }
  519. #endif
  520. // Now, we must signal, after validation, if an endstop limit is pressed or not
  521. if (stepper.axis_is_moving(X_AXIS)) {
  522. if (stepper.motor_direction(X_AXIS_HEAD)) { // -direction
  523. #if HAS_X_MIN
  524. #if ENABLED(X_DUAL_ENDSTOPS)
  525. PROCESS_DUAL_ENDSTOP(X, X2, MIN);
  526. #else
  527. if (X_MIN_TEST) PROCESS_ENDSTOP(X, MIN);
  528. #endif
  529. #endif
  530. }
  531. else { // +direction
  532. #if HAS_X_MAX
  533. #if ENABLED(X_DUAL_ENDSTOPS)
  534. PROCESS_DUAL_ENDSTOP(X, X2, MAX);
  535. #else
  536. if (X_MAX_TEST) PROCESS_ENDSTOP(X, MAX);
  537. #endif
  538. #endif
  539. }
  540. }
  541. if (stepper.axis_is_moving(Y_AXIS)) {
  542. if (stepper.motor_direction(Y_AXIS_HEAD)) { // -direction
  543. #if HAS_Y_MIN
  544. #if ENABLED(Y_DUAL_ENDSTOPS)
  545. PROCESS_DUAL_ENDSTOP(Y, Y2, MIN);
  546. #else
  547. PROCESS_ENDSTOP(Y, MIN);
  548. #endif
  549. #endif
  550. }
  551. else { // +direction
  552. #if HAS_Y_MAX
  553. #if ENABLED(Y_DUAL_ENDSTOPS)
  554. PROCESS_DUAL_ENDSTOP(Y, Y2, MAX);
  555. #else
  556. PROCESS_ENDSTOP(Y, MAX);
  557. #endif
  558. #endif
  559. }
  560. }
  561. if (stepper.axis_is_moving(Z_AXIS)) {
  562. if (stepper.motor_direction(Z_AXIS_HEAD)) { // Z -direction. Gantry down, bed up.
  563. #if HAS_Z_MIN
  564. #if ENABLED(Z_DUAL_ENDSTOPS)
  565. PROCESS_DUAL_ENDSTOP(Z, Z2, MIN);
  566. #else
  567. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  568. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  569. #elif ENABLED(Z_MIN_PROBE_ENDSTOP)
  570. if (!z_probe_enabled) PROCESS_ENDSTOP(Z, MIN);
  571. #else
  572. PROCESS_ENDSTOP(Z, MIN);
  573. #endif
  574. #endif
  575. #endif
  576. // When closing the gap check the enabled probe
  577. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  578. if (z_probe_enabled) PROCESS_ENDSTOP(Z, MIN_PROBE);
  579. #endif
  580. }
  581. else { // Z +direction. Gantry up, bed down.
  582. #if HAS_Z_MAX
  583. #if ENABLED(Z_DUAL_ENDSTOPS)
  584. PROCESS_DUAL_ENDSTOP(Z, Z2, MAX);
  585. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  586. // If this pin is not hijacked for the bed probe
  587. // then it belongs to the Z endstop
  588. PROCESS_ENDSTOP(Z, MAX);
  589. #endif
  590. #endif
  591. }
  592. }
  593. } // Endstops::update()
  594. #if ENABLED(PINS_DEBUGGING)
  595. bool Endstops::monitor_flag = false;
  596. /**
  597. * monitors endstops & Z probe for changes
  598. *
  599. * If a change is detected then the LED is toggled and
  600. * a message is sent out the serial port
  601. *
  602. * Yes, we could miss a rapid back & forth change but
  603. * that won't matter because this is all manual.
  604. *
  605. */
  606. void Endstops::monitor() {
  607. static uint16_t old_live_state_local = 0;
  608. static uint8_t local_LED_status = 0;
  609. uint16_t live_state_local = 0;
  610. #if HAS_X_MIN
  611. if (READ(X_MIN_PIN)) SBI(live_state_local, X_MIN);
  612. #endif
  613. #if HAS_X_MAX
  614. if (READ(X_MAX_PIN)) SBI(live_state_local, X_MAX);
  615. #endif
  616. #if HAS_Y_MIN
  617. if (READ(Y_MIN_PIN)) SBI(live_state_local, Y_MIN);
  618. #endif
  619. #if HAS_Y_MAX
  620. if (READ(Y_MAX_PIN)) SBI(live_state_local, Y_MAX);
  621. #endif
  622. #if HAS_Z_MIN
  623. if (READ(Z_MIN_PIN)) SBI(live_state_local, Z_MIN);
  624. #endif
  625. #if HAS_Z_MAX
  626. if (READ(Z_MAX_PIN)) SBI(live_state_local, Z_MAX);
  627. #endif
  628. #if HAS_Z_MIN_PROBE_PIN
  629. if (READ(Z_MIN_PROBE_PIN)) SBI(live_state_local, Z_MIN_PROBE);
  630. #endif
  631. #if HAS_X2_MIN
  632. if (READ(X2_MIN_PIN)) SBI(live_state_local, X2_MIN);
  633. #endif
  634. #if HAS_X2_MAX
  635. if (READ(X2_MAX_PIN)) SBI(live_state_local, X2_MAX);
  636. #endif
  637. #if HAS_Y2_MIN
  638. if (READ(Y2_MIN_PIN)) SBI(live_state_local, Y2_MIN);
  639. #endif
  640. #if HAS_Y2_MAX
  641. if (READ(Y2_MAX_PIN)) SBI(live_state_local, Y2_MAX);
  642. #endif
  643. #if HAS_Z2_MIN
  644. if (READ(Z2_MIN_PIN)) SBI(live_state_local, Z2_MIN);
  645. #endif
  646. #if HAS_Z2_MAX
  647. if (READ(Z2_MAX_PIN)) SBI(live_state_local, Z2_MAX);
  648. #endif
  649. uint16_t endstop_change = live_state_local ^ old_live_state_local;
  650. if (endstop_change) {
  651. #if HAS_X_MIN
  652. if (TEST(endstop_change, X_MIN)) SERIAL_PROTOCOLPAIR(" X_MIN:", TEST(live_state_local, X_MIN));
  653. #endif
  654. #if HAS_X_MAX
  655. if (TEST(endstop_change, X_MAX)) SERIAL_PROTOCOLPAIR(" X_MAX:", TEST(live_state_local, X_MAX));
  656. #endif
  657. #if HAS_Y_MIN
  658. if (TEST(endstop_change, Y_MIN)) SERIAL_PROTOCOLPAIR(" Y_MIN:", TEST(live_state_local, Y_MIN));
  659. #endif
  660. #if HAS_Y_MAX
  661. if (TEST(endstop_change, Y_MAX)) SERIAL_PROTOCOLPAIR(" Y_MAX:", TEST(live_state_local, Y_MAX));
  662. #endif
  663. #if HAS_Z_MIN
  664. if (TEST(endstop_change, Z_MIN)) SERIAL_PROTOCOLPAIR(" Z_MIN:", TEST(live_state_local, Z_MIN));
  665. #endif
  666. #if HAS_Z_MAX
  667. if (TEST(endstop_change, Z_MAX)) SERIAL_PROTOCOLPAIR(" Z_MAX:", TEST(live_state_local, Z_MAX));
  668. #endif
  669. #if HAS_Z_MIN_PROBE_PIN
  670. if (TEST(endstop_change, Z_MIN_PROBE)) SERIAL_PROTOCOLPAIR(" PROBE:", TEST(live_state_local, Z_MIN_PROBE));
  671. #endif
  672. #if HAS_X2_MIN
  673. if (TEST(endstop_change, X2_MIN)) SERIAL_PROTOCOLPAIR(" X2_MIN:", TEST(live_state_local, X2_MIN));
  674. #endif
  675. #if HAS_X2_MAX
  676. if (TEST(endstop_change, X2_MAX)) SERIAL_PROTOCOLPAIR(" X2_MAX:", TEST(live_state_local, X2_MAX));
  677. #endif
  678. #if HAS_Y2_MIN
  679. if (TEST(endstop_change, Y2_MIN)) SERIAL_PROTOCOLPAIR(" Y2_MIN:", TEST(live_state_local, Y2_MIN));
  680. #endif
  681. #if HAS_Y2_MAX
  682. if (TEST(endstop_change, Y2_MAX)) SERIAL_PROTOCOLPAIR(" Y2_MAX:", TEST(live_state_local, Y2_MAX));
  683. #endif
  684. #if HAS_Z2_MIN
  685. if (TEST(endstop_change, Z2_MIN)) SERIAL_PROTOCOLPAIR(" Z2_MIN:", TEST(live_state_local, Z2_MIN));
  686. #endif
  687. #if HAS_Z2_MAX
  688. if (TEST(endstop_change, Z2_MAX)) SERIAL_PROTOCOLPAIR(" Z2_MAX:", TEST(live_state_local, Z2_MAX));
  689. #endif
  690. SERIAL_PROTOCOLPGM("\n\n");
  691. analogWrite(LED_PIN, local_LED_status);
  692. local_LED_status ^= 255;
  693. old_live_state_local = live_state_local;
  694. }
  695. }
  696. #endif // PINS_DEBUGGING