Skip to content

Commit

Permalink
Advanced role jerk control
Browse files Browse the repository at this point in the history
  • Loading branch information
vovodroid committed Jan 26, 2024
1 parent 2586f56 commit 171658a
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 22 deletions.
37 changes: 37 additions & 0 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2900,6 +2900,8 @@ std::string GCodeGenerator::extrude_loop(const ExtrusionLoop &loop_src, bool rev

// reset acceleration
gcode += m_writer.set_print_acceleration(fast_round_up<unsigned int>(m_config.default_acceleration.value));
//reset jerk
gcode += m_writer.set_jerk(fast_round_up<unsigned int>(m_config.default_jerk.value));

if (m_wipe.enabled()) {
// Wipe will hide the seam.
Expand Down Expand Up @@ -2947,6 +2949,8 @@ std::string GCodeGenerator::extrude_skirt(

// reset acceleration
gcode += m_writer.set_print_acceleration(fast_round_up<unsigned int>(m_config.default_acceleration.value));
// reset jerk
gcode += m_writer.set_jerk(fast_round_up<unsigned int>(m_config.default_jerk.value));

if (m_wipe.enabled())
// Wipe will hide the seam.
Expand All @@ -2972,6 +2976,8 @@ std::string GCodeGenerator::extrude_multi_path(const ExtrusionMultiPath &multipa
m_wipe.set_path(std::move(smooth_path), true);
// reset acceleration
gcode += m_writer.set_print_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5));
// reset jerk
gcode += m_writer.set_jerk(m_config.default_jerk.value);
return gcode;
}

Expand Down Expand Up @@ -3011,6 +3017,8 @@ std::string GCodeGenerator::extrude_path(const ExtrusionPath &path, bool reverse
m_wipe.set_path(std::move(smooth_path));
// reset acceleration
gcode += m_writer.set_print_acceleration((unsigned int)floor(m_config.default_acceleration.value + 0.5));
// reset jerk
gcode += m_writer.set_jerk(m_config.default_jerk.value);
return gcode;
}

Expand Down Expand Up @@ -3170,6 +3178,31 @@ std::string GCodeGenerator::_extrude(
gcode += m_writer.set_print_acceleration((unsigned int)floor(acceleration + 0.5));
}

// adjust jerk
if (m_config.default_jerk.value > 0) {
int jerk;
if (this->on_first_layer() && m_config.first_layer_jerk.value > 0) {
jerk = m_config.first_layer_jerk.value;
} else if (this->object_layer_over_raft() && m_config.first_layer_jerk_over_raft.value > 0) {
jerk = m_config.first_layer_jerk_over_raft.value;
} else if (m_config.bridge_jerk.value > 0 && path_attr.role.is_bridge()) {
jerk = m_config.bridge_jerk.value;
} else if (m_config.top_solid_infill_jerk > 0 && path_attr.role == ExtrusionRole::TopSolidInfill) {
jerk = m_config.top_solid_infill_jerk.value;
} else if (m_config.solid_infill_jerk > 0 && path_attr.role.is_solid_infill()) {
jerk = m_config.solid_infill_jerk.value;
} else if (m_config.infill_jerk.value > 0 && path_attr.role.is_infill()) {
jerk = m_config.infill_jerk.value;
} else if (m_config.external_perimeter_jerk > 0 && path_attr.role.is_external_perimeter()) {
jerk = m_config.external_perimeter_jerk.value;
} else if (m_config.perimeter_jerk.value > 0 && path_attr.role.is_perimeter()) {
jerk = m_config.perimeter_jerk.value;
} else {
jerk = m_config.default_jerk.value;
}
gcode += m_writer.set_jerk(jerk);
}

// calculate extrusion length per distance unit
double e_per_mm = m_writer.extruder()->e_per_mm3() * path_attr.mm3_per_mm;
if (m_writer.extrusion_axis().empty())
Expand Down Expand Up @@ -3607,6 +3640,8 @@ std::string GCodeGenerator::generate_travel_gcode(
// generate G-code for the travel move
// use G1 because we rely on paths being straight (G0 may make round paths)
gcode += this->m_writer.set_travel_acceleration(acceleration);
if (m_config.default_jerk > 0 && m_config.travel_jerk > 0)
gcode += this->m_writer.set_jerk(m_config.travel_jerk);

for (const Vec3crd& point : travel) {
gcode += this->m_writer.travel_to_xyz(to_3d(this->point_to_gcode(point.head<2>()), unscaled(point.z())), comment);
Expand All @@ -3619,6 +3654,8 @@ std::string GCodeGenerator::generate_travel_gcode(
gcode += this->m_writer.set_travel_acceleration(acceleration);
}

if (m_config.default_jerk > 0 && m_config.travel_jerk > 0)
gcode += this->m_writer.set_jerk(m_config.default_jerk);
return gcode;
}

Expand Down
29 changes: 29 additions & 0 deletions src/libslic3r/GCode/GCodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ void GCodeWriter::apply_print_config(const PrintConfig &print_config)
print_config.machine_max_acceleration_extruding.values.front() : 0));
m_max_travel_acceleration = static_cast<unsigned int>(std::round((use_mach_limits && print_config.machine_limits_usage.value == MachineLimitsUsage::EmitToGCode && supports_separate_travel_acceleration(print_config.gcode_flavor.value)) ?
print_config.machine_max_acceleration_travel.values.front() : 0));

m_max_jerk_x = static_cast<unsigned int>(std::round((use_mach_limits && print_config.machine_limits_usage.value == MachineLimitsUsage::EmitToGCode) ?
print_config.machine_max_jerk_x.values.front() : 0));
m_max_jerk_y = static_cast<unsigned int>(std::round((use_mach_limits && print_config.machine_limits_usage.value == MachineLimitsUsage::EmitToGCode) ?
print_config.machine_max_jerk_y.values.front() : 0));
}

void GCodeWriter::set_extruders(std::vector<unsigned int> extruder_ids)
Expand Down Expand Up @@ -212,6 +217,30 @@ std::string GCodeWriter::set_acceleration_internal(Acceleration type, unsigned i
return gcode.str();
}

std::string GCodeWriter::set_jerk(unsigned int jerk)
{
if (jerk == 0)
return {};

// Clamp the acceleration to the allowed maximum.
int jerk_x = jerk, jerk_y = jerk;
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;

std::ostringstream gcode;
if (FLAVOR_IS(gcfRepRapFirmware))
gcode << "M566 X" << jerk_x << " Y" << jerk_y;
else
gcode << "M205 X" << jerk_x << " Y" << jerk_y;

if (this->config.gcode_comments) gcode << " ; adjust jerk";
gcode << "\n";

return gcode.str();
}

std::string GCodeWriter::reset_e(bool force)
{
return
Expand Down
4 changes: 4 additions & 0 deletions src/libslic3r/GCode/GCodeWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class GCodeWriter {
std::string set_bed_temperature(unsigned int temperature, bool wait = false);
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 set_jerk(unsigned int jerk);
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
Expand Down Expand Up @@ -112,6 +113,9 @@ class GCodeWriter {
unsigned int m_max_acceleration;
unsigned int m_max_travel_acceleration;

unsigned int m_max_jerk_x;
unsigned int m_max_jerk_y;

unsigned int m_last_bed_temperature;
bool m_last_bed_temperature_reached;
Vec3d m_pos = Vec3d::Zero();
Expand Down
3 changes: 3 additions & 0 deletions src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ static std::vector<std::string> s_Preset_print_options {
"external_perimeter_acceleration", "top_solid_infill_acceleration", "solid_infill_acceleration", "travel_acceleration",
"bridge_acceleration", "first_layer_acceleration", "first_layer_acceleration_over_raft", "default_acceleration", "skirts", "skirt_distance", "skirt_height", "draft_shield",
"draft_shield_loops",
"perimeter_jerk", "infill_jerk",
"external_perimeter_jerk", "top_solid_infill_jerk", "solid_infill_jerk", "travel_jerk",
"bridge_jerk", "first_layer_jerk", "first_layer_jerk_over_raft", "default_jerk",
"min_skirt_length", "brim_width", "brim_separation", "brim_type", "support_material", "support_material_auto", "support_material_threshold", "support_material_enforce_layers",
"raft_layers", "raft_first_layer_density", "raft_first_layer_expansion", "raft_contact_distance", "raft_expansion",
"support_material_pattern", "support_material_with_sheath", "support_material_spacing", "support_material_closing_radius", "support_material_style",
Expand Down
10 changes: 10 additions & 0 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
"between_objects_gcode",
"binary_gcode",
"bridge_acceleration",
"bridge_jerk",
"bridge_fan_speed",
"enable_dynamic_fan_speeds",
"overhang_fan_speed_0",
Expand All @@ -99,12 +100,14 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
"colorprint_heights",
"cooling",
"default_acceleration",
"default_jerk",
"deretract_speed",
"disable_fan_first_layers",
"duplicate_distance",
"end_gcode",
"end_filament_gcode",
"external_perimeter_acceleration",
"external_perimeter_jerk",
"extrusion_axis",
"extruder_clearance_height",
"extruder_clearance_radius",
Expand All @@ -122,11 +125,14 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
"filament_spool_weight",
"first_layer_acceleration",
"first_layer_acceleration_over_raft",
"first_layer_jerk",
"first_layer_jerk_over_raft",
"first_layer_bed_temperature",
"first_layer_speed_over_raft",
"gcode_comments",
"gcode_label_objects",
"infill_acceleration",
"infill_jerk",
"layer_gcode",
"min_fan_speed",
"max_fan_speed",
Expand All @@ -140,6 +146,7 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
"only_retract_when_crossing_perimeters",
"output_filename_format",
"perimeter_acceleration",
"perimeter_jerk",
"post_process",
"gcode_substitutions",
"printer_notes",
Expand All @@ -162,12 +169,15 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
"single_extruder_multi_material_priming",
"slowdown_below_layer_time",
"solid_infill_acceleration",
"solid_infill_jerk",
"standby_temperature_delta",
"start_gcode",
"start_filament_gcode",
"toolchange_gcode",
"top_solid_infill_acceleration",
"top_solid_infill_jerk",
"travel_acceleration",
"travel_jerk",
"thumbnails",
"thumbnails_format",
"use_firmware_retraction",
Expand Down
Loading

0 comments on commit 171658a

Please sign in to comment.