-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_v2settling_probe.py
186 lines (165 loc) · 8.73 KB
/
z_v2settling_probe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Custom Z-Probe object that allow for a single settling probe
# prior to sampling.
#
# Copyright (C) 2023 Mitko Haralanov <voidtrance@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
from .probe import PrinterProbe
import pins
import configparser
from .z_calibration import ZCalibrationHelper
import logging
class V2SettlingProbe(PrinterProbe):
def __init__(self, config):
self.printer = config.get_printer()
probe_config = config.getsection('probe')
try:
probe_obj = self.printer.lookup_object('probe')
mcu_probe = probe_obj.mcu_probe
except configparser.Error:
raise configparser.Error(
"Section 'z_v2settling_probe' should appear after 'probe' section")
#figure out a way to determine which type of printer we are running on
#only call __init__ for z_v2settling_probe extension for the correct type of printer
atypes = { 'none' : None, 'cartesian' : 'cartesian', 'corexy' : 'corexy',
'corexz' : 'corexz', 'hybrid_corexy' : 'hybrid_corexy', 'hybrid_corexz' : 'hybrid_corexz',
'rotary_delta' : 'rotary_delta', 'delta' : 'delta', 'deltesian' : 'deltesian',
'polar' : 'polar', 'winch' : 'winch' }
kinematics_config = config.getsection('printer')
kinematics_choice = kinematics_config.getchoice('kinematics', atypes, 'none')
if kinematics_choice in ['corexy','cartesian','corexz','hybrid_corexy','hybrid_corexz', 'rotary_delta', 'delta', 'deltesian', 'polar']:
logging.info("z_v2settling_probe ::INFO:: Printer has the following "
"kinematics: %s" % (kinematics_choice))
else:
logging.info("z_v2settling_probe ::ERROR:: Printer has the following "
"kinematics:\n kinematics = %s\n"
"This printer is not allowed to use the "
"z_v2settling_probe extension (plugin)" % (kinematics_choice))
raise self.config.error("z_v2settling_probe ::ERROR:: This printer type is "
"not allowed to use %s plugin (extension)"
% (config.get_name()))
# Unregister any pre-existing probe commands first.
gcode = self.printer.lookup_object('gcode')
gcode.register_command('PROBE', None)
gcode.register_command('QUERY_PROBE', None)
gcode.register_command('PROBE_CALIBRATE', None)
gcode.register_command('PROBE_ACCURACY', None)
gcode.register_command('Z_OFFSET_APPLY_PROBE', None)
# Remove the already-registered 'probe' pin. It will be
# replaced by this instance.
pins = self.printer.lookup_object('pins')
pins.chips.pop('probe')
pins.pin_resolvers.pop('probe')
PrinterProbe.__init__(self, probe_config, mcu_probe)
self.settling_sample = config.getboolean('settling_sample', False)
self.printer.register_event_handler("klippy:ready", self.handle_ready)
def handle_ready(self):
# This is the hacky bit:
# The "klippy:ready" event is sent after all configuration has been
# read and all objects created. So, we are sure that the 'probe'
# object already exists.
# So, we can reach into the printer objects and replace the existing 'probe'
# object with this one.
probe_obj = self.printer.objects.pop('probe', None)
self.printer.objects['probe'] = self
del probe_obj
def _run_settling_probe(self, gcmd):
gcmd.respond_info("Settling sample (ignored)...")
speed = gcmd.get_float("PROBE_SPEED", self.speed, above=0.)
lift_speed = self.get_lift_speed(gcmd)
sample_retract_dist = gcmd.get_float("SAMPLE_RETRACT_DIST",
self.sample_retract_dist, minval=0.)
pos = self._probe(speed)
pos[2] += sample_retract_dist
self._move(pos, lift_speed)
def run_probe(self, gcmd):
# The following use self.run_probe : PrinterProbe.cmd_PROBE; PrinterProbe.cmd_PROBE_CALIBRATE;
# and probe.ProbePointsHelper.start_probe
if gcmd.get_int("SETTLING_SAMPLE", self.settling_sample):
self._run_settling_probe(gcmd)
return PrinterProbe.run_probe(self, gcmd)
def cmd_PROBE_ACCURACY(self, gcmd):
# the PrinterProbe.cmd_PROBE_ACCURACY calls self._probe(speed) not self.run_probe(gcmd)
if gcmd.get_int("SETTLING_SAMPLE", self.settling_sample):
self._run_settling_probe(gcmd)
return PrinterProbe.cmd_PROBE_ACCURACY(self, gcmd)
class V2SettlingZCalibrationHelper(ZCalibrationHelper):
def __init__(self, config):
self.printer = config.get_printer()
z_calibration_config = config.getsection('z_calibration')
# Unregister any pre-existing z_calibration commands first.
self.gcode = self.printer.lookup_object('gcode')
self.gcode.register_command('CALIBRATE_Z', None)
self.gcode.register_command('PROBE_Z_ACCURACY', None)
ZCalibrationHelper.__init__(self, z_calibration_config)
self.printer.register_event_handler("klippy:ready", self.handle_ready)
def handle_ready(self):
self.probe = self.printer.lookup_object('probe')
self.settling_sample = self.probe.settling_sample
# This is the hacky bit:
# The "klippy:ready" event is sent after all configuration has been
# read and all objects created. So, we are sure that the 'z_calibration'
# object already exists.
# So, we can reach into the printer objects and replace the existing 'z_calibration'
# object with this one.
z_calibration_obj = self.printer.objects.pop('z_calibration', None)
self.printer.objects['z_calibration'] = self
del z_calibration_obj
def move_to_z_endstop(self, gcmd, lift_speed):
if self.z_homing is None:
raise gcmd.error("Must home axes first")
toolhead = self.printer.lookup_object('toolhead')
pos = toolhead.get_position()
if pos[2] < self.clearance:
# no clearance, better to move up
pos[2] += self.clearance
self._move(pos, lift_speed)
# move to z-endstop position
self._move(list(self.nozzle_site), self.speed)
def _test_cmdline_param(self, gcmd):
try:
gcmd.get_int("SETTLING_SAMPLE")
except:
pass
else:
gcmd.respond_info("Ignoring parameter 'SETTLING_SAMPLE'")
def _message_first_fast(self, gcmd):
gcmd.respond_info("Settling sample (ignored)...for each probe location [endstop, switch body, and bed] ...")
self._test_cmdline_param(gcmd)
# cmd_CALIBRATE_Z will always discard the settling_sample as long as self.first_fast is set to true
def cmd_CALIBRATE_Z(self, gcmd):
global_first_fast = self.first_fast
if global_first_fast:
self._message_first_fast(gcmd)
return ZCalibrationHelper.cmd_CALIBRATE_Z(self, gcmd)
cmd_CALIBRATE_Z_settling_sample = gcmd.get_int("SETTLING_SAMPLE", self.settling_sample)
# if first_fast is True than the first sample is already discarded!
if cmd_CALIBRATE_Z_settling_sample:
self.first_fast = True
gcmd.respond_info("Settling sample (ignored)...for each probe location [endstop, switch body, and bed] ...")
ret = ZCalibrationHelper.cmd_CALIBRATE_Z(self, gcmd)
self.first_fast = False
else:
return ZCalibrationHelper.cmd_CALIBRATE_Z(self, gcmd)
def cmd_PROBE_Z_ACCURACY(self, gcmd):
speed = gcmd.get_float("PROBE_SPEED", self.second_speed, above=0.)
lift_speed = gcmd.get_float("LIFT_SPEED", self.lift_speed, above=0.)
if gcmd.get_int("SETTLING_SAMPLE", self.settling_sample):
self.move_to_z_endstop(gcmd, lift_speed)
gcmd.respond_info("Settling sample (ignored)...")
#self._probe() does a probe and a retract
pos = self._probe(self.z_endstop, self.position_min, speed)
return ZCalibrationHelper.cmd_PROBE_Z_ACCURACY(self, gcmd)
def load_config(config):
try:
z_calibration_obj = config.get_printer().lookup_object('z_calibration')
except:
logging.info("z_v2settling_probe ::WARNING:: PROBE_Z_ACCURACY"
" command will not be available!\n"
"CALIBRATE_Z command will not be available!\n"
"This printer does not use the Klipper plugin for "
"the self calibrating Z offset!")
return V2SettlingProbe(config)
else:
return V2SettlingProbe(config), V2SettlingZCalibrationHelper(config)