Skip to content

Commit

Permalink
Fix handling of not-gcode commands (like klipper ones) by pressure eq…
Browse files Browse the repository at this point in the history
…ualizer

also better handling of ACTIVATE_EXTRUDER by fan mover & pressure equalizer.
#4112
  • Loading branch information
supermerill committed Feb 12, 2024
1 parent e51e71b commit f9a41ac
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 12 deletions.
47 changes: 44 additions & 3 deletions src/libslic3r/GCode/FanMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void FanMover::_remove_slow_fan(int16_t min_speed, float past_sec) {
}

std::string FanMover::_set_fan(int16_t speed) {
const Tool* tool = m_writer.get_tool(m_currrent_extruder < 20 ? m_currrent_extruder : 0);
const Tool* tool = m_writer.get_tool(m_current_extruder < 20 ? m_current_extruder : 0);
std::string str = GCodeWriter::set_fan(m_writer.config.gcode_flavor.value, m_writer.config.gcode_comments.value, speed, tool ? tool->fan_offset() : 0, m_writer.config.fan_percentage.value);
if(!str.empty() && str.back() == '\n')
return str.substr(0,str.size()-1);
Expand Down Expand Up @@ -248,13 +248,51 @@ void FanMover::_process_T(const std::string_view command)

// T-1 is a valid gcode line for RepRap Firmwares (used to deselects all tools) see https://github.com/prusa3d/PrusaSlicer/issues/5677
if ((flavor != gcfRepRap && flavor != gcfSprinter) || eid != -1)
m_currrent_extruder = static_cast<uint16_t>(0);
m_current_extruder = static_cast<uint16_t>(0);
} else {
m_currrent_extruder = static_cast<uint16_t>(eid);
m_current_extruder = static_cast<uint16_t>(eid);
}
}
}


void FanMover::_process_ACTIVATE_EXTRUDER(const std::string_view cmd)
{
if (size_t cmd_end = cmd.find("ACTIVATE_EXTRUDER"); cmd_end != std::string::npos) {
bool error = false;
size_t extruder_pos_start = cmd.find("EXTRUDER", cmd_end + std::string_view("ACTIVATE_EXTRUDER").size()) + std::string_view("EXTRUDER").size();
assert(cmd[extruder_pos_start - 1] == 'R');
if (extruder_pos_start != std::string::npos) {
//remove next char until '-' or [0-9]
while (extruder_pos_start < cmd.size() && (cmd[extruder_pos_start] == ' ' || cmd[extruder_pos_start] == '=' || cmd[extruder_pos_start] == '\t'))
++extruder_pos_start;
size_t extruder_pos_end = extruder_pos_start + 1;
while (extruder_pos_end < cmd.size() && cmd[extruder_pos_end] != ' ' && cmd[extruder_pos_end] != '\t' && cmd[extruder_pos_end] != '\r' && cmd[extruder_pos_end] != '\n')
++extruder_pos_end;
std::string_view extruder_name = cmd.substr(extruder_pos_start, extruder_pos_end-extruder_pos_start);
// we have a "name". It may be whatever or "extruder" + X
for (const Extruder &extruder : m_writer.extruders()) {
if (m_writer.config.tool_name.values[extruder.id()] == extruder_name) {
m_current_extruder = static_cast<uint16_t>(extruder.id());
return;
}
}
std::string extruder_str("extruder");
if (extruder_str == extruder_name) {
m_current_extruder = static_cast<uint16_t>(0);
return;
}
for (const Extruder &extruder : m_writer.extruders()) {
if (extruder_str + std::to_string(extruder.id()) == extruder_name) {
m_current_extruder = static_cast<uint16_t>(extruder.id());
return;
}
}
}
BOOST_LOG_TRIVIAL(error) << "invalid ACTIVATE_EXTRUDER gcode command: '" << cmd << "', ignored by the fam mover post-process.";
}
}

void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line)
{
// processes 'normal' gcode lines
Expand All @@ -266,6 +304,9 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
if (line.has_f())
m_current_speed = line.f() / 60.0f;
switch (::toupper(cmd[0])) {
case 'A':
_process_ACTIVATE_EXTRUDER(line.raw());
break;
case 'T':
case 't':
_process_T(cmd);
Expand Down
3 changes: 2 additions & 1 deletion src/libslic3r/GCode/FanMover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class FanMover
// in unit/second
double m_current_speed = 1000 / 60.0;
bool m_is_custom_gcode = false;
uint16_t m_currrent_extruder = 0;
uint16_t m_current_extruder = 0;

// variable for when you add a line (front of the buffer)
int m_front_buffer_fan_speed = 1;
Expand Down Expand Up @@ -86,6 +86,7 @@ class FanMover
}
// Processes the given gcode line
void _process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line);
void _process_ACTIVATE_EXTRUDER(const std::string_view command);
void _process_T(const std::string_view command);
void _put_in_middle_G1(std::list<BufferData>::iterator item_to_split, float nb_sec, BufferData&& line_to_write);
void _print_in_middle_G1(BufferData& line_to_split, float nb_sec, const std::string& line_to_write);
Expand Down
72 changes: 64 additions & 8 deletions src/libslic3r/GCode/PressureEqualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ PressureEqualizer::PressureEqualizer(const Slic3r::GCodeConfig &config) : m_use_
m_filament_crossections.push_back(float(a));
}

m_extruder_names.clear();
for (const std::string & str: config.tool_name.values) {
m_extruder_names.push_back(str);
}

// Volumetric rate of a 0.45mm x 0.2mm extrusion at 60mm/s XY movement: 0.45*0.2*60*60=5.4*60 = 324 mm^3/min
// Volumetric rate of a 0.45mm x 0.2mm extrusion at 20mm/s XY movement: 0.45*0.2*20*60=1.8*60 = 108 mm^3/min
// Slope of the volumetric rate, changing from 20mm/s to 60mm/s over 2 seconds: (5.4-1.8)*60*60/2=60*60*1.8 = 6480 mm^3/min^2 = 1.8 mm^3/s^2
Expand Down Expand Up @@ -235,7 +240,7 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
gcode = parse_int(line);
} catch (Slic3r::InvalidArgument &) {
// Ignore invalid GCodes.
eatws(line);
eatws(line);
break;
}

Expand Down Expand Up @@ -368,16 +373,26 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
// Ignore the rest of the M-codes.
break;
}
case 'A': {
parse_activate_extruder(str_line);
break;
}
case 'T':
{
// Activate an extruder head.
int new_extruder = parse_int(line);
if (new_extruder != int(m_current_extruder)) {
m_current_extruder = new_extruder;
m_retracted = true;
buf.type = GCODELINETYPE_TOOL_CHANGE;
} else {
buf.type = GCODELINETYPE_NOOP;
try {
int new_extruder = parse_int(line);
if (new_extruder != int(m_current_extruder)) {
m_current_extruder = new_extruder;
//m_retracted = true; // why?
buf.type = GCODELINETYPE_TOOL_CHANGE;
} else {
buf.type = GCODELINETYPE_NOOP;
}
} catch (Slic3r::InvalidArgument &) {
// Ignore invalid GCodes.
eatws(line);
break;
}
break;
}
Expand All @@ -393,6 +408,47 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
return true;
}

void PressureEqualizer::parse_activate_extruder(const std::string &line_str)
{
if (size_t cmd_end = line_str.find("CTIVATE_EXTRUDER"); cmd_end != std::string::npos) {
bool error = false;
size_t extruder_pos_start = line_str.find("EXTRUDER", cmd_end + std::string_view("CTIVATE_EXTRUDER").size()) +
std::string_view("EXTRUDER").size();
assert(line_str[extruder_pos_start - 1] == 'R');
if (extruder_pos_start != std::string::npos) {
// remove next char until '-' or [0-9]
while (extruder_pos_start < line_str.size() &&
(line_str[extruder_pos_start] == ' ' || line_str[extruder_pos_start] == '=' ||
line_str[extruder_pos_start] == '\t'))
++extruder_pos_start;
size_t extruder_pos_end = extruder_pos_start + 1;
while (extruder_pos_end < line_str.size() && line_str[extruder_pos_end] != ' ' &&
line_str[extruder_pos_end] != '\t' && line_str[extruder_pos_end] != '\r' &&
line_str[extruder_pos_end] != '\n')
++extruder_pos_end;
std::string extruder_name = line_str.substr(extruder_pos_start, extruder_pos_end - extruder_pos_start);
// we have a "name". It may be whatever or "extruder" + X
for (size_t extruder_idx = 0; extruder_idx < m_extruder_names.size(); ++extruder_idx) {
if (m_extruder_names[extruder_idx] == extruder_name) {
m_current_extruder = extruder_idx;
return;
}
}
std::string extruder_str("extruder");
if (extruder_str == extruder_name) {
m_current_extruder = 0;
return;
}
for (size_t extruder_idx = 0; extruder_idx < m_extruder_names.size(); ++extruder_idx) {
if (extruder_str + std::to_string(extruder_idx) == extruder_name) {
m_current_extruder = extruder_idx;
return;
}
}
}
}
}

void PressureEqualizer::output_gcode_line(const size_t line_idx)
{
GCodeLine &line = m_gcode_lines[line_idx];
Expand Down

0 comments on commit f9a41ac

Please sign in to comment.