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.

nozzle.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfig.h"
  23. #if EITHER(NOZZLE_CLEAN_FEATURE, NOZZLE_PARK_FEATURE)
  24. #include "nozzle.h"
  25. Nozzle nozzle;
  26. #include "../MarlinCore.h"
  27. #include "../module/motion.h"
  28. #if NOZZLE_CLEAN_MIN_TEMP > 20
  29. #include "../module/temperature.h"
  30. #endif
  31. #if ENABLED(NOZZLE_CLEAN_FEATURE)
  32. /**
  33. * @brief Stroke clean pattern
  34. * @details Wipes the nozzle back and forth in a linear movement
  35. *
  36. * @param start xyz_pos_t defining the starting point
  37. * @param end xyz_pos_t defining the ending point
  38. * @param strokes number of strokes to execute
  39. */
  40. void Nozzle::stroke(const xyz_pos_t &start, const xyz_pos_t &end, const uint8_t &strokes) {
  41. #if ENABLED(NOZZLE_CLEAN_GOBACK)
  42. const xyz_pos_t oldpos = current_position;
  43. #endif
  44. // Move to the starting point
  45. #if ENABLED(NOZZLE_CLEAN_NO_Z)
  46. #if ENABLED(NOZZLE_CLEAN_NO_Y)
  47. do_blocking_move_to_x(start.x);
  48. #else
  49. do_blocking_move_to_xy(start);
  50. #endif
  51. #else
  52. do_blocking_move_to(start);
  53. #endif
  54. // Start the stroke pattern
  55. LOOP_L_N(i, strokes >> 1) {
  56. #if ENABLED(NOZZLE_CLEAN_NO_Y)
  57. do_blocking_move_to_x(end.x);
  58. do_blocking_move_to_x(start.x);
  59. #else
  60. do_blocking_move_to_xy(end);
  61. do_blocking_move_to_xy(start);
  62. #endif
  63. }
  64. TERN_(NOZZLE_CLEAN_GOBACK, do_blocking_move_to(oldpos));
  65. }
  66. /**
  67. * @brief Zig-zag clean pattern
  68. * @details Apply a zig-zag cleaning pattern
  69. *
  70. * @param start xyz_pos_t defining the starting point
  71. * @param end xyz_pos_t defining the ending point
  72. * @param strokes number of strokes to execute
  73. * @param objects number of triangles to do
  74. */
  75. void Nozzle::zigzag(const xyz_pos_t &start, const xyz_pos_t &end, const uint8_t &strokes, const uint8_t &objects) {
  76. const xy_pos_t diff = end - start;
  77. if (!diff.x || !diff.y) return;
  78. #if ENABLED(NOZZLE_CLEAN_GOBACK)
  79. const xyz_pos_t back = current_position;
  80. #endif
  81. #if ENABLED(NOZZLE_CLEAN_NO_Z)
  82. do_blocking_move_to_xy(start);
  83. #else
  84. do_blocking_move_to(start);
  85. #endif
  86. const uint8_t zigs = objects << 1;
  87. const bool horiz = ABS(diff.x) >= ABS(diff.y); // Do a horizontal wipe?
  88. const float P = (horiz ? diff.x : diff.y) / zigs; // Period of each zig / zag
  89. const xyz_pos_t *side;
  90. LOOP_L_N(j, strokes) {
  91. for (int8_t i = 0; i < zigs; i++) {
  92. side = (i & 1) ? &end : &start;
  93. if (horiz)
  94. do_blocking_move_to_xy(start.x + i * P, side->y);
  95. else
  96. do_blocking_move_to_xy(side->x, start.y + i * P);
  97. }
  98. for (int8_t i = zigs; i >= 0; i--) {
  99. side = (i & 1) ? &end : &start;
  100. if (horiz)
  101. do_blocking_move_to_xy(start.x + i * P, side->y);
  102. else
  103. do_blocking_move_to_xy(side->x, start.y + i * P);
  104. }
  105. }
  106. TERN_(NOZZLE_CLEAN_GOBACK, do_blocking_move_to(back));
  107. }
  108. /**
  109. * @brief Circular clean pattern
  110. * @details Apply a circular cleaning pattern
  111. *
  112. * @param start xyz_pos_t defining the middle of circle
  113. * @param strokes number of strokes to execute
  114. * @param radius radius of circle
  115. */
  116. void Nozzle::circle(const xyz_pos_t &start, const xyz_pos_t &middle, const uint8_t &strokes, const_float_t radius) {
  117. if (strokes == 0) return;
  118. #if ENABLED(NOZZLE_CLEAN_GOBACK)
  119. const xyz_pos_t back = current_position;
  120. #endif
  121. TERN(NOZZLE_CLEAN_NO_Z, do_blocking_move_to_xy, do_blocking_move_to)(start);
  122. LOOP_L_N(s, strokes)
  123. LOOP_L_N(i, NOZZLE_CLEAN_CIRCLE_FN)
  124. do_blocking_move_to_xy(
  125. middle.x + sin((RADIANS(360) / NOZZLE_CLEAN_CIRCLE_FN) * i) * radius,
  126. middle.y + cos((RADIANS(360) / NOZZLE_CLEAN_CIRCLE_FN) * i) * radius
  127. );
  128. // Let's be safe
  129. do_blocking_move_to_xy(start);
  130. TERN_(NOZZLE_CLEAN_GOBACK, do_blocking_move_to(back));
  131. }
  132. /**
  133. * @brief Clean the nozzle
  134. * @details Starts the selected clean procedure pattern
  135. *
  136. * @param pattern one of the available patterns
  137. * @param argument depends on the cleaning pattern
  138. */
  139. void Nozzle::clean(const uint8_t &pattern, const uint8_t &strokes, const_float_t radius, const uint8_t &objects, const uint8_t cleans) {
  140. xyz_pos_t start[HOTENDS] = NOZZLE_CLEAN_START_POINT, end[HOTENDS] = NOZZLE_CLEAN_END_POINT, middle[HOTENDS] = NOZZLE_CLEAN_CIRCLE_MIDDLE;
  141. const uint8_t arrPos = EITHER(SINGLENOZZLE, MIXING_EXTRUDER) ? 0 : active_extruder;
  142. #if NOZZLE_CLEAN_MIN_TEMP > 20
  143. if (thermalManager.degTargetHotend(arrPos) < NOZZLE_CLEAN_MIN_TEMP) {
  144. #if ENABLED(NOZZLE_CLEAN_HEATUP)
  145. SERIAL_ECHOLNPGM("Nozzle too Cold - Heating");
  146. thermalManager.setTargetHotend(NOZZLE_CLEAN_MIN_TEMP, arrPos);
  147. thermalManager.wait_for_hotend(arrPos);
  148. #else
  149. SERIAL_ECHOLNPGM("Nozzle too cold - Skipping wipe");
  150. return;
  151. #endif
  152. }
  153. #endif
  154. #if HAS_SOFTWARE_ENDSTOPS
  155. #define LIMIT_AXIS(A) do{ \
  156. LIMIT( start[arrPos].A, soft_endstop.min.A, soft_endstop.max.A); \
  157. LIMIT(middle[arrPos].A, soft_endstop.min.A, soft_endstop.max.A); \
  158. LIMIT( end[arrPos].A, soft_endstop.min.A, soft_endstop.max.A); \
  159. }while(0)
  160. if (soft_endstop.enabled()) {
  161. LIMIT_AXIS(x);
  162. LIMIT_AXIS(y);
  163. LIMIT_AXIS(z);
  164. const bool radiusOutOfRange = (middle[arrPos].x + radius > soft_endstop.max.x)
  165. || (middle[arrPos].x - radius < soft_endstop.min.x)
  166. || (middle[arrPos].y + radius > soft_endstop.max.y)
  167. || (middle[arrPos].y - radius < soft_endstop.min.y);
  168. if (radiusOutOfRange && pattern == 2) {
  169. SERIAL_ECHOLNPGM("Warning: Radius Out of Range");
  170. return;
  171. }
  172. }
  173. #endif
  174. if (pattern == 2) {
  175. if (!(cleans & (_BV(X_AXIS) | _BV(Y_AXIS)))) {
  176. SERIAL_ECHOLNPGM("Warning: Clean Circle requires XY");
  177. return;
  178. }
  179. }
  180. else {
  181. if (!TEST(cleans, X_AXIS)) start[arrPos].x = end[arrPos].x = current_position.x;
  182. if (!TEST(cleans, Y_AXIS)) start[arrPos].y = end[arrPos].y = current_position.y;
  183. }
  184. if (!TEST(cleans, Z_AXIS)) start[arrPos].z = end[arrPos].z = current_position.z;
  185. switch (pattern) {
  186. case 1: zigzag(start[arrPos], end[arrPos], strokes, objects); break;
  187. case 2: circle(start[arrPos], middle[arrPos], strokes, radius); break;
  188. default: stroke(start[arrPos], end[arrPos], strokes);
  189. }
  190. }
  191. #endif // NOZZLE_CLEAN_FEATURE
  192. #if ENABLED(NOZZLE_PARK_FEATURE)
  193. float Nozzle::park_mode_0_height(const_float_t park_z) {
  194. // Apply a minimum raise, if specified. Use park.z as a minimum height instead.
  195. return _MAX(park_z, // Minimum height over 0 based on input
  196. _MIN(Z_MAX_POS, // Maximum height is fixed
  197. #ifdef NOZZLE_PARK_Z_RAISE_MIN
  198. NOZZLE_PARK_Z_RAISE_MIN + // Minimum raise...
  199. #endif
  200. current_position.z // ...over current position
  201. )
  202. );
  203. }
  204. void Nozzle::park(const uint8_t z_action, const xyz_pos_t &park/*=NOZZLE_PARK_POINT*/) {
  205. constexpr feedRate_t fr_xy = NOZZLE_PARK_XY_FEEDRATE, fr_z = NOZZLE_PARK_Z_FEEDRATE;
  206. switch (z_action) {
  207. case 1: // Go to Z-park height
  208. do_blocking_move_to_z(park.z, fr_z);
  209. break;
  210. case 2: // Raise by Z-park height
  211. do_blocking_move_to_z(_MIN(current_position.z + park.z, Z_MAX_POS), fr_z);
  212. break;
  213. default: // Raise by NOZZLE_PARK_Z_RAISE_MIN, use park.z as a minimum height
  214. do_blocking_move_to_z(park_mode_0_height(park.z), fr_z);
  215. break;
  216. }
  217. #ifndef NOZZLE_PARK_MOVE
  218. #define NOZZLE_PARK_MOVE 0
  219. #endif
  220. switch (NOZZLE_PARK_MOVE) {
  221. case 0: do_blocking_move_to_xy(park, fr_xy); break;
  222. case 1: do_blocking_move_to_x(park.x, fr_xy); break;
  223. case 2: do_blocking_move_to_y(park.y, fr_xy); break;
  224. case 3: do_blocking_move_to_x(park.x, fr_xy);
  225. do_blocking_move_to_y(park.y, fr_xy); break;
  226. case 4: do_blocking_move_to_y(park.y, fr_xy);
  227. do_blocking_move_to_x(park.x, fr_xy); break;
  228. }
  229. report_current_position();
  230. }
  231. #endif // NOZZLE_PARK_FEATURE
  232. #endif // NOZZLE_CLEAN_FEATURE || NOZZLE_PARK_FEATURE