Skip to content

Commit

Permalink
bed_mesh: Fix config issue (jschuh#12)
Browse files Browse the repository at this point in the history
 * Ensure we don't call BED_MESH_CALIBRATE with unbounded params
 * Make fast mesh level the default in START_PRINT
 * Update bed mesh documentation
  • Loading branch information
jschuh committed Oct 7, 2022
1 parent 7f65268 commit 72eae8d
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 73 deletions.
53 changes: 35 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ with g-code targeting Marlin printers. However, there are also some nice extras:
provided a much easier way to customize materials in the LCD menu (or at least
I think so). I've also added confirmation dialogs for commands that would
abort an active print.
* **[Optimized mesh bed leveling](#bed-mesh)** - Probes only within the printed
area, which can save a lot of time on smaller prints.
* **[Optimized mesh bed leveling](#bed-mesh-fast)** - Probes only within the
printed area, which can save a lot of time on smaller prints.

## A few warnings...

Expand Down Expand Up @@ -245,6 +245,27 @@ All features are configured by setting `variable_` values in the
`[gcode_macro _km_options]` section. All available variables and their purpose
are listed in [globals.cfg](globals.cfg#L5).

### Bed Mesh Fast

`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.

The following additional configuration options are available from
[globals.cfg](globals.cfg#L5).

* `variable_probe_mesh_padding` - Extra padding around the rectangle defined by
`MESH_MIN` and `MESH_MAX`.
* `variable_probe_min_count` - Minimum number of probes for partial probing of a
bed mesh.
* `variable_probe_count_scale` - Scaling factor to increase probe count for
partial bed probes.

> **Note:** See the [optional section](#bed-mesh) for additional macros.
### Bed Surface

Provides a set of macros for selecting different bed surfaces with
Expand Down Expand Up @@ -568,24 +589,16 @@ related commands, such as accelleration, jerk, and linear advance.

### Bed Mesh

`BED_MESH_CALIBRATE`
`BED_MESH_CALIBRATE` and `G20`

Wraps the equivalent Klipper 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.
Overrides the default `BED_MESH_CALIBRATE` to use `BED_MESH_CALIBRATE_FAST`
instead, and adds the `G20` command.

The following additional configuration options are available from
[globals.cfg](globals.cfg#L5).

* `variable_probe_mesh_padding` - Extra padding around the rectangle defined by
`MESH_MIN` and `MESH_MAX`.
* `variable_probe_min_count` - Minimum number of probes for partial probing of a
bed mesh.
* `variable_probe_count_scale` - Scaling factor to increase probe count for
partial bed probes.
***Configuration:***

***Configuration:** `[include klipper-macros/optional/bed_mesh.cfg]`*
```
[include klipper-macros/optional/bed_mesh.cfg]
```

***Requirements:** A properly configured `bed_mesh` section.*

Expand Down Expand Up @@ -615,6 +628,10 @@ information on how to configure specific behaviors.
(via `variable_menu_show_octoprint` and `variable_menu_show_sdcard`,
respectively).

***Configuration:** `[include klipper-macros/optional/lcd_menus.cfg]`*
***Configuration:***

```
[include klipper-macros/optional/lcd_menus.cfg]
```

***Requirements:** A properly configured `display` section.*
62 changes: 62 additions & 0 deletions bed_mesh_fast.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[gcode_macro bed_mesh_calibrate_fast]
description: Wraps BED_MESH_CALIBRATE, scaling probe count to specified area.
Usage: See Klipper documentation.
gcode:
{% set km = printer["gcode_macro _km_globals"] %}
{% set probe_mesh_padding = km.probe_mesh_padding %}
{% set probe_min_count = km.probe_min_count %}
{% set probe_count_scale = km.probe_count_scale %}
{% set bed_mesh = printer.configfile.config.bed_mesh %}

# don't have the math functions available to work on a delta bed.
{%if "mesh_radius" not in bed_mesh %}
{% set safe_min_x = bed_mesh.mesh_min.split(",")[0]|float %}
{% set safe_min_y = bed_mesh.mesh_min.split(",")[1]|float %}
{% set safe_max_x = bed_mesh.mesh_max.split(",")[0]|float %}
{% set safe_max_y = bed_mesh.mesh_max.split(",")[1]|float %}

{% if "MESH_MIN" in params %}
{% set mesh_min_x = (params.MESH_MIN.split(",")[0]|float -
probe_mesh_padding, safe_min_x)|max %}
{% set mesh_min_y = (params.MESH_MIN.split(",")[1]|float -
probe_mesh_padding, safe_min_y)|max %}
{% else %}
{% set mesh_min_x = safe_min_x %}
{% set mesh_min_y = safe_min_y %}
{% endif %}
{% if "MESH_MAX" in params %}
{% set mesh_max_x = (params.MESH_MAX.split(",")[0]|float +
probe_mesh_padding, safe_max_x)|min %}
{% set mesh_max_y = (params.MESH_MAX.split(",")[1]|float +
probe_mesh_padding, safe_max_y)|min %}
{% else %}
{% set mesh_max_x = safe_max_x %}
{% set mesh_max_y = safe_max_y %}
{% endif %}

{% set probe_count = (params.PROBE_COUNT |
default(bed_mesh.probe_count)).split(",") %}
{% set max_x_probes = probe_count[0]|int %}
{% set max_y_probes = probe_count[1]|default(max_x_probes)|int %}

{% set x_probes = (max_x_probes * (mesh_max_x - mesh_min_x) /
(safe_max_x - safe_min_x) * probe_count_scale)
| round(0) | int %}
{% set x_probes = ((x_probes, probe_min_count)|max, max_x_probes)|min %}

{% set y_probes = (max_y_probes * (mesh_max_y - mesh_min_y ) /
(safe_max_y - safe_min_y) * probe_count_scale )
| round(0) | int %}
{% set y_probes = ((y_probes, probe_min_count)|max, max_y_probes)|min %}

{% 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", x_probes~","~y_probes) %}
{% endif %}
{% if printer["gcode_macro bed_mesh_calibrate"].km_override|default(False) %}
{% set calibrate_cmd = "_km_bed_mesh_calibrate_base" %}
{% else %}
{% set calibrate_cmd = "BED_MESH_CALIBRATE" %}
{% endif %}

{calibrate_cmd}{%for k in params%}{' '~k~'="'~params[k]~'"'}{%endfor%}
55 changes: 2 additions & 53 deletions optional/bed_mesh.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,11 @@

[gcode_macro bed_mesh_calibrate]
rename_existing: _KM_BED_MESH_CALIBRATE_BASE
variable_km_override: True
description: Wraps BED_MESH_CALIBRATE, scaling probe count to specified area.
Usage: See Klipper documentation.
gcode:
{% set km = printer["gcode_macro _km_globals"] %}
{% set probe_mesh_padding = km.probe_mesh_padding %}
{% set probe_min_count = km.probe_min_count %}
{% set probe_count_scale = km.probe_count_scale %}
{% set bed_mesh = printer.configfile.config.bed_mesh %}

# don't have the math functions available to work on a delta bed.
{%if "mesh_radius" not in bed_mesh %}
{% set safe_min_x = bed_mesh.mesh_min.split(",")[0]|float %}
{% set safe_min_y = bed_mesh.mesh_min.split(",")[1]|float %}
{% set safe_max_x = bed_mesh.mesh_max.split(",")[0]|float %}
{% set safe_max_y = bed_mesh.mesh_max.split(",")[1]|float %}

{% if "MESH_MIN" in params %}
{% set mesh_min_x = (params.MESH_MIN.split(",")[0]|float -
probe_mesh_padding, safe_min_x)|max %}
{% set mesh_min_y = (params.MESH_MIN.split(",")[1]|float -
probe_mesh_padding, safe_min_y)|max %}
{% else %}
{% set mesh_min_x = safe_min_x %}
{% set mesh_min_y = safe_min_y %}
{% endif %}
{% if "MESH_MAX" in params %}
{% set mesh_max_x = (params.MESH_MAX.split(",")[0]|float +
probe_mesh_padding, safe_max_x)|min %}
{% set mesh_max_y = (params.MESH_MAX.split(",")[1]|float +
probe_mesh_padding, safe_max_y)|min %}
{% else %}
{% set mesh_max_x = safe_max_x %}
{% set mesh_max_y = safe_max_y %}
{% endif %}

{% set probe_count = (params.PROBE_COUNT |
default(bed_mesh.probe_count)).split(",") %}
{% set max_x_probes = probe_count[0]|int %}
{% set max_y_probes = probe_count[1]|default(max_x_probes)|int %}

{% set x_probes = (max_x_probes * (mesh_max_x - mesh_min_x) /
(safe_max_x - safe_min_x) * probe_count_scale)
| round(0) | int %}
{% set x_probes = ((x_probes, probe_min_count)|max, max_x_probes)|min %}

{% set y_probes = (max_y_probes * (mesh_max_y - mesh_min_y ) /
(safe_max_y - safe_min_y) * probe_count_scale )
| round(0) | int %}
{% set y_probes = ((y_probes, probe_min_count)|max, max_y_probes)|min %}

{% 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", x_probes~","~y_probes) %}
{% endif %}
_km_bed_mesh_calibrate_base{%for k in params%}{
' '~k~'="'~params[k]~'"'}{%endfor%}
BED_MESH_CALIBRATE_FAST{%for k in params%}{' '~k~'="'~params[k]~'"'}{%endfor%}

[gcode_macro g29]
gcode:
Expand Down
4 changes: 2 additions & 2 deletions start_end.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ gcode:
{% if km.start_level_bed_at_temp %}
M104 S{EXTRUDER} # set the final extruder target temperature
G28 Z # Re-home only the Z axis now that the bed has stabilized.
BED_MESH_CALIBRATE{% if MESH_MIN %} MESH_MIN={MESH_MIN}{% endif
%}{% if MESH_MAX %} MESH_MAX={MESH_MAX}{% endif %}
BED_MESH_CALIBRATE_FAST{% if MESH_MIN %} MESH_MIN={MESH_MIN}{% endif
%}{% if MESH_MAX %} MESH_MAX={MESH_MAX}{% endif %}
PARK
{% endif %}
{% if CHAMBER > 0.0 %}
Expand Down

0 comments on commit 72eae8d

Please sign in to comment.