Skip to content

Commit

Permalink
Fix fan_mover/gcodeprocessor with G2/G3
Browse files Browse the repository at this point in the history
via new test case
#4061
  • Loading branch information
supermerill committed Jan 29, 2024
1 parent b21bb9d commit f1a91d9
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 22 deletions.
64 changes: 43 additions & 21 deletions src/libslic3r/GCode/FanMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const std::string& FanMover::process_gcode(const std::string& gcode, bool flush)

if (flush) {
while (!m_buffer.empty()) {
m_process_output += m_buffer.front().raw + "\n";
remove_from_buffer(m_buffer.begin());
write_buffer_data();
}
}

Expand Down Expand Up @@ -282,6 +281,15 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
dist = std::sqrt(dist);
time = dist / m_current_speed;
}
} else if (::atoi(&cmd[1]) == 2 || ::atoi(&cmd[1]) == 3) {
// TODO: compute real dist
double distx = line.dist_X(reader);
double disty = line.dist_Y(reader);
double dist = distx * distx + disty * disty;
if (dist > 0) {
dist = std::sqrt(dist);
time = dist / m_current_speed;
}
}
break;
}
Expand Down Expand Up @@ -433,11 +441,18 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
}
if (line.has(Axis::E)) {
new_data.e = reader.e();
if (relative_e)
if (relative_e) {
assert(new_data.e == 0);
new_data.de = line.e();
else
} else
new_data.de = line.dist_E(reader);
}
assert(new_data.dx == 0 || reader.x() == new_data.x);
assert(new_data.dx == 0 || reader.x() + new_data.dx == line.x());
assert(new_data.dy == 0 ||reader.y() == new_data.y);
assert(new_data.dy == 0 || reader.y() + new_data.dy == line.y());
assert(new_data.de == 0 || reader.e() == new_data.e);
assert(new_data.de == 0 || reader.e() + new_data.de == line.e());

if (m_current_kickstart.time > 0 && time > 0) {
m_current_kickstart.time -= time;
Expand Down Expand Up @@ -472,23 +487,7 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
//if buffer too big, flush it.
if (time >= 0) {
while (!m_buffer.empty() && (need_flush || m_buffer_time_size - m_buffer.front().time > nb_seconds_delay - EPSILON) ){
BufferData& frontdata = m_buffer.front();
if (frontdata.fan_speed < 0 || frontdata.fan_speed != m_front_buffer_fan_speed || frontdata.is_kickstart) {
if (frontdata.is_kickstart && frontdata.fan_speed < m_front_buffer_fan_speed) {
//you have to slow down! not kickstart! rewrite the fan speed.
m_process_output += _set_fan(frontdata.fan_speed);//m_writer.set_fan(frontdata.fan_speed,true); //FIXME extruder id (or use the gcode writer, but then you have to disable the multi-thread thing

m_front_buffer_fan_speed = frontdata.fan_speed;
} else {
m_process_output += frontdata.raw + "\n";
if (frontdata.fan_speed >= 0) {
//note that this is the only place where the fan_speed is set and we print from the buffer, as if the fan_speed >= 0 => time == 0
//and as this flush all time == 0 lines from the back of the queue...
m_front_buffer_fan_speed = frontdata.fan_speed;
}
}
}
remove_from_buffer(m_buffer.begin());
write_buffer_data();
}
}
#if _DEBUG
Expand All @@ -498,5 +497,28 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
#endif
}

void FanMover::write_buffer_data()
{
BufferData &frontdata = m_buffer.front();
if (frontdata.fan_speed < 0 || frontdata.fan_speed != m_front_buffer_fan_speed || frontdata.is_kickstart) {
if (frontdata.is_kickstart && frontdata.fan_speed < m_front_buffer_fan_speed) {
// you have to slow down! not kickstart! rewrite the fan speed.
m_process_output += _set_fan(
frontdata.fan_speed); // m_writer.set_fan(frontdata.fan_speed,true); //FIXME extruder id (or use the
// gcode writer, but then you have to disable the multi-thread thing

m_front_buffer_fan_speed = frontdata.fan_speed;
} else {
m_process_output += frontdata.raw + "\n";
if (frontdata.fan_speed >= 0) {
// note that this is the only place where the fan_speed is set and we print from the buffer, as if the
// fan_speed >= 0 => time == 0 and as this flush all time == 0 lines from the back of the queue...
m_front_buffer_fan_speed = frontdata.fan_speed;
}
}
}
remove_from_buffer(m_buffer.begin());
}

} // namespace Slic3r

5 changes: 5 additions & 0 deletions src/libslic3r/GCode/FanMover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ namespace Slic3r {

class BufferData {
public:
//raw string, contains end position
std::string raw;
// time to go from start to end
float time;
int16_t fan_speed;
bool is_kickstart;
// start position
float x = 0, y = 0, z = 0, e = 0;
// delta to go to end position
float dx = 0, dy = 0, dz = 0, de = 0;
BufferData(std::string line, float time = 0, int16_t fan_speed = 0, float is_kickstart = false) : raw(line), time(time), fan_speed(fan_speed), is_kickstart(is_kickstart){
//avoid double \n
Expand Down Expand Up @@ -86,6 +90,7 @@ class FanMover
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);
void _remove_slow_fan(int16_t min_speed, float past_sec);
void write_buffer_data();
std::string _set_fan(int16_t speed);
};

Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/GCodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void GCodeReader::update_coordinates(GCodeLine &gline, std::pair<const char*, co
PROFILE_FUNC();
if (*command.first == 'G') {
int cmd_len = int(command.second - command.first);
if ((cmd_len == 2 && (command.first[1] == '0' || command.first[1] == '1')) ||
if ((cmd_len == 2 && (command.first[1] == '0' || command.first[1] == '1' || command.first[1] == '2' || command.first[1] == '3')) ||
(cmd_len == 3 && command.first[1] == '9' && command.first[2] == '2')) {
for (size_t i = 0; i < NUM_AXES; ++ i)
if (gline.has(Axis(i)))
Expand Down
Loading

0 comments on commit f1a91d9

Please sign in to comment.