skew_correction: Use new GCodeCommand wrappers

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-04-24 19:44:05 -04:00
parent 433c8c937a
commit 48ebb6959d
1 changed files with 40 additions and 43 deletions

View File

@ -20,7 +20,6 @@ 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.
@ -30,20 +29,19 @@ class PrinterSkew:
self.printer.register_event_handler("klippy:ready",
self._handle_ready)
self.next_transform = None
self.gcode.register_command(
'GET_CURRENT_SKEW', self.cmd_GET_CURRENT_SKEW,
desc=self.cmd_GET_CURRENT_SKEW_help)
self.gcode.register_command(
'CALC_MEASURED_SKEW', self.cmd_CALC_MEASURED_SKEW,
desc=self.cmd_CALC_MEASURED_SKEW_help)
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)
gcode = self.printer.lookup_object('gcode')
gcode.register_command('GET_CURRENT_SKEW', self.cmd_GET_CURRENT_SKEW,
desc=self.cmd_GET_CURRENT_SKEW_help)
gcode.register_command('CALC_MEASURED_SKEW',
self.cmd_CALC_MEASURED_SKEW,
desc=self.cmd_CALC_MEASURED_SKEW_help)
gcode.register_command('SET_SKEW', self.cmd_SET_SKEW,
desc=self.cmd_SET_SKEW_help)
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)
gcode = self.printer.lookup_object('gcode')
self.next_transform = 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
@ -72,7 +70,7 @@ class PrinterSkew:
corrected_pos = self.calc_skew(newpos)
self.next_transform.move(corrected_pos, speed)
cmd_GET_CURRENT_SKEW_help = "Report current printer skew"
def cmd_GET_CURRENT_SKEW(self, params):
def cmd_GET_CURRENT_SKEW(self, gcmd):
out = "Current Printer Skew:"
planes = ["XY", "XZ", "YZ"]
factors = [self.xy_factor, self.xz_factor, self.yz_factor]
@ -80,26 +78,25 @@ class PrinterSkew:
out += '\n' + plane
out += " Skew: %.6f radians, %.2f degrees" % (
fac, math.degrees(fac))
self.gcode.respond_info(out)
gcmd.respond_info(out)
cmd_CALC_MEASURED_SKEW_help = "Calculate skew from measured print"
def cmd_CALC_MEASURED_SKEW(self, params):
ac = self.gcode.get_float("AC", params, above=0.)
bd = self.gcode.get_float("BD", params, above=0.)
ad = self.gcode.get_float("AD", params, above=0.)
def cmd_CALC_MEASURED_SKEW(self, gcmd):
ac = gcmd.get_float("AC", above=0.)
bd = gcmd.get_float("BD", above=0.)
ad = gcmd.get_float("AD", above=0.)
factor = calc_skew_factor(ac, bd, ad)
self.gcode.respond_info(
"Calculated Skew: %.6f radians, %.2f degrees" %
(factor, math.degrees(factor)))
gcmd.respond_info("Calculated Skew: %.6f radians, %.2f degrees"
% (factor, math.degrees(factor)))
cmd_SET_SKEW_help = "Set skew based on lengths of measured object"
def cmd_SET_SKEW(self, params):
if self.gcode.get_int("CLEAR", params, 0):
def cmd_SET_SKEW(self, gcmd):
if gcmd.get_int("CLEAR", 0):
self.xy_factor = 0.
self.xz_factor = 0.
self.yz_factor = 0.
return
planes = ["XY", "XZ", "YZ"]
for plane in planes:
lengths = self.gcode.get_str(plane, params, None)
lengths = gcmd.get(plane, None)
if lengths is not None:
try:
lengths = lengths.strip().split(",", 2)
@ -107,25 +104,25 @@ class PrinterSkew:
if len(lengths) != 3:
raise Exception
except Exception:
raise self.gcode.error(
raise gcmd.error(
"skew_correction: improperly formatted entry for "
"plane [%s]\n%s" % (plane, params['#original']))
"plane [%s]\n%s" % (plane, gcmd.get_commandline()))
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)
def cmd_SKEW_PROFILE(self, gcmd):
if gcmd.get('LOAD', None) is not None:
name = gcmd.get('LOAD')
if name not in self.skew_profiles:
self.gcode.respond_info(
"skew_correction: Load failed, unknown profile [%s]" %
(name))
gcmd.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)
elif gcmd.get('SAVE', None) is not None:
name = gcmd.get('SAVE')
configfile = self.printer.lookup_object('configfile')
cfg_name = self.name + " " + name
configfile.set(cfg_name, 'xy_skew', self.xy_factor)
@ -137,25 +134,25 @@ class PrinterSkew:
'xz_skew': self.xz_factor,
'yz_skew': self.yz_factor
}
self.gcode.respond_info(
gcmd.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)
elif gcmd.get('REMOVE', None) is not None:
name = gmcd.get('REMOVE')
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(
gcmd.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))
gcmd.respond_info(
"skew_correction: No profile named [%s] to remove"
% (name))
def load_config(config):