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.

fwretract.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /**
  23. * fwretract.cpp - Implement firmware-based retraction
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if ENABLED(FWRETRACT)
  27. #include "fwretract.h"
  28. FWRetract fwretract; // Single instance - this calls the constructor
  29. #include "../module/motion.h"
  30. #include "../module/planner.h"
  31. #include "../gcode/gcode.h"
  32. #if ENABLED(RETRACT_SYNC_MIXING)
  33. #include "mixing.h"
  34. #endif
  35. // private:
  36. #if HAS_MULTI_EXTRUDER
  37. Flags<EXTRUDERS> FWRetract::retracted_swap; // Which extruders are swap-retracted
  38. #endif
  39. // public:
  40. fwretract_settings_t FWRetract::settings; // M207 S F Z W, M208 S F W R
  41. #if ENABLED(FWRETRACT_AUTORETRACT)
  42. bool FWRetract::autoretract_enabled; // M209 S - Autoretract switch
  43. #endif
  44. Flags<EXTRUDERS> FWRetract::retracted; // Which extruders are currently retracted
  45. float FWRetract::current_retract[EXTRUDERS], // Retract value used by planner
  46. FWRetract::current_hop;
  47. void FWRetract::reset() {
  48. TERN_(FWRETRACT_AUTORETRACT, autoretract_enabled = false);
  49. settings.retract_length = RETRACT_LENGTH;
  50. settings.retract_feedrate_mm_s = RETRACT_FEEDRATE;
  51. settings.retract_zraise = RETRACT_ZRAISE;
  52. settings.retract_recover_extra = RETRACT_RECOVER_LENGTH;
  53. settings.retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE;
  54. settings.swap_retract_length = RETRACT_LENGTH_SWAP;
  55. settings.swap_retract_recover_extra = RETRACT_RECOVER_LENGTH_SWAP;
  56. settings.swap_retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE_SWAP;
  57. current_hop = 0.0;
  58. retracted.reset();
  59. EXTRUDER_LOOP() {
  60. E_TERN_(retracted_swap.clear(e));
  61. current_retract[e] = 0.0;
  62. }
  63. }
  64. /**
  65. * Retract or recover according to firmware settings
  66. *
  67. * This function handles retract/recover moves for G10 and G11,
  68. * plus auto-retract moves sent from G0/G1 when E-only moves are done.
  69. *
  70. * To simplify the logic, doubled retract/recover moves are ignored.
  71. *
  72. * Note: Auto-retract will apply the set Z hop in addition to any Z hop
  73. * included in the G-code. Use M207 Z0 to to prevent double hop.
  74. */
  75. void FWRetract::retract(const bool retracting E_OPTARG(bool swapping/*=false*/)) {
  76. // Prevent two retracts or recovers in a row
  77. if (retracted[active_extruder] == retracting) return;
  78. // Prevent two swap-retract or recovers in a row
  79. #if HAS_MULTI_EXTRUDER
  80. // Allow G10 S1 only after G11
  81. if (swapping && retracted_swap[active_extruder] == retracting) return;
  82. // G11 priority to recover the long retract if activated
  83. if (!retracting) swapping = retracted_swap[active_extruder];
  84. #else
  85. constexpr bool swapping = false;
  86. #endif
  87. /* // debugging
  88. SERIAL_ECHOLNPGM(
  89. "retracting ", AS_DIGIT(retracting),
  90. " swapping ", swapping,
  91. " active extruder ", active_extruder
  92. );
  93. EXTRUDER_LOOP() {
  94. SERIAL_ECHOLNPGM("retracted[", e, "] ", AS_DIGIT(retracted[e]));
  95. #if HAS_MULTI_EXTRUDER
  96. SERIAL_ECHOLNPGM("retracted_swap[", e, "] ", AS_DIGIT(retracted_swap[e]));
  97. #endif
  98. }
  99. SERIAL_ECHOLNPGM("current_position.z ", current_position.z);
  100. SERIAL_ECHOLNPGM("current_position.e ", current_position.e);
  101. SERIAL_ECHOLNPGM("current_hop ", current_hop);
  102. //*/
  103. const float base_retract = TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  104. * (swapping ? settings.swap_retract_length : settings.retract_length);
  105. // The current position will be the destination for E and Z moves
  106. destination = current_position;
  107. #if ENABLED(RETRACT_SYNC_MIXING)
  108. const uint8_t old_mixing_tool = mixer.get_current_vtool();
  109. mixer.T(MIXER_AUTORETRACT_TOOL);
  110. #endif
  111. const feedRate_t fr_max_z = planner.settings.max_feedrate_mm_s[Z_AXIS];
  112. if (retracting) {
  113. // Retract by moving from a faux E position back to the current E position
  114. current_retract[active_extruder] = base_retract;
  115. prepare_internal_move_to_destination( // set current from destination
  116. settings.retract_feedrate_mm_s * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  117. );
  118. // Is a Z hop set, and has the hop not yet been done?
  119. if (!current_hop && settings.retract_zraise > 0.01f) { // Apply hop only once
  120. current_hop += settings.retract_zraise; // Add to the hop total (again, only once)
  121. // Raise up, set_current_to_destination. Maximum Z feedrate
  122. prepare_internal_move_to_destination(fr_max_z);
  123. }
  124. }
  125. else {
  126. // If a hop was done and Z hasn't changed, undo the Z hop
  127. if (current_hop) {
  128. current_hop = 0;
  129. // Lower Z, set_current_to_destination. Maximum Z feedrate
  130. prepare_internal_move_to_destination(fr_max_z);
  131. }
  132. const float extra_recover = swapping ? settings.swap_retract_recover_extra : settings.retract_recover_extra;
  133. if (extra_recover) {
  134. current_position.e -= extra_recover; // Adjust the current E position by the extra amount to recover
  135. sync_plan_position_e(); // Sync the planner position so the extra amount is recovered
  136. }
  137. current_retract[active_extruder] = 0;
  138. // Recover E, set_current_to_destination
  139. prepare_internal_move_to_destination(
  140. (swapping ? settings.swap_retract_recover_feedrate_mm_s : settings.retract_recover_feedrate_mm_s)
  141. * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  142. );
  143. }
  144. TERN_(RETRACT_SYNC_MIXING, mixer.T(old_mixing_tool)); // Restore original mixing tool
  145. retracted.set(active_extruder, retracting); // Active extruder now retracted / recovered
  146. // If swap retract/recover update the retracted_swap flag too
  147. #if HAS_MULTI_EXTRUDER
  148. if (swapping) retracted_swap.set(active_extruder, retracting);
  149. #endif
  150. /* // debugging
  151. SERIAL_ECHOLNPGM("retracting ", AS_DIGIT(retracting));
  152. SERIAL_ECHOLNPGM("swapping ", AS_DIGIT(swapping));
  153. SERIAL_ECHOLNPGM("active_extruder ", active_extruder);
  154. EXTRUDER_LOOP() {
  155. SERIAL_ECHOLNPGM("retracted[", e, "] ", AS_DIGIT(retracted[e]));
  156. #if HAS_MULTI_EXTRUDER
  157. SERIAL_ECHOLNPGM("retracted_swap[", e, "] ", AS_DIGIT(retracted_swap[e]));
  158. #endif
  159. }
  160. SERIAL_ECHOLNPGM("current_position.z ", current_position.z);
  161. SERIAL_ECHOLNPGM("current_position.e ", current_position.e);
  162. SERIAL_ECHOLNPGM("current_hop ", current_hop);
  163. //*/
  164. }
  165. //extern const char SP_Z_STR[];
  166. /**
  167. * M207: Set firmware retraction values
  168. *
  169. * S[+units] retract_length
  170. * W[+units] swap_retract_length (multi-extruder)
  171. * F[units/min] retract_feedrate_mm_s
  172. * Z[units] retract_zraise
  173. */
  174. void FWRetract::M207() {
  175. if (!parser.seen("FSWZ")) return M207_report();
  176. if (parser.seenval('S')) settings.retract_length = parser.value_axis_units(E_AXIS);
  177. if (parser.seenval('F')) settings.retract_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
  178. if (parser.seenval('Z')) settings.retract_zraise = parser.value_linear_units();
  179. if (parser.seenval('W')) settings.swap_retract_length = parser.value_axis_units(E_AXIS);
  180. }
  181. void FWRetract::M207_report() {
  182. SERIAL_ECHOLNPGM_P(
  183. PSTR(" M207 S"), LINEAR_UNIT(settings.retract_length)
  184. , PSTR(" W"), LINEAR_UNIT(settings.swap_retract_length)
  185. , PSTR(" F"), LINEAR_UNIT(MMS_TO_MMM(settings.retract_feedrate_mm_s))
  186. , SP_Z_STR, LINEAR_UNIT(settings.retract_zraise)
  187. );
  188. }
  189. /**
  190. * M208: Set firmware un-retraction values
  191. *
  192. * S[+units] retract_recover_extra (in addition to M207 S*)
  193. * W[+units] swap_retract_recover_extra (multi-extruder)
  194. * F[units/min] retract_recover_feedrate_mm_s
  195. * R[units/min] swap_retract_recover_feedrate_mm_s
  196. */
  197. void FWRetract::M208() {
  198. if (!parser.seen("FSRW")) return M208_report();
  199. if (parser.seen('S')) settings.retract_recover_extra = parser.value_axis_units(E_AXIS);
  200. if (parser.seen('F')) settings.retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
  201. if (parser.seen('R')) settings.swap_retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
  202. if (parser.seen('W')) settings.swap_retract_recover_extra = parser.value_axis_units(E_AXIS);
  203. }
  204. void FWRetract::M208_report() {
  205. SERIAL_ECHOLNPGM(
  206. " M208 S", LINEAR_UNIT(settings.retract_recover_extra)
  207. , " W", LINEAR_UNIT(settings.swap_retract_recover_extra)
  208. , " F", LINEAR_UNIT(MMS_TO_MMM(settings.retract_recover_feedrate_mm_s))
  209. );
  210. }
  211. #if ENABLED(FWRETRACT_AUTORETRACT)
  212. /**
  213. * M209: Enable automatic retract (M209 S1)
  214. * For slicers that don't support G10/11, reversed extrude-only
  215. * moves will be classified as retraction.
  216. */
  217. void FWRetract::M209() {
  218. if (!parser.seen('S')) return M209_report();
  219. if (MIN_AUTORETRACT <= MAX_AUTORETRACT)
  220. enable_autoretract(parser.value_bool());
  221. }
  222. void FWRetract::M209_report() {
  223. SERIAL_ECHOLNPGM(" M209 S", AS_DIGIT(autoretract_enabled));
  224. }
  225. #endif // FWRETRACT_AUTORETRACT
  226. #endif // FWRETRACT