2017-12-04 02:54:34 +03:00
|
|
|
# Delta calibration support
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017-2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import math, logging
|
2018-07-14 18:35:13 +03:00
|
|
|
import probe, mathutil
|
2017-12-04 02:54:34 +03:00
|
|
|
|
|
|
|
class DeltaCalibrate:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
if config.getsection('printer').get('kinematics') != 'delta':
|
|
|
|
raise config.error("Delta calibrate is only for delta printers")
|
2018-05-20 18:28:28 +03:00
|
|
|
# Calculate default probing points
|
|
|
|
radius = config.getfloat('radius', above=0.)
|
|
|
|
points = [(0., 0.)]
|
|
|
|
scatter = [.95, .90, .85, .70, .75, .80]
|
|
|
|
for i in range(6):
|
|
|
|
r = math.radians(90. + 60. * i)
|
|
|
|
dist = radius * scatter[i]
|
|
|
|
points.append((math.cos(r) * dist, math.sin(r) * dist))
|
|
|
|
self.probe_helper = probe.ProbePointsHelper(
|
|
|
|
config, self, default_points=points)
|
|
|
|
# Register DELTA_CALIBRATE command
|
2017-12-04 02:54:34 +03:00
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
|
|
|
self.gcode.register_command(
|
|
|
|
'DELTA_CALIBRATE', self.cmd_DELTA_CALIBRATE,
|
|
|
|
desc=self.cmd_DELTA_CALIBRATE_help)
|
|
|
|
cmd_DELTA_CALIBRATE_help = "Delta calibration script"
|
|
|
|
def cmd_DELTA_CALIBRATE(self, params):
|
2018-06-30 21:08:02 +03:00
|
|
|
self.gcode.run_script_from_command("G28")
|
2018-05-20 18:28:28 +03:00
|
|
|
self.probe_helper.start_probe()
|
2018-06-22 22:33:12 +03:00
|
|
|
def get_probed_position(self):
|
2017-12-04 02:54:34 +03:00
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
|
|
|
return kin.get_stable_position()
|
2018-08-18 19:25:57 +03:00
|
|
|
def finalize(self, offsets, positions):
|
|
|
|
z_offset = offsets[2]
|
2017-12-04 02:54:34 +03:00
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
2018-02-16 21:30:49 +03:00
|
|
|
logging.info("Calculating delta_calibrate with: %s", positions)
|
2017-12-04 02:54:34 +03:00
|
|
|
params = kin.get_calibrate_params()
|
2018-02-16 21:30:49 +03:00
|
|
|
logging.info("Initial delta_calibrate parameters: %s", params)
|
2017-12-04 02:54:34 +03:00
|
|
|
adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius',
|
|
|
|
'angle_a', 'angle_b')
|
|
|
|
def delta_errorfunc(params):
|
|
|
|
total_error = 0.
|
2018-07-14 18:35:13 +03:00
|
|
|
for x, y, z in kin.get_positions_from_stable(positions, params):
|
2018-03-17 21:00:37 +03:00
|
|
|
total_error += (z - z_offset)**2
|
2017-12-04 02:54:34 +03:00
|
|
|
return total_error
|
2018-03-03 02:18:35 +03:00
|
|
|
new_params = mathutil.coordinate_descent(
|
2017-12-04 02:54:34 +03:00
|
|
|
adj_params, params, delta_errorfunc)
|
2018-02-16 21:30:49 +03:00
|
|
|
logging.info("Calculated delta_calibrate parameters: %s", new_params)
|
2018-07-14 18:35:13 +03:00
|
|
|
old_positions = kin.get_positions_from_stable(positions, params)
|
|
|
|
new_positions = kin.get_positions_from_stable(positions, new_params)
|
|
|
|
for oldpos, newpos in zip(old_positions, new_positions):
|
|
|
|
logging.info("orig: %s new: %s", oldpos, newpos)
|
2017-12-04 02:54:34 +03:00
|
|
|
self.gcode.respond_info(
|
|
|
|
"stepper_a: position_endstop: %.6f angle: %.6f\n"
|
|
|
|
"stepper_b: position_endstop: %.6f angle: %.6f\n"
|
|
|
|
"stepper_c: position_endstop: %.6f angle: %.6f\n"
|
2018-07-22 18:47:49 +03:00
|
|
|
"delta_radius: %.6f\n"
|
2017-12-04 02:54:34 +03:00
|
|
|
"To use these parameters, update the printer config file with\n"
|
|
|
|
"the above and then issue a RESTART command" % (
|
|
|
|
new_params['endstop_a'], new_params['angle_a'],
|
|
|
|
new_params['endstop_b'], new_params['angle_b'],
|
|
|
|
new_params['endstop_c'], new_params['angle_c'],
|
|
|
|
new_params['radius']))
|
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return DeltaCalibrate(config)
|