Skip to content

Instantly share code, notes, and snippets.

@bernhardberger
Last active February 8, 2022 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernhardberger/555c4913d492a5213b49b2efa4f0b24e to your computer and use it in GitHub Desktop.
Save bernhardberger/555c4913d492a5213b49b2efa4f0b24e to your computer and use it in GitHub Desktop.
K-Factor for Pressure Advance
; adjust values to your needs
var temp_nozzle = 195
var temp_bed = 55
var nozzle_diameter = 0.6
var layer_height = 0.1618
var line_width = 0.6
var pa_start = 0
var pa_end = 0.05
var pa_step_size = 0.002
var print_speed_slow_mms = 30
var print_speed_fast_mms = 200
var travel_speed_mms = 300
var travel_speed_z_mms = 300
var line_spacing = 5
var origin_x = 0
var origin_y = 0
var bed_center = true
var line_length_slow = 40
var line_length_fast = 100
var fan_speed = 0.0 ; range 0.0...1.0
var dwell_time_milliseconds = 2000
var prime_nozzle = false
var prime_nozzle_macro = "0:/macros/_Printing/prime_nozzle.g" ;set to "" (empty string) if you don't want to prime the nozzle, otherwise enter the macro location
M291 R"Pressure Advance Calibration" P{"Start Factor: " ^ {var.pa_start} ^ ", End Factor: " ^ {var.pa_end} ^ ", Steps: " ^ {var.pa_step_size}}
; [!!!!!] DO NOT CHANGE VALUES AFTER THIS LINE [!!!!!!]
;------------------------------------------------------
; calculated values start
var print_speed_slow = {var.print_speed_slow_mms * 60}
var print_speed_fast = {var.print_speed_fast_mms * 60}
var travel_speed = {var.travel_speed_mms * 60}
var travel_speed_z = {var.travel_speed_z_mms * 60}
; extrusion calculation taken from slic3r docs (https://manual.slic3r.org/advanced/flow-math)
; A = rectangle + circle = (line_width - layer_height)*layer_height + pi(layer_height/2) * pi(layer_height/2)
var e_per_mm = {(var.line_width - var.layer_height) * var.layer_height + (pi * ((var.layer_height/2) * (var.layer_height/2)))}
; calculated values end
G21 ; Millimeter units
G90 ; Absolute XYZ
M83 ; Relative E
T0 ; Switch to tool 0
M104 S{var.temp_nozzle}
M190 S{var.temp_bed}
G1 Z5 F{var.travel_speed_z} ; move nozzle down
M109 S{var.temp_nozzle} ; Wait for nozzle temp
;M204 S5000 P5000; Acceleration
G92 E0 ; Reset extruder distance
M106 S{var.fan_speed} ;turn off fan
G1 X0 Y0 F{var.travel_speed} ; move nozzle to center
G1 Z{var.layer_height} F{var.travel_speed_z} ; Move to layer height
M572 D0 S0 ;reset pressure advance
; ==== PRIME NOZZLE START ====
if var.prime_nozzle == true
if var.prime_nozzle_macro != ""
M98 P{var.prime_nozzle_macro}
; ==== PRIME NOZZLE END ====
var line_length_total = {2 * var.line_length_slow + var.line_length_fast}
var pos_x_start = 0
var pos_y_start = 0
if var.bed_center == true
set var.pos_x_start = {-1 * (var.line_length_total / 2)}
set var.pos_y_start = {-1 * var.line_spacing * ((var.pa_end - var.pa_start) / var.pa_step_size) / 2}
var pos_x_end = {var.pos_x_start + var.line_length_total}
var pos_y_end = {var.pos_y_start + var.line_spacing * ((var.pa_end - var.pa_start) / var.pa_step_size)}
var anchor_y_start = {var.pos_y_start - var.line_spacing}
var anchor_y_end = {var.pos_y_end + var.line_spacing}
var anchor_y_length = {sqrt(0 + (var.anchor_y_end - var.anchor_y_start) * (var.anchor_y_end - var.anchor_y_start))} ; we only move in the y plance
; ==== ANCHOR FRAME START ====
G1 X{var.pos_x_start} Y{var.anchor_y_start} F{var.travel_speed} ; move to start
G11 ; un-retract
G1 X{var.pos_x_start} Y{var.anchor_y_end} E{var.e_per_mm * var.anchor_y_length} F{var.print_speed_slow} ; print line
G1 X{var.pos_x_start + var.line_width} Y{var.anchor_y_end} F{var.travel_speed} ; move to start
G1 X{var.pos_x_start + var.line_width} Y{var.anchor_y_start} E{var.e_per_mm * var.anchor_y_length} F{var.print_speed_slow} ; print line
G10 ; retract
G1 X{var.pos_x_end} Y{var.anchor_y_start} F{var.travel_speed} ; move to start
G11 ; un-retract
G1 X{var.pos_x_end} Y{var.anchor_y_end} E{var.e_per_mm * var.anchor_y_length} F{var.print_speed_slow} ; print line
G1 X{var.pos_x_end - var.line_width} Y{var.anchor_y_end} F{var.travel_speed} ; move to start
G1 X{var.pos_x_end - var.line_width} Y{var.anchor_y_start} E{var.e_per_mm * var.anchor_y_length} F{var.print_speed_slow} ; print line
G10 ; retract
; ==== ANCHOR FRAME END ====
G4 P{var.dwell_time_milliseconds} ; Pause (dwell) for specified milliseconds
; ==== TEST PATTERN START (loop) ====
var pa_current = {var.pa_start}
var pos_y_current = {var.pos_y_start}
while {var.pa_current <= var.pa_end}
G1 X{var.pos_x_start} Y{var.pos_y_current} F{var.travel_speed} ; move to start
M118 S"M572 D0 S " ^ {var.pa_current} L1
M572 D0 S{var.pa_current} ; set PA factor
G11 ; un-retract
G1 X{var.pos_x_start + var.line_length_slow} Y{var.pos_y_current} E{var.e_per_mm * var.line_length_slow} F{var.print_speed_slow} ; print slow line
G1 X{var.pos_x_start + var.line_length_slow + var.line_length_fast} Y{var.pos_y_current} E{var.e_per_mm * var.line_length_fast} F{var.print_speed_fast} ; print fast line
G1 X{var.pos_x_end} Y{var.pos_y_current} E{var.e_per_mm * var.line_length_slow} F{var.print_speed_slow} ; print slow line
G10 ; retract
set var.pa_current = {var.pa_current + var.pa_step_size}
set var.pos_y_current = {var.pos_y_current + var.line_spacing}
; ==== TEST PATTERN END ====
M0 ;finish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment