Skip to content

Commit

Permalink
Properly apply retraction limitations during travel
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Feb 5, 2025
1 parent 4426c45 commit c833194
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
3 changes: 2 additions & 1 deletion include/gcodeExport.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,9 @@ class GCodeExport : public NoCopy
* @param force Indicates whether we should force the retraction to happen regardless of the maximum allowed retraction count
* @param extruder_switch Indicates whether we retract for an extruder switch
* @param retract_distance A specific absolute retraction distance to be used, or nullopt to use the one in the config
* @return True if the retraction has been processed normally, false if it was skipped because of limitations
*/
void writeRetraction(const RetractionConfig& config, bool force = false, bool extruder_switch = false, const std::optional<double> retract_distance = std::nullopt);
bool writeRetraction(const RetractionConfig& config, bool force = false, bool extruder_switch = false, const std::optional<double> retract_distance = std::nullopt);

/*!
* Start a z hop with the given \p hop_height.
Expand Down
18 changes: 12 additions & 6 deletions src/LayerPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3160,11 +3160,17 @@ void LayerPlan::writeGCode(GCodeExport& gcode)
computeAntiOozeAmounts(gcode, extruder, path, z_hop_height, retraction_config, retraction_amounts, priming_amounts);
}

gcode.writeRetraction(
retraction_config->retraction_config,
false,
false,
retraction_amounts.has_value() ? std::optional{ retraction_amounts->amount_while_still } : std::nullopt);
if (! gcode.writeRetraction(
retraction_config->retraction_config,
false,
false,
retraction_amounts.has_value() ? std::make_optional(retraction_amounts->amount_while_still) : std::nullopt))
{
// Retraction was cancelled because of limitations, so also cancel retraction/priming during travel
retraction_amounts.reset();
priming_amounts.reset();
}

if (path.retract_for_nozzle_switch)
{
constexpr bool force = true;
Expand All @@ -3177,7 +3183,7 @@ void LayerPlan::writeGCode(GCodeExport& gcode)
gcode.writeZhopStart(
z_hop_height,
0.0,
retraction_amounts.has_value() ? retraction_amounts->z_hop.amount : 0.0,
retraction_amounts.has_value() ? std::make_optional(retraction_amounts->z_hop.amount) : std::nullopt,
retraction_amounts.has_value() ? retraction_amounts->z_hop.ratio : 0.0_r);
z_hop_height = retraction_config->retraction_config.zHop; // back to normal z hop
}
Expand Down
24 changes: 13 additions & 11 deletions src/gcodeExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ void GCodeExport::writeUnretractionAndPrime()
}
}

void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bool extruder_switch, const std::optional<double> retract_distance)
bool GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bool extruder_switch, const std::optional<double> retract_distance)
{
ExtruderTrainAttributes& extr_attr = extruder_attr_[current_extruder_];

Expand All @@ -1228,13 +1228,7 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo
}
extr_attr.retraction_e_amount_current_ = 1.0; // 1.0 is a stub; BFB doesn't use the actual retracted amount; retraction is performed by firmware
}
return;
}

RetractionAmounts retraction_amounts = computeRetractionAmounts(extr_attr, retract_distance.value_or(config.distance));
if (! retraction_amounts.has_retraction())
{
return;
return true;
}

{ // handle retraction limitation
Expand All @@ -1248,12 +1242,12 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo
}
if (! force && config.retraction_count_max <= 0)
{
return;
return false;
}
if (! force && extruded_volume_at_previous_n_retractions.size() == config.retraction_count_max
&& current_extruded_volume < extruded_volume_at_previous_n_retractions.back() + config.retraction_extrusion_window * extr_attr.filament_area_)
{
return;
return false;
}
extruded_volume_at_previous_n_retractions.push_front(current_extruded_volume);
if (extruded_volume_at_previous_n_retractions.size() == config.retraction_count_max + 1)
Expand All @@ -1262,11 +1256,17 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo
}
}

RetractionAmounts retraction_amounts = computeRetractionAmounts(extr_attr, retract_distance.value_or(config.distance));
if (! retraction_amounts.has_retraction())
{
return true;
}

if (extr_attr.machine_firmware_retract_)
{
if (extruder_switch && extr_attr.retraction_e_amount_current_)
{
return;
return true;
}
*output_stream_ << "G10";
if (extruder_switch && flavor_ == EGCodeFlavor::REPETIER)
Expand Down Expand Up @@ -1301,6 +1301,8 @@ void GCodeExport::writeRetraction(const RetractionConfig& config, bool force, bo
}

extr_attr.prime_volume_ += config.prime_volume;

return true;
}

void GCodeExport::writeZhopStart(const coord_t hop_height, Velocity speed /*= 0*/, const std::optional<double> retract_distance, const Ratio& retract_ratio)
Expand Down

0 comments on commit c833194

Please sign in to comment.