Skip to content

Commit

Permalink
bed_mash_fast: Support relative_reference_index
Browse files Browse the repository at this point in the history
Uses the closest point in the optimized mesh.
  • Loading branch information
jschuh committed May 11, 2023
1 parent a5918ab commit 99bc290
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,11 @@ are listed in [globals.cfg](globals.cfg#L5).
`BED_MESH_CALIBRATE_FAST`

Wraps the Klipper `BED_MESH_CALIBRATE` command to scale and redistribute the
probe points so that only the appropriate area in `MESH_MIN` and `MESH_MAX` is probed. This can dramatically reduce probing times for anything that doesn't
fill the first layer of the bed. `PRINT_START` will automatically use this for
bed mesh calibration if a `[bed_mesh]` section is detected in your config.
probe points so that only the appropriate area in `MESH_MIN` and `MESH_MAX` is
probed. This can dramatically reduce probing times for anything that doesn't
fill the first layer of the bed. If the `MESH_MIN` and `MESH_MAX` arguments
are provided to `PRINT_START` it will automatically use this for bed mesh
calibration (so long as a `[bed_mesh]` section is detected in your config).

The following additional configuration options are available from
[globals.cfg](globals.cfg#L5).
Expand All @@ -437,10 +439,14 @@ The following additional configuration options are available from

> **Note:** See the [optional section](#bed-mesh) for additional macros.
> **Note:** The bed mesh optimizations are silently disabled for delta printers
and when the mesh parameters include a [`RELATIVE_REFERENCE_INDEX`
](https://www.klipper3d.org/Bed_Mesh.html#the-relative-reference-index)
(which is icnompatible with dynamic mesh generation).
> **Note:** The bed mesh optimizations are silently disabled for delta printers.
> **Note:** If the `bed_mesh` config includes a [`relative_reference_index`
](https://www.klipper3d.org/Bed_Mesh.html#the-relative-reference-index) then
the index point selected in the optimized mesh will be the point closest to
the index point in the mesh from the config. However, if a
`RELATIVE_REFERENCE_INDEX` parameter is supplied directly to this macro it
will be used unmodified.

`BED_MESH_CHECK`

Expand Down
57 changes: 52 additions & 5 deletions bed_mesh_fast.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ gcode:
{% set probe_count_scale = km.probe_count_scale %}
{% set bed_mesh = printer.configfile.settings.bed_mesh %}

# Don't want to duplicate all the math for a delta bed, and still thinking up
# a good strategy for relative reference index.
# TODO: Handle the math for a delta bed.
{%if "mesh_radius" not in bed_mesh and
"MESH_RADIUS" not in params and
"relative_reference_index" not in bed_mesh and
"RELATIVE_REFERENCE_INDEX" not in params %}
"MESH_RADIUS" not in params %}
{% set safe_min_x = bed_mesh.mesh_min[0] %}
{% set safe_min_y = bed_mesh.mesh_min[1] %}
{% set safe_max_x = bed_mesh.mesh_max[0] %}
Expand Down Expand Up @@ -77,6 +74,56 @@ gcode:
{% set dummy = probe_count.append(probe_count[0]) %}
{% endif %}

# If the config includes a relative_reference_index then we need to find the
# point in the new mesh that's closest to the index point in the mesh that
# the config would have generated.
# TODO: Could also adjust the mesh parameters in here to ensure it includes
# the original index point, but that would be extra work and would cause
# slower probes if the mesh needs to be expanded to include the point.
{% if "relative_reference_index" in bed_mesh %}
{% set row = (bed_mesh.relative_reference_index / bed_mesh.probe_count[0]
)|int%}
{% set rrf_x = (((safe_max_x - safe_min_x) /
(bed_mesh.probe_count[0] - 1))|round(2, 'floor')) *
(bed_mesh.relative_reference_index %
bed_mesh.probe_count[-1]) %}
{% if row % 2 %}
{% set rrf_x = safe_max_x - rrf_x %}
{% else %}
{% set rrf_x = safe_min_x + rrf_x %}
{% endif %}
{% set rrf_y = (((safe_max_y - safe_min_y) /
(bed_mesh.probe_count[-1] - 1))|round(2, 'floor')) *
row + safe_min_x %}
{% set x_dist = (mesh_max_x - mesh_min_x) / (probe_count[0] - 1) %}
{% set y_dist = (mesh_max_y - mesh_min_y) / (probe_count[1] - 1) %}
{% set rrf = {'x':0, 'y':0, 'dist':safe_max_x**2+safe_max_y**2,'pos':0} %}
{% for row in range(probe_count[1])%}
{% for col in range(probe_count[0])%}
{% if row % 2 %}
{% set x = mesh_max_x - col * x_dist %}
{% else %}
{% set x = mesh_min_x + col * x_dist %}
{% endif %}
{% set y = mesh_min_y + row * y_dist %}
{% set dist = ((x - rrf_x)**2 + (y - rrf_y)**2)**0.5 %}
{% if dist < rrf.dist %}
{% set dummy = rrf.__setitem__("dist", dist) %}
{% set dummy = rrf.__setitem__("x", x) %}
{% set dummy = rrf.__setitem__("y", y) %}
{% set dummy = rrf.__setitem__("pos", row * probe_count[1] + col) %}
{% endif %}
{% endfor %}
{% endfor %}
{% if rrf.x != rrf_x or rrf.y != rrf_y %}
{action_respond_info("relative_reference_index remapped to"
" %d (%.2f,%.2f) from %d (%.2f,%.2f)" %
(rrf.pos, rrf.x, rrf.y,
bed_mesh.relative_reference_index, rrf_x, rrf_y))}
{% endif %}
{% set dummy = params.__setitem__("RELATIVE_REFERENCE_INDEX", rrf.pos) %}
{% endif %}

{% set dummy = params.__setitem__("MESH_MIN", mesh_min_x~","~mesh_min_y) %}
{% set dummy = params.__setitem__("MESH_MAX", mesh_max_x~","~mesh_max_y) %}
{% set dummy = params.__setitem__("PROBE_COUNT", probe_count|join(',')) %}
Expand Down

0 comments on commit 99bc290

Please sign in to comment.