Firmware2/Marlin/src/feature/fwretract.cpp

278 lines
9.9 KiB
C++
Raw Normal View History

2017-09-08 05:40:32 +02:00
/**
* Marlin 3D Printer Firmware
2020-02-03 15:00:57 +01:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2017-09-08 05:40:32 +02:00
*
* Based on Sprinter and grbl.
2019-06-28 06:57:50 +02:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2017-09-08 05:40:32 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2020-07-23 05:20:14 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-09-08 05:40:32 +02:00
*
*/
/**
* fwretract.cpp - Implement firmware-based retraction
*/
#include "../inc/MarlinConfig.h"
#if ENABLED(FWRETRACT)
#include "fwretract.h"
2017-09-29 15:03:28 +02:00
FWRetract fwretract; // Single instance - this calls the constructor
2017-09-08 05:40:32 +02:00
#include "../module/motion.h"
2017-09-09 06:50:46 +02:00
#include "../module/planner.h"
2017-09-29 15:03:28 +02:00
#include "../module/stepper.h"
2021-03-12 16:25:41 +01:00
#include "../gcode/parser.h"
#if ENABLED(RETRACT_SYNC_MIXING)
2019-02-04 10:41:55 +01:00
#include "mixing.h"
#endif
2017-09-29 15:03:28 +02:00
// private:
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
bool FWRetract::retracted_swap[EXTRUDERS]; // Which extruders are swap-retracted
2017-09-29 15:03:28 +02:00
#endif
// public:
2017-09-08 05:40:32 +02:00
fwretract_settings_t FWRetract::settings; // M207 S F Z W, M208 S F W R
#if ENABLED(FWRETRACT_AUTORETRACT)
bool FWRetract::autoretract_enabled; // M209 S - Autoretract switch
#endif
bool FWRetract::retracted[EXTRUDERS]; // Which extruders are currently retracted
float FWRetract::current_retract[EXTRUDERS], // Retract value used by planner
2018-09-09 04:16:41 +02:00
FWRetract::current_hop;
2017-09-08 05:40:32 +02:00
void FWRetract::reset() {
2020-04-22 23:35:03 +02:00
TERN_(FWRETRACT_AUTORETRACT, autoretract_enabled = false);
settings.retract_length = RETRACT_LENGTH;
settings.retract_feedrate_mm_s = RETRACT_FEEDRATE;
2018-10-16 23:46:24 +02:00
settings.retract_zraise = RETRACT_ZRAISE;
settings.retract_recover_extra = RETRACT_RECOVER_LENGTH;
settings.retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE;
settings.swap_retract_length = RETRACT_LENGTH_SWAP;
settings.swap_retract_recover_extra = RETRACT_RECOVER_LENGTH_SWAP;
settings.swap_retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE_SWAP;
2018-09-09 04:16:41 +02:00
current_hop = 0.0;
2017-09-29 15:03:28 +02:00
2020-03-14 05:18:16 +01:00
LOOP_L_N(i, EXTRUDERS) {
2017-09-29 15:03:28 +02:00
retracted[i] = false;
2020-09-21 01:29:08 +02:00
TERN_(HAS_MULTI_EXTRUDER, retracted_swap[i] = false);
current_retract[i] = 0.0;
2017-09-29 15:03:28 +02:00
}
2017-09-08 05:40:32 +02:00
}
/**
* Retract or recover according to firmware settings
*
* This function handles retract/recover moves for G10 and G11,
* plus auto-retract moves sent from G0/G1 when E-only moves are done.
*
* To simplify the logic, doubled retract/recover moves are ignored.
*
* Note: Auto-retract will apply the set Z hop in addition to any Z hop
* included in the G-code. Use M207 Z0 to to prevent double hop.
*/
void FWRetract::retract(const bool retracting
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
2020-04-19 09:01:30 +02:00
, bool swapping/*=false*/
2017-09-08 05:40:32 +02:00
#endif
) {
// Prevent two retracts or recovers in a row
2017-09-08 05:40:32 +02:00
if (retracted[active_extruder] == retracting) return;
// Prevent two swap-retract or recovers in a row
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
// Allow G10 S1 only after G11
if (swapping && retracted_swap[active_extruder] == retracting) return;
// G11 priority to recover the long retract if activated
2017-09-29 15:03:28 +02:00
if (!retracting) swapping = retracted_swap[active_extruder];
#else
2018-05-04 02:08:49 +02:00
constexpr bool swapping = false;
2017-09-08 05:40:32 +02:00
#endif
/* // debugging
SERIAL_ECHOLNPAIR(
"retracting ", retracting,
" swapping ", swapping,
" active extruder ", active_extruder
);
2020-03-14 05:18:16 +01:00
LOOP_L_N(i, EXTRUDERS) {
SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
2018-05-04 02:08:49 +02:00
#endif
2017-09-08 05:40:32 +02:00
}
2019-09-29 11:25:39 +02:00
SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
2018-09-09 04:16:41 +02:00
SERIAL_ECHOLNPAIR("current_hop ", current_hop);
2017-09-08 05:40:32 +02:00
//*/
2020-04-22 23:35:03 +02:00
const float base_retract = TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
2020-04-19 09:01:30 +02:00
* (swapping ? settings.swap_retract_length : settings.retract_length);
2017-09-08 05:40:32 +02:00
// The current position will be the destination for E and Z moves
2019-09-29 11:25:39 +02:00
destination = current_position;
2017-09-08 05:40:32 +02:00
#if ENABLED(RETRACT_SYNC_MIXING)
2019-02-25 21:28:01 +01:00
const uint8_t old_mixing_tool = mixer.get_current_vtool();
2018-10-16 10:38:57 +02:00
mixer.T(MIXER_AUTORETRACT_TOOL);
#endif
2019-09-26 08:28:09 +02:00
const feedRate_t fr_max_z = planner.settings.max_feedrate_mm_s[Z_AXIS];
if (retracting) {
2017-09-08 05:40:32 +02:00
// Retract by moving from a faux E position back to the current E position
2019-09-26 08:28:09 +02:00
current_retract[active_extruder] = base_retract;
prepare_internal_move_to_destination( // set current from destination
2020-04-22 23:35:03 +02:00
settings.retract_feedrate_mm_s * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
);
2017-09-08 05:40:32 +02:00
// Is a Z hop set, and has the hop not yet been done?
2019-09-26 08:28:09 +02:00
if (!current_hop && settings.retract_zraise > 0.01f) { // Apply hop only once
current_hop += settings.retract_zraise; // Add to the hop total (again, only once)
// Raise up, set_current_to_destination. Maximum Z feedrate
prepare_internal_move_to_destination(fr_max_z);
2017-09-08 05:40:32 +02:00
}
}
else {
// If a hop was done and Z hasn't changed, undo the Z hop
2018-09-09 04:16:41 +02:00
if (current_hop) {
2019-09-26 08:28:09 +02:00
current_hop = 0;
// Lower Z, set_current_to_destination. Maximum Z feedrate
prepare_internal_move_to_destination(fr_max_z);
}
2017-09-08 05:40:32 +02:00
const float extra_recover = swapping ? settings.swap_retract_recover_extra : settings.retract_recover_extra;
2019-09-26 08:28:09 +02:00
if (extra_recover) {
2019-09-29 11:25:39 +02:00
current_position.e -= extra_recover; // Adjust the current E position by the extra amount to recover
sync_plan_position_e(); // Sync the planner position so the extra amount is recovered
}
2019-09-26 08:28:09 +02:00
current_retract[active_extruder] = 0;
2020-04-22 23:35:03 +02:00
// Recover E, set_current_to_destination
prepare_internal_move_to_destination(
(swapping ? settings.swap_retract_recover_feedrate_mm_s : settings.retract_recover_feedrate_mm_s)
* TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
);
2017-09-08 05:40:32 +02:00
}
2020-04-19 09:01:30 +02:00
TERN_(RETRACT_SYNC_MIXING, mixer.T(old_mixing_tool)); // Restore original mixing tool
retracted[active_extruder] = retracting; // Active extruder now retracted / recovered
2017-09-08 05:40:32 +02:00
// If swap retract/recover update the retracted_swap flag too
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
2017-09-08 05:40:32 +02:00
if (swapping) retracted_swap[active_extruder] = retracting;
#endif
/* // debugging
SERIAL_ECHOLNPAIR("retracting ", retracting);
SERIAL_ECHOLNPAIR("swapping ", swapping);
SERIAL_ECHOLNPAIR("active_extruder ", active_extruder);
2020-03-14 05:18:16 +01:00
LOOP_L_N(i, EXTRUDERS) {
SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
2020-09-21 01:29:08 +02:00
#if HAS_MULTI_EXTRUDER
SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
2018-05-04 02:08:49 +02:00
#endif
2017-09-08 05:40:32 +02:00
}
2019-09-29 11:25:39 +02:00
SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
2018-09-09 04:16:41 +02:00
SERIAL_ECHOLNPAIR("current_hop ", current_hop);
2017-09-08 05:40:32 +02:00
//*/
}
2017-09-08 05:40:32 +02:00
2021-03-12 16:25:41 +01:00
//extern const char SP_Z_STR[];
/**
* M207: Set firmware retraction values
*
* S[+units] retract_length
* W[+units] swap_retract_length (multi-extruder)
* F[units/min] retract_feedrate_mm_s
* Z[units] retract_zraise
*/
void FWRetract::M207() {
if (!parser.seen("FSWZ")) return M207_report();
if (parser.seen('S')) settings.retract_length = parser.value_axis_units(E_AXIS);
if (parser.seen('F')) settings.retract_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
if (parser.seen('Z')) settings.retract_zraise = parser.value_linear_units();
if (parser.seen('W')) settings.swap_retract_length = parser.value_axis_units(E_AXIS);
}
void FWRetract::M207_report(const bool forReplay/*=false*/) {
if (!forReplay) { SERIAL_ECHO_MSG("; Retract: S<length> F<units/m> Z<lift>"); SERIAL_ECHO_START(); }
SERIAL_ECHOLNPAIR_P(
PSTR(" M207 S"), LINEAR_UNIT(settings.retract_length)
, PSTR(" W"), LINEAR_UNIT(settings.swap_retract_length)
, PSTR(" F"), LINEAR_UNIT(MMS_TO_MMM(settings.retract_feedrate_mm_s))
, SP_Z_STR, LINEAR_UNIT(settings.retract_zraise)
);
}
/**
* M208: Set firmware un-retraction values
*
* S[+units] retract_recover_extra (in addition to M207 S*)
* W[+units] swap_retract_recover_extra (multi-extruder)
* F[units/min] retract_recover_feedrate_mm_s
* R[units/min] swap_retract_recover_feedrate_mm_s
*/
void FWRetract::M208() {
if (!parser.seen("FSRW")) return M208_report();
if (parser.seen('S')) settings.retract_recover_extra = parser.value_axis_units(E_AXIS);
if (parser.seen('F')) settings.retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
if (parser.seen('R')) settings.swap_retract_recover_feedrate_mm_s = MMM_TO_MMS(parser.value_axis_units(E_AXIS));
if (parser.seen('W')) settings.swap_retract_recover_extra = parser.value_axis_units(E_AXIS);
}
void FWRetract::M208_report(const bool forReplay/*=false*/) {
if (!forReplay) { SERIAL_ECHO_MSG("; Recover: S<length> F<units/m>"); SERIAL_ECHO_START(); }
SERIAL_ECHOLNPAIR(
" M208 S", LINEAR_UNIT(settings.retract_recover_extra)
, " W", LINEAR_UNIT(settings.swap_retract_recover_extra)
, " F", LINEAR_UNIT(MMS_TO_MMM(settings.retract_recover_feedrate_mm_s))
);
}
#if ENABLED(FWRETRACT_AUTORETRACT)
/**
* M209: Enable automatic retract (M209 S1)
* For slicers that don't support G10/11, reversed extrude-only
* moves will be classified as retraction.
*/
void FWRetract::M209() {
if (!parser.seen('S')) return M209_report();
if (MIN_AUTORETRACT <= MAX_AUTORETRACT)
enable_autoretract(parser.value_bool());
}
void FWRetract::M209_report(const bool forReplay/*=false*/) {
if (!forReplay) { SERIAL_ECHO_MSG("; Auto-Retract: S=0 to disable, 1 to interpret E-only moves as retract/recover"); SERIAL_ECHO_START(); }
SERIAL_ECHOLNPAIR(" M209 S", autoretract_enabled);
}
#endif // FWRETRACT_AUTORETRACT
2017-09-08 05:40:32 +02:00
#endif // FWRETRACT