Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Last active April 28, 2022 07:55
Show Gist options
  • Save dz0ny/40f7426e53e901c033ba29abcff0e750 to your computer and use it in GitHub Desktop.
Save dz0ny/40f7426e53e901c033ba29abcff0e750 to your computer and use it in GitHub Desktop.
[respond]
[gcode_macro PA_TUNER]
description: Pressure Advance Tuning Macro
=========================================
parameters with defaults listed below:
TOOLHEAD=""
BED_X_LENGTH=printer.toolhead.axis.maximum.x
BED_Y_LENGTH=printer.toolhead.axis.maximum.y
FILAMENT_DIAMETER=printer...filament_diameter
EXTRUSION_WIDTH=printer...nozzle_diameter
LAYER_HEIGHT=0.3
SIDE_LENGTH=100
FAST_PRINT_SPEED=150
SLOW_PRINT_SPEED=30
COOLING_FAN_SPEED=255
PA_MIN=0.0
PA_MAX=1.0
LAYERS=60
BED_TEMP=55
PRINTING_TEMP=210
X_OFFSET=0
Y_OFFSET=0
BASE_ONLY_FLOW_PERCENT=125
STARTUP_MACRO=LOCAL_GENERIC_PA_START
END_MACRO=LOCAL_GENERIC_PA_END
=========================================
gcode:
# Set your own default parameters here
{% set bed_x = params.BED_X_LENGTH|default(printer.toolhead.axis_maximum.x)|float %}
{% set bed_y = params.BED_Y_LENGTH|default(printer.toolhead.axis_maximum.y)|float %}
{% set filament_d = params.FILAMENT_DIAMETER|default(printer.configfile.settings.extruder.filament_diameter)|float %}
{% set e_width = params.EXTRUSION_WIDTH|default(printer.configfile.settings.extruder.nozzle_diameter)|float %}
{% set tool = params.TOOLHEAD|default("") %}
{% set flow_p = params.BASE_ONLY_FLOW_PERCENT|default(125.0)|float %}
{% set rect_side_len = params.SIDE_LENGTH|default(100.0)|float %}
{% set layer_h = params.LAYER_HEIGHT|default(0.3)|float %}
{% set fspeed = params.FAST_PRINT_SPEED|default(150.0)|float %}
{% set sspeed = params.SLOW_PRINT_SPEED|default(30.0)|float %}
{% set fan_speed = params.COOLING_FAN_SPEED|default(255.0)|float %}
{% set PA_low = params.PA_MIN|default(0.0)|float %}
{% set PA_high = params.PA_MAX|default(1.0)|float %}
{% set b_temp = params.BED_TEMP|default(55.0)|float %}
{% set p_temp = params.PRINTING_TEMP|default(210.0)|float %}
{% set off_x = params.X_OFFSET|default(0.0)|float %}
{% set off_y = params.Y_OFFSET|default(0.0)|float %}
{% set layer_num = params.LAYERS|default(60)|int %}
{% set startup_code = params.START_MACRO|default("LOCAL_GENERIC_PA_START PT=" ~ p_temp ~ " BT=" ~ b_temp)|string %}
{% set end_code = params.END_MACRO|default("LOCAL_GENERIC_PA_END EJECT=" ~ bed_y)|string %}
# End default parameters
{% set PA_const = (2 * ((PA_high - PA_low) / layer_num)) - PA_low %}
{% if PA_const < 0 %}
{% set PA_sign = " + " %}
{% else %}
{% set PA_sign = " - " %}
{% endif %}
{% set info_str = "Bed X Length: " ~ bed_x %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Y Length: " ~ bed_y %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed X Offset: " ~ off_x %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Y Offset: " ~ off_y %}
RESPOND MSG="{info_str}"
{% set info_str = "Extrusion Width: " ~ e_width %}
RESPOND MSG="{info_str}"
{% set info_str = "Filament size: " ~ filament_d %}
RESPOND MSG="{info_str}"
{% set info_str = "Layer Height: " ~ layer_h %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Temperature: " ~ b_temp %}
RESPOND MSG="{info_str}"
{% set info_str = "Print Temperature: " ~ p_temp %}
RESPOND MSG="{info_str}"
{% set info_str = "Slow Printing Speed: " ~ sspeed %}
RESPOND MSG="{info_str}"
{% set info_str = "Fast Printing Speed: " ~ fspeed %}
RESPOND MSG="{info_str}"
{% set info_str = "Fan Speed: " ~ fan_speed %}
RESPOND MSG="{info_str}"
{% set info_str = "Length of Test Square: " ~ rect_side_len %}
RESPOND MSG="{info_str}"
{% set info_str = "Number of Layers: " ~ layer_num %}
RESPOND MSG="{info_str}"
{% set info_str = "Minimum Pressure Advance: " ~ PA_low %}
RESPOND MSG="{info_str}"
{% set info_str = "Maximum Pressure Advance: " ~ PA_high %}
RESPOND MSG="{info_str}"
{% set info_str = "Formula: Pressure Advance Value = Measured_Z_Height x " ~ (((PA_high - PA_low) / layer_num) / layer_h) ~ PA_sign ~ PA_const|abs %}
RESPOND MSG="{info_str}"
{% set g_cmd = tool %}
{ g_cmd }
{% set g_cmd = startup_code %}
{ g_cmd }
{% set g_cmd = "M220 S100" %}
{ g_cmd }
{% set g_cmd = "M221 S100" %}
{ g_cmd }
{% set g_cmd = "SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY=1 ACCEL=500" %}
{ g_cmd }
{% set g_cmd = "SET_PRESSURE_ADVANCE ADVANCE_LOOKAHEAD_TIME=0" %}
{ g_cmd }
{% set g_cmd = "G92 E0" %}
{ g_cmd }
{% set g_cmd = "M83" %}
{ g_cmd }
{% set current_x = (bed_x / 2) - (rect_side_len / 2) %}
{% set current_y = (bed_y / 2) - (rect_side_len / 2) %}
# move the printhead to the starting position and prime nozzle
{% set g_cmd = "G1 X" ~ (current_x + off_x) ~ " Y" ~ (current_y + off_y) ~ " Z" ~ layer_h ~ " E1.0 F" ~ (fspeed*60) %}
{ g_cmd }
{% set g_cmd = "G91" %}
{ g_cmd }
{% set pressure_advance = 0.0 %}
{% set PA_msg = "SET_PRESSURE_ADVANCE ADVANCE=" ~ pressure_advance %}
{ PA_msg }
pa_line LSPEED={20} X_L={rect_side_len} Y_L=0.0 LF={flow_p} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={20} X_L={0.0} Y_L={(rect_side_len/2)} LF={flow_p} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={20} X_L=0.0 Y_L={(rect_side_len/2)} LF={flow_p} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={20} X_L={0-rect_side_len} Y_L=0.0 LF={flow_p} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={20} X_L=0.0 Y_L={0-rect_side_len} LF={flow_p} FD={filament_d} EW={e_width} LH={layer_h}
{% set g_cmd = "G1 Z" ~ layer_h %}
{ g_cmd }
#start cooling fan
{% set g_cmd = "M106 S" ~ fan_speed %}
{ g_cmd }
{% for i in range(layer_num) %}
{% set pressure_advance = (i / layer_num) * (PA_high - PA_low) + PA_low|float %}
{% set PA_msg = "SET_PRESSURE_ADVANCE ADVANCE=" ~ pressure_advance %}
{ PA_msg }
pa_line LSPEED={fspeed} X_L={rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L={0-rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={0-rect_side_len} FD={filament_d} EW={e_width} LH={layer_h}
{% set g_cmd = "G1 Z" ~ layer_h %}
{ g_cmd }
{% endfor %}
#print two finishing layers at PA_MAX
{% set pressure_advance = PA_high|float %}
{% set PA_msg = "SET_PRESSURE_ADVANCE ADVANCE=" ~ pressure_advance %}
{ PA_msg }
pa_line LSPEED={fspeed} X_L={rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L={0-rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={0-rect_side_len} FD={filament_d} EW={e_width} LH={layer_h}
{% set g_cmd = "G1 Z" ~ layer_h %}
{ g_cmd }
pa_line LSPEED={fspeed} X_L={rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={(rect_side_len/2)} FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={fspeed} X_L={0-rect_side_len} Y_L=0.0 FD={filament_d} EW={e_width} LH={layer_h}
pa_line LSPEED={sspeed} X_L=0.0 Y_L={0-rect_side_len} FD={filament_d} EW={e_width} LH={layer_h}
{% set g_cmd = "G1 Z" ~ layer_h %}
{ g_cmd }
{% set g_cmd = "G90" %}
{ g_cmd }
{% set g_cmd = end_code %}
{ g_cmd }
{% set info_str = "Bed X Length: " ~ bed_x %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Y Length: " ~ bed_y %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed X Offset: " ~ off_x %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Y Offset: " ~ off_y %}
RESPOND MSG="{info_str}"
{% set info_str = "Extrusion Width: " ~ e_width %}
RESPOND MSG="{info_str}"
{% set info_str = "Filament size: " ~ filament_d %}
RESPOND MSG="{info_str}"
{% set info_str = "Layer Height: " ~ layer_h %}
RESPOND MSG="{info_str}"
{% set info_str = "Bed Temperature: " ~ b_temp %}
RESPOND MSG="{info_str}"
{% set info_str = "Print Temperature: " ~ p_temp %}
RESPOND MSG="{info_str}"
{% set info_str = "Slow Printing Speed: " ~ sspeed %}
RESPOND MSG="{info_str}"
{% set info_str = "Fast Printing Speed: " ~ fspeed %}
RESPOND MSG="{info_str}"
{% set info_str = "Fan Speed: " ~ fan_speed %}
RESPOND MSG="{info_str}"
{% set info_str = "Length of Test Square: " ~ rect_side_len %}
RESPOND MSG="{info_str}"
{% set info_str = "Number of Layers: " ~ layer_num %}
RESPOND MSG="{info_str}"
{% set info_str = "Minimum Pressure Advance: " ~ PA_low %}
RESPOND MSG="{info_str}"
{% set info_str = "Maximum Pressure Advance: " ~ PA_high %}
RESPOND MSG="{info_str}"
{% set info_str = "Formula: Pressure Advance Value = Measured_Z_Height x " ~ (((PA_high - PA_low) / layer_num) / layer_h) ~ PA_sign ~ PA_const|abs %}
RESPOND MSG="{info_str}"
[gcode_macro pa_line]
gcode:
{% set flow_r = params.LF|default(100.0)|float %}
{% set zoomy = params.LSPEED|default(50.0)|float %}
{% set x_lin = params.X_L|default(100.0)|float %}
{% set y_lin = params.Y_L|default(100.0)|float %}
{% set f_size = params.FD|default(1.75)|float %}
{% set e_size = params.EW|default(0.4)|float %}
{% set l_size = params.LH|default(0.2)|float %}
{% set flow_n = flow_r / 100 %}
{% set lin_length = (x_lin**2 + y_lin**2)**(1/2) %}
{% set e_len = lin_length / (f_size * f_size * 3.14159 * 0.25) %}
{% set e_num = e_len * e_size * l_size * 1.67 %}
{% set g_cmd = "G1 X" ~ x_lin ~ " Y" ~ y_lin ~ " E" ~ (e_num * flow_n) ~ " F" ~ (zoomy*60) %}
{ g_cmd }
[gcode_macro LOCAL_GENERIC_PA_START ]
description: Generic startup gcode with a prime strip
gcode:
{% set b_t = params.BT|default(0.0)|float %}
{% set p_t = params.PT|default(0.0)|float %}
{% set b_x = params.BED_X_LENGTH|default(printer.toolhead.axis_maximum.x)|float %}
{% set b_y = params.BED_Y_LENGTH|default(printer.toolhead.axis_maximum.y)|float %}
{% set f_d = params.FILAMENT_DIAMETER|default(printer.configfile.settings.extruder.filament_diameter)|float %}
{% set e_w = params.EXTRUSION_WIDTH|default(printer.configfile.settings.extruder.nozzle_diameter)|float %}
{% set o_x = params.X_OFFSET|default(0.0)|float %}
{% set o_y = params.Y_OFFSET|default(0.0)|float %}
{% set l_h = params.LAYER_HEIGHT|default(0.3)|float %}
{% set g_cmd = "M140 S" ~ b_t %}
{ g_cmd }
{% set g_cmd = "M104 S" ~ p_t %}
{ g_cmd }
{% set g_cmd = "M190 S" ~ b_t %}
{ g_cmd }
{% set g_cmd = "G28" %}
{ g_cmd }
{% set g_cmd = "G1 Z10 F1500" %}
{ g_cmd }
{% set g_cmd = "M109 S" ~ p_t %}
{ g_cmd }
{% set g_cmd = "G92 E0" %}
{ g_cmd }
{% set g_cmd = "G1 Z2.0 F3000" %}
{ g_cmd }
{% set g_cmd = "G1 X0.1 Y20 Z0.3 F5000.0" %}
{ g_cmd }
{% set g_cmd = "M83" %}
{ g_cmd }
{% set g_cmd = "G91" %}
{ g_cmd }
pa_line LSPEED=25 X_L=0.0 Y_L={(b_y - 40)} FD={f_d} EW={e_w} LH={l_h}
{% set g_cmd = "G1 X0.3 Y0 F5000.0" %}
{ g_cmd }
pa_line LSPEED=25 X_L=0.0 Y_L={0-(b_y - 40)} FD={f_d} EW={e_w} LH={l_h}
{% set g_cmd = "G90" %}
{ g_cmd }
{% set g_cmd = "M82" %}
{ g_cmd }
{% set g_cmd = "G92 E0" %}
{ g_cmd }
{% set g_cmd = "G1 Z2.0 F3000" %}
{ g_cmd }
[gcode_macro LOCAL_GENERIC_PA_END ]
description: Generic ending gcode
gcode:
{% set y_done = params.EJECT|default(0.0)|float %}
{% set g_cmd = "G91" %}
{ g_cmd }
{% set g_cmd = "G1 Z10 F450" %}
{ g_cmd }
{% set g_cmd = "G90" %}
{ g_cmd }
{% set g_cmd = "M82" %}
{ g_cmd }
{% set g_cmd = "G1 X0 Y" ~ y_done %}
{ g_cmd }
{% set g_cmd = "M106 S0" %}
{ g_cmd }
{% set g_cmd = "M104 S0" %}
{ g_cmd }
{% set g_cmd = "M140 S0" %}
{ g_cmd }
{% set g_cmd = "M84" %}
{ g_cmd }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment