From 3ff600bed07770d7afc5c59c0cf43061a831dd21 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Thu, 9 Mar 2023 09:14:54 +0100 Subject: [PATCH] Separated travel and print acceleration control --- src/libslic3r/GCode.cpp | 18 ++++++++++++---- src/libslic3r/GCodeWriter.cpp | 39 +++++++++++++++++------------------ src/libslic3r/GCodeWriter.hpp | 15 ++++++++++++-- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 2fca25c96..a9d7da76d 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -2547,7 +2547,7 @@ std::string GCode::extrude_loop(ExtrusionLoop loop, const std::string_view descr } // reset acceleration - gcode += m_writer.set_acceleration((unsigned int)(m_config.default_acceleration.value + 0.5)); + gcode += m_writer.set_print_acceleration((unsigned int)(m_config.default_acceleration.value + 0.5)); if (m_wipe.enable) { m_wipe.path = paths.front().polyline; @@ -2633,7 +2633,7 @@ std::string GCode::extrude_multi_path(ExtrusionMultiPath multipath, const std::s } } // reset acceleration - gcode += m_writer.set_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5)); + gcode += m_writer.set_print_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5)); return gcode; } @@ -2659,7 +2659,7 @@ std::string GCode::extrude_path(ExtrusionPath path, std::string_view description m_wipe.path.reverse(); } // reset acceleration - gcode += m_writer.set_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5)); + gcode += m_writer.set_print_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5)); return gcode; } @@ -2802,7 +2802,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string_view de } else { acceleration = m_config.default_acceleration.value; } - gcode += m_writer.set_acceleration((unsigned int)floor(acceleration + 0.5)); + gcode += m_writer.set_print_acceleration((unsigned int)floor(acceleration + 0.5)); } // calculate extrusion length per distance unit @@ -3071,8 +3071,18 @@ std::string GCode::travel_to(const Point &point, ExtrusionRole role, std::string // use G1 because we rely on paths being straight (G0 may make round paths) if (travel.size() >= 2) { + + gcode += m_writer.set_travel_acceleration((unsigned int)(m_config.travel_acceleration.value + 0.5)); + for (size_t i = 1; i < travel.size(); ++ i) gcode += m_writer.travel_to_xy(this->point_to_gcode(travel.points[i]), comment); + + if (! m_writer.supports_PT()) { + // In case that this flavor does not support separate print and travel acceleration, + // reset acceleration to default. + gcode += m_writer.set_travel_acceleration((unsigned int)(m_config.travel_acceleration.value + 0.5)); + } + this->set_last_pos(travel.points.back()); } return gcode; diff --git a/src/libslic3r/GCodeWriter.cpp b/src/libslic3r/GCodeWriter.cpp index c2caf5766..b323ae8b2 100644 --- a/src/libslic3r/GCodeWriter.cpp +++ b/src/libslic3r/GCodeWriter.cpp @@ -15,6 +15,11 @@ namespace Slic3r { +bool GCodeWriter::supports_PT() const +{ + return (FLAVOR_IS(gcfRepetier) || FLAVOR_IS(gcfMarlinFirmware) || FLAVOR_IS(gcfRepRapFirmware)); +} + void GCodeWriter::apply_print_config(const PrintConfig &print_config) { this->config.apply(print_config, true); @@ -154,36 +159,30 @@ std::string GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait return gcode.str(); } -std::string GCodeWriter::set_acceleration(unsigned int acceleration) +std::string GCodeWriter::set_acceleration_internal(Acceleration type, unsigned int acceleration) { // Clamp the acceleration to the allowed maximum. + // TODO: What about max travel acceleration ? Currently it is clamped by the extruding acceleration !!! if (m_max_acceleration > 0 && acceleration > m_max_acceleration) acceleration = m_max_acceleration; - if (acceleration == 0 || acceleration == m_last_acceleration) + // Are we setting travel acceleration for a flavour that supports separate travel and print acc? + bool separate_travel = (type == Acceleration::Travel && supports_PT()); + + auto& last_value = separate_travel ? m_last_travel_acceleration : m_last_acceleration ; + if (acceleration == 0 || acceleration == last_value) return std::string(); - m_last_acceleration = acceleration; + last_value = acceleration; std::ostringstream gcode; - if (FLAVOR_IS(gcfRepetier)) { - // M201: Set max printing acceleration - gcode << "M201 X" << acceleration << " Y" << acceleration; - if (this->config.gcode_comments) gcode << " ; adjust acceleration"; - gcode << "\n"; - // M202: Set max travel acceleration - gcode << "M202 X" << acceleration << " Y" << acceleration; - } else if (FLAVOR_IS(gcfRepRapFirmware)) { - // M204: Set default acceleration - gcode << "M204 P" << acceleration; - } else if (FLAVOR_IS(gcfMarlinFirmware)) { - // This is new MarlinFirmware with separated print/retraction/travel acceleration. - // Use M204 P, we don't want to override travel acc by M204 S (which is deprecated anyway). - gcode << "M204 P" << acceleration; - } else { - // M204: Set default acceleration + if (FLAVOR_IS(gcfRepetier)) + gcode << (separate_travel ? "M202 X" : "M201 X") << acceleration << " Y" << acceleration; + else if (FLAVOR_IS(gcfRepRapFirmware) || FLAVOR_IS(gcfMarlinFirmware)) + gcode << (separate_travel ? "M204 T" : "M204 P") << acceleration; + else gcode << "M204 S" << acceleration; - } + if (this->config.gcode_comments) gcode << " ; adjust acceleration"; gcode << "\n"; diff --git a/src/libslic3r/GCodeWriter.hpp b/src/libslic3r/GCodeWriter.hpp index 6c36c0d3a..2a2a488fa 100644 --- a/src/libslic3r/GCodeWriter.hpp +++ b/src/libslic3r/GCodeWriter.hpp @@ -43,7 +43,8 @@ public: std::string postamble() const; std::string set_temperature(unsigned int temperature, bool wait = false, int tool = -1) const; std::string set_bed_temperature(unsigned int temperature, bool wait = false); - std::string set_acceleration(unsigned int acceleration); + std::string set_print_acceleration(unsigned int acceleration) { return set_acceleration_internal(Acceleration::Print, acceleration); } + std::string set_travel_acceleration(unsigned int acceleration) { return set_acceleration_internal(Acceleration::Travel, acceleration); } std::string reset_e(bool force = false); std::string update_progress(unsigned int num, unsigned int tot, bool allow_100 = false) const; // return false if this extruder was already selected @@ -69,6 +70,9 @@ public: std::string unlift(); Vec3d get_position() const { return m_pos; } + // Returns whether this flavor supports separate print and travel acceleration. + bool supports_PT() const; + // To be called by the CoolingBuffer from another thread. static std::string set_fan(const GCodeFlavor gcode_flavor, bool gcode_comments, unsigned int speed); // To be called by the main thread. It always emits the G-code, it does not remember the previous state. @@ -81,7 +85,8 @@ private: std::string m_extrusion_axis; bool m_single_extruder_multi_material; Extruder* m_extruder; - unsigned int m_last_acceleration; + unsigned int m_last_acceleration = (unsigned int)(-1); + unsigned int m_last_travel_acceleration = (unsigned int)(-1); // only used for flavors supporting separate print/travel acc // Limit for setting the acceleration, to respect the machine limits set for the Marlin firmware. // If set to zero, the limit is not in action. unsigned int m_max_acceleration; @@ -90,8 +95,14 @@ private: double m_lifted; Vec3d m_pos = Vec3d::Zero(); + enum class Acceleration { + Travel, + Print + }; + std::string _travel_to_z(double z, const std::string &comment); std::string _retract(double length, double restart_extra, const std::string &comment); + std::string set_acceleration_internal(Acceleration type, unsigned int acceleration); }; class GCodeFormatter {