2016-09-15 18:20:49 +02:00
|
|
|
# Code for handling the kinematics of linear delta robots
|
|
|
|
#
|
2021-01-08 17:52:28 +01:00
|
|
|
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
|
2016-09-15 18:20:49 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import math, logging
|
2021-01-08 17:52:28 +01:00
|
|
|
import stepper, mathutil
|
2016-09-15 18:20:49 +02:00
|
|
|
|
2017-02-13 21:04:29 +01:00
|
|
|
# Slow moves once the ratio of tower to XY movement exceeds SLOW_RATIO
|
|
|
|
SLOW_RATIO = 3.
|
|
|
|
|
2016-09-15 18:20:49 +02:00
|
|
|
class DeltaKinematics:
|
2018-06-21 19:47:39 +02:00
|
|
|
def __init__(self, toolhead, config):
|
2018-07-10 00:00:58 +02:00
|
|
|
# Setup tower rails
|
2018-09-03 19:34:27 +02:00
|
|
|
stepper_configs = [config.getsection('stepper_' + a) for a in 'abc']
|
2018-06-22 17:44:25 +02:00
|
|
|
rail_a = stepper.PrinterRail(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[0], need_position_minmax = False)
|
2018-06-22 17:44:25 +02:00
|
|
|
a_endstop = rail_a.get_homing_info().position_endstop
|
|
|
|
rail_b = stepper.PrinterRail(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[1], need_position_minmax = False,
|
2018-06-22 00:48:33 +02:00
|
|
|
default_position_endstop=a_endstop)
|
2018-06-22 17:44:25 +02:00
|
|
|
rail_c = stepper.PrinterRail(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[2], need_position_minmax = False,
|
2018-06-22 00:48:33 +02:00
|
|
|
default_position_endstop=a_endstop)
|
2018-06-22 17:44:25 +02:00
|
|
|
self.rails = [rail_a, rail_b, rail_c]
|
2019-11-12 17:41:41 +01:00
|
|
|
config.get_printer().register_event_handler("stepper_enable:motor_off",
|
|
|
|
self._motor_off)
|
2021-04-25 20:53:50 +02:00
|
|
|
# Setup max velocity
|
2018-10-08 21:25:33 +02:00
|
|
|
self.max_velocity, self.max_accel = toolhead.get_max_velocity()
|
|
|
|
self.max_z_velocity = config.getfloat(
|
|
|
|
'max_z_velocity', self.max_velocity,
|
|
|
|
above=0., maxval=self.max_velocity)
|
2018-07-10 00:00:58 +02:00
|
|
|
# Read radius and arm lengths
|
2017-12-04 00:54:34 +01:00
|
|
|
self.radius = radius = config.getfloat('delta_radius', above=0.)
|
2020-01-24 03:14:09 +01:00
|
|
|
print_radius = config.getfloat('print_radius', radius, above=0.)
|
2017-10-29 19:14:12 +01:00
|
|
|
arm_length_a = stepper_configs[0].getfloat('arm_length', above=radius)
|
2017-12-04 00:54:34 +01:00
|
|
|
self.arm_lengths = arm_lengths = [
|
|
|
|
sconfig.getfloat('arm_length', arm_length_a, above=radius)
|
|
|
|
for sconfig in stepper_configs]
|
2017-10-29 19:14:12 +01:00
|
|
|
self.arm2 = [arm**2 for arm in arm_lengths]
|
2018-09-03 19:34:27 +02:00
|
|
|
self.abs_endstops = [(rail.get_homing_info().position_endstop
|
|
|
|
+ math.sqrt(arm2 - radius**2))
|
|
|
|
for rail, arm2 in zip(self.rails, self.arm2)]
|
2018-10-08 21:25:33 +02:00
|
|
|
# Determine tower locations in cartesian space
|
|
|
|
self.angles = [sconfig.getfloat('angle', angle)
|
|
|
|
for sconfig, angle in zip(stepper_configs,
|
|
|
|
[210., 330., 90.])]
|
|
|
|
self.towers = [(math.cos(math.radians(angle)) * radius,
|
|
|
|
math.sin(math.radians(angle)) * radius)
|
|
|
|
for angle in self.angles]
|
|
|
|
for r, a, t in zip(self.rails, self.arm2, self.towers):
|
|
|
|
r.setup_itersolve('delta_stepper_alloc', a, t[0], t[1])
|
2019-10-29 04:09:25 +01:00
|
|
|
for s in self.get_steppers():
|
|
|
|
s.set_trapq(toolhead.get_trapq())
|
2019-11-07 16:59:35 +01:00
|
|
|
toolhead.register_step_generator(s.generate_steps)
|
2018-07-10 00:00:58 +02:00
|
|
|
# Setup boundary checks
|
2019-10-29 04:09:25 +01:00
|
|
|
self.need_home = True
|
2016-09-15 18:20:49 +02:00
|
|
|
self.limit_xy2 = -1.
|
2018-10-08 21:36:41 +02:00
|
|
|
self.home_position = tuple(
|
|
|
|
self._actuator_to_cartesian(self.abs_endstops))
|
2018-06-22 17:44:25 +02:00
|
|
|
self.max_z = min([rail.get_homing_info().position_endstop
|
|
|
|
for rail in self.rails])
|
2017-12-23 03:44:56 +01:00
|
|
|
self.min_z = config.getfloat('minimum_z_position', 0, maxval=self.max_z)
|
2017-10-29 19:14:12 +01:00
|
|
|
self.limit_z = min([ep - arm
|
2018-09-03 19:34:27 +02:00
|
|
|
for ep, arm in zip(self.abs_endstops, arm_lengths)])
|
2017-02-13 21:04:29 +01:00
|
|
|
logging.info(
|
2021-01-08 17:52:28 +01:00
|
|
|
"Delta max build height %.2fmm (radius tapered above %.2fmm)"
|
|
|
|
% (self.max_z, self.limit_z))
|
2017-02-13 21:04:29 +01:00
|
|
|
# Find the point where an XY move could result in excessive
|
|
|
|
# tower movement
|
2018-06-22 17:44:25 +02:00
|
|
|
half_min_step_dist = min([r.get_steppers()[0].get_step_dist()
|
|
|
|
for r in self.rails]) * .5
|
2017-10-29 19:14:12 +01:00
|
|
|
min_arm_length = min(arm_lengths)
|
2021-01-08 17:52:28 +01:00
|
|
|
def ratio_to_xy(ratio):
|
2017-10-29 19:14:12 +01:00
|
|
|
return (ratio * math.sqrt(min_arm_length**2 / (ratio**2 + 1.)
|
2017-02-21 17:18:56 +01:00
|
|
|
- half_min_step_dist**2)
|
2021-01-08 17:52:28 +01:00
|
|
|
+ half_min_step_dist - radius)
|
|
|
|
self.slow_xy2 = ratio_to_xy(SLOW_RATIO)**2
|
|
|
|
self.very_slow_xy2 = ratio_to_xy(2. * SLOW_RATIO)**2
|
2020-01-24 03:14:09 +01:00
|
|
|
self.max_xy2 = min(print_radius, min_arm_length - radius,
|
2021-01-08 17:52:28 +01:00
|
|
|
ratio_to_xy(4. * SLOW_RATIO))**2
|
|
|
|
max_xy = math.sqrt(self.max_xy2)
|
2018-10-08 21:46:27 +02:00
|
|
|
logging.info("Delta max build radius %.2fmm (moves slowed past %.2fmm"
|
2021-01-08 17:52:28 +01:00
|
|
|
" and %.2fmm)"
|
|
|
|
% (max_xy, math.sqrt(self.slow_xy2),
|
|
|
|
math.sqrt(self.very_slow_xy2)))
|
|
|
|
self.axes_min = toolhead.Coord(-max_xy, -max_xy, self.min_z, 0.)
|
|
|
|
self.axes_max = toolhead.Coord(max_xy, max_xy, self.max_z, 0.)
|
2018-01-17 00:58:41 +01:00
|
|
|
self.set_position([0., 0., 0.], ())
|
2020-01-14 03:39:55 +01:00
|
|
|
def get_steppers(self):
|
2018-07-16 16:16:16 +02:00
|
|
|
return [s for rail in self.rails for s in rail.get_steppers()]
|
2018-06-22 19:57:15 +02:00
|
|
|
def _actuator_to_cartesian(self, spos):
|
|
|
|
sphere_coords = [(t[0], t[1], sp) for t, sp in zip(self.towers, spos)]
|
|
|
|
return mathutil.trilateration(sphere_coords, self.arm2)
|
2019-11-13 23:59:40 +01:00
|
|
|
def calc_tag_position(self):
|
|
|
|
spos = [rail.get_tag_position() for rail in self.rails]
|
2017-12-06 07:00:33 +01:00
|
|
|
return self._actuator_to_cartesian(spos)
|
2018-01-17 00:58:41 +01:00
|
|
|
def set_position(self, newpos, homing_axes):
|
2018-06-22 19:03:07 +02:00
|
|
|
for rail in self.rails:
|
|
|
|
rail.set_position(newpos)
|
2017-02-13 04:17:32 +01:00
|
|
|
self.limit_xy2 = -1.
|
2018-06-22 19:21:03 +02:00
|
|
|
if tuple(homing_axes) == (0, 1, 2):
|
2018-01-17 00:58:41 +01:00
|
|
|
self.need_home = False
|
2016-11-18 17:27:16 +01:00
|
|
|
def home(self, homing_state):
|
2016-09-15 18:20:49 +02:00
|
|
|
# All axes are homed simultaneously
|
2016-11-18 17:27:16 +01:00
|
|
|
homing_state.set_axes([0, 1, 2])
|
2018-10-08 21:36:41 +02:00
|
|
|
forcepos = list(self.home_position)
|
2018-10-09 03:49:56 +02:00
|
|
|
forcepos[2] = -1.5 * math.sqrt(max(self.arm2)-self.max_xy2)
|
2019-06-27 19:11:58 +02:00
|
|
|
homing_state.home_rails(self.rails, forcepos, self.home_position)
|
2019-11-12 17:41:41 +01:00
|
|
|
def _motor_off(self, print_time):
|
2016-09-15 18:20:49 +02:00
|
|
|
self.limit_xy2 = -1.
|
2019-10-29 04:09:25 +01:00
|
|
|
self.need_home = True
|
2016-09-15 18:20:49 +02:00
|
|
|
def check_move(self, move):
|
|
|
|
end_pos = move.end_pos
|
2018-10-27 17:43:24 +02:00
|
|
|
end_xy2 = end_pos[0]**2 + end_pos[1]**2
|
|
|
|
if end_xy2 <= self.limit_xy2 and not move.axes_d[2]:
|
2017-02-13 04:17:32 +01:00
|
|
|
# Normal XY move
|
|
|
|
return
|
|
|
|
if self.need_home:
|
2020-09-03 22:22:54 +02:00
|
|
|
raise move.move_error("Must home first")
|
2018-10-27 17:43:24 +02:00
|
|
|
end_z = end_pos[2]
|
2017-02-13 04:17:32 +01:00
|
|
|
limit_xy2 = self.max_xy2
|
2018-10-27 17:43:24 +02:00
|
|
|
if end_z > self.limit_z:
|
|
|
|
limit_xy2 = min(limit_xy2, (self.max_z - end_z)**2)
|
|
|
|
if end_xy2 > limit_xy2 or end_z > self.max_z or end_z < self.min_z:
|
|
|
|
# Move out of range - verify not a homing move
|
|
|
|
if (end_pos[:2] != self.home_position[:2]
|
|
|
|
or end_z < self.min_z or end_z > self.home_position[2]):
|
2020-09-03 22:22:54 +02:00
|
|
|
raise move.move_error()
|
2018-10-27 17:54:17 +02:00
|
|
|
limit_xy2 = -1.
|
2016-12-01 22:04:48 +01:00
|
|
|
if move.axes_d[2]:
|
2017-02-13 04:17:32 +01:00
|
|
|
move.limit_speed(self.max_z_velocity, move.accel)
|
|
|
|
limit_xy2 = -1.
|
2017-02-13 21:04:29 +01:00
|
|
|
# Limit the speed/accel of this move if is is at the extreme
|
|
|
|
# end of the build envelope
|
2018-10-27 17:43:24 +02:00
|
|
|
extreme_xy2 = max(end_xy2, move.start_pos[0]**2 + move.start_pos[1]**2)
|
2017-02-13 21:04:29 +01:00
|
|
|
if extreme_xy2 > self.slow_xy2:
|
2017-02-21 17:18:56 +01:00
|
|
|
r = 0.5
|
|
|
|
if extreme_xy2 > self.very_slow_xy2:
|
|
|
|
r = 0.25
|
2017-02-13 21:04:29 +01:00
|
|
|
max_velocity = self.max_velocity
|
|
|
|
if move.axes_d[2]:
|
|
|
|
max_velocity = self.max_z_velocity
|
2017-02-21 17:18:56 +01:00
|
|
|
move.limit_speed(max_velocity * r, self.max_accel * r)
|
2017-02-13 21:04:29 +01:00
|
|
|
limit_xy2 = -1.
|
|
|
|
self.limit_xy2 = min(limit_xy2, self.slow_xy2)
|
2019-11-25 01:16:21 +01:00
|
|
|
def get_status(self, eventtime):
|
2020-12-27 23:37:32 +01:00
|
|
|
return {
|
|
|
|
'homed_axes': '' if self.need_home else 'xyz',
|
2021-01-08 17:52:28 +01:00
|
|
|
'axis_minimum': self.axes_min,
|
|
|
|
'axis_maximum': self.axes_max,
|
2020-12-27 23:37:32 +01:00
|
|
|
}
|
2019-12-05 16:44:08 +01:00
|
|
|
def get_calibration(self):
|
2019-12-05 17:00:45 +01:00
|
|
|
endstops = [rail.get_homing_info().position_endstop
|
|
|
|
for rail in self.rails]
|
|
|
|
stepdists = [rail.get_steppers()[0].get_step_dist()
|
|
|
|
for rail in self.rails]
|
|
|
|
return DeltaCalibration(self.radius, self.angles, self.arm_lengths,
|
|
|
|
endstops, stepdists)
|
2019-12-05 16:44:08 +01:00
|
|
|
|
|
|
|
# Delta parameter calibration for DELTA_CALIBRATE tool
|
|
|
|
class DeltaCalibration:
|
2019-12-05 17:00:45 +01:00
|
|
|
def __init__(self, radius, angles, arms, endstops, stepdists):
|
|
|
|
self.radius = radius
|
|
|
|
self.angles = angles
|
|
|
|
self.arms = arms
|
|
|
|
self.endstops = endstops
|
|
|
|
self.stepdists = stepdists
|
2019-12-05 16:44:08 +01:00
|
|
|
# Calculate the XY cartesian coordinates of the delta towers
|
2019-12-05 17:00:45 +01:00
|
|
|
radian_angles = [math.radians(a) for a in angles]
|
|
|
|
self.towers = [(math.cos(a) * radius, math.sin(a) * radius)
|
2019-12-05 16:44:08 +01:00
|
|
|
for a in radian_angles]
|
|
|
|
# Calculate the absolute Z height of each tower endstop
|
2019-12-05 17:00:45 +01:00
|
|
|
radius2 = radius**2
|
2019-12-05 16:44:08 +01:00
|
|
|
self.abs_endstops = [e + math.sqrt(a**2 - radius2)
|
2019-12-05 17:00:45 +01:00
|
|
|
for e, a in zip(endstops, arms)]
|
|
|
|
def coordinate_descent_params(self, is_extended):
|
|
|
|
# Determine adjustment parameters (for use with coordinate_descent)
|
|
|
|
adj_params = ('radius', 'angle_a', 'angle_b',
|
|
|
|
'endstop_a', 'endstop_b', 'endstop_c')
|
|
|
|
if is_extended:
|
|
|
|
adj_params += ('arm_a', 'arm_b', 'arm_c')
|
|
|
|
params = { 'radius': self.radius }
|
|
|
|
for i, axis in enumerate('abc'):
|
|
|
|
params['angle_'+axis] = self.angles[i]
|
|
|
|
params['arm_'+axis] = self.arms[i]
|
|
|
|
params['endstop_'+axis] = self.endstops[i]
|
|
|
|
params['stepdist_'+axis] = self.stepdists[i]
|
|
|
|
return adj_params, params
|
2019-12-05 16:44:08 +01:00
|
|
|
def new_calibration(self, params):
|
2019-12-05 17:00:45 +01:00
|
|
|
# Create a new calibration object from coordinate_descent params
|
|
|
|
radius = params['radius']
|
|
|
|
angles = [params['angle_'+a] for a in 'abc']
|
|
|
|
arms = [params['arm_'+a] for a in 'abc']
|
|
|
|
endstops = [params['endstop_'+a] for a in 'abc']
|
|
|
|
stepdists = [params['stepdist_'+a] for a in 'abc']
|
|
|
|
return DeltaCalibration(radius, angles, arms, endstops, stepdists)
|
2019-12-05 16:44:08 +01:00
|
|
|
def get_position_from_stable(self, stable_position):
|
|
|
|
# Return cartesian coordinates for the given stable_position
|
|
|
|
sphere_coords = [
|
|
|
|
(t[0], t[1], es - sp * sd)
|
|
|
|
for sd, t, es, sp in zip(self.stepdists, self.towers,
|
|
|
|
self.abs_endstops, stable_position) ]
|
|
|
|
return mathutil.trilateration(sphere_coords, [a**2 for a in self.arms])
|
|
|
|
def calc_stable_position(self, coord):
|
|
|
|
# Return a stable_position from a cartesian coordinate
|
|
|
|
steppos = [
|
|
|
|
math.sqrt(a**2 - (t[0]-coord[0])**2 - (t[1]-coord[1])**2) + coord[2]
|
|
|
|
for t, a in zip(self.towers, self.arms) ]
|
|
|
|
return [(ep - sp) / sd
|
|
|
|
for sd, ep, sp in zip(self.stepdists,
|
|
|
|
self.abs_endstops, steppos)]
|
|
|
|
def save_state(self, configfile):
|
|
|
|
# Save the current parameters (for use with SAVE_CONFIG)
|
2019-12-05 17:00:45 +01:00
|
|
|
configfile.set('printer', 'delta_radius', "%.6f" % (self.radius,))
|
|
|
|
for i, axis in enumerate('abc'):
|
|
|
|
configfile.set('stepper_'+axis, 'angle', "%.6f" % (self.angles[i],))
|
2019-12-05 16:44:08 +01:00
|
|
|
configfile.set('stepper_'+axis, 'arm_length',
|
2019-12-05 17:00:45 +01:00
|
|
|
"%.6f" % (self.arms[i],))
|
2019-12-05 16:44:08 +01:00
|
|
|
configfile.set('stepper_'+axis, 'position_endstop',
|
2019-12-05 17:00:45 +01:00
|
|
|
"%.6f" % (self.endstops[i],))
|
2019-12-05 16:44:08 +01:00
|
|
|
gcode = configfile.get_printer().lookup_object("gcode")
|
|
|
|
gcode.respond_info(
|
|
|
|
"stepper_a: position_endstop: %.6f angle: %.6f arm: %.6f\n"
|
|
|
|
"stepper_b: position_endstop: %.6f angle: %.6f arm: %.6f\n"
|
|
|
|
"stepper_c: position_endstop: %.6f angle: %.6f arm: %.6f\n"
|
2019-12-05 17:00:45 +01:00
|
|
|
"delta_radius: %.6f"
|
|
|
|
% (self.endstops[0], self.angles[0], self.arms[0],
|
|
|
|
self.endstops[1], self.angles[1], self.arms[1],
|
|
|
|
self.endstops[2], self.angles[2], self.arms[2],
|
|
|
|
self.radius))
|
2018-07-13 04:15:45 +02:00
|
|
|
|
|
|
|
def load_kinematics(toolhead, config):
|
|
|
|
return DeltaKinematics(toolhead, config)
|