Skip to content

Commit

Permalink
pa calib: print flow value and acceleration (#7178)
Browse files Browse the repository at this point in the history
* pa calib: print flow value and acceleration

Print flow value and acceleration for PA pattern calibration.
This should help keep track during adaptive PA calibration.

* pa pattern: fix legent section too wide in some cases

Rework pa pattern legent generation to correctly estimate
width of the legend section.

Current flow value now has variable length which is a longest of
PA value and the acceleration for a given test.

Few examples:
1. PA value are 4 characters:        0.04
   Acceleratioion 3 chars:           400
   Flow value will be 4 chars long:  7.98, or 11.3

2. PA:      0.018
   Accel:   1000
   Flow:    12.35, or 6.345

3. PA:      0.04
   Accel:   15000
   Flow:    34.34, or 4.567

Rework number-to-string conversion flow to correctly round values at
given precision.
  • Loading branch information
buzzhuzz authored Oct 24, 2024
1 parent be5bc63 commit 983f1a8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
64 changes: 53 additions & 11 deletions src/libslic3r/calib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ double CalibPressureAdvance::e_per_mm(
return line_flow.mm3_per_mm() * print_flow_ratio / filament_area ;
}

std::string CalibPressureAdvance::convert_number_to_string(double num) const
std::string CalibPressureAdvance::convert_number_to_string(double num, unsigned int precision) const
{
auto sNumber = std::to_string(num);
sNumber.erase(sNumber.find_last_not_of('0') + 1, std::string::npos);
sNumber.erase(sNumber.find_last_not_of('.') + 1, std::string::npos);
std::ostringstream stream;

return sNumber;
if (precision) {
/* if number is > 1000 then there are no way we'll fit fractional part into 5 glyphs, so
* in this case we keep full precision.
* Otherwise we reduce precision by 1 to accomodate decimal separator */
stream << std::setprecision(num >= 1000 ? precision : precision - 1);
}

stream << num;

return stream.str();
}

std::string CalibPressureAdvance::draw_digit(
Expand Down Expand Up @@ -201,12 +208,12 @@ std::string CalibPressureAdvance::draw_number(double
double speed,
GCodeWriter &writer)
{
auto sNumber = convert_number_to_string(value);
auto sNumber = convert_number_to_string(value, m_number_len);
std::stringstream gcode;
gcode << writer.set_speed(speed);

for (std::string::size_type i = 0; i < sNumber.length(); ++i) {
if (i > m_max_number_len) {
if (i >= m_number_len) {
break;
}
switch (mode) {
Expand Down Expand Up @@ -537,6 +544,18 @@ CalibPressureAdvancePattern::CalibPressureAdvancePattern(
this->m_draw_digit_mode = DrawDigitMode::Bottom_To_Top;

refresh_setup(config, is_bbl_machine, model, origin);
}

double CalibPressureAdvancePattern::flow_val() const
{
double flow_mult = m_config.option<ConfigOptionFloats>("filament_flow_ratio")->get_at(0);
double nozzle_diameter = m_config.option<ConfigOptionFloats>("nozzle_diameter")->get_at(0);
double line_width = m_config.get_abs_value("line_width", nozzle_diameter);
double layer_height = m_config.get_abs_value("layer_height");
double speed = m_config.opt_float("outer_wall_speed");
Flow pattern_line = Flow(line_width, layer_height, m_config.option<ConfigOptionFloats>("nozzle_diameter")->get_at(0));

return speed * pattern_line.mm3_per_mm() * flow_mult;
};

void CalibPressureAdvancePattern::generate_custom_gcodes(const DynamicPrintConfig &config,
Expand Down Expand Up @@ -564,7 +583,7 @@ void CalibPressureAdvancePattern::generate_custom_gcodes(const DynamicPrintConfi
draw_box_opt_args.is_filled = true;
draw_box_opt_args.num_perimeters = wall_count();
gcode << draw_box(m_writer, m_starting_point.x(), m_starting_point.y() + frame_size_y() + line_spacing_first_layer(),
glyph_tab_max_x() - m_starting_point.x(),
print_size_x(),
max_numbering_height() + line_spacing_first_layer() + m_glyph_padding_vertical * 2, draw_box_opt_args);

std::vector<CustomGCode::Item> gcode_items;
Expand Down Expand Up @@ -593,6 +612,8 @@ void CalibPressureAdvancePattern::generate_custom_gcodes(const DynamicPrintConfi

// line numbering
if (i == 1) {
m_number_len = max_numbering_length();

gcode << m_writer.set_pressure_advance(m_params.start);

double number_e_per_mm = e_per_mm(line_width(), height_layer(),
Expand All @@ -606,6 +627,18 @@ void CalibPressureAdvancePattern::generate_custom_gcodes(const DynamicPrintConfi
m_params.start + (j * m_params.step), m_draw_digit_mode, line_width(), number_e_per_mm,
speed_first_layer(), m_writer);
}

// flow value
int line_num = num_patterns + 2;
gcode << draw_number(glyph_start_x(line_num), m_starting_point.y() + frame_size_y() + m_glyph_padding_vertical + line_width(),
flow_val(), m_draw_digit_mode, line_width(), number_e_per_mm,
speed_first_layer(), m_writer);

// acceleration
line_num = num_patterns + 4;
gcode << draw_number(glyph_start_x(line_num), m_starting_point.y() + frame_size_y() + m_glyph_padding_vertical + line_width(),
m_config.opt_float("default_acceleration"), m_draw_digit_mode, line_width(), number_e_per_mm,
speed_first_layer(), m_writer);
}


Expand Down Expand Up @@ -790,7 +823,7 @@ double CalibPressureAdvancePattern::glyph_tab_max_x() const
(glyph_length_x() - line_width() / 2) + padding;
}

double CalibPressureAdvancePattern::max_numbering_height() const
size_t CalibPressureAdvancePattern::max_numbering_length() const
{
std::string::size_type most_characters = 0;
const int num_patterns = get_num_patterns();
Expand All @@ -804,9 +837,18 @@ double CalibPressureAdvancePattern::max_numbering_height() const
}
}

most_characters = std::min(most_characters, m_max_number_len);
std::string sAccel = convert_number_to_string(m_config.opt_float("default_acceleration"));
most_characters = std::max(most_characters, sAccel.length());

return (most_characters * m_digit_segment_len) + ((most_characters - 1) * m_digit_gap_len);
/* don't actually check flow value: we'll print as many fractional digits as fits */

return std::min(most_characters, m_max_number_len);
}

double CalibPressureAdvancePattern::max_numbering_height() const
{
std::string::size_type num_characters = max_numbering_length();
return (num_characters * m_digit_segment_len) + ((num_characters - 1) * m_digit_gap_len);
}

double CalibPressureAdvancePattern::pattern_shift() const
Expand Down
5 changes: 4 additions & 1 deletion src/libslic3r/calib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class CalibPressureAdvance
double e_per_mm(double line_width, double layer_height, float nozzle_diameter, float filament_diameter, float print_flow_ratio) const;
double speed_adjust(int speed) const { return speed * 60; };

std::string convert_number_to_string(double num) const;
std::string convert_number_to_string(double num, unsigned precision = 0) const;
double number_spacing() const { return m_digit_segment_len + m_digit_gap_len; };
std::string draw_digit(double startx,
double starty,
Expand Down Expand Up @@ -188,6 +188,7 @@ class CalibPressureAdvance
const double m_digit_segment_len{2};
const double m_digit_gap_len{1};
const std::string::size_type m_max_number_len{5};
std::string::size_type m_number_len{m_max_number_len}; /* Current length of number labels */
};

class CalibPressureAdvanceLine : public CalibPressureAdvance
Expand Down Expand Up @@ -255,6 +256,7 @@ class CalibPressureAdvancePattern : public CalibPressureAdvance
double print_size_x() const { return object_size_x() + pattern_shift(); };
double print_size_y() const { return object_size_y(); };
double max_layer_z() const { return height_first_layer() + ((m_num_layers - 1) * height_layer()); };
double flow_val() const;

void generate_custom_gcodes(const DynamicPrintConfig &config, bool is_bbl_machine, Model &model, const Vec3d &origin);

Expand Down Expand Up @@ -296,6 +298,7 @@ class CalibPressureAdvancePattern : public CalibPressureAdvance
double glyph_length_x() const;
double glyph_tab_max_x() const;
double max_numbering_height() const;
size_t max_numbering_length() const;

double pattern_shift() const;

Expand Down

0 comments on commit 983f1a8

Please sign in to comment.