My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

fwretract.cpp 9.6KB

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