2017-09-07 22:40:32 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-09-07 22:40:32 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-09-07 22:40:32 -05: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-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-09-07 22:40:32 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fwretract.cpp - Implement firmware-based retraction
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#if ENABLED(FWRETRACT)
|
|
|
|
|
|
|
|
#include "fwretract.h"
|
|
|
|
|
2017-09-29 08:03:28 -05:00
|
|
|
FWRetract fwretract; // Single instance - this calls the constructor
|
2017-09-07 22:40:32 -05:00
|
|
|
|
|
|
|
#include "../module/motion.h"
|
2017-09-08 23:50:46 -05:00
|
|
|
#include "../module/planner.h"
|
2017-09-29 08:03:28 -05:00
|
|
|
#include "../module/stepper.h"
|
|
|
|
|
2018-10-01 23:32:02 -05:00
|
|
|
#if ENABLED(RETRACT_SYNC_MIXING)
|
2019-02-04 03:41:55 -06:00
|
|
|
#include "mixing.h"
|
2018-10-01 23:32:02 -05:00
|
|
|
#endif
|
|
|
|
|
2017-09-29 08:03:28 -05:00
|
|
|
// private:
|
|
|
|
|
|
|
|
#if EXTRUDERS > 1
|
2018-10-10 09:45:20 -05:00
|
|
|
bool FWRetract::retracted_swap[EXTRUDERS]; // Which extruders are swap-retracted
|
2017-09-29 08:03:28 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// public:
|
2017-09-07 22:40:32 -05:00
|
|
|
|
2018-10-10 09:45:20 -05: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-08 21:16:41 -05:00
|
|
|
FWRetract::current_hop;
|
2017-09-07 22:40:32 -05:00
|
|
|
|
|
|
|
void FWRetract::reset() {
|
2020-04-22 16:35:03 -05:00
|
|
|
TERN_(FWRETRACT_AUTORETRACT, autoretract_enabled = false);
|
2018-10-10 09:45:20 -05:00
|
|
|
settings.retract_length = RETRACT_LENGTH;
|
|
|
|
settings.retract_feedrate_mm_s = RETRACT_FEEDRATE;
|
2018-10-16 16:46:24 -05:00
|
|
|
settings.retract_zraise = RETRACT_ZRAISE;
|
2019-03-14 02:26:07 -05:00
|
|
|
settings.retract_recover_extra = RETRACT_RECOVER_LENGTH;
|
2018-10-10 09:45:20 -05:00
|
|
|
settings.retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE;
|
|
|
|
settings.swap_retract_length = RETRACT_LENGTH_SWAP;
|
2019-03-14 02:26:07 -05:00
|
|
|
settings.swap_retract_recover_extra = RETRACT_RECOVER_LENGTH_SWAP;
|
2018-10-10 09:45:20 -05:00
|
|
|
settings.swap_retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE_SWAP;
|
2018-09-08 21:16:41 -05:00
|
|
|
current_hop = 0.0;
|
2017-09-29 08:03:28 -05:00
|
|
|
|
2020-03-13 23:18:16 -05:00
|
|
|
LOOP_L_N(i, EXTRUDERS) {
|
2017-09-29 08:03:28 -05:00
|
|
|
retracted[i] = false;
|
|
|
|
#if EXTRUDERS > 1
|
|
|
|
retracted_swap[i] = false;
|
|
|
|
#endif
|
2018-09-16 21:24:15 -05:00
|
|
|
current_retract[i] = 0.0;
|
2017-09-29 08:03:28 -05:00
|
|
|
}
|
2017-09-07 22:40:32 -05: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
|
|
|
|
#if EXTRUDERS > 1
|
2020-04-19 02:01:30 -05:00
|
|
|
, bool swapping/*=false*/
|
2017-09-07 22:40:32 -05:00
|
|
|
#endif
|
|
|
|
) {
|
2017-10-26 16:33:19 -05:00
|
|
|
// Prevent two retracts or recovers in a row
|
2017-09-07 22:40:32 -05:00
|
|
|
if (retracted[active_extruder] == retracting) return;
|
|
|
|
|
2017-10-26 16:33:19 -05:00
|
|
|
// Prevent two swap-retract or recovers in a row
|
2017-09-29 08:03:28 -05:00
|
|
|
#if EXTRUDERS > 1
|
2019-04-26 02:36:29 -05:00
|
|
|
// Allow G10 S1 only after G11
|
2017-10-26 16:33:19 -05:00
|
|
|
if (swapping && retracted_swap[active_extruder] == retracting) return;
|
|
|
|
// G11 priority to recover the long retract if activated
|
2017-09-29 08:03:28 -05:00
|
|
|
if (!retracting) swapping = retracted_swap[active_extruder];
|
|
|
|
#else
|
2018-05-03 19:08:49 -05:00
|
|
|
constexpr bool swapping = false;
|
2017-09-07 22:40:32 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* // debugging
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR(
|
|
|
|
"retracting ", retracting,
|
2019-04-26 02:36:29 -05:00
|
|
|
" swapping ", swapping,
|
|
|
|
" active extruder ", active_extruder
|
2019-03-05 06:46:19 -06:00
|
|
|
);
|
2020-03-13 23:18:16 -05:00
|
|
|
LOOP_L_N(i, EXTRUDERS) {
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
|
2018-05-03 19:08:49 -05:00
|
|
|
#if EXTRUDERS > 1
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
|
2018-05-03 19:08:49 -05:00
|
|
|
#endif
|
2017-09-07 22:40:32 -05:00
|
|
|
}
|
2019-09-29 04:25:39 -05:00
|
|
|
SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
|
|
|
|
SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
|
2018-09-08 21:16:41 -05:00
|
|
|
SERIAL_ECHOLNPAIR("current_hop ", current_hop);
|
2017-09-07 22:40:32 -05:00
|
|
|
//*/
|
|
|
|
|
2020-04-22 16:35:03 -05:00
|
|
|
const float base_retract = TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
|
2020-04-19 02:01:30 -05:00
|
|
|
* (swapping ? settings.swap_retract_length : settings.retract_length);
|
2017-09-07 22:40:32 -05:00
|
|
|
|
|
|
|
// The current position will be the destination for E and Z moves
|
2019-09-29 04:25:39 -05:00
|
|
|
destination = current_position;
|
2017-09-07 22:40:32 -05:00
|
|
|
|
2018-10-01 23:32:02 -05:00
|
|
|
#if ENABLED(RETRACT_SYNC_MIXING)
|
2019-02-25 14:28:01 -06:00
|
|
|
const uint8_t old_mixing_tool = mixer.get_current_vtool();
|
2018-10-16 03:38:57 -05:00
|
|
|
mixer.T(MIXER_AUTORETRACT_TOOL);
|
2018-10-01 23:32:02 -05:00
|
|
|
#endif
|
|
|
|
|
2019-09-26 01:28:09 -05:00
|
|
|
const feedRate_t fr_max_z = planner.settings.max_feedrate_mm_s[Z_AXIS];
|
2017-10-26 16:33:19 -05:00
|
|
|
if (retracting) {
|
2017-09-07 22:40:32 -05:00
|
|
|
// Retract by moving from a faux E position back to the current E position
|
2019-09-26 01:28:09 -05:00
|
|
|
current_retract[active_extruder] = base_retract;
|
2019-09-29 04:25:39 -05:00
|
|
|
prepare_internal_move_to_destination( // set current to destination
|
2020-04-22 16:35:03 -05:00
|
|
|
settings.retract_feedrate_mm_s * TERN1(RETRACT_SYNC_MIXING, (MIXING_STEPPERS))
|
2018-10-01 23:32:02 -05:00
|
|
|
);
|
2017-09-07 22:40:32 -05:00
|
|
|
|
|
|
|
// Is a Z hop set, and has the hop not yet been done?
|
2019-09-26 01:28:09 -05: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-07 22:40:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// If a hop was done and Z hasn't changed, undo the Z hop
|
2018-09-08 21:16:41 -05:00
|
|
|
if (current_hop) {
|
2019-09-26 01:28:09 -05:00
|
|
|
current_hop = 0;
|
|
|
|
// Lower Z, set_current_to_destination. Maximum Z feedrate
|
|
|
|
prepare_internal_move_to_destination(fr_max_z);
|
2018-03-31 23:01:13 -05:00
|
|
|
}
|
2017-09-07 22:40:32 -05:00
|
|
|
|
2019-03-14 02:26:07 -05:00
|
|
|
const float extra_recover = swapping ? settings.swap_retract_recover_extra : settings.retract_recover_extra;
|
2019-09-26 01:28:09 -05:00
|
|
|
if (extra_recover) {
|
2019-09-29 04:25:39 -05:00
|
|
|
current_position.e -= extra_recover; // Adjust the current E position by the extra amount to recover
|
2018-09-16 21:24:15 -05:00
|
|
|
sync_plan_position_e(); // Sync the planner position so the extra amount is recovered
|
|
|
|
}
|
|
|
|
|
2019-09-26 01:28:09 -05:00
|
|
|
current_retract[active_extruder] = 0;
|
|
|
|
|
2020-04-22 16:35:03 -05: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-07 22:40:32 -05:00
|
|
|
}
|
|
|
|
|
2020-04-19 02:01:30 -05:00
|
|
|
TERN_(RETRACT_SYNC_MIXING, mixer.T(old_mixing_tool)); // Restore original mixing tool
|
2018-10-01 23:32:02 -05:00
|
|
|
|
2017-10-26 16:33:19 -05:00
|
|
|
retracted[active_extruder] = retracting; // Active extruder now retracted / recovered
|
2017-09-07 22:40:32 -05:00
|
|
|
|
2017-10-26 16:33:19 -05:00
|
|
|
// If swap retract/recover update the retracted_swap flag too
|
2017-09-07 22:40:32 -05:00
|
|
|
#if EXTRUDERS > 1
|
|
|
|
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-13 23:18:16 -05:00
|
|
|
LOOP_L_N(i, EXTRUDERS) {
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("retracted[", i, "] ", retracted[i]);
|
2018-05-03 19:08:49 -05:00
|
|
|
#if EXTRUDERS > 1
|
2019-03-05 06:46:19 -06:00
|
|
|
SERIAL_ECHOLNPAIR("retracted_swap[", i, "] ", retracted_swap[i]);
|
2018-05-03 19:08:49 -05:00
|
|
|
#endif
|
2017-09-07 22:40:32 -05:00
|
|
|
}
|
2019-09-29 04:25:39 -05:00
|
|
|
SERIAL_ECHOLNPAIR("current_position.z ", current_position.z);
|
|
|
|
SERIAL_ECHOLNPAIR("current_position.e ", current_position.e);
|
2018-09-08 21:16:41 -05:00
|
|
|
SERIAL_ECHOLNPAIR("current_hop ", current_hop);
|
2017-09-07 22:40:32 -05:00
|
|
|
//*/
|
2017-10-26 16:33:19 -05:00
|
|
|
}
|
2017-09-07 22:40:32 -05:00
|
|
|
|
|
|
|
#endif // FWRETRACT
|