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

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