Skip to content

Commit

Permalink
bed_mesh: add ZFADE parameter to BED_MESH_OFFSET
Browse files Browse the repository at this point in the history
When a ZFADE value is passed to BED_MESH_OFFSET it is used
to adjust how fade is applied.   This resolves issues with
fade when SET_GCODE_OFFSET is used during a tool change.

Signed-off-by:  Eric Callahan <[email protected]>
  • Loading branch information
Arksine authored and KevinOConnor committed Feb 15, 2024
1 parent 1b24f6a commit 0cd16e9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions klippy/extras/bed_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self, config):
self.log_fade_complete = False
self.base_fade_target = config.getfloat('fade_target', None)
self.fade_target = 0.
self.tool_offset = 0.
self.gcode = self.printer.lookup_object('gcode')
self.splitter = MoveSplitter(config, self.gcode)
# setup persistent storage
Expand Down Expand Up @@ -157,13 +158,15 @@ def set_mesh(self, mesh):
"mesh max: %.4f" % (self.fade_dist, min_z, max_z))
else:
self.fade_target = 0.
self.tool_offset = 0.
self.z_mesh = mesh
self.splitter.initialize(mesh, self.fade_target)
# cache the current position before a transform takes place
gcode_move = self.printer.lookup_object('gcode_move')
gcode_move.reset_last_position()
self.update_status()
def get_z_factor(self, z_pos):
z_pos += self.tool_offset
if z_pos >= self.fade_end:
return 0.
elif z_pos >= self.fade_start:
Expand All @@ -182,14 +185,15 @@ def get_position(self):
max_adj = self.z_mesh.calc_z(x, y)
factor = 1.
z_adj = max_adj - self.fade_target
if min(z, (z - max_adj)) >= self.fade_end:
fade_z_pos = z + self.tool_offset
if min(fade_z_pos, (fade_z_pos - max_adj)) >= self.fade_end:
# Fade out is complete, no factor
factor = 0.
elif max(z, (z - max_adj)) >= self.fade_start:
elif max(fade_z_pos, (fade_z_pos - max_adj)) >= self.fade_start:
# Likely in the process of fading out adjustment.
# Because we don't yet know the gcode z position, use
# algebra to calculate the factor from the toolhead pos
factor = ((self.fade_end + self.fade_target - z) /
factor = ((self.fade_end + self.fade_target - fade_z_pos) /
(self.fade_dist - z_adj))
factor = constrain(factor, 0., 1.)
final_z_adj = factor * z_adj + self.fade_target
Expand Down Expand Up @@ -271,6 +275,9 @@ def cmd_BED_MESH_OFFSET(self, gcmd):
for i, axis in enumerate(['X', 'Y']):
offsets[i] = gcmd.get_float(axis, None)
self.z_mesh.set_mesh_offsets(offsets)
tool_offset = gcmd.get_float("ZFADE", None)
if tool_offset is not None:
self.tool_offset = tool_offset
gcode_move = self.printer.lookup_object('gcode_move')
gcode_move.reset_last_position()
else:
Expand Down

0 comments on commit 0cd16e9

Please sign in to comment.