fwretract.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 "MarlinConfig.h"
  26. #if ENABLED(FWRETRACT)
  27. #include "fwretract.h"
  28. #include "Marlin.h"
  29. #include "planner.h"
  30. #include "stepper.h"
  31. FWRetract fwretract; // Single instance - this calls the constructor
  32. // private:
  33. #if EXTRUDERS > 1
  34. bool FWRetract::retracted_swap[EXTRUDERS]; // Which extruders are swap-retracted
  35. #endif
  36. // public:
  37. bool FWRetract::autoretract_enabled, // M209 S - Autoretract switch
  38. FWRetract::retracted[EXTRUDERS]; // Which extruders are currently retracted
  39. float FWRetract::retract_length, // M207 S - G10 Retract length
  40. FWRetract::retract_feedrate_mm_s, // M207 F - G10 Retract feedrate
  41. FWRetract::retract_zlift, // M207 Z - G10 Retract hop size
  42. FWRetract::retract_recover_length, // M208 S - G11 Recover length
  43. FWRetract::retract_recover_feedrate_mm_s, // M208 F - G11 Recover feedrate
  44. FWRetract::swap_retract_length, // M207 W - G10 Swap Retract length
  45. FWRetract::swap_retract_recover_length, // M208 W - G11 Swap Recover length
  46. FWRetract::swap_retract_recover_feedrate_mm_s, // M208 R - G11 Swap Recover feedrate
  47. FWRetract::hop_amount;
  48. void FWRetract::reset() {
  49. autoretract_enabled = false;
  50. retract_length = RETRACT_LENGTH;
  51. retract_feedrate_mm_s = RETRACT_FEEDRATE;
  52. retract_zlift = RETRACT_ZLIFT;
  53. retract_recover_length = RETRACT_RECOVER_LENGTH;
  54. retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE;
  55. swap_retract_length = RETRACT_LENGTH_SWAP;
  56. swap_retract_recover_length = RETRACT_RECOVER_LENGTH_SWAP;
  57. swap_retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE_SWAP;
  58. hop_amount = 0.0;
  59. for (uint8_t i = 0; i < EXTRUDERS; ++i) {
  60. retracted[i] = false;
  61. #if EXTRUDERS > 1
  62. retracted_swap[i] = false;
  63. #endif
  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: Z lift is done transparently to the planner. Aborting
  75. * a print between G10 and G11 may corrupt the Z position.
  76. *
  77. * Note: Auto-retract will apply the set Z hop in addition to any Z hop
  78. * included in the G-code. Use M207 Z0 to to prevent double hop.
  79. */
  80. void FWRetract::retract(const bool retracting
  81. #if EXTRUDERS > 1
  82. , bool swapping /* =false */
  83. #endif
  84. ) {
  85. static float hop_amount = 0.0; // Total amount lifted, for use in recover
  86. // Prevent two retracts or recovers in a row
  87. if (retracted[active_extruder] == retracting) return;
  88. // Prevent two swap-retract or recovers in a row
  89. #if EXTRUDERS > 1
  90. // Allow G10 S1 only after G10
  91. if (swapping && retracted_swap[active_extruder] == retracting) return;
  92. // G11 priority to recover the long retract if activated
  93. if (!retracting) swapping = retracted_swap[active_extruder];
  94. #else
  95. constexpr bool swapping = false;
  96. #endif
  97. /* // debugging
  98. SERIAL_ECHOLNPAIR("retracting ", retracting);
  99. SERIAL_ECHOLNPAIR("swapping ", swapping);
  100. SERIAL_ECHOLNPAIR("active extruder ", active_extruder);
  101. for (uint8_t i = 0; i < EXTRUDERS; ++i) {
  102. SERIAL_ECHOPAIR("retracted[", i);
  103. SERIAL_ECHOLNPAIR("] ", retracted[i]);
  104. #if EXTRUDERS > 1
  105. SERIAL_ECHOPAIR("retracted_swap[", i);
  106. SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
  107. #endif
  108. }
  109. SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
  110. SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_CART]);
  111. SERIAL_ECHOLNPAIR("hop_amount ", hop_amount);
  112. //*/
  113. const float old_feedrate_mm_s = feedrate_mm_s,
  114. renormalize = RECIPROCAL(planner.e_factor[active_extruder]),
  115. base_retract = swapping ? swap_retract_length : retract_length,
  116. old_z = current_position[Z_AXIS],
  117. old_e = current_position[E_CART];
  118. // The current position will be the destination for E and Z moves
  119. set_destination_from_current();
  120. if (retracting) {
  121. // Retract by moving from a faux E position back to the current E position
  122. feedrate_mm_s = retract_feedrate_mm_s;
  123. destination[E_CART] -= base_retract * renormalize;
  124. prepare_move_to_destination(); // set_current_to_destination
  125. // Is a Z hop set, and has the hop not yet been done?
  126. if (retract_zlift > 0.01 && !hop_amount) { // Apply hop only once
  127. hop_amount += retract_zlift; // Add to the hop total (again, only once)
  128. destination[Z_AXIS] += retract_zlift; // Raise Z by the zlift (M207 Z) amount
  129. feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS]; // Maximum Z feedrate
  130. prepare_move_to_destination(); // Raise up, set_current_to_destination
  131. }
  132. }
  133. else {
  134. // If a hop was done and Z hasn't changed, undo the Z hop
  135. if (hop_amount) {
  136. current_position[Z_AXIS] += hop_amount; // Restore the actual Z position
  137. SYNC_PLAN_POSITION_KINEMATIC(); // Unspoof the position planner
  138. feedrate_mm_s = planner.max_feedrate_mm_s[Z_AXIS]; // Z feedrate to max
  139. prepare_move_to_destination(); // Lower Z, set_current_to_destination
  140. hop_amount = 0.0; // Clear the hop amount
  141. }
  142. destination[E_CART] += (base_retract + (swapping ? swap_retract_recover_length : retract_recover_length)) * renormalize;
  143. feedrate_mm_s = swapping ? swap_retract_recover_feedrate_mm_s : retract_recover_feedrate_mm_s;
  144. prepare_move_to_destination(); // Recover E, set_current_to_destination
  145. }
  146. feedrate_mm_s = old_feedrate_mm_s; // Restore original feedrate
  147. current_position[Z_AXIS] = old_z; // Restore Z and E positions
  148. current_position[E_CART] = old_e;
  149. SYNC_PLAN_POSITION_KINEMATIC(); // As if the move never took place
  150. retracted[active_extruder] = retracting; // Active extruder now retracted / recovered
  151. // If swap retract/recover update the retracted_swap flag too
  152. #if EXTRUDERS > 1
  153. if (swapping) retracted_swap[active_extruder] = retracting;
  154. #endif
  155. /* // debugging
  156. SERIAL_ECHOLNPAIR("retracting ", retracting);
  157. SERIAL_ECHOLNPAIR("swapping ", swapping);
  158. SERIAL_ECHOLNPAIR("active_extruder ", active_extruder);
  159. for (uint8_t i = 0; i < EXTRUDERS; ++i) {
  160. SERIAL_ECHOPAIR("retracted[", i);
  161. SERIAL_ECHOLNPAIR("] ", retracted[i]);
  162. #if EXTRUDERS > 1
  163. SERIAL_ECHOPAIR("retracted_swap[", i);
  164. SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
  165. #endif
  166. }
  167. SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
  168. SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_CART]);
  169. SERIAL_ECHOLNPAIR("hop_amount ", hop_amount);
  170. //*/
  171. }
  172. #endif // FWRETRACT