fix regression: double retraction when retract_before_wipe is 0%

-> fix Fix extra unwanted unretract in wipetower (caacfed4c3)
This commit is contained in:
supermerill 2026-09-11 00:38:26 +02:00
parent 1767a1298c
commit 21ef1066d9
2 changed files with 83 additions and 4 deletions

View file

@ -52,10 +52,10 @@ std::pair<double, double> Tool::extrude(double dE)
dE = m_formatter.quantize_e(dE);
m_E += dE;
m_absolute_E += dE;
if (m_retracted != 0.) {
m_retracted -= dE;
m_retracted = std::max(m_retracted, 0.);
}
// Negative E starts or extends a retraction (including a wipe with no
// pre-retraction). Positive E repays it, including WipeTower priming;
// extrusion beyond that point must not create a negative retraction.
m_retracted = std::max(m_retracted - dE, 0.);
return std::make_pair(dE, m_E);
}

View file

@ -6,6 +6,85 @@
using namespace Slic3r;
TEST_CASE("Wipe extrusion is counted before the remaining retraction", "[GCodeWriter][retraction-accounting]")
{
const bool relative_e = GENERATE(false, true);
const double before_wipe = GENERATE(0., 80.);
const double wipe_fraction = GENERATE(0.5, 1.);
CAPTURE(relative_e, before_wipe, wipe_fraction);
PrintConfig config;
config.use_relative_e_distances.value = relative_e;
config.use_firmware_retraction.value = false;
config.use_volumetric_e.value = false;
config.gcode_flavor.value = gcfMarlinFirmware;
config.retract_length.set_at(0.4, 0);
config.retract_before_wipe.set_at(before_wipe, 0);
config.retract_restart_extra.set_at(0., 0);
config.retract_restart_extra_toolchange.set_at(0., 0);
GCodeWriter writer;
writer.apply_print_config(config);
writer.set_extruders({0});
writer.set_tool(0);
// Use the same writer calls as retract_and_wipe(), without mesh slicing.
const std::string pre_retract = writer.retract(true);
CHECK(pre_retract.empty() == (before_wipe == 0.));
const double initial_retraction = 0.4 * before_wipe / 100.;
const double wiped = (0.4 - initial_retraction) * wipe_fraction;
REQUIRE_FALSE(writer.extrude_to_xy(Vec2d(1., 0.), -wiped / 2.).empty());
REQUIRE_FALSE(writer.extrude_to_xy(Vec2d(2., 0.), -wiped / 2.).empty());
CHECK(writer.tool()->retracted() == Approx(initial_retraction + wiped));
const std::string remaining_retract = writer.retract();
CHECK(remaining_retract.empty() == (wipe_fraction == 1.));
CHECK(writer.tool()->retracted() == Approx(0.4));
REQUIRE_FALSE(writer.unretract().empty());
CHECK(writer.tool()->retracted() == 0.);
CHECK(writer.tool()->used_filament() == Approx(0.).margin(1e-9));
CHECK(writer.unretract().empty());
}
TEST_CASE("Priming through extrude consumes outstanding retraction", "[GCodeWriter][retraction-accounting]")
{
const bool relative_e = GENERATE(false, true);
GCodeConfig config;
config.use_relative_e_distances.value = relative_e;
config.use_volumetric_e.value = false;
config.retract_restart_extra_toolchange.set_at(0., 0);
Extruder extruder(0, config);
// WipeTower priming uses extrude(), so a later unretract() must only
// restore the outstanding amount, never repeat the already emitted E.
REQUIRE(extruder.retract(0.4, 0., 0.).first == Approx(0.4));
SECTION("Partial priming leaves only the remainder") {
extruder.extrude(0.15);
CHECK(extruder.retracted() == Approx(0.25));
CHECK(extruder.unretract().first == Approx(0.25));
CHECK(extruder.used_filament() == Approx(0.).margin(1e-9));
}
SECTION("Complete priming does not require another unretraction") {
extruder.extrude(0.4);
CHECK_FALSE(extruder.need_unretract());
CHECK(extruder.unretract().first == 0.);
}
SECTION("Excess priming is deposited filament, not negative retraction") {
extruder.extrude(0.6);
CHECK(extruder.retracted() == 0.);
CHECK_FALSE(extruder.need_unretract());
CHECK(extruder.used_filament() == Approx(0.2));
}
SECTION("Priming preserves the configured restart extra") {
extruder.set_retracted(0.4, 0.1);
extruder.extrude(0.4);
CHECK(extruder.need_unretract());
CHECK(extruder.unretract().first == Approx(0.1));
CHECK(extruder.used_filament() == Approx(0.1));
}
CHECK(extruder.retracted() == 0.);
CHECK(extruder.unretract().first == 0.);
}
//note: erased by prusa, maybe it's wrong now?
SCENARIO("lift() is not ignored after unlift() at normal values of Z", "[GCodeWriter]") {
GIVEN("A config from a file and a single extruder.") {