Skip to content

Instantly share code, notes, and snippets.

@dderg
Forked from ChipCE/readme.md
Last active April 2, 2023 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dderg/62489361cf2e88d0aaa057fedee4b215 to your computer and use it in GitHub Desktop.
Save dderg/62489361cf2e88d0aaa057fedee4b215 to your computer and use it in GitHub Desktop.
Klipper bed mesh on print area only macro install guide

Klipper mesh on print area only install guide

Compared to original script this one relies on exclude_object data. Thus no additional starting g-code parameters are needed.

What this macro do

  • This macro will dynamically changing the bed mesh area based on the size of the parts will be printed. The fw will only probe on the area that the part will be printed (plus mesh_area_offset value).

Setup guide

  • (1) Add the following macrro to your printer config, this will replace the default BED_MESH_CALIBRATE command.
[gcode_macro BED_MESH_CALIBRATE]
rename_existing: BED_MESH_CALIBRATE_BASE
; gcode parameters
variable_parameter_AREA_START : 0,0
variable_parameter_AREA_END : 0,0
; the clearance between print area and probe area 
variable_mesh_area_offset : 5.0
; number of sample per probe point
variable_probe_samples : 2
; minimum probe count
variable_min_probe_count : 4
; scale up the probe count, should be 1.0 ~ < variable_max_probe_count/variable_min_probe_count
variable_probe_count_scale_factor : 1.0
; enable preference index
variable_enable_reference_index : False
gcode:
  {% set all_points = printer.exclude_object.objects | map(attribute='polygon') | sum(start=[]) %}
  {% set bedMeshConfig = printer["configfile"].config["bed_mesh"] %}
  {% set safe_min_x = bedMeshConfig.mesh_min.split(",")[0]|float %}
  {% set safe_min_y = bedMeshConfig.mesh_min.split(",")[1]|float %}
  {% set safe_max_x = bedMeshConfig.mesh_max.split(",")[0]|float %}
  {% set safe_max_y = bedMeshConfig.mesh_max.split(",")[1]|float %}

  {% set area_min_x = (all_points | map(attribute=0) | min | default(safe_min_x + mesh_area_offset)) - mesh_area_offset %}
  {% set area_min_y = (all_points | map(attribute=1) | min | default(safe_min_y + mesh_area_offset)) - mesh_area_offset %}
  {% set area_max_x = (all_points | map(attribute=0) | max | default(safe_max_x + mesh_area_offset)) - mesh_area_offset %}
  {% set area_max_y = (all_points | map(attribute=1) | max | default(safe_max_y + mesh_area_offset)) - mesh_area_offset %}
  {% set area_min_x = ([area_min_x, safe_min_x] | max) %}
  {% set area_min_y = ([area_min_y, safe_min_y] | max) %}
  {% set area_max_x = ([area_max_x, safe_max_x] | min) %}
  {% set area_max_y = ([area_max_y, safe_max_y] | min) %}


  {% if bedMeshConfig.probe_count.split(",")|length == 2 %}
      {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
      {% set meshPointY = bedMeshConfig.probe_count.split(",")[1]|int %}
  {% else %}
      {% set meshPointX = bedMeshConfig.probe_count.split(",")[0]|int %}
      {% set meshPointY = bedMeshConfig.probe_count.split(",")[0]|int %}
  {% endif %}

  {% set meshMaxPointX = meshPointX %}
  {% set meshMaxPointY = meshPointY %}


  {% if (area_min_x < area_max_x) and (area_min_y < area_max_y) %}
      {% set meshPointX = (meshPointX * (area_max_x - area_min_x) / (safe_max_x - safe_min_x) * probe_count_scale_factor)|round(0)|int %}
      {% if meshPointX < min_probe_count %}
          {% set meshPointX = min_probe_count %}
      {% endif %}
  {% if meshPointX > meshMaxPointX %}
          {% set meshPointX = meshMaxPointX %}
      {% endif %}

      {% set meshPointY = (meshPointY * (area_max_y -area_min_y ) / (safe_max_y - safe_min_y) * probe_count_scale_factor )|round(0)|int %}
      {% if meshPointY < min_probe_count %}
          {% set meshPointY = min_probe_count %}
      {% endif %}
  {% if meshPointY > meshMaxPointY %}
          {% set meshPointY = meshMaxPointY %}
      {% endif %}

      {% set algorithm = "bicubic" %}
  {% if "algorithm" in bedMeshConfig %}
      {% set algorithm = bedMeshConfig.algorithm %}
      {% endif %}
      {% if meshPointX >=7 or meshPointY >=7 %}
          {% set algorithm = "bicubic" %}
      {% endif %}

      {% if enable_reference_index %}
          {% set referenceIndex = (meshPointX * meshPointY / 2 - 1 )|round(0)|int %}
          BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm} relative_reference_index={referenceIndex}
      {% else %}
          BED_MESH_CALIBRATE_BASE mesh_min={area_min_x},{area_min_y} mesh_max={area_max_x},{area_max_y} probe_count={meshPointX},{meshPointY} samples={probe_samples|int} algorithm={algorithm}
      {% endif %}
  {% else %}
      BED_MESH_CALIBRATE_BASE
  {% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment