Skip to content

Commit

Permalink
Add code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wawanbreton committed Aug 23, 2024
1 parent cf3ece9 commit 6fc32c1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 23 deletions.
66 changes: 64 additions & 2 deletions include/LayerPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,34 @@ class LayerPlan : public NoCopy
const Ratio flow_ratio,
const double fan_speed);

/*!
* @brief Send a GCodePath line to the communication object, applying proper Z offsets
* @param path The path to be sent
* @param position The start position (which is not included in the path points)
* @param extrude_speed The actual used extrusion speed
*/
void sendLineTo(const GCodePath& path, const Point3LL& position, const double extrude_speed);

/*!
* @brief Write a travel move and properly apply the various Z offsets
* @param gcode The actual GCode exporter
* @param position The position to move to. The Z coordinate is an offset to the current layer position
* @param speed The actual used speed
* @param path_z_offset The global path Z offset to be applied
* @note This function is to be used when dealing with 3D coordinates. If you have 2D coordinates, just call gcode.writeTravel()
*/
void writeTravelRelativeZ(GCodeExport& gcode, const Point3LL& position, const Velocity& speed, const coord_t path_z_offset);

/*!
* \brief Write an extrusion move and properly apply the various Z offsets
* \param gcode The actual GCode exporter
* \param position The position to move to. The Z coordinate is an offset to the current layer position
* \param speed The actual used speed
* \param path_z_offset The global path Z offset to be applied
* \param extrusion_mm3_per_mm The desired flow rate
* \param feature The current feature being printed
* \param update_extrusion_offset whether to update the extrusion offset to match the current flow rate
*/
void writeExtrusionRelativeZ(
GCodeExport& gcode,
const Point3LL& position,
Expand All @@ -837,6 +861,38 @@ class LayerPlan : public NoCopy
PrintFeatureType feature,
bool update_extrusion_offset = false);

/*!
* \brief Add a wall to the gcode with optimized order
* \param wall The full wall to be added
* \param wall_length The pre-calculated full wall length
* \param start_idx The index of the point where to start printing the wall
* \param direction The direction along which to print the wall, which should be 1 or -1
* \param max_index The last index to be used when iterating over the wall segments
* \param settings The settings which should apply to this wall added to the layer plan
* \param default_config The config with which to print the wall lines that are not spanning a bridge or are exposed to air
* \param roofing_config The config with which to print the wall lines that are exposed to air
* \param bridge_config The config with which to print the wall lines that are spanning a bridge
* \param flow_ratio The ratio with which to multiply the extrusion amount
* \param line_width_ratio The line width ratio to be applied
* \param non_bridge_line_volume A pseudo-volume that is derived from the print speed and flow of the non-bridge lines that have preceded this lin
* \param min_bridge_line_len The minimum line width to allow an extrusion move to be processed as a bridge move
* \param always_retract Whether to force a retraction when moving to the start of the polygon (used for outer walls)
* \param is_small_feature Indicates whether the wall is so small that it should be processed differently
* \param small_feature_speed_factor The speed factor to be applied to small feature walls
* \param max_area_deviation The maximum allowed area deviation to split a segment into pieces
* \param max_resolution The maximum resolution to split a segment into pieces
* \param scarf_seam_length The length of the scarf joint seam, which may be 0 if there is none
* \param scarf_seam_start_ratio The ratio of the line thickness to start the scarf seam with
* \param scarf_split_distance The maximum length of a segment to apply the scarf seam gradient, longer segments will be splitted
* \param scarf_max_z_offset The maximum Z offset te be applied at the lowest position of the scarf seam
* \param speed_split_distance The maximum length of a segment to apply the acceleration/deceleration gradient, longer segments will be splitted
* \param start_speed_ratio The ratio of the top speed to be applied when starting the segment, then accelerate gradually to full speed
* \param accelerate_length The pre-calculated length of the acceleration phase
* \param end_speed_ratio The ratio of the top speed to be applied when finishing a segment
* \param decelerate_length The pre-calculated length of the deceleration phase
* \param is_scarf_closure Indicates whether this function is called to make the scarf closure (overlap over the first scarf pass) or
* the normal first pass of the wall
*/
void addWallSplitted(
const ExtrusionLine& wall,
const coord_t wall_length,
Expand All @@ -848,7 +904,7 @@ class LayerPlan : public NoCopy
const GCodePathConfig& roofing_config,
const GCodePathConfig& bridge_config,
const double flow_ratio,
const Ratio nominal_line_width_multiplier,
const Ratio line_width_ratio,
double& non_bridge_line_volume,
const coord_t min_bridge_line_len,
const bool always_retract,
Expand All @@ -867,7 +923,13 @@ class LayerPlan : public NoCopy
const coord_t decelerate_length,
const bool is_scarf_closure);

// helper function to calculate the distance from the start of the current wall line to the first bridge segment
/*!
* \brief Helper function to calculate the distance from the start of the current wall line to the first bridge segment
* \param wall The currently processed wall
* \param current_index The index of the currently processed point
* \param min_bridge_line_len The minimum line width to allow an extrusion move to be processed as a bridge move
* \return The distance from the start of the current wall line to the first bridge segment
*/
coord_t computeDistanceToBridgeStart(const ExtrusionLine& wall, const size_t current_index, const coord_t min_bridge_line_len) const;
};

Expand Down
48 changes: 27 additions & 21 deletions src/LayerPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ void LayerPlan::addWallSplitted(
const GCodePathConfig& roofing_config,
const GCodePathConfig& bridge_config,
const double flow_ratio,
const Ratio nominal_line_width_multiplier,
const Ratio line_width_ratio,
double& non_bridge_line_volume,
const coord_t min_bridge_line_len,
const bool always_retract,
Expand Down Expand Up @@ -1037,11 +1037,11 @@ void LayerPlan::addWallSplitted(
split_origin.z_ = scarf_max_z_offset;
}

coord_t wall_processed_distance = 0;
double scarf_factor_origin = 0.0;
double accelerate_factor_origin = 0.0;
double decelerate_factor_origin = 0.0;
const coord_t start_decelerating_position = wall_length - decelerate_length;
coord_t wall_processed_distance = 0; // This will grow while we travel along the wall, to the total wall length
double scarf_factor_origin = 0.0; // Interpolation factor at the current point for the scarf
double accelerate_factor_origin = 0.0; // Interpolation factor at the current point for the acceleration
double decelerate_factor_origin = 0.0; // Interpolation factor at the current point for the deceleration
const coord_t start_decelerate_position = wall_length - decelerate_length;

for (size_t point_idx = 1; point_idx < max_index; point_idx++)
{
Expand Down Expand Up @@ -1097,14 +1097,7 @@ void LayerPlan::addWallSplitted(
if (is_small_feature && ! is_scarf_closure)
{
constexpr bool spiralize = false;
addExtrusionMove(
destination,
default_config,
SpaceFillType::Polygons,
flow_ratio,
line_width * nominal_line_width_multiplier,
spiralize,
small_feature_speed_factor);
addExtrusionMove(destination, default_config, SpaceFillType::Polygons, flow_ratio, line_width * line_width_ratio, spiralize, small_feature_speed_factor);
}
else
{
Expand All @@ -1113,6 +1106,8 @@ void LayerPlan::addWallSplitted(
// Cut piece into smaller parts for scarf seam and acceleration/deceleration
while ((! is_scarf_closure && piece_remaining_distance > 0) || (is_scarf_closure && wall_processed_distance < scarf_seam_length))
{
// Make a list of all the possible incoming positions where we would eventually want to stop next
// The positions are expressed in distance from wall start along the wall segments
std::vector<coord_t> split_positions{ wall_processed_distance + piece_remaining_distance };

const bool process_scarf = wall_processed_distance < scarf_seam_length;
Expand All @@ -1132,31 +1127,37 @@ void LayerPlan::addWallSplitted(
bool deceleration_started = false;
if (! is_scarf_closure && decelerate_length > 0)
{
deceleration_started = wall_processed_distance >= start_decelerating_position;
deceleration_started = wall_processed_distance >= start_decelerate_position;
if (deceleration_started)
{
split_positions.push_back(wall_processed_distance + speed_split_distance);
}
else
{
split_positions.push_back(start_decelerating_position);
split_positions.push_back(start_decelerate_position);
}
}

// Now take the closest position candidate and make a sub-segment to it
const coord_t destination_position = *std::min_element(split_positions.begin(), split_positions.end());
const coord_t length_to_process = destination_position - wall_processed_distance;
Point3LL split_destination = split_origin + normal(line_vector, length_to_process);

double scarf_segment_flow_ratio = 1.0;
double scarf_factor_destination = 1.0;
double scarf_factor_destination = 1.0; // Out of range, scarf is done => 1.0
if (process_scarf)
{
// Calculate scarf interpolation factor on the destination point
scarf_factor_destination = static_cast<double>(destination_position) / static_cast<double>(scarf_seam_length);

// Interpolate Z offset according to interpolation factor
if (! is_scarf_closure)
{
split_destination.z_ = std::llrint(std::lerp(scarf_max_z_offset, 0.0, scarf_factor_destination));
}

// Interpolate flow according to interpolation factor average, because it can't be different
// at start and end positions
const double scarf_factor_average = (scarf_factor_origin + scarf_factor_destination) / 2.0;
if (is_scarf_closure)
{
Expand All @@ -1176,23 +1177,28 @@ void LayerPlan::addWallSplitted(
}

Ratio accelerate_speed_factor = 1.0_r;
double accelerate_factor_destination = 1.0;
double accelerate_factor_destination = 1.0; // Out of range, acceleration is done => 1.0
if (process_acceleration)
{
// Interpolate speed according to interpolation factor average, because it can't be different
// at start and end positions
accelerate_factor_destination = static_cast<double>(destination_position) / static_cast<double>(accelerate_length);
const double accelerate_factor_average = (accelerate_factor_origin + accelerate_factor_destination) / 2.0;
accelerate_speed_factor = std::lerp(start_speed_ratio, 1.0, accelerate_factor_average);
}

Ratio decelerate_speed_factor = is_scarf_closure ? end_speed_ratio : 1.0_r;
double decelerate_factor_destination = 0.0;
double decelerate_factor_destination = 0.0; // Out of range, deceleration is not started => 0.0
if (deceleration_started)
{
// Interpolate speed according to interpolation factor average, because it can't be different
// at start and end positions
decelerate_factor_destination = 1.0 - (static_cast<double>(wall_length - destination_position) / static_cast<double>(decelerate_length));
const double decelerate_factor_average = (decelerate_factor_origin + decelerate_factor_destination) / 2.0;
decelerate_speed_factor = std::lerp(1.0, end_speed_ratio, decelerate_factor_average);
}

// now add the (sub-)segment
constexpr bool travel_to_z = false;
addWallLine(
split_origin,
Expand All @@ -1202,7 +1208,7 @@ void LayerPlan::addWallSplitted(
roofing_config,
bridge_config,
flow_ratio * scarf_segment_flow_ratio,
line_width * nominal_line_width_multiplier,
line_width * line_width_ratio,
non_bridge_line_volume,
accelerate_speed_factor * decelerate_speed_factor,
distance_to_bridge_start,
Expand Down Expand Up @@ -1340,7 +1346,7 @@ void LayerPlan::addWall(

const coord_t wall_length = wall.length();
const coord_t small_feature_max_length = settings.get<coord_t>("small_feature_max_length");
const bool is_small_feature = (small_feature_max_length > 0) && (layer_nr_ == 0 || wall.inset_idx_ == 0) && wall.shorterThan(small_feature_max_length);
const bool is_small_feature = (small_feature_max_length > 0) && (layer_nr_ == 0 || wall.inset_idx_ == 0) && wall_length < small_feature_max_length;
const Velocity min_speed = fan_speed_layer_time_settings_per_extruder_[getLastPlannedExtruderTrain()->extruder_nr_].cool_min_speed;
Ratio small_feature_speed_factor = settings.get<Ratio>((layer_nr_ == 0) ? "small_feature_speed_factor_0" : "small_feature_speed_factor");
small_feature_speed_factor = std::max(static_cast<double>(small_feature_speed_factor), static_cast<double>(min_speed / default_config.getSpeed()));
Expand Down

0 comments on commit 6fc32c1

Please sign in to comment.