Skip to content

Commit

Permalink
bed_mesh: Support manipulation of meshes
Browse files Browse the repository at this point in the history
Adds an extra SET parameter option to BED_MESH_PROFILE
to specify the mesh as a parameter (string form of the
dict describing the mesh).
In addition status printer.bed_mesh.profiles is added
to expose the dict of profiles defined in the module
currently in the same format as SET (after conversion
to a string) can accept.

Main goal of these changes are to allow manipulation
of the meshes from macros, to also allow storing the
meshes without needing a reset, ex. stored in a
saved variable.
  • Loading branch information
Misterke committed Feb 7, 2022
1 parent 6d7c033 commit 2fa1f3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
19 changes: 11 additions & 8 deletions docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ generate height maps approximating the bed's surface.
adjustment. It is recommended to put this in your end-gcode.

#### BED_MESH_PROFILE
`BED_MESH_PROFILE LOAD=<name> SAVE=<name> REMOVE=<name>`: This command
provides profile management for mesh state. LOAD will restore the mesh
state from the profile matching the supplied name. SAVE will save the
current mesh state to a profile matching the supplied name. Remove
will delete the profile matching the supplied name from persistent
memory. Note that after SAVE or REMOVE operations have been run the
SAVE_CONFIG gcode must be run to make the changes to persistent memory
permanent.
`BED_MESH_PROFILE LOAD=<name> SAVE=<name> REMOVE=<name> SET=<def>`:
This command provides profile management for mesh state. LOAD will
restore the mesh state from the profile matching the supplied
name. SAVE will save the current mesh state to a profile matching the
supplied name. REMOVE will delete the profile matching the supplied
name from persistent memory. Note that after SAVE or REMOVE operations
have been run the SAVE_CONFIG gcode must be run to make the changes to
persistent memory permanent. SET allows setting the mesh by passing
a string-representation of the dict that defines the mesh. This is
the same dict as found in printer.bed_mesh.profiles for each of the
currently defined profiles.

#### BED_MESH_OFFSET
`BED_MESH_OFFSET [X=<value>] [Y=<value>]`: Applies X and/or Y offsets
Expand Down
2 changes: 2 additions & 0 deletions docs/Status_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The following information is available in the
[bed_mesh](Config_Reference.md#bed_mesh) object:
- `profile_name`, `mesh_min`, `mesh_max`, `probed_matrix`,
`mesh_matrix`: Information on the currently active bed_mesh.
- `profiles`: The set of currently defined profiles as setup
using BED_MESH_PROFILE.

## configfile

Expand Down
36 changes: 34 additions & 2 deletions klippy/extras/bed_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright (C) 2018-2019 Eric Callahan <arksine.code@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging, math, json, collections
import logging, math, json, ast, collections, traceback
from . import probe

PROFILE_VERSION = 1
Expand Down Expand Up @@ -221,7 +221,8 @@ def get_status(self, eventtime=None):
"mesh_min": (0., 0.),
"mesh_max": (0., 0.),
"probed_matrix": [[]],
"mesh_matrix": [[]]
"mesh_matrix": [[]],
"profiles": self.pmgr.get_profiles()
}
if self.z_mesh is not None:
params = self.z_mesh.get_mesh_params()
Expand Down Expand Up @@ -1134,6 +1135,13 @@ def initialize(self):
self._check_incompatible_profiles()
if "default" in self.profiles:
self.load_profile("default")
def get_profiles(self):
result = dict()
for name in self.profiles:
profile = dict(self.profiles[name])
profile["mesh_params"] = dict(profile["mesh_params"])
result[name] = profile
return result
def get_current_profile(self):
return self.current_profile
def _check_incompatible_profiles(self):
Expand Down Expand Up @@ -1179,6 +1187,29 @@ def save_profile(self, prof_name):
"for the current session. The SAVE_CONFIG command will\n"
"update the printer config file and restart the printer."
% (prof_name))
def set_profile(self, profile):
try:
profile = ast.literal_eval(profile)
probed_matrix = profile["points"]
mesh_params = profile["mesh_params"];
for key, t in PROFILE_OPTIONS.items():
if t is int:
mesh_params[key] = int(mesh_params[key])
elif t is float:
mesh_params[key] = float(mesh_params[key])
elif t is str:
mesh_params[key] = str(mesh_params[key])
z_mesh = ZMesh(mesh_params)
try:
z_mesh.build_mesh(probed_matrix)
except BedMeshError as e:
raise self.gcode.error(str(e))
self.current_profile = "default"
self.bedmesh.set_mesh(z_mesh)
self.gcode.respond_info(
"Bed Mesh state set to profile \"default\".")
except:
raise self.gcode.error(traceback.format_exc())
def load_profile(self, prof_name):
profile = self.profiles.get(prof_name, None)
if profile is None:
Expand Down Expand Up @@ -1208,6 +1239,7 @@ def remove_profile(self, prof_name):
cmd_BED_MESH_PROFILE_help = "Bed Mesh Persistent Storage management"
def cmd_BED_MESH_PROFILE(self, gcmd):
options = collections.OrderedDict({
'SET': self.set_profile,
'LOAD': self.load_profile,
'SAVE': self.save_profile,
'REMOVE': self.remove_profile
Expand Down

0 comments on commit 2fa1f3e

Please sign in to comment.