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

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