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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-07-12 22:15:45 -04:00
parent 7d897d84d7
commit 8faab46ed2
9 changed files with 37 additions and 17 deletions

View File

@ -4,7 +4,7 @@
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging import math, logging
import probe, delta, mathutil import probe, mathutil, kinematics.delta
class DeltaCalibrate: class DeltaCalibrate:
def __init__(self, config): def __init__(self, config):
@ -40,10 +40,11 @@ class DeltaCalibrate:
logging.info("Initial delta_calibrate parameters: %s", params) logging.info("Initial delta_calibrate parameters: %s", params)
adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius', adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius',
'angle_a', 'angle_b') 'angle_a', 'angle_b')
get_position_from_stable = kinematics.delta.get_position_from_stable
def delta_errorfunc(params): def delta_errorfunc(params):
total_error = 0. total_error = 0.
for spos in positions: 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 total_error += (z - z_offset)**2
return total_error return total_error
new_params = mathutil.coordinate_descent( new_params = mathutil.coordinate_descent(
@ -51,8 +52,8 @@ class DeltaCalibrate:
logging.info("Calculated delta_calibrate parameters: %s", new_params) logging.info("Calculated delta_calibrate parameters: %s", new_params)
for spos in positions: for spos in positions:
logging.info("orig: %s new: %s", logging.info("orig: %s new: %s",
delta.get_position_from_stable(spos, params), get_position_from_stable(spos, params),
delta.get_position_from_stable(spos, new_params)) get_position_from_stable(spos, new_params))
self.gcode.respond_info( self.gcode.respond_info(
"stepper_a: position_endstop: %.6f angle: %.6f\n" "stepper_a: position_endstop: %.6f angle: %.6f\n"
"stepper_b: position_endstop: %.6f angle: %.6f\n" "stepper_b: position_endstop: %.6f angle: %.6f\n"

View File

@ -4,7 +4,7 @@
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import os, re, logging, collections import os, re, logging, collections
import homing, extruder import homing, kinematics.extruder
class error(Exception): class error(Exception):
pass pass
@ -116,7 +116,7 @@ class GCodeParser:
if self.move_transform is None: if self.move_transform is None:
self.move_with_transform = self.toolhead.move self.move_with_transform = self.toolhead.move
self.position_with_transform = self.toolhead.get_position 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: if extruders:
self.extruder = extruders[0] self.extruder = extruders[0]
self.toolhead.set_extruder(self.extruder) self.toolhead.set_extruder(self.extruder)
@ -396,7 +396,7 @@ class GCodeParser:
self.respond_info('Unknown command:"%s"' % (cmd,)) self.respond_info('Unknown command:"%s"' % (cmd,))
def cmd_Tn(self, params): def cmd_Tn(self, params):
# Select Tool # 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) index = self.get_int('T', params, minval=0, maxval=len(extruders)-1)
e = extruders[index] e = extruders[index]
if self.extruder is e: if self.extruder is e:

View File

@ -0,0 +1,5 @@
# Package definition for the kinematics directory
#
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

View File

@ -168,3 +168,6 @@ class CartKinematics:
carriage = gcode.get_int('CARRIAGE', params, minval=0, maxval=1) carriage = gcode.get_int('CARRIAGE', params, minval=0, maxval=1)
self._activate_carriage(carriage) self._activate_carriage(carriage)
gcode.reset_last_position() gcode.reset_last_position()
def load_kinematics(toolhead, config):
return CartKinematics(toolhead, config)

View File

@ -141,3 +141,6 @@ class CoreXYKinematics:
rail_y.step_itersolve(cmove) rail_y.step_itersolve(cmove)
if axes_d[2]: if axes_d[2]:
rail_z.step_itersolve(cmove) rail_z.step_itersolve(cmove)
def load_kinematics(toolhead, config):
return CoreXYKinematics(toolhead, config)

View File

@ -198,3 +198,6 @@ def get_position_from_stable(spos, params):
sphere_coords = [(t[0], t[1], es + math.sqrt(a2 - radius2) - p) sphere_coords = [(t[0], t[1], es + math.sqrt(a2 - radius2) - p)
for t, es, a2, p in zip(towers, endstops, arm2, spos)] for t, es, a2, p in zip(towers, endstops, arm2, spos)]
return mathutil.trilateration(sphere_coords, arm2) return mathutil.trilateration(sphere_coords, arm2)
def load_kinematics(toolhead, config):
return DeltaKinematics(toolhead, config)

View File

@ -7,7 +7,7 @@
import sys, os, optparse, logging, time, threading import sys, os, optparse, logging, time, threading
import collections, ConfigParser, importlib import collections, ConfigParser, importlib
import util, reactor, queuelogger, msgproto import util, reactor, queuelogger, msgproto
import gcode, pins, heater, mcu, toolhead, extruder import gcode, pins, heater, mcu, toolhead
message_ready = "Printer is ready" message_ready = "Printer is ready"
@ -216,7 +216,7 @@ class Printer:
m.add_printer_objects(config) m.add_printer_objects(config)
for section in fileconfig.sections(): for section in fileconfig.sections():
self.try_load_module(config, section) self.try_load_module(config, section)
for m in [toolhead, extruder]: for m in [toolhead]:
m.add_printer_objects(config) m.add_printer_objects(config)
# Validate that there are no undefined parameters in the config file # Validate that there are no undefined parameters in the config file
valid_sections = { s: 1 for s, o in self.all_config_options } valid_sections = { s: 1 for s, o in self.all_config_options }

View File

@ -3,8 +3,8 @@
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging import math, logging, importlib
import mcu, homing, cartesian, corexy, delta, extruder import mcu, homing, kinematics.extruder
# Common suffixes: _d is distance (in mm), _v is velocity (in # 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 # 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_timer = self.reactor.register_timer(
self._motor_off_handler, self.reactor.NOW) self._motor_off_handler, self.reactor.NOW)
# Create kinematics class # Create kinematics class
self.extruder = extruder.DummyExtruder() self.extruder = kinematics.extruder.DummyExtruder()
self.move_queue.set_extruder(self.extruder) self.move_queue.set_extruder(self.extruder)
kintypes = {'cartesian': cartesian.CartKinematics, kin_name = config.get('kinematics')
'corexy': corexy.CoreXYKinematics, try:
'delta': delta.DeltaKinematics} mod = importlib.import_module('kinematics.' + kin_name)
self.kin = config.getchoice('kinematics', kintypes)(self, config) 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 # SET_VELOCITY_LIMIT command
gcode = self.printer.lookup_object('gcode') gcode = self.printer.lookup_object('gcode')
gcode.register_command('SET_VELOCITY_LIMIT', self.cmd_SET_VELOCITY_LIMIT, gcode.register_command('SET_VELOCITY_LIMIT', self.cmd_SET_VELOCITY_LIMIT,
@ -353,7 +357,7 @@ class ToolHead:
self.dwell(STALL_TIME) self.dwell(STALL_TIME)
last_move_time = self.get_last_move_time() last_move_time = self.get_last_move_time()
self.kin.motor_off(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) ext.motor_off(last_move_time)
self.dwell(STALL_TIME) self.dwell(STALL_TIME)
self.need_motor_off = False self.need_motor_off = False
@ -444,3 +448,4 @@ class ToolHead:
def add_printer_objects(config): def add_printer_objects(config):
config.get_printer().add_object('toolhead', ToolHead(config)) config.get_printer().add_object('toolhead', ToolHead(config))
kinematics.extruder.add_printer_objects(config)