From cb80a0db84003d6f44e6f0f84d544f2068b44273 Mon Sep 17 00:00:00 2001 From: Sergei <119299827+ghzserg@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:58:00 +0500 Subject: [PATCH] Fix/wipetower klipper m400 stalls (#17246) * WipeTower2: on Klipper, sync motion only before temperature commands Follow-up to #15133. On Klipper, flush_planner_queue() emitted M400 at every toolchange boundary, stalling the toolhead over the tower. Keep the sync only before M104/M109/M591, and drop the sync line left behind when GCode.cpp strips the interface-temperature M109. End the deretraction placeholder with a newline: without the M400 that used to follow it, the next tower move was glued to the placeholder line, skipped post_process_wipe_tower_moves and kept tower-local Y. Creator 5 Pro, 247 toolchanges: 1235 M400 -> 247 (~30 min saved). * Update src/libslic3r/GCode.cpp Good point, done. The strip now uses flush_planner_queue_command() and follows whatever the tower writes. Co-authored-by: Kris Austin * tests: move the wipe tower slice and shared checks into one helper * WipeTower2: keep the sync at the end of the unload on Klipper [change_filament_gcode] and Tn are macros on Klipper; their non-motion parts run when parsed, so without the sync a filament-change macro could run while the unload and ramming moves are still queued. Creator 5 Pro, 247 toolchanges: 494 M400 instead of 1235 (247 in this PR before). The other boundary flushes, which stalled the toolhead over the tower, stay removed. * tests: check the unload sync before each change_filament_gcode * WipeTower2: don't sync the cooldown after the interface wipe The reset to the base temperature after the interface wipe isn't timing-critical, but its M400 stopped the head over the tower before the tower perimeter. Test print, Creator 5 Pro, 247 toolchanges: 3:22 with the sync, 3:17 without it. * tests: check only the interface cooldown for a missing sync * WipeTower2: skip the cooldown sync on Klipper only --------- Co-authored-by: Kris Austin Co-authored-by: GhZserG --- src/libslic3r/GCode.cpp | 7 ++- src/libslic3r/GCode/WipeTower.cpp | 5 +- src/libslic3r/GCode/WipeTower2.cpp | 20 +++--- tests/fff_print/test_wipe_tower.cpp | 96 ++++++++++++++++++++++++++--- 4 files changed, 110 insertions(+), 18 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index bc6b91ab84..ae63ee16b5 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1910,7 +1910,12 @@ static std::vector get_path_of_change_filament(const Print& print) } } } - if (!skip_line) { + if (skip_line) { + // Also drop the planner sync WipeTower2 put right before the stripped M109. + const std::string sync = flush_planner_queue_command(gcodegen.config().gcode_flavor.value); + if (boost::ends_with(out, sync)) + out.resize(out.size() - sync.size()); + } else { out.append(line); if (line_end < tcr_rotated_gcode.size()) out.push_back('\n'); diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp index 8880c465bf..184ecf8cec 100644 --- a/src/libslic3r/GCode/WipeTower.cpp +++ b/src/libslic3r/GCode/WipeTower.cpp @@ -1298,7 +1298,10 @@ public: WipeTowerWriter& flush_planner_queue() { - m_gcode += flush_planner_queue_command(m_gcode_flavor); + // Only called at toolchange boundaries; format_line_M104 syncs temperature changes itself. + // On Klipper M400 here only stalls the toolhead over the tower. + if (m_gcode_flavor != gcfKlipper) + m_gcode += flush_planner_queue_command(m_gcode_flavor); return *this; } diff --git a/src/libslic3r/GCode/WipeTower2.cpp b/src/libslic3r/GCode/WipeTower2.cpp index 9f79f2bd11..681a03e883 100644 --- a/src/libslic3r/GCode/WipeTower2.cpp +++ b/src/libslic3r/GCode/WipeTower2.cpp @@ -405,7 +405,7 @@ public: } WipeTowerWriter2& switch_filament_monitoring(bool enable) { - flush_planner_queue(); + m_gcode += flush_planner_queue_command(m_gcode_flavor); m_gcode += enable ? "M591 R\n" : "M591 S0\n"; return *this; } @@ -644,9 +644,10 @@ public: } // Set extruder temperature, don't wait by default. - WipeTowerWriter2& set_extruder_temp(int temperature, bool wait = false, const std::string& comment = std::string()) + WipeTowerWriter2& set_extruder_temp(int temperature, bool wait = false, const std::string& comment = std::string(), bool sync = true) { - flush_planner_queue(); + if (sync) // sync the temperature change with motion + m_gcode += flush_planner_queue_command(m_gcode_flavor); m_gcode += "M" + std::to_string(wait ? 109 : 104) + " S" + std::to_string(temperature); if (!comment.empty()) m_gcode += " " + comment; @@ -702,7 +703,9 @@ public: WipeTowerWriter2& flush_planner_queue() { - m_gcode += flush_planner_queue_command(m_gcode_flavor); + // Toolchange boundaries: on Klipper M400 here only stalls the toolhead over the tower. + if (m_gcode_flavor != gcfKlipper) + m_gcode += flush_planner_queue_command(m_gcode_flavor); return *this; } @@ -1413,7 +1416,7 @@ WipeTower::ToolChangeResult WipeTower2::tool_change(size_t tool) if (interface_temp > 0 && interface_temp != base_temp && !m_wait_for_temp_on_wipe_tower) writer.set_extruder_temp(interface_temp, true); if (m_enable_tower_interface_cooldown_during_tower && interface_temp > 0 && interface_temp != base_temp) - writer.set_extruder_temp(base_temp, false); + writer.set_extruder_temp(base_temp, false, {}, m_gcode_flavor != gcfKlipper); // a cooldown back to base isn't timing-critical float pre_dist = m_filpar[tool].tower_interface_pre_extrusion_dist; float pre_len = m_filpar[tool].tower_interface_pre_extrusion_length; if (pre_dist > 0.f && pre_len > 0.f) { @@ -1426,7 +1429,7 @@ WipeTower::ToolChangeResult WipeTower2::tool_change(size_t tool) if (interface_layer) { int interface_temp = m_filpar[tool].interface_print_temperature; if (!m_enable_tower_interface_cooldown_during_tower && interface_temp > 0 && interface_temp != base_temp) - writer.set_extruder_temp(base_temp, false); + writer.set_extruder_temp(base_temp, false, {}, m_gcode_flavor != gcfKlipper); // on Klipper M400 would stall the head over the tower } writer.append(";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Wipe_Tower_End) + "\n"); ++ m_num_tool_changes; @@ -1712,8 +1715,9 @@ void WipeTower2::toolchange_Unload( cleaning_box.ld.y() + m_depth_traversed + wipe_start_offset_after_ram(0.f, is_first_layer()) + m_perimeter_width)); } + // Sync even on Klipper: [change_filament_gcode] and Tn must not run while the unload is still queued. writer.resume_preview() - .flush_planner_queue(); + .append(flush_planner_queue_command(m_gcode_flavor)); } // Change the tool, set a speed override for soluble and flex materials. @@ -1836,7 +1840,7 @@ void WipeTower2::toolchange_Change( if (wait_for_temp_here && !wait_beside_tower) writer.set_extruder_temp(wait_for_temp, true, wait_for_temp_tag()); - writer.append("[deretraction_from_wipe_tower_generator]"); + writer.append("[deretraction_from_wipe_tower_generator]\n"); // The toolchange Tn command will be inserted later, only in case that the user does // not provide a custom toolchange gcode. diff --git a/tests/fff_print/test_wipe_tower.cpp b/tests/fff_print/test_wipe_tower.cpp index f1cb6733de..7811a2ffb1 100644 --- a/tests/fff_print/test_wipe_tower.cpp +++ b/tests/fff_print/test_wipe_tower.cpp @@ -198,6 +198,60 @@ static std::string slice_with_prime_tower(const DynamicPrintConfig &config) return gcode(print); } +// change_filament_gcode for the Klipper sections: a marker comment, so no slicer output changes. +static const char *const toolchange_mark = "; TOOLCHANGE_MARK\n"; + +// On Klipper the tower syncs motion only right before a temperature command and, with +// `sync_before_change`, once per toolchange at the end of the unload, so change_filament_gcode +// can't run while the unload is still queued. Any other M400 just stalls the toolhead. +static bool m400_only_where_needed(const std::string &gcode, bool sync_before_change) +{ + bool synced = false; // a bare M400 since the last change_filament_gcode + std::istringstream lines(gcode); + for (std::string line, prev; std::getline(lines, line); prev = line) { + if (prev == "M400" && line.rfind("M104", 0) != 0 && line.rfind("M109", 0) != 0) { + if (!sync_before_change || synced) + return false; + synced = true; + } + if (line.find("; TOOLCHANGE_MARK") != std::string::npos) { + if (sync_before_change && !synced) + return false; + synced = false; + } + } + return true; +} + +// The cooldown back to the base temperature after an interface wipe closes the toolchange, so a +// wipe tower region ends right after it. Returns how many there are, or 0 if any of them is synced. +static size_t unsynced_interface_cooldowns(const std::string &tower) +{ + const std::string next_region = ";" + GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Wipe_Tower_Start); + size_t cooldowns = 0; + for (size_t pos = tower.find("\nM104 "); pos != std::string::npos; pos = tower.find("\nM104 ", pos + 1)) { + const size_t eol = tower.find('\n', pos + 1); + if (eol == std::string::npos + || (tower.compare(eol + 1, std::string::npos, ";") != 0 && tower.compare(eol + 1, next_region.size(), next_region) != 0)) + continue; // not the last line of its region + if (pos >= 4 && tower.compare(pos - 4, 4, "M400") == 0) + return 0; + ++cooldowns; + } + return cooldowns; +} + +// The wipe tower regions of a slice under `config`. Post-processed tower moves start with "G1 " +// (two spaces); a bare "G1 Y" is a move glued to a placeholder line, which skipped +// post_process_wipe_tower_moves and kept tower-local coordinates. +static std::string sliced_wipe_tower(const DynamicPrintConfig &config) +{ + const std::string tower = wipe_tower_regions(slice_with_prime_tower(config)); + REQUIRE_FALSE(tower.empty()); + CHECK_THAT(tower, !Catch::Matchers::ContainsSubstring("\nG1 Y")); + return tower; +} + TEST_CASE("Tower consumption is classified separately for each printer kind", "[WipeTower][Regression]") { const auto [is_bbl, tower_type] = GENERATE(table({ @@ -233,14 +287,40 @@ TEST_CASE("Tower consumption is classified separately for each printer kind", "[ TEST_CASE("The wipe tower's toolchange planner flush follows the gcode flavor", "[WipeTower]") { - auto [flavor, expected, unexpected] = GENERATE(table({ - { "klipper", "M400", "G4 S0" }, - { "marlin", "G4 S0", "M400" } })); - DYNAMIC_SECTION(flavor) { - const std::string tower = wipe_tower_regions(slice_with_prime_tower(wipe_tower_toolchange_config(flavor))); - REQUIRE_FALSE(tower.empty()); - CHECK_THAT(tower, Catch::Matchers::ContainsSubstring(expected)); - CHECK_THAT(tower, !Catch::Matchers::ContainsSubstring(unexpected)); + SECTION("klipper") { + const std::string tower_type = GENERATE("type1", "type2"); + CAPTURE(tower_type); + DynamicPrintConfig config = wipe_tower_toolchange_config("klipper"); + config.set_deserialize_strict({ { "wipe_tower_type", tower_type }, { "change_filament_gcode", toolchange_mark } }); + const std::string tower = sliced_wipe_tower(config); + CHECK_THAT(tower, !Catch::Matchers::ContainsSubstring("G4 S0")); + // Only type2 syncs at the end of the unload; type1's toolchange_Unload is compiled out. + CHECK(m400_only_where_needed(tower, tower_type == std::string("type2"))); + } + SECTION("marlin") { + const std::string tower = sliced_wipe_tower(wipe_tower_toolchange_config("marlin")); + CHECK_THAT(tower, Catch::Matchers::ContainsSubstring("G4 S0")); + CHECK_THAT(tower, !Catch::Matchers::ContainsSubstring("M400")); + } + SECTION("klipper, single extruder multi material") { + // The SEMM path changes the temperature inside the tower; the sync before M104 must stay. + DynamicPrintConfig config = wipe_tower_toolchange_config("klipper"); + config.set_deserialize_strict({ { "single_extruder_multi_material", "1" }, { "change_filament_gcode", toolchange_mark } }); + const std::string tower = sliced_wipe_tower(config); + CHECK_THAT(tower, Catch::Matchers::ContainsSubstring("M400\nM104")); + CHECK(m400_only_where_needed(tower, true)); + } + SECTION("klipper, tower interface features") { + // The interface M109 is stripped from the tower by GCode.cpp; its sync line must go with it. + // The cooldown back to the base temperature after the wipe isn't synced either; other M104s + // (the single extruder multi material path of this config) still are. + DynamicPrintConfig config = wipe_tower_toolchange_config("klipper"); + config.set_deserialize_strict({ { "enable_tower_interface_features", "1" }, + { "nozzle_temperature_range_high", "250,250" }, + { "change_filament_gcode", toolchange_mark } }); + const std::string tower = sliced_wipe_tower(config); + CHECK(m400_only_where_needed(tower, true)); + CHECK(unsynced_interface_cooldowns(tower) > 0); } }