From b1488b5c0cc870396828e27eeb99507a57dbdc81 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:36:56 +0800 Subject: [PATCH] Fix Adaptive PA Speed When the Extrusion Inherits the Travel Feedrate (#16857) The cooling buffer drops a G1 F that repeats the feedrate the preceding travel, wipe or retraction already set, so an extrusion printed at the travel speed reaches the adaptive pressure advance processor without a feedrate line of its own. The processor only read standalone G1 F lines and fell back to the last one it had seen, often the first layer's, so the predicted pressure advance stayed at the first-layer value for the rest of the print. Track the feedrate every move with an F word sets, as the firmware does, and use it for an extrusion that no G1 F precedes. --- src/libslic3r/GCode/AdaptivePAProcessor.cpp | 29 ++++++++ src/libslic3r/GCode/AdaptivePAProcessor.hpp | 1 + tests/fff_print/CMakeLists.txt | 1 + .../test_adaptive_pressure_advance.cpp | 72 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 tests/fff_print/test_adaptive_pressure_advance.cpp diff --git a/src/libslic3r/GCode/AdaptivePAProcessor.cpp b/src/libslic3r/GCode/AdaptivePAProcessor.cpp index a5252e5f36..06894c111a 100644 --- a/src/libslic3r/GCode/AdaptivePAProcessor.cpp +++ b/src/libslic3r/GCode/AdaptivePAProcessor.cpp @@ -43,6 +43,7 @@ AdaptivePAProcessor::AdaptivePAProcessor(GCode &gcodegen) m_max_next_feedrate(0.0), m_next_feedrate(0.0), m_current_feedrate(0.0), + m_modal_feedrate(0.0), m_last_config_index(-1), m_pa_change_pattern(R"(; PA_CHANGE:T(\d+) MM3MM:([0-9]*\.[0-9]+) ACCEL:(\d+) BR:(\d+) RC:(\d+) OV:(\d+))"), m_g1_f_pattern(R"(G1 F([0-9]+))") @@ -65,6 +66,17 @@ AdaptivePAInterpolator* AdaptivePAProcessor::getInterpolator(unsigned int config return it->second.get(); } +// The feedrate, in mm/s, a G0/G1/G2/G3 line sets with its F word, or 0 when it has none. +static double feedrate_word(const std::string &line) +{ + if (line.size() < 3 || line[0] != 'G' || line[2] != ' ' || (line[1] != '0' && line[1] != '1' && line[1] != '2' && line[1] != '3')) + return 0.0; + const std::size_t pos = line.find(" F"); + if (pos == std::string::npos || pos + 2 >= line.size() || !std::isdigit(static_cast(line[pos + 2])) || line.find(';') < pos) + return 0.0; + return std::stod(line.substr(pos + 2)) / 60.0; +} + /** * @brief Processes a layer of G-code and applies adaptive pressure advance. * @@ -106,6 +118,10 @@ std::string AdaptivePAProcessor::process_layer(std::string &&gcode) { } } + // Every move with an F word sets the feedrate the firmware runs the following moves at, including travels and wipes. + if (const double feedrate = feedrate_word(line); feedrate > 0) + m_modal_feedrate = feedrate; + // Wipe end found, continue searching for current feed rate. if (line.find("WIPE_END") != std::string::npos) { wipe_command = false; @@ -143,6 +159,7 @@ std::string AdaptivePAProcessor::process_layer(std::string &&gcode) { std::streampos current_pos = stream.tellg(); std::string next_line; double temp_feed_rate = 0; + double modal_feedrate = m_modal_feedrate; bool extrude_move_found = false; int line_counter = 0; @@ -152,6 +169,11 @@ std::string AdaptivePAProcessor::process_layer(std::string &&gcode) { // until a feature change pattern or a wipe command is detected while (std::getline(stream, next_line)) { line_counter++; + // Up to the first extrusion, every move with an F word (a travel, a lift, the extrusion itself) sets the + // feedrate the extrusion inherits when no G1 F precedes it + if (!extrude_move_found) + if (const double feedrate = feedrate_word(next_line); feedrate > 0) + modal_feedrate = feedrate; // Found an extrude move, set extrude move found flag and move to the next line if ((!extrude_move_found) && next_line.find("G1 ") == 0 && next_line.find('X') != std::string::npos && @@ -159,6 +181,13 @@ std::string AdaptivePAProcessor::process_layer(std::string &&gcode) { next_line.find('E') != std::string::npos) { // Pattern matched, break the loop extrude_move_found = true; + // No G1 F preceded the extrusion: the cooling buffer drops a feedrate the preceding travel or wipe + // already set, so the extrusion runs at that feedrate rather than at the last G1 F seen. + if (temp_feed_rate == 0 && modal_feedrate > 0) { + temp_feed_rate = modal_feedrate; + m_next_feedrate = modal_feedrate; + m_current_feedrate = modal_feedrate; + } continue; } diff --git a/src/libslic3r/GCode/AdaptivePAProcessor.hpp b/src/libslic3r/GCode/AdaptivePAProcessor.hpp index f3aef6ce82..e3d9ea5bfb 100644 --- a/src/libslic3r/GCode/AdaptivePAProcessor.hpp +++ b/src/libslic3r/GCode/AdaptivePAProcessor.hpp @@ -77,6 +77,7 @@ private: double m_max_next_feedrate; ///< Maximum feed rate (speed) for the upcomming island. If no speed is found, the previous island speed is used. double m_next_feedrate; ///< First feed rate (speed) for the upcomming island. double m_current_feedrate; ///< Current, latest feedrate. + double m_modal_feedrate; ///< Feedrate the last move with an F word set, travels and wipes included: what an extrusion without its own G1 F runs at. int m_last_config_index; ///< Filament config index of the last PA_CHANGE tag. bool m_enabled{false}; ///< Whether any filament config index has both PA and adaptive PA on, the only ones that emit PA_CHANGE tags. diff --git a/tests/fff_print/CMakeLists.txt b/tests/fff_print/CMakeLists.txt index f32e62eb9e..feaba1210b 100644 --- a/tests/fff_print/CMakeLists.txt +++ b/tests/fff_print/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(${_TEST_NAME}_tests ${_TEST_NAME}_tests.cpp test_helpers.cpp test_helpers.hpp + test_adaptive_pressure_advance.cpp test_cooling.cpp test_extrusion_entity.cpp test_extrusion_processor.cpp diff --git a/tests/fff_print/test_adaptive_pressure_advance.cpp b/tests/fff_print/test_adaptive_pressure_advance.cpp new file mode 100644 index 0000000000..700e79de96 --- /dev/null +++ b/tests/fff_print/test_adaptive_pressure_advance.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "libslic3r/PrintConfig.hpp" +#include "test_helpers.hpp" + +using namespace Slic3r; +using namespace Slic3r::Test; + +// The speeds, in mm/s, the adaptive pressure advance comments report for the extrusions above the first layer. +static std::vector adaptive_pa_speeds_above_first_layer(const std::string &gcode) +{ + const std::string token = "; APA Speed Used: "; + std::vector speeds; + int layer = 0; + std::istringstream stream(gcode); + for (std::string line; std::getline(stream, line);) { + if (line.rfind(";LAYER_CHANGE", 0) == 0) + ++layer; + else if (layer > 1 && line.rfind(token, 0) == 0) + speeds.push_back(std::stod(line.substr(token.size()))); + } + return speeds; +} + +// The cooling buffer drops a G1 F that repeats the feedrate the preceding travel set, so the wall +// extrusion then inherits the travel feedrate without a G1 F line of its own. +TEST_CASE("Adaptive pressure advance reads the speed of an extrusion that inherits the travel feedrate", "[AdaptivePressureAdvance]") +{ + const int travel_speed = GENERATE(120, 150); + DYNAMIC_SECTION("travel speed " << travel_speed) { + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "gcode_flavor", "marlin2" }, + { "gcode_comments", 1 }, + { "enable_pressure_advance", "1" }, + { "adaptive_pressure_advance", "1" }, + { "adaptive_pressure_advance_model", "0.08,1,4000\n0.04,20,4000\n0.08,1,100000\n0.04,20,100000" }, + { "pressure_advance", "0.08" }, + { "wall_loops", 1 }, + { "sparse_infill_density", 0 }, + { "top_shell_layers", 0 }, + { "bottom_shell_layers", 0 }, + { "skirt_loops", 0 }, + { "brim_type", "no_brim" }, + { "initial_layer_speed", 30 }, + // the wall speed must not be capped by the volumetric limit + { "filament_max_volumetric_speed", "200" }, + { "outer_wall_speed", 150 }, + { "inner_wall_speed", 150 }, + { "travel_speed", travel_speed }, + { "initial_layer_travel_speed", travel_speed }, + { "slow_down_for_layer_cooling", "0" }, + { "slow_down_layer_time", "0" }, + { "enable_overhang_speed", "0" }, + { "wipe", "0" }, + { "retraction_length", "0" }, + }); + const std::string gcode = slice({ cube(20) }, config); + + const std::vector speeds = adaptive_pa_speeds_above_first_layer(gcode); + REQUIRE(!speeds.empty()); + // every wall above the first layer prints at the wall speed, whatever the travel speed + for (const double speed : speeds) + CHECK_THAT(speed, Catch::Matchers::WithinAbs(150.0, 0.01)); + } +}