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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * endstops.cpp - A singleton object to manage endstops
  24. */
  25. #include "Marlin.h"
  26. #include "cardreader.h"
  27. #include "endstops.h"
  28. #include "temperature.h"
  29. #include "stepper.h"
  30. #include "ultralcd.h"
  31. // TEST_ENDSTOP: test the old and the current status of an endstop
  32. #define TEST_ENDSTOP(ENDSTOP) (TEST(current_endstop_bits & old_endstop_bits, ENDSTOP))
  33. Endstops endstops;
  34. // public:
  35. bool Endstops::enabled = true,
  36. Endstops::enabled_globally =
  37. #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
  38. false
  39. #else
  40. true
  41. #endif
  42. ;
  43. volatile char Endstops::endstop_hit_bits; // use X_MIN, Y_MIN, Z_MIN and Z_MIN_PROBE as BIT value
  44. #if ENABLED(Z_DUAL_ENDSTOPS)
  45. uint16_t
  46. #else
  47. byte
  48. #endif
  49. Endstops::current_endstop_bits = 0,
  50. Endstops::old_endstop_bits = 0;
  51. #if HAS_BED_PROBE
  52. volatile bool Endstops::z_probe_enabled = false;
  53. #endif
  54. /**
  55. * Class and Instance Methods
  56. */
  57. Endstops::Endstops() {
  58. enable_globally(
  59. #if ENABLED(ENDSTOPS_ONLY_FOR_HOMING)
  60. false
  61. #else
  62. true
  63. #endif
  64. );
  65. enable(true);
  66. #if HAS_BED_PROBE
  67. enable_z_probe(false);
  68. #endif
  69. } // Endstops::Endstops
  70. void Endstops::init() {
  71. #if HAS_X_MIN
  72. SET_INPUT(X_MIN_PIN);
  73. #if ENABLED(ENDSTOPPULLUP_XMIN)
  74. WRITE(X_MIN_PIN,HIGH);
  75. #endif
  76. #endif
  77. #if HAS_Y_MIN
  78. SET_INPUT(Y_MIN_PIN);
  79. #if ENABLED(ENDSTOPPULLUP_YMIN)
  80. WRITE(Y_MIN_PIN,HIGH);
  81. #endif
  82. #endif
  83. #if HAS_Z_MIN
  84. SET_INPUT(Z_MIN_PIN);
  85. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  86. WRITE(Z_MIN_PIN,HIGH);
  87. #endif
  88. #endif
  89. #if HAS_Z2_MIN
  90. SET_INPUT(Z2_MIN_PIN);
  91. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  92. WRITE(Z2_MIN_PIN,HIGH);
  93. #endif
  94. #endif
  95. #if HAS_X_MAX
  96. SET_INPUT(X_MAX_PIN);
  97. #if ENABLED(ENDSTOPPULLUP_XMAX)
  98. WRITE(X_MAX_PIN,HIGH);
  99. #endif
  100. #endif
  101. #if HAS_Y_MAX
  102. SET_INPUT(Y_MAX_PIN);
  103. #if ENABLED(ENDSTOPPULLUP_YMAX)
  104. WRITE(Y_MAX_PIN,HIGH);
  105. #endif
  106. #endif
  107. #if HAS_Z_MAX
  108. SET_INPUT(Z_MAX_PIN);
  109. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  110. WRITE(Z_MAX_PIN,HIGH);
  111. #endif
  112. #endif
  113. #if HAS_Z2_MAX
  114. SET_INPUT(Z2_MAX_PIN);
  115. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  116. WRITE(Z2_MAX_PIN,HIGH);
  117. #endif
  118. #endif
  119. #if HAS_Z_MIN_PROBE_PIN && ENABLED(Z_MIN_PROBE_ENDSTOP) // Check for Z_MIN_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
  120. SET_INPUT(Z_MIN_PROBE_PIN);
  121. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  122. WRITE(Z_MIN_PROBE_PIN,HIGH);
  123. #endif
  124. #endif
  125. } // Endstops::init
  126. void Endstops::report_state() {
  127. if (endstop_hit_bits) {
  128. #if ENABLED(ULTRA_LCD)
  129. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  130. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  131. #else
  132. #define _SET_STOP_CHAR(A,C) ;
  133. #endif
  134. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  135. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
  136. _SET_STOP_CHAR(A,C); }while(0)
  137. #define _ENDSTOP_HIT_TEST(A,C) \
  138. if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
  139. _ENDSTOP_HIT_ECHO(A,C)
  140. SERIAL_ECHO_START;
  141. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  142. _ENDSTOP_HIT_TEST(X, 'X');
  143. _ENDSTOP_HIT_TEST(Y, 'Y');
  144. _ENDSTOP_HIT_TEST(Z, 'Z');
  145. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  146. #define P_AXIS Z_AXIS
  147. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  148. #endif
  149. SERIAL_EOL;
  150. #if ENABLED(ULTRA_LCD)
  151. char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
  152. sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  153. lcd_setstatus(msg);
  154. #endif
  155. hit_on_purpose();
  156. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  157. if (stepper.abort_on_endstop_hit) {
  158. card.sdprinting = false;
  159. card.closefile();
  160. stepper.quick_stop();
  161. thermalManager.disable_all_heaters(); // switch off all heaters.
  162. }
  163. #endif
  164. }
  165. } // Endstops::report_state
  166. void Endstops::M119() {
  167. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  168. #if HAS_X_MIN
  169. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  170. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  171. #endif
  172. #if HAS_X_MAX
  173. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  174. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  175. #endif
  176. #if HAS_Y_MIN
  177. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  178. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  179. #endif
  180. #if HAS_Y_MAX
  181. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  182. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  183. #endif
  184. #if HAS_Z_MIN
  185. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  186. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  187. #endif
  188. #if HAS_Z_MAX
  189. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  190. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  191. #endif
  192. #if HAS_Z2_MAX
  193. SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
  194. SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  195. #endif
  196. #if HAS_Z_MIN_PROBE_PIN
  197. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  198. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  199. #endif
  200. } // Endstops::M119
  201. #if ENABLED(Z_DUAL_ENDSTOPS)
  202. // Pass the result of the endstop test
  203. void Endstops::test_dual_z_endstops(EndstopEnum es1, EndstopEnum es2) {
  204. byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
  205. if (stepper.current_block->steps[Z_AXIS] > 0) {
  206. stepper.endstop_triggered(Z_AXIS);
  207. SBI(endstop_hit_bits, Z_MIN);
  208. if (!stepper.performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  209. stepper.kill_current_block();
  210. }
  211. }
  212. #endif
  213. // Check endstops - Called from ISR!
  214. void Endstops::update() {
  215. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  216. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  217. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  218. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  219. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  220. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  221. // COPY_BIT: copy the value of COPY_BIT to BIT in bits
  222. #define COPY_BIT(bits, COPY_BIT, BIT) SET_BIT(bits, BIT, TEST(bits, COPY_BIT))
  223. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  224. UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
  225. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
  226. _ENDSTOP_HIT(AXIS); \
  227. stepper.endstop_triggered(_AXIS(AXIS)); \
  228. } \
  229. } while(0)
  230. #if ENABLED(COREXY) || ENABLED(COREXZ)
  231. // Head direction in -X axis for CoreXY and CoreXZ bots.
  232. // If DeltaA == -DeltaB, the movement is only in Y or Z axis
  233. if ((stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(CORE_AXIS_1) == stepper.motor_direction(CORE_AXIS_2))) {
  234. if (stepper.motor_direction(X_HEAD))
  235. #else
  236. if (stepper.motor_direction(X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  237. #endif
  238. { // -direction
  239. #if ENABLED(DUAL_X_CARRIAGE)
  240. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  241. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == -1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  242. #endif
  243. {
  244. #if HAS_X_MIN
  245. UPDATE_ENDSTOP(X, MIN);
  246. #endif
  247. }
  248. }
  249. else { // +direction
  250. #if ENABLED(DUAL_X_CARRIAGE)
  251. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  252. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == 1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  253. #endif
  254. {
  255. #if HAS_X_MAX
  256. UPDATE_ENDSTOP(X, MAX);
  257. #endif
  258. }
  259. }
  260. #if ENABLED(COREXY) || ENABLED(COREXZ)
  261. }
  262. #endif
  263. #if ENABLED(COREXY) || ENABLED(COREYZ)
  264. // Head direction in -Y axis for CoreXY / CoreYZ bots.
  265. // If DeltaA == DeltaB, the movement is only in X or Y axis
  266. if ((stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(CORE_AXIS_1) != stepper.motor_direction(CORE_AXIS_2))) {
  267. if (stepper.motor_direction(Y_HEAD))
  268. #else
  269. if (stepper.motor_direction(Y_AXIS)) // -direction
  270. #endif
  271. { // -direction
  272. #if HAS_Y_MIN
  273. UPDATE_ENDSTOP(Y, MIN);
  274. #endif
  275. }
  276. else { // +direction
  277. #if HAS_Y_MAX
  278. UPDATE_ENDSTOP(Y, MAX);
  279. #endif
  280. }
  281. #if ENABLED(COREXY) || ENABLED(COREYZ)
  282. }
  283. #endif
  284. #if ENABLED(COREXZ) || ENABLED(COREYZ)
  285. // Head direction in -Z axis for CoreXZ or CoreYZ bots.
  286. // If DeltaA == DeltaB, the movement is only in X or Y axis
  287. if ((stepper.current_block->steps[CORE_AXIS_1] != stepper.current_block->steps[CORE_AXIS_2]) || (stepper.motor_direction(CORE_AXIS_1) != stepper.motor_direction(CORE_AXIS_2))) {
  288. if (stepper.motor_direction(Z_HEAD))
  289. #else
  290. if (stepper.motor_direction(Z_AXIS))
  291. #endif
  292. { // z -direction
  293. #if HAS_Z_MIN
  294. #if ENABLED(Z_DUAL_ENDSTOPS)
  295. UPDATE_ENDSTOP_BIT(Z, MIN);
  296. #if HAS_Z2_MIN
  297. UPDATE_ENDSTOP_BIT(Z2, MIN);
  298. #else
  299. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  300. #endif
  301. test_dual_z_endstops(Z_MIN, Z2_MIN);
  302. #else // !Z_DUAL_ENDSTOPS
  303. #if HAS_BED_PROBE && ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  304. if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
  305. #else
  306. UPDATE_ENDSTOP(Z, MIN);
  307. #endif
  308. #endif // !Z_DUAL_ENDSTOPS
  309. #endif // HAS_Z_MIN
  310. #if HAS_BED_PROBE && ENABLED(Z_MIN_PROBE_ENDSTOP) && DISABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  311. if (z_probe_enabled) {
  312. UPDATE_ENDSTOP(Z, MIN_PROBE);
  313. if (TEST_ENDSTOP(Z_MIN_PROBE)) SBI(endstop_hit_bits, Z_MIN_PROBE);
  314. }
  315. #endif
  316. }
  317. else { // z +direction
  318. #if HAS_Z_MAX
  319. #if ENABLED(Z_DUAL_ENDSTOPS)
  320. UPDATE_ENDSTOP_BIT(Z, MAX);
  321. #if HAS_Z2_MAX
  322. UPDATE_ENDSTOP_BIT(Z2, MAX);
  323. #else
  324. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  325. #endif
  326. test_dual_z_endstops(Z_MAX, Z2_MAX);
  327. #else // !Z_DUAL_ENDSTOPS
  328. UPDATE_ENDSTOP(Z, MAX);
  329. #endif // !Z_DUAL_ENDSTOPS
  330. #endif // Z_MAX_PIN
  331. }
  332. #if ENABLED(COREXZ)
  333. }
  334. #endif
  335. old_endstop_bits = current_endstop_bits;
  336. } // Endstops::update()