My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

endstops.cpp 23KB

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