2018-01-21 05:47:36 +01:00
|
|
|
# Bed tilt compensation
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging
|
2018-03-03 00:18:35 +01:00
|
|
|
import probe, mathutil
|
2018-01-21 05:47:36 +01:00
|
|
|
|
|
|
|
class BedTilt:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2019-01-08 17:09:55 +01:00
|
|
|
self.printer.register_event_handler("klippy:connect",
|
|
|
|
self.handle_connect)
|
2018-01-21 05:47:36 +01:00
|
|
|
self.x_adjust = config.getfloat('x_adjust', 0.)
|
|
|
|
self.y_adjust = config.getfloat('y_adjust', 0.)
|
2018-08-18 17:28:42 +02:00
|
|
|
self.z_adjust = config.getfloat('z_adjust', 0.)
|
2018-01-21 05:47:36 +01:00
|
|
|
if config.get('points', None) is not None:
|
|
|
|
BedTiltCalibrate(config, self)
|
|
|
|
self.toolhead = None
|
2018-08-18 17:28:42 +02:00
|
|
|
# Register move transform with g-code class
|
2018-01-21 05:47:36 +01:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.set_move_transform(self)
|
2019-01-08 17:09:55 +01:00
|
|
|
def handle_connect(self):
|
|
|
|
self.toolhead = self.printer.lookup_object('toolhead')
|
2018-01-21 05:47:36 +01:00
|
|
|
def get_position(self):
|
|
|
|
x, y, z, e = self.toolhead.get_position()
|
2018-04-03 18:02:17 +02:00
|
|
|
return [x, y, z - x*self.x_adjust - y*self.y_adjust - self.z_adjust, e]
|
2018-01-21 05:47:36 +01:00
|
|
|
def move(self, newpos, speed):
|
|
|
|
x, y, z, e = newpos
|
2018-04-03 18:02:17 +02:00
|
|
|
self.toolhead.move([x, y, z + x*self.x_adjust + y*self.y_adjust
|
|
|
|
+ self.z_adjust, e], speed)
|
2018-08-18 17:28:42 +02:00
|
|
|
def update_adjust(self, x_adjust, y_adjust, z_adjust):
|
|
|
|
self.x_adjust = x_adjust
|
|
|
|
self.y_adjust = y_adjust
|
|
|
|
self.z_adjust = z_adjust
|
|
|
|
configfile = self.printer.lookup_object('configfile')
|
|
|
|
configfile.set('bed_tilt', 'x_adjust', "%.6f" % (x_adjust,))
|
|
|
|
configfile.set('bed_tilt', 'y_adjust', "%.6f" % (y_adjust,))
|
|
|
|
configfile.set('bed_tilt', 'z_adjust', "%.6f" % (z_adjust,))
|
2018-01-21 05:47:36 +01:00
|
|
|
|
|
|
|
# Helper script to calibrate the bed tilt
|
|
|
|
class BedTiltCalibrate:
|
|
|
|
def __init__(self, config, bedtilt):
|
|
|
|
self.printer = config.get_printer()
|
2018-05-20 17:28:28 +02:00
|
|
|
self.bedtilt = bedtilt
|
2018-09-26 16:32:57 +02:00
|
|
|
self.probe_helper = probe.ProbePointsHelper(config, self.probe_finalize)
|
2018-05-20 17:28:28 +02:00
|
|
|
# Register BED_TILT_CALIBRATE command
|
2018-01-21 05:47:36 +01:00
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
|
|
|
self.gcode.register_command(
|
|
|
|
'BED_TILT_CALIBRATE', self.cmd_BED_TILT_CALIBRATE,
|
|
|
|
desc=self.cmd_BED_TILT_CALIBRATE_help)
|
|
|
|
cmd_BED_TILT_CALIBRATE_help = "Bed tilt calibration script"
|
|
|
|
def cmd_BED_TILT_CALIBRATE(self, params):
|
2018-09-26 19:07:18 +02:00
|
|
|
self.probe_helper.start_probe(params)
|
2018-09-26 16:32:57 +02:00
|
|
|
def probe_finalize(self, offsets, positions):
|
2018-09-26 19:38:35 +02:00
|
|
|
# Setup for coordinate descent analysis
|
2018-08-18 18:25:57 +02:00
|
|
|
z_offset = offsets[2]
|
2018-02-16 19:30:49 +01:00
|
|
|
logging.info("Calculating bed_tilt with: %s", positions)
|
2018-01-21 05:47:36 +01:00
|
|
|
params = { 'x_adjust': self.bedtilt.x_adjust,
|
|
|
|
'y_adjust': self.bedtilt.y_adjust,
|
2018-03-17 19:00:37 +01:00
|
|
|
'z_adjust': z_offset }
|
2018-02-16 19:30:49 +01:00
|
|
|
logging.info("Initial bed_tilt parameters: %s", params)
|
2018-09-26 19:38:35 +02:00
|
|
|
# Perform coordinate descent
|
2018-01-21 05:47:36 +01:00
|
|
|
def adjusted_height(pos, params):
|
|
|
|
x, y, z = pos
|
2018-02-05 20:52:38 +01:00
|
|
|
return (z - x*params['x_adjust'] - y*params['y_adjust']
|
2018-01-21 05:47:36 +01:00
|
|
|
- params['z_adjust'])
|
|
|
|
def errorfunc(params):
|
|
|
|
total_error = 0.
|
|
|
|
for pos in positions:
|
|
|
|
total_error += adjusted_height(pos, params)**2
|
|
|
|
return total_error
|
2018-03-03 00:18:35 +01:00
|
|
|
new_params = mathutil.coordinate_descent(
|
|
|
|
params.keys(), params, errorfunc)
|
2018-04-03 18:02:17 +02:00
|
|
|
# Update current bed_tilt calculations
|
2018-09-26 20:26:55 +02:00
|
|
|
x_adjust = new_params['x_adjust']
|
|
|
|
y_adjust = new_params['y_adjust']
|
|
|
|
z_adjust = (new_params['z_adjust'] - z_offset
|
|
|
|
- x_adjust * offsets[0] - y_adjust * offsets[1])
|
|
|
|
self.bedtilt.update_adjust(x_adjust, y_adjust, z_adjust)
|
2018-04-03 18:02:17 +02:00
|
|
|
self.gcode.reset_last_position()
|
2018-09-26 19:38:35 +02:00
|
|
|
# Log and report results
|
|
|
|
logging.info("Calculated bed_tilt parameters: %s", new_params)
|
|
|
|
for pos in positions:
|
|
|
|
logging.info("orig: %s new: %s", adjusted_height(pos, params),
|
|
|
|
adjusted_height(pos, new_params))
|
|
|
|
msg = "x_adjust: %.6f y_adjust: %.6f z_adjust: %.6f" % (
|
2018-09-26 20:26:55 +02:00
|
|
|
x_adjust, y_adjust, z_adjust)
|
2018-04-03 18:02:17 +02:00
|
|
|
self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg,))
|
2018-01-21 05:47:36 +01:00
|
|
|
self.gcode.respond_info(
|
2018-04-03 18:02:17 +02:00
|
|
|
"%s\nThe above parameters have been applied to the current\n"
|
2018-08-18 17:28:42 +02:00
|
|
|
"session. The SAVE_CONFIG command will update the printer\n"
|
|
|
|
"config file and restart the printer." % (msg,))
|
2018-01-21 05:47:36 +01:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return BedTilt(config)
|