From 926d78de01efb114d00e021364b04bbe6f28690b Mon Sep 17 00:00:00 2001 From: alfrix Date: Wed, 8 Mar 2023 04:43:47 -0600 Subject: [PATCH] bed_level: remove screws only if in list and simplify detection fixes #910 --- panels/bed_level.py | 154 ++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 105 deletions(-) diff --git a/panels/bed_level.py b/panels/bed_level.py index 31f905d1..95421a48 100644 --- a/panels/bed_level.py +++ b/panels/bed_level.py @@ -85,6 +85,33 @@ class BedLevelPanel(ScreenPanel): elif "bed_screws" in self._printer.get_config_section_list(): self.screws = self._get_screws("bed_screws") logging.info(f"bed_screws: {self.screws}") + nscrews = len(self.screws) + # KS config + valid_positions = True + valid_screws = ["bl", "fl", "fr", "br", "bm", "fm", "lm", "rm"] + if self.ks_printer_cfg is not None: + screw_positions = self.ks_printer_cfg.get("screw_positions", "") + if screw_positions: + screw_positions = [str(i.strip()) for i in screw_positions.split(',')] + logging.info(f"Positions: {screw_positions}") + for screw in screw_positions: + if screw not in valid_screws: + logging.error(f"Unknown screw: {screw}") + self._screen.show_popup_message(_("Unknown screw position") + f": {screw}") + valid_positions = False + if not (3 <= len(screw_positions) <= 8): + valid_positions = False + else: + if nscrews in (3, 5, 7): + valid_positions = False + screw_positions = valid_screws + rotation = self.ks_printer_cfg.getint("screw_rotation", 0) + logging.info(f"Rotation: {rotation}") + else: + valid_positions = False + if 'bed_screws' in self._config.get_config(): + rotation = self._config.get_config()['bed_screws'].getint("rotation", 0) + logging.debug(f"Rotation: {rotation}") # get dimensions x_positions = {x[0] for x in self.screws} @@ -95,8 +122,12 @@ class BedLevelPanel(ScreenPanel): min_x = min(x_positions) max_x = max(x_positions) + mid_x = round((min_x + max_x) / 2) + min_y = min(y_positions) max_y = max(y_positions) + mid_y = round((min_y + max_y) / 2) + max_distance = math.ceil( math.hypot(max_x - min_x, max_y - min_y) / min(self.x_cnt, self.y_cnt, 3) @@ -105,77 +136,22 @@ class BedLevelPanel(ScreenPanel): logging.debug(f"Using max_distance: {max_distance} to fit: {len(self.screws)} screws.") remaining_screws = self.screws[:] - remaining_positions = [] - fl = find_closest(remaining_screws, (min_x, min_y), max_distance, remove=True) - bl = find_closest(remaining_screws, (min_x, max_y), max_distance, remove=True) - br = find_closest(remaining_screws, (max_x, max_y), max_distance, remove=True) - fr = find_closest(remaining_screws, (max_x, min_y), max_distance, remove=True) + fl = find_closest(remaining_screws, (min_x, min_y), max_distance, remove="fl" in screw_positions) + bl = find_closest(remaining_screws, (min_x, max_y), max_distance, remove="bl" in screw_positions) + br = find_closest(remaining_screws, (max_x, max_y), max_distance, remove="br" in screw_positions) + fr = find_closest(remaining_screws, (max_x, min_y), max_distance, remove="fr" in screw_positions) - if self.x_cnt == 3: - mid_x = [x for x in list(zip(*self.screws))[0] if x not in (min_x, max_x)][0] - fm = find_closest(remaining_screws, (mid_x, min_y), max_distance, remove=True) - bm = find_closest(remaining_screws, (mid_x, max_y), max_distance, remove=True) - fmp = bmp = None - else: - mid_x = round((min_x + max_x) / 2) - fmp = (mid_x, min_y) - bmp = (mid_x, max_y) - remaining_positions.extend([fmp, bmp]) - fm = bm = None + fm = find_closest(remaining_screws, (mid_x, min_y), max_distance, remove="fm" in screw_positions) + bm = find_closest(remaining_screws, (mid_x, max_y), max_distance, remove="bm" in screw_positions) - if self.y_cnt == 3: - mid_y = [y for y in list(zip(*self.screws))[1] if y not in (min_y, max_y)][0] - lm = find_closest(remaining_screws, (min_x, mid_y), max_distance, remove=True) - rm = find_closest(remaining_screws, (max_x, mid_y), max_distance, remove=True) - lmp = rmp = None - else: - mid_y = round((min_y + max_y) / 2) - lmp = (min_x, mid_y) - rmp = (max_x, mid_y) - remaining_positions.extend([lmp, rmp]) - lm = rm = None + lm = find_closest(remaining_screws, (min_x, mid_y), max_distance, remove="lm" in screw_positions) + rm = find_closest(remaining_screws, (max_x, mid_y), max_distance, remove="rm" in screw_positions) - remaining_count = len(remaining_screws) - while remaining_count > 0: - logging.debug(f"Screws remaining: {remaining_screws}") - logging.debug(f"Positions remaining: {remaining_positions}") - for screw in remaining_screws: - pos = find_closest(remaining_positions, screw, max_distance) - closest = find_closest(remaining_screws, pos, max_distance) - if closest != screw: - continue - elif pos == fmp: - fm = screw - elif pos == bmp: - bm = screw - elif pos == lmp: - lm = screw - elif pos == rmp: - rm = screw - - logging.debug(f"Fitted screw {screw} close to {pos}") - remaining_positions.remove(pos) - remaining_screws.remove(screw) - - if remaining_count == len(remaining_screws): - logging.warning( - f"Remaining screws: {remaining_screws}" + - f" don't fit to positions: {remaining_positions}") - break - remaining_count = len(remaining_screws) - - if remaining_count == 0: - # All screws fitted. - if self.x_cnt > 3: - self.x_cnt = 3 - if self.y_cnt > 3: - self.y_cnt = 3 - else: + if len(remaining_screws) != 0: logging.debug(f"Screws not used: {remaining_screws}") - used_screw_cnt = len(self.screws) - len(remaining_screws) - logging.debug(f"Using {used_screw_cnt}-screw locations [x,y] [{self.x_cnt}x{self.y_cnt}]") + logging.debug(f"Using {len(self.screws) - len(remaining_screws)}/{len(self.screws)}-screw locations") button_scale = 2 @@ -188,56 +164,24 @@ class BedLevelPanel(ScreenPanel): self.buttons['fm'] = self._gtk.Button("bed-level-b-m", scale=button_scale) self.buttons['bm'] = self._gtk.Button("bed-level-t-m", scale=button_scale) - valid_positions = True - if self.ks_printer_cfg is not None: - screw_positions = self.ks_printer_cfg.get("screw_positions", "") - screw_positions = [str(i.strip()) for i in screw_positions.split(',')] - logging.info(f"Positions: {screw_positions}") - for screw in screw_positions: - if screw not in ("bl", "fl", "fr", "br", "bm", "fm", "lm", "rm", ""): - logging.error(f"Unknown screw: {screw}") - self._screen.show_popup_message(_("Unknown screw position") + f": {screw}") - valid_positions = False - if not (3 <= len(screw_positions) <= 8): - valid_positions = False - rotation = self.ks_printer_cfg.getint("screw_rotation", 0) - logging.info(f"Rotation: {rotation}") - else: - valid_positions = False - if 'bed_screws' in self._config.get_config(): - rotation = self._config.get_config()['bed_screws'].getint("rotation", 0) - logging.debug(f"Rotation: {rotation}") - bedgrid = Gtk.Grid() - nscrews = len(self.screws) if valid_positions: - if "bl" in screw_positions: + if "bl" in screw_positions and bl: bedgrid.attach(self.buttons['bl'], 1, 0, 1, 1) - if "fl" in screw_positions: + if "fl" in screw_positions and fl: bedgrid.attach(self.buttons['fl'], 1, 2, 1, 1) - if "fr" in screw_positions: + if "fr" in screw_positions and fr: bedgrid.attach(self.buttons['fr'], 3, 2, 1, 1) - if "br" in screw_positions: + if "br" in screw_positions and br: bedgrid.attach(self.buttons['br'], 3, 0, 1, 1) - if "bm" in screw_positions: + if "bm" in screw_positions and bm: bedgrid.attach(self.buttons['bm'], 2, 0, 1, 1) - if "fm" in screw_positions: + if "fm" in screw_positions and fm: bedgrid.attach(self.buttons['fm'], 2, 2, 1, 1) - if "lm" in screw_positions: - bedgrid.attach(self.buttons['lm'], 1, 1, 1, 1) - if "rm" in screw_positions: - bedgrid.attach(self.buttons['rm'], 3, 1, 1, 1) - elif nscrews in {4, 6, 8}: - bedgrid.attach(self.buttons['bl'], 1, 0, 1, 1) - bedgrid.attach(self.buttons['fl'], 1, 2, 1, 1) - bedgrid.attach(self.buttons['fr'], 3, 2, 1, 1) - bedgrid.attach(self.buttons['br'], 3, 0, 1, 1) - if self.x_cnt == 3: - bedgrid.attach(self.buttons['bm'], 2, 0, 1, 1) - bedgrid.attach(self.buttons['fm'], 2, 2, 1, 1) - if self.y_cnt == 3: + if "lm" in screw_positions and lm: bedgrid.attach(self.buttons['lm'], 1, 1, 1, 1) + if "rm" in screw_positions and rm: bedgrid.attach(self.buttons['rm'], 3, 1, 1, 1) else: label = Gtk.Label(