2017-12-04 00:54:34 +01: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 17:35:13 +02:00
|
|
|
import probe, mathutil
|
2017-12-04 00:54:34 +01: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 17:28:28 +02: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 00:54:34 +01: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 20:08:02 +02:00
|
|
|
self.gcode.run_script_from_command("G28")
|
2018-05-20 17:28:28 +02:00
|
|
|
self.probe_helper.start_probe()
|
2018-06-22 21:33:12 +02:00
|
|
|
def get_probed_position(self):
|
2017-12-04 00:54:34 +01:00
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
|
|
|
return kin.get_stable_position()
|
2018-08-18 18:25:57 +02:00
|
|
|
def finalize(self, offsets, positions):
|
|
|
|
z_offset = offsets[2]
|
2017-12-04 00:54:34 +01:00
|
|
|
kin = self.printer.lookup_object('toolhead').get_kinematics()
|
2018-02-16 19:30:49 +01:00
|
|
|
logging.info("Calculating delta_calibrate with: %s", positions)
|
2017-12-04 00:54:34 +01:00
|
|
|
params = kin.get_calibrate_params()
|
2018-02-16 19:30:49 +01:00
|
|
|
logging.info("Initial delta_calibrate parameters: %s", params)
|
2017-12-04 00:54:34 +01:00
|
|
|
adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius',
|
|
|
|
'angle_a', 'angle_b')
|
|
|
|
def delta_errorfunc(params):
|
|
|
|
total_error = 0.
|
2018-07-14 17:35:13 +02:00
|
|
|
for x, y, z in kin.get_positions_from_stable(positions, params):
|
2018-03-17 19:00:37 +01:00
|
|
|
total_error += (z - z_offset)**2
|
2017-12-04 00:54:34 +01:00
|
|
|
return total_error
|
2018-03-03 00:18:35 +01:00
|
|
|
new_params = mathutil.coordinate_descent(
|
2017-12-04 00:54:34 +01:00
|
|
|
adj_params, params, delta_errorfunc)
|
2018-02-16 19:30:49 +01:00
|
|
|
logging.info("Calculated delta_calibrate parameters: %s", new_params)
|
2018-07-14 17:35:13 +02: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 00:54:34 +01: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 17:47:49 +02:00
|
|
|
"delta_radius: %.6f\n"
|
2017-12-04 00:54:34 +01: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)
|