From bbffe8f4dfce601873c9cefd2735f0d6915598d4 Mon Sep 17 00:00:00 2001 From: Gazi Faysal Jubayer Date: Wed, 28 Oct 2026 20:57:43 +0600 Subject: [PATCH] Use the assigned filament's settings in the PA Line calibration test (#16882) The PA Line test took its speed limit, line width and extrusion from filament 0, whatever filament the test was assigned to. On any other AMS slot or filament, the test printed with the wrong speeds and the wrong extrusion (#16530). The test now uses the filament the G-code generator has selected for it: - the fast lines are capped by that filament's max volumetric speed; - the line width uses the nozzle diameter of that filament's extruder; - the lines and the number box use Extruder::e_per_mm() times print_flow_ratio, like a regular print. With one filament whose flow ratio is not 1, the PA lines now use that flow ratio too. PA Pattern output is unchanged. Adds a regression test that checks the PA line feedrate and extrusion for a test assigned to the second of two different filaments. (cherry picked from commit 4b68224ff4544524a268e2852c07bf62a84b5c1d) --- src/libslic3r/GCode.cpp | 5 +- src/libslic3r/calib.cpp | 58 +++++++------ src/libslic3r/calib.hpp | 10 ++- tests/fff_print/CMakeLists.txt | 1 + tests/fff_print/test_calib.cpp | 149 +++++++++++++++++++++++++++++++++ 5 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 tests/fff_print/test_calib.cpp diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 7a51938e56..a12237d7c3 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -4581,7 +4581,10 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato CalibPressureAdvanceLine pa_test(this); - auto fast_speed = CalibPressureAdvance::find_optimal_PA_speed(print.full_print_config(), pa_test.line_width(), pa_test.height_layer()); + // Cap the speed by the volumetric speed of the filament the test prints with, selected above. + const Extruder *pa_filament = m_writer.filament(); + auto fast_speed = CalibPressureAdvance::find_optimal_PA_speed(print.full_print_config(), pa_test.line_width(), pa_test.height_layer(), + int(pa_filament->extruder_id()), pa_filament->config_index()); auto slow_speed = std::max(10.0, fast_speed / 10.0); if (fast_speed < slow_speed + 5) fast_speed = slow_speed + 5; diff --git a/src/libslic3r/calib.cpp b/src/libslic3r/calib.cpp index 3217edf1da..e800258ccf 100644 --- a/src/libslic3r/calib.cpp +++ b/src/libslic3r/calib.cpp @@ -2,6 +2,7 @@ #include "GCode/BeltKinematics.hpp" #include "BoundingBox.hpp" #include "Config.hpp" +#include "Extruder.hpp" #include "Flow.hpp" #include "GCodeWriter.hpp" #include "GCode/GCodeProcessor.hpp" @@ -267,12 +268,13 @@ double CalibPressureAdvance::get_distance(Vec2d from, Vec2d to) const } std::string CalibPressureAdvance::draw_line( - GCodeWriter &writer, Vec2d to_pt, double line_width, double layer_height, double speed, const std::string &comment) + GCodeWriter &writer, Vec2d to_pt, double line_width, double layer_height, double speed, const std::string &comment, double e_per_mm) { - const double e_per_mm = CalibPressureAdvance::e_per_mm(line_width, layer_height, - m_config.option("nozzle_diameter")->get_at(0), - m_config.option("filament_diameter")->get_at(0), - m_config.option("filament_flow_ratio")->get_at(0)); + if (e_per_mm <= 0.) + e_per_mm = CalibPressureAdvance::e_per_mm(line_width, layer_height, + m_config.option("nozzle_diameter")->get_at(0), + m_config.option("filament_diameter")->get_at(0), + m_config.option("filament_flow_ratio")->get_at(0)); const double length = get_distance(Vec2d(m_last_pos.x(), m_last_pos.y()), to_pt); auto dE = e_per_mm * length; @@ -323,19 +325,19 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do y += size_y - i * spacing * 2; comment = "Draw perimeter (up)"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); x += size_x - i * spacing * 2; comment = "Draw perimeter (right)"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); y -= size_y - i * spacing * 2; comment = "Draw perimeter (down)"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); x -= size_x - i * spacing * 2; comment = "Draw perimeter (left)"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } if (!opt_args.is_filled) { @@ -372,7 +374,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do y += x - x_min_bound; x = x_min_bound; comment = "Fill: Print up/left"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } else { y += spacing_45; x = x_min_bound; @@ -381,7 +383,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x += y - y_min_bound; y = y_min_bound; comment = "Fill: Print down/right"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } } else if (i < std::max(x_count, y_count)) { if (x_count > y_count) { @@ -394,7 +396,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x -= y_max_bound - y_min_bound; y = y_max_bound; comment = "Fill: Print up/left"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } else { if (i == y_count) { x += spacing_45 - y_remainder; @@ -408,7 +410,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x += y_max_bound - y_min_bound; y = y_min_bound; comment = "Fill: Print down/right"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } } else { // box is taller than wide @@ -425,7 +427,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x = x_min_bound; y += x_max_bound - x_min_bound; comment = "Fill: Print up/left"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } else { x = x_min_bound; y += spacing_45; @@ -434,7 +436,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x = x_max_bound; y -= x_max_bound - x_min_bound; comment = "Fill: Print down/right"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } } } else { @@ -450,7 +452,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do x -= y_max_bound - y; y = y_max_bound; comment = "Fill: Print up/left"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } else { if (i == y_count) { x += spacing_45 - y_remainder; @@ -463,7 +465,7 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do y -= x_max_bound - x; x = x_max_bound; comment = "Fill: Print down/right"; - gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment); + gcode << draw_line(writer, Vec2d(x, y), line_arg_line_width, line_arg_height, line_arg_speed, comment, opt_args.e_per_mm); } } } @@ -471,8 +473,11 @@ std::string CalibPressureAdvance::draw_box(GCodeWriter &writer, double min_x, do return gcode.str(); } CalibPressureAdvanceLine::CalibPressureAdvanceLine(GCode* gcodegen) - : CalibPressureAdvance(gcodegen->config()), mp_gcodegen(gcodegen), m_nozzle_diameter(gcodegen->config().nozzle_diameter.get_at(0)) + : CalibPressureAdvance(gcodegen->config()), mp_gcodegen(gcodegen) { + // The test prints with the filament the G-code generator has selected, on that filament's extruder. + const Extruder *filament = gcodegen->writer().filament(); + m_nozzle_diameter = gcodegen->config().nozzle_diameter.get_at(filament ? filament->extruder_id() : 0); m_line_width = m_nozzle_diameter < 0.51 ? m_nozzle_diameter * 1.5 : m_nozzle_diameter * 1.05; m_height_layer = gcodegen->config().initial_layer_print_height; m_number_line_width = m_thin_line_width = m_nozzle_diameter; @@ -530,16 +535,16 @@ std::string CalibPressureAdvanceLine::print_pa_lines(double start_x, double star auto &writer = mp_gcodegen->writer(); const auto &config = mp_gcodegen->config(); - const auto filament_diameter = config.filament_diameter.get_at(0); - const auto print_flow_ratio = config.print_flow_ratio; const auto z_offset = config.z_offset; - const double e_per_mm = CalibPressureAdvance::e_per_mm(m_line_width, m_height_layer, m_nozzle_diameter, filament_diameter, - print_flow_ratio); - const double thin_e_per_mm = CalibPressureAdvance::e_per_mm(m_thin_line_width, m_height_layer, m_nozzle_diameter, filament_diameter, - print_flow_ratio); - const double number_e_per_mm = CalibPressureAdvance::e_per_mm(m_number_line_width, m_height_layer, m_nozzle_diameter, filament_diameter, - print_flow_ratio); + // Extrude like a regular print with the selected filament: its diameter and flow ratio, times the print flow ratio. + const Extruder *filament = writer.filament(); + const auto line_e_per_mm = [&](double line_width) { + return filament->e_per_mm(Flow(line_width, m_height_layer, m_nozzle_diameter).mm3_per_mm() * config.print_flow_ratio); + }; + const double e_per_mm = line_e_per_mm(m_line_width); + const double thin_e_per_mm = line_e_per_mm(m_thin_line_width); + const double number_e_per_mm = line_e_per_mm(m_number_line_width); const double fast = CalibPressureAdvance::speed_adjust(m_fast_speed); const double slow = CalibPressureAdvance::speed_adjust(m_slow_speed); @@ -590,6 +595,7 @@ std::string CalibPressureAdvanceLine::print_pa_lines(double start_x, double star DrawBoxOptArgs default_box_opt_args(2, m_height_layer, m_line_width, fast); //Draw box default_box_opt_args.is_filled = true; + default_box_opt_args.e_per_mm = e_per_mm; // same line width and height as the PA lines gcode << ";" << GCodeProcessor::reserved_tag(GCodeProcessor::ETags::Role) << "Bottom surface\n"; gcode << draw_box(writer, box_start_x, start_y - m_space_y, number_spacing() * 8, (num + 1) * m_space_y, default_box_opt_args); diff --git a/src/libslic3r/calib.hpp b/src/libslic3r/calib.hpp index 4ef676cdb1..6a63a243b9 100644 --- a/src/libslic3r/calib.hpp +++ b/src/libslic3r/calib.hpp @@ -198,6 +198,7 @@ struct DrawBoxOptArgs double height; double line_width; double speed; + double e_per_mm{0.}; // 0: extrude with the first filament's settings }; class CalibPressureAdvance { @@ -236,8 +237,13 @@ protected: double speed, GCodeWriter &writer); - std::string draw_line( - GCodeWriter &writer, Vec2d to_pt, double line_width, double layer_height, double speed, const std::string &comment = std::string()); + std::string draw_line(GCodeWriter &writer, + Vec2d to_pt, + double line_width, + double layer_height, + double speed, + const std::string &comment = std::string(), + double e_per_mm = 0.); std::string draw_box(GCodeWriter &writer, double min_x, double min_y, double size_x, double size_y, DrawBoxOptArgs opt_args); double to_radians(double degrees) const { return degrees * M_PI / 180; }; diff --git a/tests/fff_print/CMakeLists.txt b/tests/fff_print/CMakeLists.txt index feaba1210b..6046339fc9 100644 --- a/tests/fff_print/CMakeLists.txt +++ b/tests/fff_print/CMakeLists.txt @@ -4,6 +4,7 @@ add_executable(${_TEST_NAME}_tests test_helpers.cpp test_helpers.hpp test_adaptive_pressure_advance.cpp + test_calib.cpp test_cooling.cpp test_extrusion_entity.cpp test_extrusion_processor.cpp diff --git a/tests/fff_print/test_calib.cpp b/tests/fff_print/test_calib.cpp new file mode 100644 index 0000000000..b365f0f0c4 --- /dev/null +++ b/tests/fff_print/test_calib.cpp @@ -0,0 +1,149 @@ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "libslic3r/Config.hpp" +#include "libslic3r/Flow.hpp" +#include "libslic3r/Model.hpp" +#include "libslic3r/Print.hpp" +#include "libslic3r/PrintConfig.hpp" +#include "libslic3r/TriangleMesh.hpp" +#include "libslic3r/calib.hpp" +#include "libslic3r/libslic3r.h" +#include "test_helpers.hpp" + +using namespace Slic3r; + +namespace { + +struct Move +{ + double feedrate; // mm/min + double e_per_mm; +}; + +// Extruding moves under a ";TYPE:" tag: only the first one after each tag, or all of them. The PA line test prints +// no object, so its fast segments are the first move after each ";TYPE:Outer wall" and the box under its numbers is +// all of ";TYPE:Bottom surface". +std::vector extrusions_of_type(const std::string &gcode, const std::string &type, bool first_only) +{ + const std::string tag = ";TYPE:" + type; + std::vector out; + std::istringstream lines(gcode); + std::string line; + bool relative = false; + bool armed = false; + double x = 0., y = 0., e = 0., f = 0.; + while (std::getline(lines, line)) { + if (line.rfind(";TYPE:", 0) == 0) { + armed = line == tag; + continue; + } + if (line.rfind("M83", 0) == 0) + relative = true; + else if (line.rfind("M82", 0) == 0) + relative = false; + const bool is_move = line.rfind("G1 ", 0) == 0 || line.rfind("G0 ", 0) == 0; + if (!is_move && line.rfind("G92 ", 0) != 0) + continue; + + std::istringstream words(line.substr(0, line.find(';'))); + std::string word; + words >> word; // the command + double nx = x, ny = y, ev = 0.; + bool has_e = false; + while (words >> word) { + const double v = std::stod(word.substr(1)); + switch (word[0]) { + case 'X': nx = v; break; + case 'Y': ny = v; break; + case 'E': ev = v; has_e = true; break; + case 'F': f = v; break; + default: break; + } + } + if (!is_move) { // G92 + if (has_e) + e = ev; + continue; + } + const double length = std::hypot(nx - x, ny - y); + if (has_e && armed && length > 0.) { + out.push_back({f, (relative ? ev : ev - e) / length}); + armed = !first_only; + } + if (has_e) + e = relative ? e + ev : ev; + x = nx; + y = ny; + } + return out; +} + +} // namespace + +TEST_CASE("The PA line test prints with the speed and extrusion of the filament assigned to it", "[Calib]") +{ + // Filament 1: 1.75 mm, flow ratio 1, up to 21 mm3/s. Filament 2: 2.85 mm, flow ratio 0.9, up to 4 mm3/s, so its + // volumetric limit caps the fast lines well below filament 1's. + const int filament = GENERATE(1, 2); + const bool print_numbers = GENERATE(false, true); + CAPTURE(filament, print_numbers); + DynamicPrintConfig config = Test::multifilament_config(2, { + {"filament_self_index", "1,2"}, + {"filament_extruder_variant", "Direct Drive Standard;Direct Drive Standard"}, + {"filament_diameter", "1.75,2.85"}, + {"filament_flow_ratio", "1,0.9"}, + {"filament_max_volumetric_speed", "21,4"}, + {"print_flow_ratio", "1"}, + {"outer_wall_speed", "200"}, + {"initial_layer_print_height", "0.2"}, + {"enable_prime_tower", 0}, + {"skirt_loops", 0}, + {"brim_type", "no_brim"}, + }); + Print print; + Model model; + const std::vector> overrides{{{"extruder", std::to_string(filament)}}}; + Test::init_print({Test::cube(20)}, print, model, config, &overrides); + Calib_Params params; + params.mode = CalibMode::Calib_PA_Line; + params.start = 0.; + params.end = 0.02; + params.step = 0.002; + params.print_numbers = print_numbers; + print.set_calib_params(params); + const std::string gcode = Test::gcode(print); + const std::vector segments = extrusions_of_type(gcode, "Outer wall", true); + REQUIRE_FALSE(segments.empty()); + // The box under the numbers is drawn with the same line width and height as the PA lines. + const std::vector box = extrusions_of_type(gcode, "Bottom surface", false); + CHECK(box.empty() != print_numbers); + + // The test prints 1.5 nozzle-wide lines on the 0.2 mm first layer (CalibPressureAdvanceLine). + const size_t idx = size_t(filament - 1); + const double mm3_per_mm = Flow(0.4 * 1.5, 0.2, 0.4).mm3_per_mm(); + const double max_vol = config.option("filament_max_volumetric_speed")->get_at(idx); + const double diameter = config.option("filament_diameter")->get_at(idx); + const double flow_ratio = config.option("filament_flow_ratio")->get_at(idx); + // Fast lines run at the outer wall speed (at least 100 mm/s), capped by the filament's volumetric speed. + const double fast_speed = std::floor(std::min(std::max(100., 200.), max_vol / mm3_per_mm)); + const double e_per_mm = mm3_per_mm * flow_ratio / (PI * diameter * diameter / 4.); + for (const Move &move : segments) { + CHECK_THAT(move.feedrate, Catch::Matchers::WithinAbs(fast_speed * 60., 1.)); + CHECK_THAT(move.e_per_mm, Catch::Matchers::WithinRel(e_per_mm, 0.005)); + } + for (const Move &move : box) + CHECK_THAT(move.e_per_mm, Catch::Matchers::WithinRel(e_per_mm, 0.005)); +}