From 8faab46ed2fc05495e63bbca8fe3dfa6828f7db3 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Thu, 12 Jul 2018 22:15:45 -0400 Subject: [PATCH] toolhead: Move kinematic modules to new kinematics/ directory Move extruder.py, cartesian.py, corexy.py, and delta.py to a new kinematics/ sub-directory. This is intended to make adding new kinematics a little easier. Signed-off-by: Kevin O'Connor --- klippy/extras/delta_calibrate.py | 9 +++++---- klippy/gcode.py | 6 +++--- klippy/kinematics/__init__.py | 5 +++++ klippy/{ => kinematics}/cartesian.py | 3 +++ klippy/{ => kinematics}/corexy.py | 3 +++ klippy/{ => kinematics}/delta.py | 3 +++ klippy/{ => kinematics}/extruder.py | 0 klippy/klippy.py | 4 ++-- klippy/toolhead.py | 21 +++++++++++++-------- 9 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 klippy/kinematics/__init__.py rename klippy/{ => kinematics}/cartesian.py (98%) rename klippy/{ => kinematics}/corexy.py (98%) rename klippy/{ => kinematics}/delta.py (99%) rename klippy/{ => kinematics}/extruder.py (100%) diff --git a/klippy/extras/delta_calibrate.py b/klippy/extras/delta_calibrate.py index 15b9001c..42104d29 100644 --- a/klippy/extras/delta_calibrate.py +++ b/klippy/extras/delta_calibrate.py @@ -4,7 +4,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import math, logging -import probe, delta, mathutil +import probe, mathutil, kinematics.delta class DeltaCalibrate: def __init__(self, config): @@ -40,10 +40,11 @@ class DeltaCalibrate: logging.info("Initial delta_calibrate parameters: %s", params) adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius', 'angle_a', 'angle_b') + get_position_from_stable = kinematics.delta.get_position_from_stable def delta_errorfunc(params): total_error = 0. for spos in positions: - x, y, z = delta.get_position_from_stable(spos, params) + x, y, z = get_position_from_stable(spos, params) total_error += (z - z_offset)**2 return total_error new_params = mathutil.coordinate_descent( @@ -51,8 +52,8 @@ class DeltaCalibrate: logging.info("Calculated delta_calibrate parameters: %s", new_params) for spos in positions: logging.info("orig: %s new: %s", - delta.get_position_from_stable(spos, params), - delta.get_position_from_stable(spos, new_params)) + get_position_from_stable(spos, params), + get_position_from_stable(spos, new_params)) self.gcode.respond_info( "stepper_a: position_endstop: %.6f angle: %.6f\n" "stepper_b: position_endstop: %.6f angle: %.6f\n" diff --git a/klippy/gcode.py b/klippy/gcode.py index 68e367c0..e9c20055 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -4,7 +4,7 @@ # # This file may be distributed under the terms of the GNU GPLv3 license. import os, re, logging, collections -import homing, extruder +import homing, kinematics.extruder class error(Exception): pass @@ -116,7 +116,7 @@ class GCodeParser: if self.move_transform is None: self.move_with_transform = self.toolhead.move self.position_with_transform = self.toolhead.get_position - extruders = extruder.get_printer_extruders(self.printer) + extruders = kinematics.extruder.get_printer_extruders(self.printer) if extruders: self.extruder = extruders[0] self.toolhead.set_extruder(self.extruder) @@ -396,7 +396,7 @@ class GCodeParser: self.respond_info('Unknown command:"%s"' % (cmd,)) def cmd_Tn(self, params): # Select Tool - extruders = extruder.get_printer_extruders(self.printer) + extruders = kinematics.extruder.get_printer_extruders(self.printer) index = self.get_int('T', params, minval=0, maxval=len(extruders)-1) e = extruders[index] if self.extruder is e: diff --git a/klippy/kinematics/__init__.py b/klippy/kinematics/__init__.py new file mode 100644 index 00000000..09f968af --- /dev/null +++ b/klippy/kinematics/__init__.py @@ -0,0 +1,5 @@ +# Package definition for the kinematics directory +# +# Copyright (C) 2018 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. diff --git a/klippy/cartesian.py b/klippy/kinematics/cartesian.py similarity index 98% rename from klippy/cartesian.py rename to klippy/kinematics/cartesian.py index 362ba555..22ee5b98 100644 --- a/klippy/cartesian.py +++ b/klippy/kinematics/cartesian.py @@ -168,3 +168,6 @@ class CartKinematics: carriage = gcode.get_int('CARRIAGE', params, minval=0, maxval=1) self._activate_carriage(carriage) gcode.reset_last_position() + +def load_kinematics(toolhead, config): + return CartKinematics(toolhead, config) diff --git a/klippy/corexy.py b/klippy/kinematics/corexy.py similarity index 98% rename from klippy/corexy.py rename to klippy/kinematics/corexy.py index 07330641..931793e7 100644 --- a/klippy/corexy.py +++ b/klippy/kinematics/corexy.py @@ -141,3 +141,6 @@ class CoreXYKinematics: rail_y.step_itersolve(cmove) if axes_d[2]: rail_z.step_itersolve(cmove) + +def load_kinematics(toolhead, config): + return CoreXYKinematics(toolhead, config) diff --git a/klippy/delta.py b/klippy/kinematics/delta.py similarity index 99% rename from klippy/delta.py rename to klippy/kinematics/delta.py index 328d01f5..ac023242 100644 --- a/klippy/delta.py +++ b/klippy/kinematics/delta.py @@ -198,3 +198,6 @@ def get_position_from_stable(spos, params): sphere_coords = [(t[0], t[1], es + math.sqrt(a2 - radius2) - p) for t, es, a2, p in zip(towers, endstops, arm2, spos)] return mathutil.trilateration(sphere_coords, arm2) + +def load_kinematics(toolhead, config): + return DeltaKinematics(toolhead, config) diff --git a/klippy/extruder.py b/klippy/kinematics/extruder.py similarity index 100% rename from klippy/extruder.py rename to klippy/kinematics/extruder.py diff --git a/klippy/klippy.py b/klippy/klippy.py index 03d71eff..ba6b4579 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -7,7 +7,7 @@ import sys, os, optparse, logging, time, threading import collections, ConfigParser, importlib import util, reactor, queuelogger, msgproto -import gcode, pins, heater, mcu, toolhead, extruder +import gcode, pins, heater, mcu, toolhead message_ready = "Printer is ready" @@ -216,7 +216,7 @@ class Printer: m.add_printer_objects(config) for section in fileconfig.sections(): self.try_load_module(config, section) - for m in [toolhead, extruder]: + for m in [toolhead]: m.add_printer_objects(config) # Validate that there are no undefined parameters in the config file valid_sections = { s: 1 for s, o in self.all_config_options } diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 6b0058d3..d9b26ef8 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -3,8 +3,8 @@ # Copyright (C) 2016-2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import math, logging -import mcu, homing, cartesian, corexy, delta, extruder +import math, logging, importlib +import mcu, homing, kinematics.extruder # Common suffixes: _d is distance (in mm), _v is velocity (in # mm/second), _v2 is velocity squared (mm^2/s^2), _t is time (in @@ -227,12 +227,16 @@ class ToolHead: self.motor_off_timer = self.reactor.register_timer( self._motor_off_handler, self.reactor.NOW) # Create kinematics class - self.extruder = extruder.DummyExtruder() + self.extruder = kinematics.extruder.DummyExtruder() self.move_queue.set_extruder(self.extruder) - kintypes = {'cartesian': cartesian.CartKinematics, - 'corexy': corexy.CoreXYKinematics, - 'delta': delta.DeltaKinematics} - self.kin = config.getchoice('kinematics', kintypes)(self, config) + kin_name = config.get('kinematics') + try: + mod = importlib.import_module('kinematics.' + kin_name) + self.kin = mod.load_kinematics(self, config) + except: + msg = "Error loading kinematics '%s'" % (kin_name,) + logging.exception(msg) + raise config.error(msg) # SET_VELOCITY_LIMIT command gcode = self.printer.lookup_object('gcode') gcode.register_command('SET_VELOCITY_LIMIT', self.cmd_SET_VELOCITY_LIMIT, @@ -353,7 +357,7 @@ class ToolHead: self.dwell(STALL_TIME) last_move_time = self.get_last_move_time() self.kin.motor_off(last_move_time) - for ext in extruder.get_printer_extruders(self.printer): + for ext in kinematics.extruder.get_printer_extruders(self.printer): ext.motor_off(last_move_time) self.dwell(STALL_TIME) self.need_motor_off = False @@ -444,3 +448,4 @@ class ToolHead: def add_printer_objects(config): config.get_printer().add_object('toolhead', ToolHead(config)) + kinematics.extruder.add_printer_objects(config)