From b2916dc05228f138ccf619eedcd8ce923de8a8c0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix=20Boisselier?= <felix@fboisselier.fr>
Date: Sun, 30 Apr 2023 17:06:31 +0000
Subject: [PATCH] modified _MODULE_PRIMELINE to be more efficient

---
 config/hardware/extruder/cw1.cfg       |  1 +
 config/hardware/extruder/cw2.cfg       |  1 +
 config/hardware/extruder/galileo.cfg   |  1 +
 config/hardware/extruder/lgx_heavy.cfg |  1 +
 config/hardware/extruder/lgx_lite.cfg  |  1 +
 macros/base/start_print.cfg            | 24 +++++++++++++-----------
 user_templates/variables.cfg           |  6 ++++--
 7 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/config/hardware/extruder/cw1.cfg b/config/hardware/extruder/cw1.cfg
index cf4a2234b..812f8723e 100644
--- a/config/hardware/extruder/cw1.cfg
+++ b/config/hardware/extruder/cw1.cfg
@@ -9,6 +9,7 @@ full_steps_per_rotation: 200
 nozzle_diameter: 0.400
 filament_diameter: 1.75
 max_extrude_only_distance: 110
+max_extrude_cross_section: 5
 sensor_type: ATC Semitec 104GT-2
 min_temp: 10
 max_temp: 270
diff --git a/config/hardware/extruder/cw2.cfg b/config/hardware/extruder/cw2.cfg
index e30dee6c7..9d4d8d22b 100644
--- a/config/hardware/extruder/cw2.cfg
+++ b/config/hardware/extruder/cw2.cfg
@@ -9,6 +9,7 @@ full_steps_per_rotation: 200
 nozzle_diameter: 0.400
 filament_diameter: 1.75
 max_extrude_only_distance: 110
+max_extrude_cross_section: 5
 sensor_type: ATC Semitec 104GT-2
 min_temp: 10
 max_temp: 270
diff --git a/config/hardware/extruder/galileo.cfg b/config/hardware/extruder/galileo.cfg
index 28cddf042..8044a55f4 100644
--- a/config/hardware/extruder/galileo.cfg
+++ b/config/hardware/extruder/galileo.cfg
@@ -9,6 +9,7 @@ full_steps_per_rotation: 200
 nozzle_diameter: 0.400
 filament_diameter: 1.75
 max_extrude_only_distance: 110
+max_extrude_cross_section: 5
 sensor_type: ATC Semitec 104GT-2
 min_temp: 10
 max_temp: 270
diff --git a/config/hardware/extruder/lgx_heavy.cfg b/config/hardware/extruder/lgx_heavy.cfg
index 5e54a7ca5..ca683d8b6 100644
--- a/config/hardware/extruder/lgx_heavy.cfg
+++ b/config/hardware/extruder/lgx_heavy.cfg
@@ -9,6 +9,7 @@ full_steps_per_rotation: 200
 nozzle_diameter: 0.400
 filament_diameter: 1.75
 max_extrude_only_distance: 110
+max_extrude_cross_section: 5
 sensor_type: ATC Semitec 104GT-2
 min_temp: 10
 max_temp: 270
diff --git a/config/hardware/extruder/lgx_lite.cfg b/config/hardware/extruder/lgx_lite.cfg
index 78dfd3f2e..674fa9d06 100644
--- a/config/hardware/extruder/lgx_lite.cfg
+++ b/config/hardware/extruder/lgx_lite.cfg
@@ -9,6 +9,7 @@ full_steps_per_rotation: 200
 nozzle_diameter: 0.400
 filament_diameter: 1.75
 max_extrude_only_distance: 110
+max_extrude_cross_section: 5
 sensor_type: ATC Semitec 104GT-2
 min_temp: 10
 max_temp: 270
diff --git a/macros/base/start_print.cfg b/macros/base/start_print.cfg
index e509b8855..c7d5acd9e 100644
--- a/macros/base/start_print.cfg
+++ b/macros/base/start_print.cfg
@@ -160,31 +160,34 @@ gcode:
     {% set St = printer["gcode_macro _USER_VARIABLES"].travel_speed * 60 %}
     {% set Sz = printer["gcode_macro _USER_VARIABLES"].z_drop_speed * 60 %}
     {% set prime_line_x, prime_line_y = printer["gcode_macro _USER_VARIABLES"].prime_line_xy|map('float') %}
+    {% set prime_line_length = printer["gcode_macro _USER_VARIABLES"].prime_line_length|float %}
+    {% set prime_line_flowrate = printer["gcode_macro _USER_VARIABLES"].prime_line_flowrate|float %}
+
+    {% set max_extrude_cross_section = printer.configfile.settings.extruder.max_extrude_cross_section | float %}
+    {% set Sp = (prime_line_flowrate / max_extrude_cross_section) * 60 | float %}
 
     G91
     M83
     G1 Z5 F{Sz}
 
-    ; Starting position
+    # Starting position
     G90
     G0 X{prime_line_x} Y{prime_line_y} F{St}
-    G1 Z0.3 F{Sz|int / 2}
+    G1 Z0.6 F{Sz|int / 2}
 
-    ; Add pressure in the nozzle
+    # Add pressure in the nozzle
     G92 E0
     G1 E18 F300
 
-    ; Prime line
-    G92 E0
-    G1 Y{prime_line_y + 80} E10 F2500
+    # Prime line
     G92 E0
-    G1 Y{prime_line_y + 130} E5 F1500
+    G1 Y{prime_line_y + prime_line_length} E{prime_line_length} F{Sp}
 
-    ; Retract and Z-hop
+    # Retract and Z-hop
     G92 E0
-    G1 Z2.0 E-0.1 F{Sz}
+    G1 E-0.2 F2100
     G92 E0
-    G1 Z5 F{Sz}
+    G1 Z3 F{Sz}
 
 
 [gcode_macro _MODULE_HEATSOAK_BED]
@@ -301,7 +304,6 @@ gcode:
     {% endif %}
 
     M109 S{EXTRUDER_TEMP}
-    G4 P{60000 * 0.5}
 
     {% if purgeclean_servo_enabled %}
         _SERVO_RETRACT ITEM="purge"
diff --git a/user_templates/variables.cfg b/user_templates/variables.cfg
index e50095401..583c73f51 100644
--- a/user_templates/variables.cfg
+++ b/user_templates/variables.cfg
@@ -54,8 +54,10 @@ variable_probe_dock_margin_xy: 0, 0
 ## Temperature of the extruder used to preheat the chamber temperature and do some START_PRINT actions
 variable_safe_extruder_temp: 150
 
-## Prime line start position
-variable_prime_line_xy: 2.5, 20
+## Prime line parameters
+variable_prime_line_xy: 2.5, 5
+variable_prime_line_length: 40
+variable_prime_line_flowrate: 5 # mm3/s used for the prime line
 
 ## Park position used when pause, end_print, etc...
 variable_park_position_xy: -1, -1