From 3fcce4bb4b2087d975ff8e877afb111585e2bcfc Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Thu, 3 Jun 2021 14:16:43 -0400 Subject: [PATCH] bed_mesh: implement BED_MESH_OFFSET The BED_MESH_OFFSET gcode can be used to apply X and/or Y offsets the the mesh lookup. This allows printers with independent extruders to correct the Z adjustment when changing a tool. Signed-off-by: Eric Callahan --- klippy/extras/bed_mesh.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/klippy/extras/bed_mesh.py b/klippy/extras/bed_mesh.py index 4a2a3b61..d513db8c 100644 --- a/klippy/extras/bed_mesh.py +++ b/klippy/extras/bed_mesh.py @@ -93,6 +93,9 @@ class BedMesh: self.gcode.register_command( 'BED_MESH_CLEAR', self.cmd_BED_MESH_CLEAR, desc=self.cmd_BED_MESH_CLEAR_help) + self.gcode.register_command( + 'BED_MESH_OFFSET', self.cmd_BED_MESH_OFFSET, + desc=self.cmd_BED_MESH_OFFSET_help) # Register transform gcode_move = self.printer.load_object(config, 'gcode_move') gcode_move.set_move_transform(self) @@ -230,9 +233,20 @@ class BedMesh: gcmd.respond_raw("mesh_map_output " + json.dumps(outdict)) else: gcmd.respond_info("Bed has not been probed") - cmd_BED_MESH_CLEAR_help = "Clear the Mesh so no z-adjusment is made" + cmd_BED_MESH_CLEAR_help = "Clear the Mesh so no z-adjustment is made" def cmd_BED_MESH_CLEAR(self, gcmd): self.set_mesh(None) + cmd_BED_MESH_OFFSET_help = "Add X/Y offsets to the mesh lookup" + def cmd_BED_MESH_OFFSET(self, gcmd): + if self.z_mesh is not None: + offsets = [None, None] + for i, axis in enumerate(['X', 'Y']): + offsets[i] = gcmd.get_float(axis, None) + self.z_mesh.set_mesh_offsets(offsets) + gcode_move = self.printer.lookup_object('gcode_move') + gcode_move.reset_last_position() + else: + gcmd.respond_info("No mesh loaded to offset") class BedMeshCalibrate: @@ -767,6 +781,7 @@ class ZMesh: self.probed_matrix = self.mesh_matrix = None self.mesh_params = params self.avg_z = 0. + self.mesh_offsets = [0., 0.] logging.debug('bed_mesh: probe/mesh parameters:') for key, value in self.mesh_params.items(): logging.debug("%s : %s" % (key, value)) @@ -828,6 +843,8 @@ class ZMesh: msg = "Mesh X,Y: %d,%d\n" % (self.mesh_x_count, self.mesh_y_count) if move_z is not None: msg += "Search Height: %d\n" % (move_z) + msg += "Mesh Offsets: X=%.4f, Y=%.4f\n" % ( + self.mesh_offsets[0], self.mesh_offsets[1]) msg += "Mesh Average: %.2f\n" % (self.avg_z) rng = self.get_z_range() msg += "Mesh Range: min=%.4f max=%.4f\n" % (rng[0], rng[1]) @@ -851,6 +868,10 @@ class ZMesh: # z step distances self.avg_z = round(self.avg_z, 2) self.print_mesh(logging.debug) + def set_mesh_offsets(self, offsets): + for i, o in enumerate(offsets): + if o is not None: + self.mesh_offsets[i] = o def get_x_coordinate(self, index): return self.mesh_x_min + self.mesh_x_dist * index def get_y_coordinate(self, index): @@ -858,8 +879,8 @@ class ZMesh: def calc_z(self, x, y): if self.mesh_matrix is not None: tbl = self.mesh_matrix - tx, xidx = self._get_linear_index(x, 0) - ty, yidx = self._get_linear_index(y, 1) + tx, xidx = self._get_linear_index(x + self.mesh_offsets[0], 0) + ty, yidx = self._get_linear_index(y + self.mesh_offsets[1], 1) z0 = lerp(tx, tbl[yidx][xidx], tbl[yidx][xidx+1]) z1 = lerp(tx, tbl[yidx+1][xidx], tbl[yidx+1][xidx+1]) return lerp(ty, z0, z1)