From 70b55326951bd236e83ad1c6fd5d249b7da13635 Mon Sep 17 00:00:00 2001 From: alfrix Date: Tue, 18 Oct 2022 10:14:25 -0300 Subject: [PATCH] bed_mesh: fix issues and improve Fix sometimes not showing the current mesh Remove calibrate from the list, (use the top bar) Minimum Zscale is now -1 to 1 mm Colorscale is now -0.2 to 0.2 mm tends to clip less on the usable range Code cleanup and de-duplication --- panels/bed_mesh.py | 152 ++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 100 deletions(-) diff --git a/panels/bed_mesh.py b/panels/bed_mesh.py index a2e81ae0..9d151787 100644 --- a/panels/bed_mesh.py +++ b/panels/bed_mesh.py @@ -37,16 +37,16 @@ class BedMeshPanel(ScreenPanel): clear = self._gtk.ButtonImage("cancel", " " + _("Clear"), "color2", .66, Gtk.PositionType.LEFT, 1) clear.connect("clicked", self._clear_mesh) clear.set_hexpand(True) - top_calibrate = self._gtk.ButtonImage("refresh", " " + _("Calibrate"), "color3", .66, Gtk.PositionType.LEFT, 1) - top_calibrate.connect("clicked", self._send_calibrate) - top_calibrate.set_hexpand(True) + calibrate = self._gtk.ButtonImage("refresh", " " + _("Calibrate"), "color3", .66, Gtk.PositionType.LEFT, 1) + calibrate.connect("clicked", self.calibrate_mesh) + calibrate.set_hexpand(True) topbar = Gtk.Box(spacing=5) topbar.set_hexpand(True) topbar.set_vexpand(False) topbar.add(addprofile) topbar.add(clear) - topbar.add(top_calibrate) + topbar.add(calibrate) # Create a grid for all profiles self.labels['profiles'] = Gtk.Grid() @@ -68,45 +68,24 @@ class BedMeshPanel(ScreenPanel): self.content.add(self.labels['main_box']) def activate(self): - for child in self.content.get_children(): - self.content.remove(child) - self.content.add(self.labels['main_box']) - - self.activate_mesh( - self._screen.printer.get_stat("bed_mesh", "profile_name") - ) + with contextlib.suppress(KeyError): + self.activate_mesh(self._screen.printer.get_stat("bed_mesh", "profile_name")) def activate_mesh(self, profile): if profile == "": - profile = "default" - - logging.debug(f"Activating profile: {profile} Current: {self.active_mesh}") + logging.info("Clearing active profile") + self.profiles[self.active_mesh]['button_box'].add(self.profiles[self.active_mesh]['load']) + self.active_mesh = None + return if profile not in self.profiles: self.add_profile(profile) - if self.active_mesh is not None and self.active_mesh in self.profiles: - a = self.profiles[self.active_mesh] - with contextlib.suppress(KeyError): - a['button_box'].remove(a['calibrate']) - a['button_box'].pack_start(a['load'], False, False, 0) - logging.info(f"Active {self.active_mesh} changing to {profile}") + self.profiles[profile]['button_box'].remove(self.profiles[profile]['load']) self.active_mesh = profile - if self.active_mesh is not None: - a = self.profiles[profile] - with contextlib.suppress(KeyError): - a['button_box'].remove(a['load']) - with contextlib.suppress(KeyError): - a['button_box'].remove(a['view']) - a['button_box'].pack_start(a['calibrate'], False, False, 0) - if self._printer.get_config_section(f"bed_mesh {profile}"): - a['button_box'].pack_end(a["view"], False, False, 0) - self._screen.show_all() - def add_profile(self, profile): - - logging.debug(f"Adding Profile: {profile} Current: {self.active_mesh}") + logging.debug(f"Adding Profile: {profile}") name = Gtk.Label() name.set_markup(f"{profile}") name.set_hexpand(True) @@ -116,17 +95,15 @@ class BedMeshPanel(ScreenPanel): name.set_line_wrap_mode(Pango.WrapMode.WORD_CHAR) buttons = { - "calibrate": self._gtk.ButtonImage("refresh", _("Calibrate"), "color4"), - "load": self._gtk.ButtonImage("load", _("Load"), "color2"), "save": self._gtk.ButtonImage("complete", _("Save"), "color3"), "delete": self._gtk.ButtonImage("cancel", _("Delete"), "color3"), "view": self._gtk.ButtonImage("bed-level", _("View Mesh"), "color1"), + "load": self._gtk.ButtonImage("load", _("Load"), "color2"), } - buttons["calibrate"].connect("clicked", self.calibrate_mesh) - buttons["load"].connect("clicked", self.send_load_mesh, profile) buttons["save"].connect("clicked", self.send_save_mesh, profile) buttons["delete"].connect("clicked", self.send_remove_mesh, profile) buttons["view"].connect("clicked", self.show_mesh, profile) + buttons["load"].connect("clicked", self.send_load_mesh, profile) for b in buttons.values(): b.set_hexpand(False) @@ -134,13 +111,11 @@ class BedMeshPanel(ScreenPanel): b.set_halign(Gtk.Align.END) button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5) - button_box.add(buttons["calibrate"]) if profile != "default": button_box.add(buttons["save"]) - button_box.add(buttons["delete"]) - if self._printer.get_config_section(f"bed_mesh {profile}"): - button_box.add(buttons["load"]) - button_box.add(buttons["view"]) + button_box.add(buttons["delete"]) + button_box.add(buttons["view"]) + button_box.add(buttons["load"]) box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=5) box.pack_start(name, True, True, 0) @@ -153,7 +128,6 @@ class BedMeshPanel(ScreenPanel): self.profiles[profile] = { "button_box": button_box, "row": frame, - "calibrate": buttons["calibrate"], "load": buttons["load"], "save": buttons["save"], "delete": buttons["delete"], @@ -185,6 +159,7 @@ class BedMeshPanel(ScreenPanel): def process_update(self, action, data): if action == "notify_status_update": with contextlib.suppress(KeyError): + logging.info(data['bed_mesh']) self.activate_mesh(data['bed_mesh']['profile_name']) def remove_create(self): @@ -249,41 +224,33 @@ class BedMeshPanel(ScreenPanel): self._screen.show_keyboard(entry=self.labels['profile_name']) def show_mesh(self, widget, profile): - - bm = self._printer.get_config_section(f"bed_mesh {profile}") - if bm is False: - logging.info(f"Unable to load profile: {profile}") - return - if profile == self.active_mesh: - abm = self._printer.get_stat("bed_mesh") - if abm is None: + bm = self._printer.get_stat("bed_mesh") + if bm is None: logging.info(f"Unable to load active mesh: {profile}") return - x_range = [int(abm['mesh_min'][0]), int(abm['mesh_max'][0])] - y_range = [int(abm['mesh_min'][1]), int(abm['mesh_max'][1])] - minz_mesh = min(min(abm['mesh_matrix'])) - maxz_mesh = max(max(abm['mesh_matrix'])) - # Do not use a very small zscale, because that could be misleading - minz_mesh = min(minz_mesh, -0.5) - maxz_mesh = max(maxz_mesh, 0.5) - z_range = [minz_mesh, maxz_mesh] - counts = [len(abm['mesh_matrix'][0]), len(abm['mesh_matrix'])] - deltas = [(x_range[1] - x_range[0]) / (counts[0] - 1), (y_range[1] - y_range[0]) / (counts[1] - 1)] - x = [(i * deltas[0]) + x_range[0] for i in range(counts[0])] - y = [(i * deltas[0]) + y_range[0] for i in range(counts[1])] - x, y = np.meshgrid(x, y) - z = np.asarray(abm['mesh_matrix']) + matrix = 'probed_matrix' + # if 'mesh_matrix' in bm and bm['mesh_matrix'][0]: + # matrix = 'mesh_matrix' + x_range = [int(bm['mesh_min'][0]), int(bm['mesh_max'][0])] + y_range = [int(bm['mesh_min'][1]), int(bm['mesh_max'][1])] else: + bm = self._printer.get_config_section(f"bed_mesh {profile}") + if bm is False: + logging.info(f"Unable to load profile: {profile}") + self.remove_profile(profile) + return + matrix = 'points' x_range = [int(bm['min_x']), int(bm['max_x'])] y_range = [int(bm['min_y']), int(bm['max_y'])] - z_range = [min(min(bm['points'])), max(max(bm['points']))] - deltas = [(x_range[1] - x_range[0]) / (int(bm['x_count']) - 1), - (y_range[1] - y_range[0]) / (int(bm['y_count']) - 1)] - x = [(i * deltas[0]) + x_range[0] for i in range(bm['x_count'])] - y = [(i * deltas[0]) + y_range[0] for i in range(bm['y_count'])] - x, y = np.meshgrid(x, y) - z = np.asarray(bm['points']) + # Zscale can be offered as a slider instead of hardcoded values reasonable values 0.5 - 2 (mm) + z_range = [min(min(min(bm[matrix])), -1), max(max(max(bm[matrix])), 1)] + counts = [len(bm[matrix][0]), len(bm[matrix])] + deltas = [(x_range[1] - x_range[0]) / (counts[0] - 1), (y_range[1] - y_range[0]) / (counts[1] - 1)] + x = [(i * deltas[0]) + x_range[0] for i in range(counts[0])] + y = [(i * deltas[0]) + y_range[0] for i in range(counts[1])] + x, y = np.meshgrid(x, y) + z = np.asarray(bm[matrix]) rc('axes', edgecolor="#e2e2e2", labelcolor="#e2e2e2") rc(('xtick', 'ytick'), color="#e2e2e2") @@ -292,7 +259,8 @@ class BedMeshPanel(ScreenPanel): ax.set(title=profile, xlabel="X", ylabel="Y", facecolor='none') ax.spines['bottom'].set_color("#e2e2e2") fig.add_axes(ax) - surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm, vmin=-0.1, vmax=0.1) + # Color gradient could also be configurable as a slider reasonable values 0.1 - 0.2 (mm) + surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm, vmin=-0.2, vmax=0.2) chartbox = ax.get_position() ax.set_position([chartbox.x0, chartbox.y0 + 0.1, chartbox.width * .92, chartbox.height]) @@ -303,38 +271,30 @@ class BedMeshPanel(ScreenPanel): ax.zaxis.set_major_formatter('{x:.02f}') fig.colorbar(surf, shrink=0.7, aspect=5, pad=0.25) - box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) - box.set_hexpand(True) - box.set_vexpand(True) title = Gtk.Label() title.set_markup(f"{profile}") title.set_hexpand(True) title.set_halign(Gtk.Align.CENTER) - canvas_box = Gtk.Box() - canvas_box.set_hexpand(True) - canvas_box.set_vexpand(True) - - box.add(title) - box.add(canvas_box) - - buttons = [ - {"name": _("Close"), "response": Gtk.ResponseType.CANCEL} - ] - self._gtk.Dialog(self._screen, buttons, box, self._close_dialog) - - alloc = canvas_box.get_allocation() canvas = FigureCanvas(fig) - canvas.set_size_request(alloc.width, self._screen.height / 3 * 2) - canvas_box.add(canvas) - canvas_box.show_all() + canvas.set_size_request(self._screen.width * .9, self._screen.height / 3 * 2) # Remove the "matplotlib-canvas" class which forces a white background. # https://github.com/matplotlib/matplotlib/commit/3c832377fb4c4b32fcbdbc60fdfedb57296bc8c0 style_ctx = canvas.get_style_context() for css_class in style_ctx.list_classes(): style_ctx.remove_class(css_class) + box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) + box.add(title) + box.add(canvas) + box.show_all() + + buttons = [ + {"name": _("Close"), "response": Gtk.ResponseType.CANCEL} + ] + self._gtk.Dialog(self._screen, buttons, box, self._close_dialog) + @staticmethod def _close_dialog(widget, response): widget.destroy() @@ -365,14 +325,6 @@ class BedMeshPanel(ScreenPanel): "BED_MESH_CLEAR" ) - def _send_calibrate(self, widget): - if self._screen.printer.get_stat("toolhead", "homed_axes") != "xyz": - self._screen._ws.klippy.gcode_script(KlippyGcodes.HOME) - - self._screen._ws.klippy.gcode_script( - "BED_MESH_CALIBRATE" - ) - def send_load_mesh(self, widget, profile): self._screen._ws.klippy.gcode_script( KlippyGcodes.bed_mesh_load(profile)