My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

endstops.cpp 25KB

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