Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

screws_tilt_adjust: add option to compute probe-relative points automatically, as well as raise z at the end #6787

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions klippy/extras/screws_tilt_adjust.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ def __init__(self, config):
self.results = []
self.max_diff = None
self.max_diff_error = False
# Read config
for i in range(99):
prefix = "screw%d" % (i + 1,)
if config.get(prefix, None) is None:
break
screw_coord = config.getfloatlist(prefix, count=2)
screw_name = "screw at %.3f,%.3f" % screw_coord
screw_name = config.get(prefix + "_name", screw_name)
self.screws.append((screw_coord, screw_name))
if len(self.screws) < 3:
raise config.error("screws_tilt_adjust: Must have "
"at least three screws")
self.end_z = config.getfloat("end_z", None)

use_bed_screws = config.get("use_bed_screws", "no").lower()
if use_bed_screws in ["yes", "true", "1"]:
self.need_probe_offsets = True
self._load_screws_from_bed_screws()
else:
self.need_probe_offsets = False
self._load_screws(self.config)
self.threads = {'CW-M3': 0, 'CCW-M3': 1, 'CW-M4': 2, 'CCW-M4': 3,
'CW-M5': 4, 'CCW-M5': 5, 'CW-M6': 6, 'CCW-M6': 7}
self.thread = config.getchoice('screw_thread', self.threads,
default='CW-M3')
# Initialize ProbePointsHelper
points = [coord for coord, name in self.screws]
self.probe_helper = probe.ProbePointsHelper(self.config,
self.probe_finalize,
default_points=points)
self.probe_helper.minimum_points(3)
default_points=[(0,0)])
# Register command
self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command("SCREWS_TILT_CALCULATE",
Expand All @@ -46,6 +41,52 @@ def __init__(self, config):
"screws by calculating the number " \
"of turns to level it."

def _load_screws_from_bed_screws(self):
config = self.config
if not config.has_section('bed_screws'):
raise config.error("screws_tilt_adjust: must have bed_screws "
"if use_bed_screws is set")
bed_screws = config.getsection('bed_screws')
self._load_screws(bed_screws)

def _load_screws(self, config):
# Read config
for i in range(99):
prefix = "screw%d" % (i + 1,)
if config.get(prefix, None) is None:
break
screw_coord = config.getfloatlist(prefix, count=2)
screw_name = "screw at %.3f,%.3f" % screw_coord
screw_name = config.get(prefix + "_name", screw_name)
self.screws.append((screw_coord, screw_name))
if len(self.screws) < 3:
raise config.error("screws_tilt_adjust: Must have "
"at least three screws")

def _offset_screws(self, gcmd):
if not self.need_probe_offsets:
return
th = self.printer.lookup_object('toolhead')
xrange = th.get_kinematics().rails[0].get_range()
yrange = th.get_kinematics().rails[1].get_range()
probe = self.printer.lookup_object('probe')
probe_x, probe_y, _ = probe.get_offsets()
for i in range(len(self.screws)):
screw_coord, screw_name = self.screws[i]
sx, sy = screw_coord
sx -= probe_x
sy -= probe_y
if xrange[0] >= sx or sx >= xrange[1]:
sx = max(xrange[0]+1.0, min(sx, xrange[1]-1.0))
gcmd.respond_info(f"screws_tilt_adjust: screw {screw_name} X"
f"out of range, adjusting to {sx}")
if yrange[0] >= sy or sy >= yrange[1]:
sy = max(yrange[0]+1.0, min(sy, yrange[1]-1.0))
gcmd.respond_info(f"screws_tilt_adjust: screw {screw_name} Y"
f"out of range, adjusting to {sy}")
self.screws[i] = ((sx, sy), screw_name)
self.need_probe_offsets = False

def cmd_SCREWS_TILT_CALCULATE(self, gcmd):
self.max_diff = gcmd.get_float("MAX_DEVIATION", None)
# Option to force all turns to be in the given direction (CW or CCW)
Expand All @@ -57,6 +98,9 @@ def cmd_SCREWS_TILT_CALCULATE(self, gcmd):
"Error on '%s': DIRECTION must be either CW or CCW" % (
gcmd.get_commandline(),))
self.direction = direction
self._offset_screws(gcmd)
points = [coord for coord, name in self.screws]
self.probe_helper.update_probe_points(points, 3)
self.probe_helper.start_probe(gcmd)

def get_status(self, eventtime):
Expand All @@ -65,6 +109,9 @@ def get_status(self, eventtime):
'results': self.results}

def probe_finalize(self, offsets, positions):
if self.end_z is not None:
th = self.printer.lookup_object('toolhead')
th.manual_move([None, None, self.end_z], 10)
self.results = {}
self.max_diff_error = False
# Factors used for CW-M3, CCW-M3, CW-M4, CCW-M4, CW-M5, CCW-M5, CW-M6
Expand Down
Loading