This repository has been archived on 2022-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
Marlin-Artillery-M600/Marlin/src/feature/fwretract.cpp

226 lines
8.2 KiB
C++
Raw Normal View History

2017-09-08 05:40:32 +02:00
/**
* Marlin 3D Printer Firmware
2019-02-12 22:06:53 +01:00
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2017-09-08 05:40:32 +02:00
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* 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"
#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:
#if EXTRUDERS > 1
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() {
#if ENABLED(FWRETRACT_AUTORETRACT)
autoretract_enabled = false;
#endif
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_length = RETRACT_RECOVER_LENGTH;
settings.retract_recover_feedrate_mm_s = RETRACT_RECOVER_FEEDRATE;
settings.swap_retract_length = RETRACT_LENGTH_SWAP;
settings.swap_retract_recover_length = 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
for (uint8_t i = 0; i < EXTRUDERS; ++i) {
retracted[i] = false;
#if EXTRUDERS > 1
retracted_swap[i] = false;
#endif
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
#if EXTRUDERS > 1
, bool swapping /* =false */
#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
2017-09-29 15:03:28 +02:00
#if EXTRUDERS > 1
// Allow G10 S1 only after G10
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);
SERIAL_ECHOLNPAIR("swapping ", swapping);
SERIAL_ECHOLNPAIR("active extruder ", active_extruder);
for (uint8_t i = 0; i < EXTRUDERS; ++i) {
SERIAL_ECHOPAIR("retracted[", i);
SERIAL_ECHOLNPAIR("] ", retracted[i]);
2018-05-04 02:08:49 +02:00
#if EXTRUDERS > 1
SERIAL_ECHOPAIR("retracted_swap[", i);
SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
#endif
2017-09-08 05:40:32 +02:00
}
SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_AXIS]);
2018-09-09 04:16:41 +02:00
SERIAL_ECHOLNPAIR("current_hop ", current_hop);
2017-09-08 05:40:32 +02:00
//*/
2018-05-04 02:08:49 +02:00
const float old_feedrate_mm_s = feedrate_mm_s,
unscale_e = RECIPROCAL(planner.e_factor[active_extruder]),
unscale_fr = 100.0 / feedrate_percentage, // Disable feedrate scaling for retract moves
base_retract = (
(swapping ? settings.swap_retract_length : settings.retract_length)
#if ENABLED(RETRACT_SYNC_MIXING)
* (MIXING_STEPPERS)
#endif
);
2017-09-08 05:40:32 +02:00
// The current position will be the destination for E and Z moves
set_destination_from_current();
2017-09-08 05:40:32 +02:00
#if ENABLED(RETRACT_SYNC_MIXING)
2019-02-03 06:30:26 +01:00
uint8_t old_mixing_tool = mixer.get_current_vtool();
2018-10-16 10:38:57 +02:00
mixer.T(MIXER_AUTORETRACT_TOOL);
#endif
if (retracting) {
2017-09-08 05:40:32 +02:00
// Retract by moving from a faux E position back to the current E position
feedrate_mm_s = (
settings.retract_feedrate_mm_s * unscale_fr
#if ENABLED(RETRACT_SYNC_MIXING)
* (MIXING_STEPPERS)
#endif
);
current_retract[active_extruder] = base_retract * unscale_e;
2018-05-04 02:08:49 +02:00
prepare_move_to_destination(); // set_current_to_destination
planner.synchronize(); // Wait for move to complete
2017-09-08 05:40:32 +02:00
// Is a Z hop set, and has the hop not yet been done?
2018-10-16 23:46:24 +02:00
if (settings.retract_zraise > 0.01 && !current_hop) { // Apply hop only once
current_hop += settings.retract_zraise; // Add to the hop total (again, only once)
feedrate_mm_s = planner.settings.max_feedrate_mm_s[Z_AXIS] * unscale_fr; // Maximum Z feedrate
prepare_move_to_destination(); // Raise up, set_current_to_destination
planner.synchronize(); // Wait for move to complete
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) {
current_hop = 0.0;
feedrate_mm_s = planner.settings.max_feedrate_mm_s[Z_AXIS] * unscale_fr; // Z feedrate to max
prepare_move_to_destination(); // Lower Z, set_current_to_destination
planner.synchronize(); // Wait for move to complete
}
2017-09-08 05:40:32 +02:00
const float extra_recover = swapping ? settings.swap_retract_recover_length : settings.retract_recover_length;
if (extra_recover != 0.0) {
current_position[E_AXIS] -= 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
}
current_retract[active_extruder] = 0.0;
feedrate_mm_s = (
(swapping ? settings.swap_retract_recover_feedrate_mm_s : settings.retract_recover_feedrate_mm_s) * unscale_fr
#if ENABLED(RETRACT_SYNC_MIXING)
* (MIXING_STEPPERS)
#endif
);
prepare_move_to_destination(); // Recover E, set_current_to_destination
planner.synchronize(); // Wait for move to complete
2017-09-08 05:40:32 +02:00
}
#if ENABLED(RETRACT_SYNC_MIXING)
2018-10-16 10:38:57 +02:00
mixer.T(old_mixing_tool); // Restore original mixing tool
#endif
feedrate_mm_s = old_feedrate_mm_s; // Restore original feedrate
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
2017-09-08 05:40:32 +02: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);
for (uint8_t i = 0; i < EXTRUDERS; ++i) {
SERIAL_ECHOPAIR("retracted[", i);
SERIAL_ECHOLNPAIR("] ", retracted[i]);
2018-05-04 02:08:49 +02:00
#if EXTRUDERS > 1
SERIAL_ECHOPAIR("retracted_swap[", i);
SERIAL_ECHOLNPAIR("] ", retracted_swap[i]);
#endif
2017-09-08 05:40:32 +02:00
}
SERIAL_ECHOLNPAIR("current_position[z] ", current_position[Z_AXIS]);
SERIAL_ECHOLNPAIR("current_position[e] ", current_position[E_AXIS]);
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
#endif // FWRETRACT