2017-12-06 07:00:33 +01:00
|
|
|
# Z-Probe support
|
|
|
|
#
|
2021-01-08 18:37:57 +01:00
|
|
|
# Copyright (C) 2017-2021 Kevin O'Connor <kevin@koconnor.net>
|
2017-12-06 07:00:33 +01:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2020-02-18 17:47:04 +01:00
|
|
|
import logging
|
2021-01-08 18:37:57 +01:00
|
|
|
import pins
|
2020-06-12 15:55:57 +02:00
|
|
|
from . import manual_probe
|
2017-12-06 07:00:33 +01:00
|
|
|
|
2018-03-13 03:51:44 +01:00
|
|
|
HINT_TIMEOUT = """
|
2020-09-04 18:39:11 +02:00
|
|
|
If the probe did not move far enough to trigger, then
|
|
|
|
consider reducing the Z axis minimum position so the probe
|
|
|
|
can travel further (the Z minimum position can be negative).
|
2018-03-13 03:51:44 +01:00
|
|
|
"""
|
|
|
|
|
2017-12-06 07:00:33 +01:00
|
|
|
class PrinterProbe:
|
2018-11-25 03:41:25 +01:00
|
|
|
def __init__(self, config, mcu_probe):
|
2017-12-06 07:00:33 +01:00
|
|
|
self.printer = config.get_printer()
|
2019-02-10 00:20:28 +01:00
|
|
|
self.name = config.get_name()
|
2018-11-25 03:41:25 +01:00
|
|
|
self.mcu_probe = mcu_probe
|
2020-01-30 21:36:51 +01:00
|
|
|
self.speed = config.getfloat('speed', 5.0, above=0.)
|
|
|
|
self.lift_speed = config.getfloat('lift_speed', self.speed, above=0.)
|
2018-08-18 18:25:57 +02:00
|
|
|
self.x_offset = config.getfloat('x_offset', 0.)
|
|
|
|
self.y_offset = config.getfloat('y_offset', 0.)
|
2018-03-17 19:00:37 +01:00
|
|
|
self.z_offset = config.getfloat('z_offset')
|
2019-03-08 18:34:13 +01:00
|
|
|
self.probe_calibrate_z = 0.
|
2020-02-12 19:37:55 +01:00
|
|
|
self.multi_probe_pending = False
|
2020-09-10 05:10:35 +02:00
|
|
|
self.last_state = False
|
2021-02-19 04:03:32 +01:00
|
|
|
self.last_z_result = 0.
|
2021-07-26 02:20:15 +02:00
|
|
|
self.gcode_move = self.printer.load_object(config, "gcode_move")
|
2018-03-14 20:30:58 +01:00
|
|
|
# Infer Z position to move to during a probe
|
|
|
|
if config.has_section('stepper_z'):
|
|
|
|
zconfig = config.getsection('stepper_z')
|
2020-12-13 16:26:27 +01:00
|
|
|
self.z_position = zconfig.getfloat('position_min', 0.,
|
|
|
|
note_valid=False)
|
2018-03-14 20:30:58 +01:00
|
|
|
else:
|
|
|
|
pconfig = config.getsection('printer')
|
2020-12-13 16:26:27 +01:00
|
|
|
self.z_position = pconfig.getfloat('minimum_z_position', 0.,
|
|
|
|
note_valid=False)
|
2019-05-31 19:47:15 +02:00
|
|
|
# Multi-sample support (for improved accuracy)
|
2019-06-21 20:44:28 +02:00
|
|
|
self.sample_count = config.getint('samples', 1, minval=1)
|
2019-06-21 19:52:29 +02:00
|
|
|
self.sample_retract_dist = config.getfloat('sample_retract_dist', 2.,
|
|
|
|
above=0.)
|
2019-05-31 20:16:59 +02:00
|
|
|
atypes = {'median': 'median', 'average': 'average'}
|
|
|
|
self.samples_result = config.getchoice('samples_result', atypes,
|
|
|
|
'average')
|
2019-06-21 20:44:28 +02:00
|
|
|
self.samples_tolerance = config.getfloat('samples_tolerance', 0.100,
|
|
|
|
minval=0.)
|
|
|
|
self.samples_retries = config.getint('samples_tolerance_retries', 0,
|
|
|
|
minval=0)
|
2018-11-25 03:41:25 +01:00
|
|
|
# Register z_virtual_endstop pin
|
|
|
|
self.printer.lookup_object('pins').register_chip('probe', self)
|
2020-02-12 19:03:42 +01:00
|
|
|
# Register homing event handlers
|
|
|
|
self.printer.register_event_handler("homing:homing_move_begin",
|
|
|
|
self._handle_homing_move_begin)
|
|
|
|
self.printer.register_event_handler("homing:homing_move_end",
|
|
|
|
self._handle_homing_move_end)
|
2020-02-12 19:37:55 +01:00
|
|
|
self.printer.register_event_handler("homing:home_rails_begin",
|
|
|
|
self._handle_home_rails_begin)
|
|
|
|
self.printer.register_event_handler("homing:home_rails_end",
|
|
|
|
self._handle_home_rails_end)
|
|
|
|
self.printer.register_event_handler("gcode:command_error",
|
2020-02-18 17:47:04 +01:00
|
|
|
self._handle_command_error)
|
2018-01-23 18:47:28 +01:00
|
|
|
# Register PROBE/QUERY_PROBE commands
|
2017-12-06 07:00:33 +01:00
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
2019-02-10 00:20:28 +01:00
|
|
|
self.gcode.register_command('PROBE', self.cmd_PROBE,
|
|
|
|
desc=self.cmd_PROBE_help)
|
|
|
|
self.gcode.register_command('QUERY_PROBE', self.cmd_QUERY_PROBE,
|
|
|
|
desc=self.cmd_QUERY_PROBE_help)
|
|
|
|
self.gcode.register_command('PROBE_CALIBRATE', self.cmd_PROBE_CALIBRATE,
|
|
|
|
desc=self.cmd_PROBE_CALIBRATE_help)
|
2019-02-18 06:23:32 +01:00
|
|
|
self.gcode.register_command('PROBE_ACCURACY', self.cmd_PROBE_ACCURACY,
|
|
|
|
desc=self.cmd_PROBE_ACCURACY_help)
|
2021-07-26 02:20:15 +02:00
|
|
|
self.gcode.register_command('Z_OFFSET_APPLY_PROBE',
|
|
|
|
self.cmd_Z_OFFSET_APPLY_PROBE,
|
|
|
|
desc=self.cmd_Z_OFFSET_APPLY_PROBE_help)
|
2021-03-29 20:23:46 +02:00
|
|
|
def _handle_homing_move_begin(self, hmove):
|
|
|
|
if self.mcu_probe in hmove.get_mcu_endstops():
|
|
|
|
self.mcu_probe.probe_prepare(hmove)
|
|
|
|
def _handle_homing_move_end(self, hmove):
|
|
|
|
if self.mcu_probe in hmove.get_mcu_endstops():
|
|
|
|
self.mcu_probe.probe_finish(hmove)
|
2021-01-08 18:37:57 +01:00
|
|
|
def _handle_home_rails_begin(self, homing_state, rails):
|
2020-02-12 19:37:55 +01:00
|
|
|
endstops = [es for rail in rails for es, name in rail.get_endstops()]
|
|
|
|
if self.mcu_probe in endstops:
|
|
|
|
self.multi_probe_begin()
|
2021-01-08 18:37:57 +01:00
|
|
|
def _handle_home_rails_end(self, homing_state, rails):
|
2020-02-12 19:37:55 +01:00
|
|
|
endstops = [es for rail in rails for es, name in rail.get_endstops()]
|
|
|
|
if self.mcu_probe in endstops:
|
|
|
|
self.multi_probe_end()
|
2020-02-18 17:47:04 +01:00
|
|
|
def _handle_command_error(self):
|
|
|
|
try:
|
|
|
|
self.multi_probe_end()
|
|
|
|
except:
|
|
|
|
logging.exception("Multi-probe end")
|
2020-02-12 19:37:55 +01:00
|
|
|
def multi_probe_begin(self):
|
|
|
|
self.mcu_probe.multi_probe_begin()
|
|
|
|
self.multi_probe_pending = True
|
|
|
|
def multi_probe_end(self):
|
|
|
|
if self.multi_probe_pending:
|
|
|
|
self.multi_probe_pending = False
|
|
|
|
self.mcu_probe.multi_probe_end()
|
2018-07-26 15:44:45 +02:00
|
|
|
def setup_pin(self, pin_type, pin_params):
|
|
|
|
if pin_type != 'endstop' or pin_params['pin'] != 'z_virtual_endstop':
|
2018-01-23 18:47:28 +01:00
|
|
|
raise pins.error("Probe virtual endstop only useful as endstop pin")
|
|
|
|
if pin_params['invert'] or pin_params['pullup']:
|
|
|
|
raise pins.error("Can not pullup/invert probe virtual endstop")
|
2018-09-27 15:47:37 +02:00
|
|
|
return self.mcu_probe
|
2020-04-25 03:51:33 +02:00
|
|
|
def get_lift_speed(self, gcmd=None):
|
|
|
|
if gcmd is not None:
|
|
|
|
return gcmd.get_float("LIFT_SPEED", self.lift_speed, above=0.)
|
2020-01-30 21:36:51 +01:00
|
|
|
return self.lift_speed
|
2018-08-18 18:25:57 +02:00
|
|
|
def get_offsets(self):
|
|
|
|
return self.x_offset, self.y_offset, self.z_offset
|
2019-02-18 06:23:32 +01:00
|
|
|
def _probe(self, speed):
|
2017-12-06 07:00:33 +01:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
2020-09-04 18:39:11 +02:00
|
|
|
curtime = self.printer.get_reactor().monotonic()
|
|
|
|
if 'z' not in toolhead.get_status(curtime)['homed_axes']:
|
|
|
|
raise self.printer.command_error("Must home before probe")
|
2021-03-29 16:05:39 +02:00
|
|
|
phoming = self.printer.lookup_object('homing')
|
2017-12-06 07:00:33 +01:00
|
|
|
pos = toolhead.get_position()
|
|
|
|
pos[2] = self.z_position
|
|
|
|
try:
|
2021-03-29 16:05:39 +02:00
|
|
|
epos = phoming.probing_move(self.mcu_probe, pos, speed)
|
2020-09-04 17:49:43 +02:00
|
|
|
except self.printer.command_error as e:
|
2018-03-13 03:51:44 +01:00
|
|
|
reason = str(e)
|
|
|
|
if "Timeout during endstop homing" in reason:
|
|
|
|
reason += HINT_TIMEOUT
|
2020-09-04 17:49:43 +02:00
|
|
|
raise self.printer.command_error(reason)
|
2020-04-25 03:51:33 +02:00
|
|
|
self.gcode.respond_info("probe at %.3f,%.3f is z=%.6f"
|
2021-03-29 16:05:39 +02:00
|
|
|
% (epos[0], epos[1], epos[2]))
|
|
|
|
return epos[:3]
|
2019-05-31 19:47:15 +02:00
|
|
|
def _move(self, coord, speed):
|
2020-08-17 04:43:03 +02:00
|
|
|
self.printer.lookup_object('toolhead').manual_move(coord, speed)
|
2019-05-31 20:16:59 +02:00
|
|
|
def _calc_mean(self, positions):
|
|
|
|
count = float(len(positions))
|
|
|
|
return [sum([pos[i] for pos in positions]) / count
|
|
|
|
for i in range(3)]
|
|
|
|
def _calc_median(self, positions):
|
|
|
|
z_sorted = sorted(positions, key=(lambda p: p[2]))
|
|
|
|
middle = len(positions) // 2
|
|
|
|
if (len(positions) & 1) == 1:
|
|
|
|
# odd number of samples
|
|
|
|
return z_sorted[middle]
|
|
|
|
# even number of samples
|
|
|
|
return self._calc_mean(z_sorted[middle-1:middle+1])
|
2020-04-25 03:51:33 +02:00
|
|
|
def run_probe(self, gcmd):
|
|
|
|
speed = gcmd.get_float("PROBE_SPEED", self.speed, above=0.)
|
|
|
|
lift_speed = self.get_lift_speed(gcmd)
|
|
|
|
sample_count = gcmd.get_int("SAMPLES", self.sample_count, minval=1)
|
|
|
|
sample_retract_dist = gcmd.get_float("SAMPLE_RETRACT_DIST",
|
|
|
|
self.sample_retract_dist, above=0.)
|
|
|
|
samples_tolerance = gcmd.get_float("SAMPLES_TOLERANCE",
|
|
|
|
self.samples_tolerance, minval=0.)
|
|
|
|
samples_retries = gcmd.get_int("SAMPLES_TOLERANCE_RETRIES",
|
|
|
|
self.samples_retries, minval=0)
|
|
|
|
samples_result = gcmd.get("SAMPLES_RESULT", self.samples_result)
|
2020-02-12 19:37:55 +01:00
|
|
|
must_notify_multi_probe = not self.multi_probe_pending
|
|
|
|
if must_notify_multi_probe:
|
|
|
|
self.multi_probe_begin()
|
2021-04-05 16:18:02 +02:00
|
|
|
probexy = self.printer.lookup_object('toolhead').get_position()[:2]
|
2019-06-21 19:52:29 +02:00
|
|
|
retries = 0
|
2019-05-31 19:47:15 +02:00
|
|
|
positions = []
|
2019-06-21 20:44:28 +02:00
|
|
|
while len(positions) < sample_count:
|
2019-06-21 19:52:29 +02:00
|
|
|
# Probe position
|
2019-06-21 20:44:28 +02:00
|
|
|
pos = self._probe(speed)
|
2019-05-31 19:47:15 +02:00
|
|
|
positions.append(pos)
|
2019-06-21 19:52:29 +02:00
|
|
|
# Check samples tolerance
|
|
|
|
z_positions = [p[2] for p in positions]
|
2019-06-21 20:44:28 +02:00
|
|
|
if max(z_positions) - min(z_positions) > samples_tolerance:
|
|
|
|
if retries >= samples_retries:
|
2020-09-04 17:49:43 +02:00
|
|
|
raise gcmd.error("Probe samples exceed samples_tolerance")
|
2020-04-25 03:51:33 +02:00
|
|
|
gcmd.respond_info("Probe samples exceed tolerance. Retrying...")
|
2019-06-21 19:52:29 +02:00
|
|
|
retries += 1
|
|
|
|
positions = []
|
|
|
|
# Retract
|
2019-06-21 20:44:28 +02:00
|
|
|
if len(positions) < sample_count:
|
2021-04-05 16:18:02 +02:00
|
|
|
self._move(probexy + [pos[2] + sample_retract_dist], lift_speed)
|
2020-02-12 19:37:55 +01:00
|
|
|
if must_notify_multi_probe:
|
|
|
|
self.multi_probe_end()
|
2019-06-21 19:52:29 +02:00
|
|
|
# Calculate and return result
|
2019-06-21 20:44:28 +02:00
|
|
|
if samples_result == 'median':
|
2019-05-31 20:16:59 +02:00
|
|
|
return self._calc_median(positions)
|
|
|
|
return self._calc_mean(positions)
|
2019-05-31 19:47:15 +02:00
|
|
|
cmd_PROBE_help = "Probe Z-height at current XY position"
|
2020-04-25 03:51:33 +02:00
|
|
|
def cmd_PROBE(self, gcmd):
|
|
|
|
pos = self.run_probe(gcmd)
|
|
|
|
gcmd.respond_info("Result is z=%.6f" % (pos[2],))
|
2021-02-19 04:03:32 +01:00
|
|
|
self.last_z_result = pos[2]
|
2017-12-06 07:00:33 +01:00
|
|
|
cmd_QUERY_PROBE_help = "Return the status of the z-probe"
|
2020-04-25 03:51:33 +02:00
|
|
|
def cmd_QUERY_PROBE(self, gcmd):
|
2017-12-06 07:00:33 +01:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
print_time = toolhead.get_last_move_time()
|
2019-06-26 16:45:06 +02:00
|
|
|
res = self.mcu_probe.query_endstop(print_time)
|
2020-09-10 05:10:35 +02:00
|
|
|
self.last_state = res
|
2020-04-25 03:51:33 +02:00
|
|
|
gcmd.respond_info("probe: %s" % (["open", "TRIGGERED"][not not res],))
|
2020-09-10 05:10:35 +02:00
|
|
|
def get_status(self, eventtime):
|
2021-02-19 04:03:32 +01:00
|
|
|
return {'last_query': self.last_state,
|
|
|
|
'last_z_result': self.last_z_result}
|
2019-02-18 06:23:32 +01:00
|
|
|
cmd_PROBE_ACCURACY_help = "Probe Z-height accuracy at current XY position"
|
2020-04-25 03:51:33 +02:00
|
|
|
def cmd_PROBE_ACCURACY(self, gcmd):
|
|
|
|
speed = gcmd.get_float("PROBE_SPEED", self.speed, above=0.)
|
|
|
|
lift_speed = self.get_lift_speed(gcmd)
|
|
|
|
sample_count = gcmd.get_int("SAMPLES", 10, minval=1)
|
|
|
|
sample_retract_dist = gcmd.get_float("SAMPLE_RETRACT_DIST",
|
|
|
|
self.sample_retract_dist, above=0.)
|
2019-02-18 06:23:32 +01:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
pos = toolhead.get_position()
|
2020-04-25 03:51:33 +02:00
|
|
|
gcmd.respond_info("PROBE_ACCURACY at X:%.3f Y:%.3f Z:%.3f"
|
|
|
|
" (samples=%d retract=%.3f"
|
|
|
|
" speed=%.1f lift_speed=%.1f)\n"
|
|
|
|
% (pos[0], pos[1], pos[2],
|
|
|
|
sample_count, sample_retract_dist,
|
|
|
|
speed, lift_speed))
|
2019-07-10 21:11:11 +02:00
|
|
|
# Probe bed sample_count times
|
2020-02-12 19:37:55 +01:00
|
|
|
self.multi_probe_begin()
|
2019-05-31 20:16:59 +02:00
|
|
|
positions = []
|
2019-07-10 21:11:11 +02:00
|
|
|
while len(positions) < sample_count:
|
|
|
|
# Probe position
|
2019-05-31 19:47:15 +02:00
|
|
|
pos = self._probe(speed)
|
2019-05-31 20:16:59 +02:00
|
|
|
positions.append(pos)
|
2019-07-10 21:11:11 +02:00
|
|
|
# Retract
|
|
|
|
liftpos = [None, None, pos[2] + sample_retract_dist]
|
2020-01-30 21:36:51 +01:00
|
|
|
self._move(liftpos, lift_speed)
|
2020-02-12 19:37:55 +01:00
|
|
|
self.multi_probe_end()
|
2019-02-18 06:23:32 +01:00
|
|
|
# Calculate maximum, minimum and average values
|
2019-05-31 20:16:59 +02:00
|
|
|
max_value = max([p[2] for p in positions])
|
|
|
|
min_value = min([p[2] for p in positions])
|
2019-09-29 22:56:46 +02:00
|
|
|
range_value = max_value - min_value
|
2019-05-31 20:16:59 +02:00
|
|
|
avg_value = self._calc_mean(positions)[2]
|
|
|
|
median = self._calc_median(positions)[2]
|
2019-02-18 06:23:32 +01:00
|
|
|
# calculate the standard deviation
|
|
|
|
deviation_sum = 0
|
2019-07-10 21:11:11 +02:00
|
|
|
for i in range(len(positions)):
|
|
|
|
deviation_sum += pow(positions[i][2] - avg_value, 2.)
|
|
|
|
sigma = (deviation_sum / len(positions)) ** 0.5
|
2019-02-18 06:23:32 +01:00
|
|
|
# Show information
|
2020-04-25 03:51:33 +02:00
|
|
|
gcmd.respond_info(
|
2019-09-29 22:56:46 +02:00
|
|
|
"probe accuracy results: maximum %.6f, minimum %.6f, range %.6f, "
|
2019-02-18 06:23:32 +01:00
|
|
|
"average %.6f, median %.6f, standard deviation %.6f" % (
|
2019-09-29 22:56:46 +02:00
|
|
|
max_value, min_value, range_value, avg_value, median, sigma))
|
2019-02-10 00:20:28 +01:00
|
|
|
def probe_calibrate_finalize(self, kin_pos):
|
|
|
|
if kin_pos is None:
|
|
|
|
return
|
2019-03-08 18:34:13 +01:00
|
|
|
z_offset = self.probe_calibrate_z - kin_pos[2]
|
2019-02-10 00:20:28 +01:00
|
|
|
self.gcode.respond_info(
|
|
|
|
"%s: z_offset: %.3f\n"
|
|
|
|
"The SAVE_CONFIG command will update the printer config file\n"
|
2019-03-08 18:34:13 +01:00
|
|
|
"with the above and restart the printer." % (self.name, z_offset))
|
2019-02-10 00:20:28 +01:00
|
|
|
configfile = self.printer.lookup_object('configfile')
|
2019-03-08 18:34:13 +01:00
|
|
|
configfile.set(self.name, 'z_offset', "%.3f" % (z_offset,))
|
2019-02-10 00:20:28 +01:00
|
|
|
cmd_PROBE_CALIBRATE_help = "Calibrate the probe's z_offset"
|
2020-04-25 03:51:33 +02:00
|
|
|
def cmd_PROBE_CALIBRATE(self, gcmd):
|
2019-06-05 02:36:30 +02:00
|
|
|
manual_probe.verify_no_manual_probe(self.printer)
|
2019-02-10 00:20:28 +01:00
|
|
|
# Perform initial probe
|
2020-04-25 03:51:33 +02:00
|
|
|
lift_speed = self.get_lift_speed(gcmd)
|
|
|
|
curpos = self.run_probe(gcmd)
|
2019-02-10 00:20:28 +01:00
|
|
|
# Move away from the bed
|
2019-03-08 18:34:13 +01:00
|
|
|
self.probe_calibrate_z = curpos[2]
|
2019-02-10 00:20:28 +01:00
|
|
|
curpos[2] += 5.
|
2020-01-30 21:36:51 +01:00
|
|
|
self._move(curpos, lift_speed)
|
2019-02-10 00:20:28 +01:00
|
|
|
# Move the nozzle over the probe point
|
|
|
|
curpos[0] += self.x_offset
|
|
|
|
curpos[1] += self.y_offset
|
2019-03-08 18:32:01 +01:00
|
|
|
self._move(curpos, self.speed)
|
2019-02-10 00:20:28 +01:00
|
|
|
# Start manual probe
|
2020-04-25 03:51:33 +02:00
|
|
|
manual_probe.ManualProbeHelper(self.printer, gcmd,
|
2019-02-10 00:20:28 +01:00
|
|
|
self.probe_calibrate_finalize)
|
2021-07-26 02:20:15 +02:00
|
|
|
def cmd_Z_OFFSET_APPLY_PROBE(self,gcmd):
|
|
|
|
z_offset = self.probe_calibrate_z
|
|
|
|
offset = self.gcode_move.get_status()['homing_origin'].z
|
|
|
|
configfile = self.printer.lookup_object('configfile')
|
|
|
|
if offset == 0:
|
|
|
|
self.gcode.respond_info("Nothing to do: Z Offset is 0")
|
|
|
|
else:
|
|
|
|
new_calibrate = self.probe_calibrate_z - offset
|
|
|
|
self.gcode.respond_info(
|
|
|
|
"%s: z_offset: %.3f\n"
|
|
|
|
"The SAVE_CONFIG command will update the printer config file\n"
|
|
|
|
"with the above and restart the printer."
|
|
|
|
% (self.name, new_calibrate))
|
|
|
|
configfile.set(self.name, 'z_offset', "%.3f" % (new_calibrate,))
|
|
|
|
cmd_Z_OFFSET_APPLY_PROBE_help = "Adjust the probe's z_offset"
|
2017-12-06 07:00:33 +01:00
|
|
|
|
2018-09-27 15:47:37 +02:00
|
|
|
# Endstop wrapper that enables probe specific features
|
2018-01-23 18:26:18 +01:00
|
|
|
class ProbeEndstopWrapper:
|
2018-09-27 15:47:37 +02:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.position_endstop = config.getfloat('z_offset')
|
2021-04-24 19:39:58 +02:00
|
|
|
self.stow_on_each_sample = config.getboolean(
|
|
|
|
'deactivate_on_each_sample', True)
|
2020-05-05 20:10:30 +02:00
|
|
|
gcode_macro = self.printer.load_object(config, 'gcode_macro')
|
2019-04-03 04:04:22 +02:00
|
|
|
self.activate_gcode = gcode_macro.load_template(
|
2019-06-07 16:51:30 +02:00
|
|
|
config, 'activate_gcode', '')
|
2019-04-03 04:04:22 +02:00
|
|
|
self.deactivate_gcode = gcode_macro.load_template(
|
2019-06-07 16:51:30 +02:00
|
|
|
config, 'deactivate_gcode', '')
|
2018-09-27 15:47:37 +02:00
|
|
|
# Create an "endstop" object to handle the probe pin
|
|
|
|
ppins = self.printer.lookup_object('pins')
|
|
|
|
pin = config.get('pin')
|
|
|
|
pin_params = ppins.lookup_pin(pin, can_invert=True, can_pullup=True)
|
|
|
|
mcu = pin_params['chip']
|
|
|
|
self.mcu_endstop = mcu.setup_pin('endstop', pin_params)
|
2021-02-17 23:48:03 +01:00
|
|
|
self.printer.register_event_handler('klippy:mcu_identify',
|
|
|
|
self._handle_mcu_identify)
|
2018-01-23 18:26:18 +01:00
|
|
|
# Wrappers
|
|
|
|
self.get_mcu = self.mcu_endstop.get_mcu
|
|
|
|
self.add_stepper = self.mcu_endstop.add_stepper
|
|
|
|
self.get_steppers = self.mcu_endstop.get_steppers
|
|
|
|
self.home_start = self.mcu_endstop.home_start
|
|
|
|
self.home_wait = self.mcu_endstop.home_wait
|
|
|
|
self.query_endstop = self.mcu_endstop.query_endstop
|
2021-04-24 19:39:58 +02:00
|
|
|
# multi probes state
|
|
|
|
self.multi = 'OFF'
|
2021-02-17 23:48:03 +01:00
|
|
|
def _handle_mcu_identify(self):
|
2018-09-27 15:47:37 +02:00
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
2020-01-14 03:39:55 +01:00
|
|
|
for stepper in kin.get_steppers():
|
|
|
|
if stepper.is_active_axis('z'):
|
|
|
|
self.add_stepper(stepper)
|
2021-04-24 19:39:58 +02:00
|
|
|
def raise_probe(self):
|
2019-06-04 00:10:29 +02:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
start_pos = toolhead.get_position()
|
2021-04-24 19:39:58 +02:00
|
|
|
self.deactivate_gcode.run_gcode_from_command()
|
2019-06-04 00:10:29 +02:00
|
|
|
if toolhead.get_position()[:3] != start_pos[:3]:
|
2020-09-04 17:49:43 +02:00
|
|
|
raise self.printer.command_error(
|
2019-06-04 00:10:29 +02:00
|
|
|
"Toolhead moved during probe activate_gcode script")
|
2021-04-24 19:39:58 +02:00
|
|
|
def lower_probe(self):
|
2019-06-04 00:10:29 +02:00
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
start_pos = toolhead.get_position()
|
2021-04-24 19:39:58 +02:00
|
|
|
self.activate_gcode.run_gcode_from_command()
|
2019-06-04 00:10:29 +02:00
|
|
|
if toolhead.get_position()[:3] != start_pos[:3]:
|
2020-09-04 17:49:43 +02:00
|
|
|
raise self.printer.command_error(
|
2019-06-04 00:10:29 +02:00
|
|
|
"Toolhead moved during probe deactivate_gcode script")
|
2021-04-24 19:39:58 +02:00
|
|
|
def multi_probe_begin(self):
|
|
|
|
if self.stow_on_each_sample:
|
|
|
|
return
|
|
|
|
self.multi = 'FIRST'
|
|
|
|
def multi_probe_end(self):
|
|
|
|
if self.stow_on_each_sample:
|
|
|
|
return
|
|
|
|
self.raise_probe()
|
|
|
|
self.multi = 'OFF'
|
|
|
|
def probe_prepare(self, hmove):
|
|
|
|
if self.multi == 'OFF' or self.multi == 'FIRST':
|
|
|
|
self.lower_probe()
|
|
|
|
if self.multi == 'FIRST':
|
|
|
|
self.multi = 'ON'
|
|
|
|
def probe_finish(self, hmove):
|
|
|
|
if self.multi == 'OFF':
|
|
|
|
self.raise_probe()
|
2018-09-27 01:29:58 +02:00
|
|
|
def get_position_endstop(self):
|
2018-09-27 15:47:37 +02:00
|
|
|
return self.position_endstop
|
2018-01-23 18:26:18 +01:00
|
|
|
|
2017-12-04 00:54:34 +01:00
|
|
|
# Helper code that can probe a series of points and report the
|
|
|
|
# position at each point.
|
|
|
|
class ProbePointsHelper:
|
2018-09-26 16:32:57 +02:00
|
|
|
def __init__(self, config, finalize_callback, default_points=None):
|
2018-05-20 17:28:28 +02:00
|
|
|
self.printer = config.get_printer()
|
2018-09-26 16:32:57 +02:00
|
|
|
self.finalize_callback = finalize_callback
|
2018-05-20 17:28:28 +02:00
|
|
|
self.probe_points = default_points
|
2019-05-18 05:47:27 +02:00
|
|
|
self.name = config.get_name()
|
2019-05-31 21:30:24 +02:00
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
2018-05-20 17:28:28 +02:00
|
|
|
# Read config settings
|
|
|
|
if default_points is None or config.get('points', None) is not None:
|
|
|
|
points = config.get('points').split('\n')
|
|
|
|
try:
|
|
|
|
points = [line.split(',', 1) for line in points if line.strip()]
|
|
|
|
self.probe_points = [(float(p[0].strip()), float(p[1].strip()))
|
|
|
|
for p in points]
|
|
|
|
except:
|
|
|
|
raise config.error("Unable to parse probe points in %s" % (
|
2019-05-18 05:47:27 +02:00
|
|
|
self.name))
|
2018-05-20 17:28:28 +02:00
|
|
|
self.horizontal_move_z = config.getfloat('horizontal_move_z', 5.)
|
2019-05-31 21:30:24 +02:00
|
|
|
self.speed = config.getfloat('speed', 50., above=0.)
|
2019-11-14 22:37:31 +01:00
|
|
|
self.use_offsets = False
|
2018-05-20 17:28:28 +02:00
|
|
|
# Internal probing state
|
2019-05-31 21:30:24 +02:00
|
|
|
self.lift_speed = self.speed
|
|
|
|
self.probe_offsets = (0., 0., 0.)
|
2017-12-04 00:54:34 +01:00
|
|
|
self.results = []
|
2019-05-18 05:47:27 +02:00
|
|
|
def minimum_points(self,n):
|
|
|
|
if len(self.probe_points) < n:
|
|
|
|
raise self.printer.config_error(
|
|
|
|
"Need at least %d probe points for %s" % (n, self.name))
|
2020-08-24 11:28:33 +02:00
|
|
|
def update_probe_points(self, points, min_points):
|
|
|
|
self.probe_points = points
|
|
|
|
self.minimum_points(min_points)
|
2019-11-14 22:37:31 +01:00
|
|
|
def use_xy_offsets(self, use_offsets):
|
|
|
|
self.use_offsets = use_offsets
|
2018-05-21 20:48:01 +02:00
|
|
|
def get_lift_speed(self):
|
|
|
|
return self.lift_speed
|
2019-05-31 21:30:24 +02:00
|
|
|
def _move_next(self):
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
2018-08-25 16:55:12 +02:00
|
|
|
# Lift toolhead
|
2019-05-31 21:30:24 +02:00
|
|
|
speed = self.lift_speed
|
|
|
|
if not self.results:
|
|
|
|
# Use full speed to first probe position
|
|
|
|
speed = self.speed
|
2020-08-17 04:43:03 +02:00
|
|
|
toolhead.manual_move([None, None, self.horizontal_move_z], speed)
|
2018-09-26 17:25:56 +02:00
|
|
|
# Check if done probing
|
2018-09-26 18:15:39 +02:00
|
|
|
if len(self.results) >= len(self.probe_points):
|
2019-05-31 21:30:24 +02:00
|
|
|
toolhead.get_last_move_time()
|
2019-05-24 01:20:20 +02:00
|
|
|
res = self.finalize_callback(self.probe_offsets, self.results)
|
|
|
|
if res != "retry":
|
|
|
|
return True
|
|
|
|
self.results = []
|
2018-09-26 17:25:56 +02:00
|
|
|
# Move to next XY probe point
|
2020-08-17 04:43:03 +02:00
|
|
|
nextpos = list(self.probe_points[len(self.results)])
|
2019-11-14 22:37:31 +01:00
|
|
|
if self.use_offsets:
|
2020-08-17 04:43:03 +02:00
|
|
|
nextpos[0] -= self.probe_offsets[0]
|
|
|
|
nextpos[1] -= self.probe_offsets[1]
|
|
|
|
toolhead.manual_move(nextpos, self.speed)
|
2019-05-31 21:30:24 +02:00
|
|
|
return False
|
2020-04-25 03:51:33 +02:00
|
|
|
def start_probe(self, gcmd):
|
2019-06-05 02:36:30 +02:00
|
|
|
manual_probe.verify_no_manual_probe(self.printer)
|
2018-09-26 19:07:18 +02:00
|
|
|
# Lookup objects
|
|
|
|
probe = self.printer.lookup_object('probe', None)
|
2020-04-25 03:51:33 +02:00
|
|
|
method = gcmd.get('METHOD', 'automatic').lower()
|
2019-05-31 21:30:24 +02:00
|
|
|
self.results = []
|
|
|
|
if probe is None or method != 'automatic':
|
|
|
|
# Manual probe
|
2018-09-26 19:07:18 +02:00
|
|
|
self.lift_speed = self.speed
|
|
|
|
self.probe_offsets = (0., 0., 0.)
|
2019-05-31 21:30:24 +02:00
|
|
|
self._manual_probe_start()
|
|
|
|
return
|
|
|
|
# Perform automatic probing
|
2020-04-25 03:51:33 +02:00
|
|
|
self.lift_speed = probe.get_lift_speed(gcmd)
|
2019-05-31 21:30:24 +02:00
|
|
|
self.probe_offsets = probe.get_offsets()
|
|
|
|
if self.horizontal_move_z < self.probe_offsets[2]:
|
2020-04-25 03:51:33 +02:00
|
|
|
raise gcmd.error("horizontal_move_z can't be less than"
|
|
|
|
" probe's z_offset")
|
2020-02-12 19:37:55 +01:00
|
|
|
probe.multi_probe_begin()
|
2019-05-31 21:30:24 +02:00
|
|
|
while 1:
|
|
|
|
done = self._move_next()
|
|
|
|
if done:
|
|
|
|
break
|
2020-04-25 03:51:33 +02:00
|
|
|
pos = probe.run_probe(gcmd)
|
2019-05-31 21:30:24 +02:00
|
|
|
self.results.append(pos)
|
2020-02-12 19:37:55 +01:00
|
|
|
probe.multi_probe_end()
|
2019-05-31 21:30:24 +02:00
|
|
|
def _manual_probe_start(self):
|
|
|
|
done = self._move_next()
|
|
|
|
if not done:
|
2020-04-22 18:40:32 +02:00
|
|
|
gcmd = self.gcode.create_gcode_command("", "", {})
|
|
|
|
manual_probe.ManualProbeHelper(self.printer, gcmd,
|
2019-05-31 21:30:24 +02:00
|
|
|
self._manual_probe_finalize)
|
2019-02-10 04:54:24 +01:00
|
|
|
def _manual_probe_finalize(self, kin_pos):
|
|
|
|
if kin_pos is None:
|
|
|
|
return
|
|
|
|
self.results.append(kin_pos)
|
2019-05-31 21:30:24 +02:00
|
|
|
self._manual_probe_start()
|
2017-12-04 00:54:34 +01:00
|
|
|
|
2017-12-06 07:00:33 +01:00
|
|
|
def load_config(config):
|
2018-11-25 03:41:25 +01:00
|
|
|
return PrinterProbe(config, ProbeEndstopWrapper(config))
|