From 126de4d648fd1964fb3fcc2303f09d7b207f6bec Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 26 Mar 2024 10:56:41 +0100 Subject: [PATCH] Chamber temperature G-code emission --- src/libslic3r/GCode.cpp | 30 +++++++++++++++++++++++++++-- src/libslic3r/GCode.hpp | 1 + src/libslic3r/GCode/GCodeWriter.cpp | 21 ++++++++++++++++++++ src/libslic3r/GCode/GCodeWriter.hpp | 1 + 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 6a7f825815..ed7bd86c64 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1211,9 +1211,9 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail DoExport::init_ooze_prevention(print, m_ooze_prevention); std::string start_gcode = this->placeholder_parser_process("start_gcode", print.config().start_gcode.value, initial_extruder_id); - // Set bed temperature if the start G-code does not contain any bed temp control G-codes. + + this->_print_first_layer_chamber_temperature(file, print, start_gcode, initial_extruder_id, false); this->_print_first_layer_bed_temperature(file, print, start_gcode, initial_extruder_id, true); - // Set extruder(s) temperature before and after start G-code. this->_print_first_layer_extruder_temperatures(file, print, start_gcode, initial_extruder_id, false); // adds tag for processor @@ -1223,6 +1223,7 @@ void GCodeGenerator::_do_export(Print& print, GCodeOutputStream &file, Thumbnail file.writeln(start_gcode); this->_print_first_layer_extruder_temperatures(file, print, start_gcode, initial_extruder_id, true); + this->_print_first_layer_chamber_temperature(file, print, start_gcode, initial_extruder_id, true); print.throw_if_canceled(); // Set other general things. @@ -1889,6 +1890,31 @@ void GCodeGenerator::_print_first_layer_bed_temperature(GCodeOutputStream &file, file.write(set_temp_gcode); } + + +// Write chamber temperatures into the G-code. +// Only do that if the start G-code does not already contain any M-code controlling chamber temperature. +// M141 - Set chamber Temperature +// M191 - Set chamber Temperature and Wait +void GCodeGenerator::_print_first_layer_chamber_temperature(GCodeOutputStream &file, const Print &print, const std::string &gcode, unsigned int first_printing_extruder_id, bool wait) +{ + bool autoemit = print.config().autoemit_temperature_commands; + // Initial bed temperature based on the first extruder. + int temp = print.config().chamber_temperature.get_at(first_printing_extruder_id); + // Is the bed temperature set by the provided custom G-code? + int temp_by_gcode = -1; + bool temp_set_by_gcode = custom_gcode_sets_temperature(gcode, 141, 191, false, temp_by_gcode); + if (autoemit && temp_set_by_gcode && temp_by_gcode >= 0 && temp_by_gcode < 1000) + temp = temp_by_gcode; + // Always call m_writer.set_bed_temperature() so it will set the internal "current" state of the bed temp as if + // the custom start G-code emited these. + std::string set_temp_gcode = m_writer.set_chamber_temperature(temp, wait); + if (autoemit && ! temp_set_by_gcode) + file.write(set_temp_gcode); +} + + + // Write 1st layer extruder temperatures into the G-code. // Only do that if the start G-code does not already contain any M-code controlling an extruder temperature. // M104 - Set Extruder Temperature diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 3aa59fb286..7f5020af1e 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -468,6 +468,7 @@ private: std::string _extrude( const ExtrusionAttributes &attribs, const Geometry::ArcWelder::Path &path, const std::string_view description, double speed = -1); void print_machine_envelope(GCodeOutputStream &file, const Print &print); + void _print_first_layer_chamber_temperature(GCodeOutputStream &file, const Print &print, const std::string &gcode, unsigned int first_printing_extruder_id, bool wait); void _print_first_layer_bed_temperature(GCodeOutputStream &file, const Print &print, const std::string &gcode, unsigned int first_printing_extruder_id, bool wait); void _print_first_layer_extruder_temperatures(GCodeOutputStream &file, const Print &print, const std::string &gcode, unsigned int first_printing_extruder_id, bool wait); // On the first printing layer. This flag triggers first layer speeds. diff --git a/src/libslic3r/GCode/GCodeWriter.cpp b/src/libslic3r/GCode/GCodeWriter.cpp index 2d3b3f798e..95166c143a 100644 --- a/src/libslic3r/GCode/GCodeWriter.cpp +++ b/src/libslic3r/GCode/GCodeWriter.cpp @@ -181,6 +181,27 @@ std::string GCodeWriter::set_bed_temperature(unsigned int temperature, bool wait return gcode.str(); } + + +std::string GCodeWriter::set_chamber_temperature(unsigned int temperature, bool wait) const +{ + std::string_view code, comment; + if (wait) { + code = "M191"sv; + comment = "set chamber temperature and wait for it to be reached"sv; + } else { + code = "M141"sv; + comment = "set chamber temperature"sv; + } + + std::ostringstream gcode; + gcode << code << " S" << temperature << " ; " << comment << "\n"; + + return gcode.str(); +} + + + std::string GCodeWriter::set_acceleration_internal(Acceleration type, unsigned int acceleration) { // Clamp the acceleration to the allowed maximum. diff --git a/src/libslic3r/GCode/GCodeWriter.hpp b/src/libslic3r/GCode/GCodeWriter.hpp index e0b18c4f8e..dcb2e00ea9 100644 --- a/src/libslic3r/GCode/GCodeWriter.hpp +++ b/src/libslic3r/GCode/GCodeWriter.hpp @@ -52,6 +52,7 @@ 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_chamber_temperature(unsigned int temperature, bool wait) const; 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);