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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <http://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. #if ENABLED(RETRACT_SYNC_MIXING)
  33. #include "mixing.h"
  34. #endif
  35. // private:
  36. #if EXTRUDERS > 1
  37. bool FWRetract::retracted_swap[EXTRUDERS]; // 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. bool FWRetract::retracted[EXTRUDERS]; // 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. LOOP_L_N(i, EXTRUDERS) {
  59. retracted[i] = false;
  60. #if EXTRUDERS > 1
  61. retracted_swap[i] = false;
  62. #endif
  63. current_retract[i] = 0.0;
  64. }
  65. }
  66. /**
  67. * Retract or recover according to firmware settings
  68. *
  69. * This function handles retract/recover moves for G10 and G11,
  70. * plus auto-retract moves sent from G0/G1 when E-only moves are done.
  71. *
  72. * To simplify the logic, doubled retract/recover moves are ignored.
  73. *
  74. * Note: Auto-retract will apply the set Z hop in addition to any Z hop
  75. * included in the G-code. Use M207 Z0 to to prevent double hop.
  76. */
  77. void FWRetract::retract(const bool retracting
  78. #if EXTRUDERS > 1
  79. , bool swapping/*=false*/
  80. #endif
  81. ) {
  82. // Prevent two retracts or recovers in a row
  83. if (retracted[active_extruder] == retracting) return;
  84. // Prevent two swap-retract or recovers in a row
  85. #if EXTRUDERS > 1
  86. // Allow G10 S1 only after G11
  87. if (swapping && retracted_swap[active_extruder] == retracting) return;
  88. // G11 priority to recover the long retract if activated
  89. if (!retracting) swapping = retracted_swap[active_extruder];
  90. #else
  91. constexpr bool swapping = false;
  92. #endif
  93. /* // debugging
  94. SERIAL_ECHOLNPAIR(
  95. "retracting ", retracting,
  96. " swapping ", swapping,
  97. " active extruder ", active_extruder
  98. );
  99. LOOP_L_N(i, EXTRUDERS) {
  100. SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
  101. #if EXTRUDERS > 1
  102. SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
  103. #endif
  104. }
  105. SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
  106. SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
  107. SERIAL_ECHOLNPAIR("current_hop ", current_hop);
  108. //*/
  109. const float base_retract = TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  110. * (swapping ? settings.swap_retract_length : settings.retract_length);
  111. // The current position will be the destination for E and Z moves
  112. destination = current_position;
  113. #if ENABLED(RETRACT_SYNC_MIXING)
  114. const uint8_t old_mixing_tool = mixer.get_current_vtool();
  115. mixer.T(MIXER_AUTORETRACT_TOOL);
  116. #endif
  117. const feedRate_t fr_max_z = planner.settings.max_feedrate_mm_s[Z_AXIS];
  118. if (retracting) {
  119. // Retract by moving from a faux E position back to the current E position
  120. current_retract[active_extruder] = base_retract;
  121. prepare_internal_move_to_destination( // set current to destination
  122. settings.retract_feedrate_mm_s * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  123. );
  124. // Is a Z hop set, and has the hop not yet been done?
  125. if (!current_hop && settings.retract_zraise > 0.01f) { // Apply hop only once
  126. current_hop += settings.retract_zraise; // Add to the hop total (again, only once)
  127. // Raise up, set_current_to_destination. Maximum Z feedrate
  128. prepare_internal_move_to_destination(fr_max_z);
  129. }
  130. }
  131. else {
  132. // If a hop was done and Z hasn't changed, undo the Z hop
  133. if (current_hop) {
  134. current_hop = 0;
  135. // Lower Z, set_current_to_destination. Maximum Z feedrate
  136. prepare_internal_move_to_destination(fr_max_z);
  137. }
  138. const float extra_recover = swapping ? settings.swap_retract_recover_extra : settings.retract_recover_extra;
  139. if (extra_recover) {
  140. current_position.e -= extra_recover; // Adjust the current E position by the extra amount to recover
  141. sync_plan_position_e(); // Sync the planner position so the extra amount is recovered
  142. }
  143. current_retract[active_extruder] = 0;
  144. // Recover E, set_current_to_destination
  145. prepare_internal_move_to_destination(
  146. (swapping ? settings.swap_retract_recover_feedrate_mm_s : settings.retract_recover_feedrate_mm_s)
  147. * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
  148. );
  149. }
  150. TERN_(RETRACT_SYNC_MIXING, mixer.T(old_mixing_tool)); // Restore original mixing tool
  151. retracted[active_extruder] = retracting; // Active extruder now retracted / recovered
  152. // If swap retract/recover update the retracted_swap flag too
  153. #if EXTRUDERS > 1
  154. if (swapping) retracted_swap[active_extruder] = retracting;
  155. #endif
  156. /* // debugging
  157. SERIAL_ECHOLNPAIR("retracting ", retracting);
  158. SERIAL_ECHOLNPAIR("swapping ", swapping);
  159. SERIAL_ECHOLNPAIR("active_extruder ", active_extruder);
  160. LOOP_L_N(i, EXTRUDERS) {
  161. SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
  162. #if EXTRUDERS > 1
  163. SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
  164. #endif
  165. }
  166. SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
  167. SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
  168. SERIAL_ECHOLNPAIR("current_hop ", current_hop);
  169. //*/
  170. }
  171. #endif // FWRETRACT