Skip to content

Commit

Permalink
Jerk - per axis maximum limit (SoftFever#6252)
Browse files Browse the repository at this point in the history
* Jerk - per axis maximum limit

* Fixing Klipper and refactoring

* Remove unused variables
  • Loading branch information
vovodroid authored Aug 7, 2024
1 parent 98a243c commit a4cfc14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
39 changes: 27 additions & 12 deletions src/libslic3r/GCodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ void GCodeWriter::apply_print_config(const PrintConfig &print_config)
std::round((use_mach_limits && supports_separate_travel_acceleration(print_config.gcode_flavor.value)) ?
print_config.machine_max_acceleration_travel.values.front() :
0));
m_max_jerk = std::lrint(
use_mach_limits ? std::min(print_config.machine_max_jerk_x.values.front(), print_config.machine_max_jerk_y.values.front()) : 0);
if (use_mach_limits) {
m_max_jerk_x = std::lrint(print_config.machine_max_jerk_x.values.front());
m_max_jerk_y = std::lrint(print_config.machine_max_jerk_y.values.front());
};
m_max_jerk_z = print_config.machine_max_jerk_z.values.front();
m_max_jerk_e = print_config.machine_max_jerk_e.values.front();
}
Expand Down Expand Up @@ -230,20 +232,31 @@ std::string GCodeWriter::set_acceleration_internal(Acceleration type, unsigned i

std::string GCodeWriter::set_jerk_xy(double jerk)
{
// Clamp the jerk to the allowed maximum.
if (m_max_jerk > 0 && jerk > m_max_jerk)
jerk = m_max_jerk;

if (jerk < 0.01 || is_approx(jerk, m_last_jerk))
return std::string();

m_last_jerk = jerk;

std::ostringstream gcode;
if(FLAVOR_IS(gcfKlipper))
if (FLAVOR_IS(gcfKlipper)) {
// Clamp the jerk to the allowed maximum.
if (m_max_jerk_x > 0 && jerk > m_max_jerk_x)
jerk = m_max_jerk_x;
if (m_max_jerk_y > 0 && jerk > m_max_jerk_y)
jerk = m_max_jerk_y;

gcode << "SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=" << jerk;
else
gcode << "M205 X" << jerk << " Y" << jerk;
} else {
double jerk_x = jerk;
double jerk_y = jerk;
// Clamp the axis jerk to the allowed maximum.
if (m_max_jerk_x > 0 && jerk > m_max_jerk_x)
jerk_x = m_max_jerk_x;
if (m_max_jerk_y > 0 && jerk > m_max_jerk_y)
jerk_y = m_max_jerk_y;

gcode << "M205 X" << jerk_x << " Y" << jerk_y;
}

if (m_is_bbl_printers)
gcode << std::setprecision(2) << " Z" << m_max_jerk_z << " E" << m_max_jerk_e;
Expand Down Expand Up @@ -277,8 +290,10 @@ std::string GCodeWriter::set_accel_and_jerk(unsigned int acceleration, double je
is_empty = false;
}
// Clamp the jerk to the allowed maximum.
if (m_max_jerk > 0 && jerk > m_max_jerk)
jerk = m_max_jerk;
if (m_max_jerk_x > 0 && jerk > m_max_jerk_x)
jerk = m_max_jerk_x;
if (m_max_jerk_y > 0 && jerk > m_max_jerk_y)
jerk = m_max_jerk_y;

if (jerk > 0.01 && !is_approx(jerk, m_last_jerk)) {
gcode << " SQUARE_CORNER_VELOCITY=" << jerk;
Expand Down
5 changes: 3 additions & 2 deletions src/libslic3r/GCodeWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GCodeWriter {
multiple_extruders(false), m_extruder(nullptr),
m_single_extruder_multi_material(false),
m_last_acceleration(0), m_max_acceleration(0),m_last_travel_acceleration(0), m_max_travel_acceleration(0),
m_last_jerk(0), m_max_jerk(0),
m_last_jerk(0), m_max_jerk_x(0), m_max_jerk_y(0),
m_last_bed_temperature(0), m_last_bed_temperature_reached(true),
m_lifted(0),
m_to_lift(0),
Expand Down Expand Up @@ -130,7 +130,8 @@ class GCodeWriter {
// 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;
double m_max_jerk;
double m_max_jerk_x;
double m_max_jerk_y;
double m_last_jerk;
double m_max_jerk_z;
double m_max_jerk_e;
Expand Down

0 comments on commit a4cfc14

Please sign in to comment.