diff --git a/panels/zcalibrate.py b/panels/zcalibrate.py index 0392ec91..ebaf239c 100644 --- a/panels/zcalibrate.py +++ b/panels/zcalibrate.py @@ -24,32 +24,65 @@ class ZCalibratePanel(ScreenPanel): _ = self.lang.gettext grid = Gtk.Grid() - label = Gtk.Label(_("Z Offset") + ": \n") - self.widgets['zposition'] = Gtk.Label(_("Homing")) + pos_label = Gtk.Label(_("Z Position") + ": \n") + self.widgets['zposition'] = Gtk.Label("?") pos = Gtk.VBox() pos.set_vexpand(False) pos.set_valign(Gtk.Align.CENTER) - pos.add(label) + pos.add(pos_label) pos.add(self.widgets['zposition']) self.widgets['zpos'] = self._gtk.ButtonImage('z-farther', _("Raise Nozzle"), 'color4') self.widgets['zpos'].connect("clicked", self.move, "+") self.widgets['zneg'] = self._gtk.ButtonImage('z-closer', _("Lower Nozzle"), 'color1') self.widgets['zneg'].connect("clicked", self.move, "-") - if (self._printer.config_section_exists("probe") or self._printer.config_section_exists("bltouch")): - self.widgets['start'] = self._gtk.ButtonImage('resume', _("Start") + "\n(Probe)", 'color3') - self.widgets['start'].connect("clicked", self.start_calibration) - elif "Mesh" in self.title: - self.widgets['start'] = self._gtk.ButtonImage('resume', _("Start") + "\n(Mesh)", 'color3') - self.widgets['start'].connect("clicked", self.start_mesh) - else: - self.widgets['start'] = self._gtk.ButtonImage('resume', _("Start") + "\n(Endstop)", 'color3') - self.widgets['start'].connect("clicked", self.start_calibration) + self.widgets['start'] = self._gtk.ButtonImage('resume', _("Start"), 'color3') self.widgets['complete'] = self._gtk.ButtonImage('complete', _('Accept'), 'color3') self.widgets['complete'].connect("clicked", self.accept) self.widgets['cancel'] = self._gtk.ButtonImage('cancel', _('Abort'), 'color2') self.widgets['cancel'].connect("clicked", self.abort) + functions = ["endstop", "probe", "mesh", "delta"] + pobox = Gtk.VBox() + if not self._screen.printer.get_config_section("stepper_z")['endstop_pin'].startswith("probe"): + endstop = self._gtk.Button(label="Endstop") + endstop.connect("clicked", self.start_calibration, "endstop") + pobox.pack_start(endstop, True, True, 5) + else: + functions.remove("endstop") + + if (self._printer.config_section_exists("probe") or self._printer.config_section_exists("bltouch")): + probe = self._gtk.Button(label="Probe") + probe.connect("clicked", self.start_calibration, "probe") + pobox.pack_start(probe, True, True, 5) + functions.remove("mesh") + else: + functions.remove("probe") + # This is used to do a manual bed mesh if there is no probe + if self._printer.config_section_exists("bed_mesh"): + mesh = self._gtk.Button(label="Bed mesh") + mesh.connect("clicked", self.start_calibration, "mesh") + pobox.pack_start(mesh, True, True, 5) + else: + functions.remove("mesh") + + if "delta" in self._screen.printer.get_config_section("printer")['kinematics']: + delta = self._gtk.Button(label="Delta") + delta.connect("clicked", self.start_calibration, "delta") + pobox.pack_start(delta, True, True, 5) + else: + functions.remove("delta") + logging.info("Available functions: %s" % functions) + + self.labels['popover'] = Gtk.Popover() + self.labels['popover'].add(pobox) + self.labels['popover'].set_position(Gtk.PositionType.BOTTOM) + + if len(functions) > 1: + self.widgets['start'].connect("clicked", self.on_popover_clicked) + else: + self.widgets['start'].connect("clicked", self.start_calibration, functions[0]) + distgrid = Gtk.Grid() j = 0 for i in self.distances: @@ -94,34 +127,81 @@ class ZCalibratePanel(ScreenPanel): grid.attach(distances, 0, 2, 3, 1) self.buttons_not_calibrating() + self.content.add(grid) - def start_mesh(self, widget): + def on_popover_clicked(self, widget): + self.labels['popover'].set_relative_to(widget) + self.labels['popover'].show_all() + + def start_calibration(self, widget, method): + self.labels['popover'].popdown() 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") + if method == "probe": + # Get position from config + printer = self._screen.connected_printer + printer_cfg = self._config.get_printer_config(printer) + logging.info(printer_cfg) + if printer_cfg is not None: + x_position = printer_cfg.getint("calibrate_x_position", 0) + y_position = printer_cfg.getint("calibrate_y_position", 0) + elif 'z_calibrate_position' in self._config.get_config(): + x_position = self._config.get_config()['z_calibrate_position'].getint("calibrate_x_position", 0) + y_position = self._config.get_config()['z_calibrate_position'].getint("calibrate_y_position", 0) - def start_calibration(self, widget): - if self._screen.printer.get_stat("toolhead", "homed_axes") != "xyz": - self._screen._ws.klippy.gcode_script(KlippyGcodes.HOME) - - if 'z_calibrate_position' in self._config.get_config(): - x_position = self._config.get_config()['z_calibrate_position'].getint("calibrate_x_position", 0) - y_position = self._config.get_config()['z_calibrate_position'].getint("calibrate_y_position", 0) if x_position > 0 and y_position > 0: + logging.debug("Configured probing position X: %.0f Y: %.0f", x_position, y_position) + self._screen._ws.klippy.gcode_script('G0 X%d Y%d F3000' % (x_position, y_position)) + elif "delta" in self._screen.printer.get_config_section("printer")['kinematics']: + logging.info("Detected delta kinematics calibrating at 0,0") + self._screen._ws.klippy.gcode_script('G0 X%d Y%d F3000' % (0, 0)) + else: + logging.debug("Position not configured, probing the middle of the bed") + x_position = int(int(self._screen.printer.get_config_section("stepper_x")['position_max'])/2) + y_position = int(int(self._screen.printer.get_config_section("stepper_y")['position_max'])/2) + + # Find probe offset + klipper_cfg = self._screen.printer.get_config_section_list() + x_offset = y_offset = 0 + if "bltouch" in klipper_cfg: + bltouch = self._screen.printer.get_config_section("bltouch") + if "x_offset" in bltouch: + x_offset = float(bltouch['x_offset']) + if "y_offset" in bltouch: + y_offset = float(bltouch['y_offset']) + elif "probe" in klipper_cfg: + probe = self._screen.printer.get_config_section("probe") + if "x_offset" in probe: + x_offset = float(probe['x_offset']) + if "y_offset" in probe: + y_offset = float(probe['y_offset']) + if x_offset > 0 or y_offset > 0: + logging.debug("Substracting probe offset X: %.0f Y: %.0f", x_offset, y_offset) + x_position = self.apply_probe_offset(x_position, x_offset) + y_position = self.apply_probe_offset(y_position, y_offset) + # Move self._screen._ws.klippy.gcode_script('G0 X%d Y%d F3000' % (x_position, y_position)) - if (self._printer.config_section_exists("probe") or self._printer.config_section_exists("bltouch")): self._screen._ws.klippy.gcode_script(KlippyGcodes.PROBE_CALIBRATE) - else: + elif method == "mesh": + self._screen._ws.klippy.gcode_script("BED_MESH_CALIBRATE") + elif method == "delta": + self._screen._ws.klippy.gcode_script("DELTA_CALIBRATE") + elif method == "endstop": self._screen._ws.klippy.gcode_script(KlippyGcodes.Z_ENDSTOP_CALIBRATE) + def apply_probe_offset(self, pos, offset): + return max(0, int(float(pos) - offset)) + def process_update(self, action, data): _ = self.lang.gettext if action == "notify_status_update": - if "toolhead" in data and "position" in data['toolhead']: + if self._screen.printer.get_stat("toolhead", "homed_axes") != "xyz": + self.widgets['zposition'].set_text("?") + elif "toolhead" in data and "position" in data['toolhead']: self.updatePosition(data['toolhead']['position']) elif action == "notify_gcode_response": if "unknown" in data.lower():