skew_correction: add SKEW_PROFILE gcode

Add profile managment similar to that of bed_mesh.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2019-06-28 14:29:30 -04:00 committed by KevinOConnor
parent 026752e399
commit 0c379d060f
1 changed files with 63 additions and 0 deletions

View File

@ -19,11 +19,14 @@ def calc_skew_factor(ac, bd, ad):
class PrinterSkew:
def __init__(self, config):
self.printer = config.get_printer()
self.name = config.get_name()
self.gcode = self.printer.lookup_object('gcode')
self.toolhead = None
self.xy_factor = 0.
self.xz_factor = 0.
self.yz_factor = 0.
self.skew_profiles = {}
self._load_storage(config)
self.printer.register_event_handler("klippy:ready",
self._handle_ready)
self.next_transform = None
@ -36,8 +39,23 @@ class PrinterSkew:
self.gcode.register_command(
'SET_SKEW', self.cmd_SET_SKEW,
desc=self.cmd_SET_SKEW_help)
self.gcode.register_command(
'SKEW_PROFILE', self.cmd_SKEW_PROFILE,
desc=self.cmd_SKEW_PROFILE_help)
def _handle_ready(self):
self.next_transform = self.gcode.set_move_transform(self, force=True)
def _load_storage(self, config):
stored_profs = config.get_prefix_sections(self.name)
# Remove primary skew_correction section, as it is not a stored profile
stored_profs = [s for s in stored_profs
if s.get_name() != self.name]
for profile in stored_profs:
name = profile.get_name().split(' ', 1)[1]
self.skew_profiles[name] = {
'xy_skew': profile.getfloat("xy_skew"),
'xz_skew': profile.getfloat("xz_skew"),
'yz_skew': profile.getfloat("yz_skew"),
}
def calc_skew(self, pos):
skewed_x = pos[0] - pos[1] * self.xy_factor \
- pos[2] * (self.xz_factor - (self.xy_factor * self.yz_factor))
@ -94,6 +112,51 @@ class PrinterSkew:
"plane [%s]\n%s" % (plane, params['#original']))
factor = plane.lower() + '_factor'
setattr(self, factor, calc_skew_factor(*lengths))
cmd_SKEW_PROFILE_help = "Profile management for skew_correction"
def cmd_SKEW_PROFILE(self, params):
if 'LOAD' in params:
name = self.gcode.get_str('LOAD', params)
if name not in self.skew_profiles:
self.gcode.respond_info(
"skew_correction: Load failed, unknown profile [%s]" %
(name))
return
self.xy_factor = self.skew_profiles[name]['xy_skew']
self.xz_factor = self.skew_profiles[name]['xz_skew']
self.yz_factor = self.skew_profiles[name]['yz_skew']
elif 'SAVE' in params:
name = self.gcode.get_str('SAVE', params)
configfile = self.printer.lookup_object('configfile')
cfg_name = self.name + " " + name
configfile.set(cfg_name, 'xy_skew', self.xy_factor)
configfile.set(cfg_name, 'xz_skew', self.xz_factor)
configfile.set(cfg_name, 'yz_skew', self.yz_factor)
# Copy to local storage
self.skew_profiles[name] = {
'xy_skew': self.xy_factor,
'xz_skew': self.xz_factor,
'yz_skew': self.yz_factor
}
self.gcode.respond_info(
"Skew Correction state has been saved to profile [%s]\n"
"for the current session. The SAVE_CONFIG command will\n"
"update the printer config file and restart the printer."
% (name))
elif 'REMOVE' in params:
name = self.gcode.get_str('REMOVE', params)
if name in self.skew_profiles:
configfile = self.printer.lookup_object('configfile')
configfile.remove_section('skew_correction ' + name)
del self.skew_profiles[name]
self.gcode.respond_info(
"Profile [%s] removed from storage for this session.\n"
"The SAVE_CONFIG command will update the printer\n"
"configuration and restart the printer" % (name))
else:
self.gcode.respond_info(
"skew_correction: No profile named [%s] to remove" %
(name))
def load_config(config):
return PrinterSkew(config)