My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

endstops.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_ALWAYS_ON_DEFAULT)
  38. (true)
  39. #else
  40. (false)
  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. void Endstops::init() {
  58. #if HAS_X_MIN
  59. SET_INPUT(X_MIN_PIN);
  60. #if ENABLED(ENDSTOPPULLUP_XMIN)
  61. WRITE(X_MIN_PIN,HIGH);
  62. #endif
  63. #endif
  64. #if HAS_Y_MIN
  65. SET_INPUT(Y_MIN_PIN);
  66. #if ENABLED(ENDSTOPPULLUP_YMIN)
  67. WRITE(Y_MIN_PIN,HIGH);
  68. #endif
  69. #endif
  70. #if HAS_Z_MIN
  71. SET_INPUT(Z_MIN_PIN);
  72. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  73. WRITE(Z_MIN_PIN,HIGH);
  74. #endif
  75. #endif
  76. #if HAS_Z2_MIN
  77. SET_INPUT(Z2_MIN_PIN);
  78. #if ENABLED(ENDSTOPPULLUP_ZMIN)
  79. WRITE(Z2_MIN_PIN,HIGH);
  80. #endif
  81. #endif
  82. #if HAS_X_MAX
  83. SET_INPUT(X_MAX_PIN);
  84. #if ENABLED(ENDSTOPPULLUP_XMAX)
  85. WRITE(X_MAX_PIN,HIGH);
  86. #endif
  87. #endif
  88. #if HAS_Y_MAX
  89. SET_INPUT(Y_MAX_PIN);
  90. #if ENABLED(ENDSTOPPULLUP_YMAX)
  91. WRITE(Y_MAX_PIN,HIGH);
  92. #endif
  93. #endif
  94. #if HAS_Z_MAX
  95. SET_INPUT(Z_MAX_PIN);
  96. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  97. WRITE(Z_MAX_PIN,HIGH);
  98. #endif
  99. #endif
  100. #if HAS_Z2_MAX
  101. SET_INPUT(Z2_MAX_PIN);
  102. #if ENABLED(ENDSTOPPULLUP_ZMAX)
  103. WRITE(Z2_MAX_PIN,HIGH);
  104. #endif
  105. #endif
  106. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  107. SET_INPUT(Z_MIN_PROBE_PIN);
  108. #if ENABLED(ENDSTOPPULLUP_ZMIN_PROBE)
  109. WRITE(Z_MIN_PROBE_PIN,HIGH);
  110. #endif
  111. #endif
  112. } // Endstops::init
  113. void Endstops::report_state() {
  114. if (endstop_hit_bits) {
  115. #if ENABLED(ULTRA_LCD)
  116. char chrX = ' ', chrY = ' ', chrZ = ' ', chrP = ' ';
  117. #define _SET_STOP_CHAR(A,C) (chr## A = C)
  118. #else
  119. #define _SET_STOP_CHAR(A,C) ;
  120. #endif
  121. #define _ENDSTOP_HIT_ECHO(A,C) do{ \
  122. SERIAL_ECHOPAIR(" " STRINGIFY(A) ":", stepper.triggered_position_mm(A ##_AXIS)); \
  123. _SET_STOP_CHAR(A,C); }while(0)
  124. #define _ENDSTOP_HIT_TEST(A,C) \
  125. if (TEST(endstop_hit_bits, A ##_MIN) || TEST(endstop_hit_bits, A ##_MAX)) \
  126. _ENDSTOP_HIT_ECHO(A,C)
  127. SERIAL_ECHO_START;
  128. SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
  129. _ENDSTOP_HIT_TEST(X, 'X');
  130. _ENDSTOP_HIT_TEST(Y, 'Y');
  131. _ENDSTOP_HIT_TEST(Z, 'Z');
  132. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  133. #define P_AXIS Z_AXIS
  134. if (TEST(endstop_hit_bits, Z_MIN_PROBE)) _ENDSTOP_HIT_ECHO(P, 'P');
  135. #endif
  136. SERIAL_EOL;
  137. #if ENABLED(ULTRA_LCD)
  138. char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
  139. sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
  140. lcd_setstatus(msg);
  141. #endif
  142. hit_on_purpose();
  143. #if ENABLED(ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED) && ENABLED(SDSUPPORT)
  144. if (stepper.abort_on_endstop_hit) {
  145. card.sdprinting = false;
  146. card.closefile();
  147. quickstop_stepper();
  148. thermalManager.disable_all_heaters(); // switch off all heaters.
  149. }
  150. #endif
  151. }
  152. } // Endstops::report_state
  153. void Endstops::M119() {
  154. SERIAL_PROTOCOLLNPGM(MSG_M119_REPORT);
  155. #if HAS_X_MIN
  156. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  157. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  158. #endif
  159. #if HAS_X_MAX
  160. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  161. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  162. #endif
  163. #if HAS_Y_MIN
  164. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  165. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  166. #endif
  167. #if HAS_Y_MAX
  168. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  169. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  170. #endif
  171. #if HAS_Z_MIN
  172. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  173. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  174. #endif
  175. #if HAS_Z2_MIN
  176. SERIAL_PROTOCOLPGM(MSG_Z2_MIN);
  177. SERIAL_PROTOCOLLN(((READ(Z2_MIN_PIN)^Z2_MIN_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  178. #endif
  179. #if HAS_Z_MAX
  180. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  181. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  182. #endif
  183. #if HAS_Z2_MAX
  184. SERIAL_PROTOCOLPGM(MSG_Z2_MAX);
  185. SERIAL_PROTOCOLLN(((READ(Z2_MAX_PIN)^Z2_MAX_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  186. #endif
  187. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  188. SERIAL_PROTOCOLPGM(MSG_Z_PROBE);
  189. SERIAL_PROTOCOLLN(((READ(Z_MIN_PROBE_PIN)^Z_MIN_PROBE_ENDSTOP_INVERTING) ? MSG_ENDSTOP_HIT : MSG_ENDSTOP_OPEN));
  190. #endif
  191. } // Endstops::M119
  192. #if ENABLED(Z_DUAL_ENDSTOPS)
  193. // Pass the result of the endstop test
  194. void Endstops::test_dual_z_endstops(const EndstopEnum es1, const EndstopEnum es2) {
  195. byte z_test = TEST_ENDSTOP(es1) | (TEST_ENDSTOP(es2) << 1); // bit 0 for Z, bit 1 for Z2
  196. if (z_test && stepper.current_block->steps[Z_AXIS] > 0) {
  197. SBI(endstop_hit_bits, Z_MIN);
  198. if (!stepper.performing_homing || (z_test == 0x3)) //if not performing home or if both endstops were trigged during homing...
  199. stepper.kill_current_block();
  200. }
  201. }
  202. #endif
  203. // Check endstops - Called from ISR!
  204. void Endstops::update() {
  205. #define _ENDSTOP(AXIS, MINMAX) AXIS ##_## MINMAX
  206. #define _ENDSTOP_PIN(AXIS, MINMAX) AXIS ##_## MINMAX ##_PIN
  207. #define _ENDSTOP_INVERTING(AXIS, MINMAX) AXIS ##_## MINMAX ##_ENDSTOP_INVERTING
  208. #define _ENDSTOP_HIT(AXIS) SBI(endstop_hit_bits, _ENDSTOP(AXIS, MIN))
  209. // UPDATE_ENDSTOP_BIT: set the current endstop bits for an endstop to its status
  210. #define UPDATE_ENDSTOP_BIT(AXIS, MINMAX) SET_BIT(current_endstop_bits, _ENDSTOP(AXIS, MINMAX), (READ(_ENDSTOP_PIN(AXIS, MINMAX)) != _ENDSTOP_INVERTING(AXIS, MINMAX)))
  211. // COPY_BIT: copy the value of SRC_BIT to DST_BIT in DST
  212. #define COPY_BIT(DST, SRC_BIT, DST_BIT) SET_BIT(DST, DST_BIT, TEST(DST, SRC_BIT))
  213. #define _UPDATE_ENDSTOP(AXIS,MINMAX,CODE) do { \
  214. UPDATE_ENDSTOP_BIT(AXIS, MINMAX); \
  215. if (TEST_ENDSTOP(_ENDSTOP(AXIS, MINMAX)) && stepper.current_block->steps[_AXIS(AXIS)] > 0) { \
  216. _ENDSTOP_HIT(AXIS); \
  217. stepper.endstop_triggered(_AXIS(AXIS)); \
  218. CODE; \
  219. } \
  220. } while(0)
  221. #if ENABLED(G38_PROBE_TARGET) && PIN_EXISTS(Z_MIN) // If G38 command then check Z_MIN for every axis and every direction
  222. #define UPDATE_ENDSTOP(AXIS,MINMAX) do { \
  223. _UPDATE_ENDSTOP(AXIS,MINMAX,NOOP); \
  224. if (G38_move) _UPDATE_ENDSTOP(Z, MIN, G38_endstop_hit = true); \
  225. } while(0)
  226. #else
  227. #define UPDATE_ENDSTOP(AXIS,MINMAX) _UPDATE_ENDSTOP(AXIS,MINMAX,NOOP)
  228. #endif
  229. #if CORE_IS_XY || CORE_IS_XZ
  230. // Head direction in -X axis for CoreXY and CoreXZ bots.
  231. // If DeltaA == -DeltaB, the movement is only in Y or Z axis
  232. 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))) {
  233. if (stepper.motor_direction(X_HEAD))
  234. #else
  235. if (stepper.motor_direction(X_AXIS)) // stepping along -X axis (regular Cartesian bot)
  236. #endif
  237. { // -direction
  238. #if ENABLED(DUAL_X_CARRIAGE)
  239. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  240. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == -1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == -1))
  241. #endif
  242. {
  243. #if HAS_X_MIN
  244. UPDATE_ENDSTOP(X, MIN);
  245. #endif
  246. }
  247. }
  248. else { // +direction
  249. #if ENABLED(DUAL_X_CARRIAGE)
  250. // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
  251. if ((stepper.current_block->active_extruder == 0 && X_HOME_DIR == 1) || (stepper.current_block->active_extruder != 0 && X2_HOME_DIR == 1))
  252. #endif
  253. {
  254. #if HAS_X_MAX
  255. UPDATE_ENDSTOP(X, MAX);
  256. #endif
  257. }
  258. }
  259. #if CORE_IS_XY || CORE_IS_XZ
  260. }
  261. #endif
  262. #if CORE_IS_XY || CORE_IS_YZ
  263. // Head direction in -Y axis for CoreXY / CoreYZ bots.
  264. // If DeltaA == DeltaB, the movement is only in X or Y axis
  265. 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))) {
  266. if (stepper.motor_direction(Y_HEAD))
  267. #else
  268. if (stepper.motor_direction(Y_AXIS)) // -direction
  269. #endif
  270. { // -direction
  271. #if HAS_Y_MIN
  272. UPDATE_ENDSTOP(Y, MIN);
  273. #endif
  274. }
  275. else { // +direction
  276. #if HAS_Y_MAX
  277. UPDATE_ENDSTOP(Y, MAX);
  278. #endif
  279. }
  280. #if CORE_IS_XY || CORE_IS_YZ
  281. }
  282. #endif
  283. #if CORE_IS_XZ || CORE_IS_YZ
  284. // Head direction in -Z axis for CoreXZ or CoreYZ bots.
  285. // If DeltaA == DeltaB, the movement is only in X or Y axis
  286. 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))) {
  287. if (stepper.motor_direction(Z_HEAD))
  288. #else
  289. if (stepper.motor_direction(Z_AXIS))
  290. #endif
  291. { // Z -direction. Gantry down, bed up.
  292. #if HAS_Z_MIN
  293. #if ENABLED(Z_DUAL_ENDSTOPS)
  294. UPDATE_ENDSTOP_BIT(Z, MIN);
  295. #if HAS_Z2_MIN
  296. UPDATE_ENDSTOP_BIT(Z2, MIN);
  297. #else
  298. COPY_BIT(current_endstop_bits, Z_MIN, Z2_MIN);
  299. #endif
  300. test_dual_z_endstops(Z_MIN, Z2_MIN);
  301. #else // !Z_DUAL_ENDSTOPS
  302. #if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)
  303. if (z_probe_enabled) UPDATE_ENDSTOP(Z, MIN);
  304. #else
  305. UPDATE_ENDSTOP(Z, MIN);
  306. #endif
  307. #endif // !Z_DUAL_ENDSTOPS
  308. #endif // HAS_Z_MIN
  309. // When closing the gap check the enabled probe
  310. #if ENABLED(Z_MIN_PROBE_ENDSTOP)
  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. Gantry up, bed down.
  318. #if HAS_Z_MAX
  319. // Check both Z dual endstops
  320. #if ENABLED(Z_DUAL_ENDSTOPS)
  321. UPDATE_ENDSTOP_BIT(Z, MAX);
  322. #if HAS_Z2_MAX
  323. UPDATE_ENDSTOP_BIT(Z2, MAX);
  324. #else
  325. COPY_BIT(current_endstop_bits, Z_MAX, Z2_MAX);
  326. #endif
  327. test_dual_z_endstops(Z_MAX, Z2_MAX);
  328. // If this pin is not hijacked for the bed probe
  329. // then it belongs to the Z endstop
  330. #elif DISABLED(Z_MIN_PROBE_ENDSTOP) || Z_MAX_PIN != Z_MIN_PROBE_PIN
  331. UPDATE_ENDSTOP(Z, MAX);
  332. #endif // !Z_MIN_PROBE_PIN...
  333. #endif // Z_MAX_PIN
  334. }
  335. #if CORE_IS_XZ || CORE_IS_YZ
  336. }
  337. #endif
  338. old_endstop_bits = current_endstop_bits;
  339. } // Endstops::update()