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.

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