Skip to content

Commit

Permalink
Adaptive Mesh and Purge
Browse files Browse the repository at this point in the history
  • Loading branch information
rootiest committed Nov 25, 2022
1 parent 8d0c728 commit 4199ce8
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
100 changes: 100 additions & 0 deletions Adaptive_Mesh.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# # # Klipper Adaptive Meshing # # #

# Heads up! If you have any other BED_MESH_CALIBRATE macros defined elsewhere in your config, you will need to comment out / remove them for this to work. (Klicky/Euclid Probe)
# You will also need to be sure that [exclude_object] is defined in printer.cfg, and your slicer is labeling objects.
# This macro will parse information from objects in your gcode to define a min and max mesh area to probe, creating an adaptive mesh!
# This macro will not increase probe_count values in your [bed_mesh] config. If you want richer meshes, be sure to increase probe_count. We recommend at least 5,5.

[gcode_macro BED_MESH_CALIBRATE]
rename_existing: _BED_MESH_CALIBRATE

### This section allows control of status LEDs your printer may have.

variable_led_enable: True # Enables/disables the use of status LEDs in this macro.
variable_status_macro: 'status_meshing' # If you have status LEDs in your printer (StealthBurner), you can use the macro that changes their status here.

### This section configures mesh point fuzzing, which allows probe points to be varied slightly if printing multiples of the same G-code file.

variable_fuzz_enable: False # Enables/disables the use of mesh point fuzzing to slightly randomize probing points to spread out wear on a build surface, default is False.
variable_fuzz_min: 0 # If enabled, the minimum amount in mm a probe point can be randomized, default is 0.
variable_fuzz_max: 4 # If enabled, the maximum amount in mm a probe point can be randomized, default is 4.

### This section is for those using a dockable probe that is stored outside of the print area. ###

variable_probe_dock_enable: False # Enables/disables the use of a dockable probe that is stored outside of the print area, default is False.
variable_attach_macro: 'Attach_Probe' # Here is where you define the macro that ATTACHES the probe to the printhead. E.g. 'Attach_Probe'
variable_detach_macro: 'Dock_Probe' # Here is where you define the macro that DETACHES the probe from the printhead. E.g. 'Dock_Probe'

gcode:
{% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
{% set bed_mesh_min = printer.configfile.settings.bed_mesh.mesh_min %}
{% set bed_mesh_max = printer.configfile.settings.bed_mesh.mesh_max %}
{% set max_probe_point_distance_x = ( bed_mesh_max[0] - bed_mesh_min[0] ) / (printer.configfile.settings.bed_mesh.probe_count[0]-2) %}
{% set max_probe_point_distance_y = ( bed_mesh_max[1] - bed_mesh_min[1] ) / (printer.configfile.settings.bed_mesh.probe_count[1]-2) %}
{% set x_min = bed_mesh_min[0] %}
{% set y_min = bed_mesh_min[1] %}
{% set x_max = bed_mesh_max[0] %}
{% set y_max = bed_mesh_max[1] %}

{ action_respond_info("{} points, clamping to mesh [{!r} {!r}]".format(
all_points | count,
bed_mesh_min,
bed_mesh_max,
)) }

{% if fuzz_enable == True %}
{% if all_points %}
{% set fuzz_range = range(fuzz_min * 100 | int, fuzz_max * 100 | int) %}
{% set x_min = ( bed_mesh_min[0], ((all_points | map(attribute=0) | min - (fuzz_range | random / 100.0)) | default(bed_mesh_min[0])) ) | max %}
{% set y_min = ( bed_mesh_min[1], ((all_points | map(attribute=1) | min - (fuzz_range | random / 100.0)) | default(bed_mesh_min[1])) ) | max %}
{% set x_max = ( bed_mesh_max[0], ((all_points | map(attribute=0) | max + (fuzz_range | random / 100.0)) | default(bed_mesh_max[0])) ) | min %}
{% set y_max = ( bed_mesh_max[1], ((all_points | map(attribute=1) | max + (fuzz_range | random / 100.0)) | default(bed_mesh_max[1])) ) | min %}
{% endif %}
{% else %}
{% set x_min = [ bed_mesh_min[0], (all_points | map(attribute=0) | min | default(bed_mesh_min[0])) ] | max %}
{% set y_min = [ bed_mesh_min[1], (all_points | map(attribute=1) | min | default(bed_mesh_min[1])) ] | max %}
{% set x_max = [ bed_mesh_max[0], (all_points | map(attribute=0) | max | default(bed_mesh_max[0])) ] | min %}
{% set y_max = [ bed_mesh_max[1], (all_points | map(attribute=1) | max | default(bed_mesh_max[1])) ] | min %}
{% endif %}

{ action_respond_info("Object bounds, clamped to the bed_mesh: {!r}, {!r}".format(
(x_min, y_min),
(x_max, y_max),
)) }

{% set points_x = (((x_max - x_min) / max_probe_point_distance_x) | int) + 2 %}
{% set points_y = (((y_max - y_min) / max_probe_point_distance_y) | int) + 2 %}

{% if (([points_x, points_y]|max) > 6) %}
{% set algorithm = "bicubic" %}
{% set min_points = 4 %}
{% else %}
{% set algorithm = "lagrange" %}
{% set min_points = 3 %}
{% endif %}
{ action_respond_info( "Algorithm: {}".format(algorithm)) }

{% set points_x = [points_x, min_points]|max %}
{% set points_y = [points_y, min_points]|max %}
{ action_respond_info( "Points: x: {}, y: {}".format(points_x, points_y) ) }

{% if printer.configfile.settings.bed_mesh.relative_reference_index is defined %}
{% set ref_index = (points_x * points_y / 2) | int %}
{ action_respond_info( "Reference index: {}".format(ref_index) ) }
{% else %}
{% set ref_index = -1 %}
{% endif %}

{% if probe_dock_enable == True %}
{attach_macro} # Attach/deploy a probe if the probe is stored somewhere outside of the print area
{% endif %}

{% if led_enable == True %}
{status_macro} # Set status LEDs
{% endif %}

_BED_MESH_CALIBRATE mesh_min={x_min},{y_min} mesh_max={x_max},{y_max} ALGORITHM={algorithm} PROBE_COUNT={points_x},{points_y} RELATIVE_REFERENCE_INDEX={ref_index}

{% if probe_dock_enable == True %}
{detach_macro} # Detach/stow a probe if the probe is stored somewhere outside of the print area
{% endif %}
63 changes: 63 additions & 0 deletions Adaptive_Purge.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# # # Klipper Adaptive Purging - VoronDesign Logo # # #

# This macro will parse information from objects in your gcode to define a min and max area, creating a nearby purge with Voron flair!
# For successful purging, you may need to configure:
#
# [extruder]
# max_extrude_cross_section: 5


[gcode_macro ADAPTIVE_PURGE]
description: A purge macro that adapts to be near your actual printed objects

variable_adaptive_enable: True # Change to False if you'd like the purge to be in the same spot every print
variable_z_height: 0.4 # Height above the bed to purge
variable_tip_distance: 10 # Distance between filament tip and nozzle before purge (this will require some tuning)
variable_purge_amount: 40 # Amount of filament to purge
variable_flow_rate: 10 # Desired flow rate in mm3/s
variable_x_default: 10 # X location to purge, overwritten if adaptive is True
variable_y_default: 10 # Y location to purge, overwritten if adaptive is True
variable_size: 10 # Size of the logo
variable_distance_to_object_x: 15 # Distance in x to the print area
variable_distance_to_object_y: 0 # Distance in y to the print area
variable_travel_speed: 300 # Travel speed

gcode:
{% if adaptive_enable == True %}
{% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
{% set x_origin = (all_points | map(attribute=0) | min | default(x_default + distance_to_object_x + size)) - distance_to_object_x - size %}
{% set y_origin = (all_points | map(attribute=1) | min | default(y_default + distance_to_object_y + size)) - distance_to_object_y - size %}
{% set x_origin = ([x_origin, 0] | max) %}
{% set y_origin = ([y_origin, 0] | max) %}
{% else %}
{% set x_origin = x_default | float %}
{% set y_origin = y_default | float %}
{% endif %}
{% set purge_move_speed = 2.31 * size * flow_rate / (purge_amount * 2.405) %}
{% set prepurge_speed = flow_rate / 2.405 %}
{ action_respond_info( "x: " + x_origin|string + " y: " + y_origin|string + " purge_move_speed: " + purge_move_speed|string + " prepurge_speed: " + prepurge_speed|string ) }

STATUS_CLEANING
G92 E0
G0 F{travel_speed*60} # Set travel speed
G90 # Absolute positioning
G0 X{x_origin} Y{y_origin+size/2} # Move to purge position
G0 Z{z_height} # Move to purge Z height
M83 # Relative extrusion mode
G1 E{tip_distance} F{prepurge_speed*60} # Move tip of filament to nozzle
G1 X{x_origin+size*0.289} Y{y_origin+size} E{purge_amount/4} F{purge_move_speed*60} # Purge first line of logo
G1 E-.5 F2100 # Retract
G0 Z{z_height*2} # Z hop
G0 X{x_origin+size*0.789} Y{y_origin+size} # Move to second purge line origin
G0 Z{z_height} # Move to purge Z height
G1 E.5 F2100 # Recover
G1 X{x_origin+size*0.211} Y{y_origin} E{purge_amount/2} F{purge_move_speed*60} # Purge second line of logo
G1 E-.5 F2100 # Retract
G0 Z{z_height*2} # Z hop
G0 X{x_origin+size*0.711} Y{y_origin} # Move to third purge line origin
G0 Z{z_height} # Move to purge Z height
G1 E.5 F2100 # Recover
G1 X{x_origin+size} Y{y_origin+size/2} E{purge_amount/4} F{purge_move_speed*60} # Purge third line of logo
G1 E-.5 F2100 # Retract
G92 E0 # Reset extruder distance
G0 Z{z_height*2} # Z hop
File renamed without changes.
2 changes: 1 addition & 1 deletion machine/tool-board.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ full_steps_per_rotation: 200
gear_ratio: 50:10
nozzle_diameter: 0.400
filament_diameter: 1.750
max_extrude_cross_section: 110
max_extrude_cross_section: 5
max_extrude_only_distance: 1100
min_extrude_temp: 180
pressure_advance = 0.035
Expand Down
4 changes: 4 additions & 0 deletions machine/zippy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
# Timelapse
[include ../timelapse.cfg]

# Adaptive mesh and purge
[include ../Adaptive_Mesh.cfg]
[include ../Adaptive_Purge.cfg]

[esp32_webscale]
ip: 192.168.86.25
9 changes: 5 additions & 4 deletions macros/START_PRINT.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ gcode:
{% set FILTER = params.FILTER|default(0)|int %}

# Enable drawing wipe line
{% set WIPE = params.WIPE|default(1)|int %}
{% set WIPE = params.WIPE|default(0)|int %}

# Save print configuration specs for later use
SET_GCODE_VARIABLE MACRO=START_PRINT VARIABLE=x_max VALUE={X_MAX}
Expand Down Expand Up @@ -224,8 +224,8 @@ gcode:
STATUS_CALIBRATING_Z
Z_TILT_ADJUST ; adjust z-tilt
M117 Mesh print area
STATUS_MESHING
BED_MESH_CALIBRATE PRINT_MIN={params.PRINT_MIN} PRINT_MAX={params.PRINT_MAX}
#BED_MESH_CALIBRATE PRINT_MIN={params.PRINT_MIN} PRINT_MAX={params.PRINT_MAX}
BED_MESH_CALIBRATE
M117 Preheating tool
STATUS_HEATING
M104 S{EXTRUDER_TEMP} ; set final nozzle temp
Expand All @@ -240,10 +240,11 @@ gcode:

PRINT_START_TUNE ; audio feedback

STATUS_CLEANING
{% if WIPE == 1 %}
STATUS_CLEANING
WIPE_LINE ; Draw wipe line
{% endif %}
ADAPTIVE_PURGE
M117 Print Started
{% else %}
M117 Sequence: {SEQUENCE_NUM} of {SEQUENTIAL}
Expand Down

0 comments on commit 4199ce8

Please sign in to comment.