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.2KB

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