2016-09-15 19:20:49 +03:00
|
|
|
# Code for handling the kinematics of linear delta robots
|
|
|
|
#
|
2018-06-22 18:44:25 +03:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-09-15 19:20:49 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import math, logging
|
2018-07-13 18:24:36 +03:00
|
|
|
import stepper, homing, mathutil
|
2016-09-15 19:20:49 +03:00
|
|
|
|
2017-02-13 23:04:29 +03:00
|
|
|
# Slow moves once the ratio of tower to XY movement exceeds SLOW_RATIO
|
|
|
|
SLOW_RATIO = 3.
|
|
|
|
|
2016-09-15 19:20:49 +03:00
|
|
|
class DeltaKinematics:
|
2018-06-21 20:47:39 +03:00
|
|
|
def __init__(self, toolhead, config):
|
2018-07-10 01:00:58 +03:00
|
|
|
# Setup tower rails
|
2018-09-03 20:34:27 +03:00
|
|
|
stepper_configs = [config.getsection('stepper_' + a) for a in 'abc']
|
2018-06-22 18:44:25 +03:00
|
|
|
rail_a = stepper.PrinterRail(
|
2018-06-21 20:47:39 +03:00
|
|
|
stepper_configs[0], need_position_minmax = False)
|
2018-06-22 18:44:25 +03:00
|
|
|
a_endstop = rail_a.get_homing_info().position_endstop
|
|
|
|
rail_b = stepper.PrinterRail(
|
2018-06-21 20:47:39 +03:00
|
|
|
stepper_configs[1], need_position_minmax = False,
|
2018-06-22 01:48:33 +03:00
|
|
|
default_position_endstop=a_endstop)
|
2018-06-22 18:44:25 +03:00
|
|
|
rail_c = stepper.PrinterRail(
|
2018-06-21 20:47:39 +03:00
|
|
|
stepper_configs[2], need_position_minmax = False,
|
2018-06-22 01:48:33 +03:00
|
|
|
default_position_endstop=a_endstop)
|
2018-06-22 18:44:25 +03:00
|
|
|
self.rails = [rail_a, rail_b, rail_c]
|
2018-10-08 22:25:33 +03:00
|
|
|
# Setup stepper max halt velocity
|
|
|
|
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)
|
|
|
|
max_halt_velocity = toolhead.get_max_axis_halt() * SLOW_RATIO
|
|
|
|
max_halt_accel = self.max_accel * SLOW_RATIO
|
|
|
|
for rail in self.rails:
|
|
|
|
rail.set_max_jerk(max_halt_velocity, max_halt_accel)
|
2018-07-10 01:00:58 +03:00
|
|
|
# Read radius and arm lengths
|
2017-12-04 02:54:34 +03:00
|
|
|
self.radius = radius = config.getfloat('delta_radius', above=0.)
|
2017-10-29 21:14:12 +03:00
|
|
|
arm_length_a = stepper_configs[0].getfloat('arm_length', above=radius)
|
2017-12-04 02:54:34 +03:00
|
|
|
self.arm_lengths = arm_lengths = [
|
|
|
|
sconfig.getfloat('arm_length', arm_length_a, above=radius)
|
|
|
|
for sconfig in stepper_configs]
|
2017-10-29 21:14:12 +03:00
|
|
|
self.arm2 = [arm**2 for arm in arm_lengths]
|
2018-09-03 20:34:27 +03: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 22:25:33 +03: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])
|
2018-07-10 01:00:58 +03:00
|
|
|
# Setup boundary checks
|
|
|
|
self.need_motor_enable = self.need_home = True
|
2016-09-15 19:20:49 +03:00
|
|
|
self.limit_xy2 = -1.
|
2018-10-08 22:36:41 +03:00
|
|
|
self.home_position = tuple(
|
|
|
|
self._actuator_to_cartesian(self.abs_endstops))
|
2018-06-22 18:44:25 +03:00
|
|
|
self.max_z = min([rail.get_homing_info().position_endstop
|
|
|
|
for rail in self.rails])
|
2017-12-23 05:44:56 +03:00
|
|
|
self.min_z = config.getfloat('minimum_z_position', 0, maxval=self.max_z)
|
2017-10-29 21:14:12 +03:00
|
|
|
self.limit_z = min([ep - arm
|
2018-09-03 20:34:27 +03:00
|
|
|
for ep, arm in zip(self.abs_endstops, arm_lengths)])
|
2017-02-13 23:04:29 +03:00
|
|
|
logging.info(
|
|
|
|
"Delta max build height %.2fmm (radius tapered above %.2fmm)" % (
|
|
|
|
self.max_z, self.limit_z))
|
|
|
|
# Find the point where an XY move could result in excessive
|
|
|
|
# tower movement
|
2018-06-22 18:44:25 +03:00
|
|
|
half_min_step_dist = min([r.get_steppers()[0].get_step_dist()
|
|
|
|
for r in self.rails]) * .5
|
2017-10-29 21:14:12 +03:00
|
|
|
min_arm_length = min(arm_lengths)
|
2017-02-21 19:18:56 +03:00
|
|
|
def ratio_to_dist(ratio):
|
2017-10-29 21:14:12 +03:00
|
|
|
return (ratio * math.sqrt(min_arm_length**2 / (ratio**2 + 1.)
|
2017-02-21 19:18:56 +03:00
|
|
|
- half_min_step_dist**2)
|
|
|
|
+ half_min_step_dist)
|
|
|
|
self.slow_xy2 = (ratio_to_dist(SLOW_RATIO) - radius)**2
|
|
|
|
self.very_slow_xy2 = (ratio_to_dist(2. * SLOW_RATIO) - radius)**2
|
2017-10-29 21:14:12 +03:00
|
|
|
self.max_xy2 = min(radius, min_arm_length - radius,
|
2017-02-21 19:18:56 +03:00
|
|
|
ratio_to_dist(4. * SLOW_RATIO) - radius)**2
|
2018-10-08 22:46:27 +03:00
|
|
|
logging.info("Delta max build radius %.2fmm (moves slowed past %.2fmm"
|
|
|
|
" and %.2fmm)" % (
|
|
|
|
math.sqrt(self.max_xy2), math.sqrt(self.slow_xy2),
|
|
|
|
math.sqrt(self.very_slow_xy2)))
|
2018-01-17 02:58:41 +03:00
|
|
|
self.set_position([0., 0., 0.], ())
|
2018-07-16 17:16:16 +03:00
|
|
|
def get_steppers(self, flags=""):
|
|
|
|
return [s for rail in self.rails for s in rail.get_steppers()]
|
2018-06-22 20:57:15 +03: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)
|
2018-06-22 19:27:37 +03:00
|
|
|
def calc_position(self):
|
2018-06-22 18:44:25 +03:00
|
|
|
spos = [rail.get_commanded_position() for rail in self.rails]
|
2017-12-06 09:00:33 +03:00
|
|
|
return self._actuator_to_cartesian(spos)
|
2018-01-17 02:58:41 +03:00
|
|
|
def set_position(self, newpos, homing_axes):
|
2018-06-22 20:03:07 +03:00
|
|
|
for rail in self.rails:
|
|
|
|
rail.set_position(newpos)
|
2017-02-13 06:17:32 +03:00
|
|
|
self.limit_xy2 = -1.
|
2018-06-22 20:21:03 +03:00
|
|
|
if tuple(homing_axes) == (0, 1, 2):
|
2018-01-17 02:58:41 +03:00
|
|
|
self.need_home = False
|
2016-11-18 19:27:16 +03:00
|
|
|
def home(self, homing_state):
|
2016-09-15 19:20:49 +03:00
|
|
|
# All axes are homed simultaneously
|
2016-11-18 19:27:16 +03:00
|
|
|
homing_state.set_axes([0, 1, 2])
|
2018-10-08 22:36:41 +03:00
|
|
|
forcepos = list(self.home_position)
|
2018-10-09 04:49:56 +03:00
|
|
|
forcepos[2] = -1.5 * math.sqrt(max(self.arm2)-self.max_xy2)
|
2019-06-27 20:11:58 +03:00
|
|
|
homing_state.home_rails(self.rails, forcepos, self.home_position)
|
2017-09-12 19:47:40 +03:00
|
|
|
def motor_off(self, print_time):
|
2016-09-15 19:20:49 +03:00
|
|
|
self.limit_xy2 = -1.
|
2018-06-22 18:44:25 +03:00
|
|
|
for rail in self.rails:
|
|
|
|
rail.motor_enable(print_time, 0)
|
2017-02-13 06:17:32 +03:00
|
|
|
self.need_motor_enable = self.need_home = True
|
2017-09-12 19:47:40 +03:00
|
|
|
def _check_motor_enable(self, print_time):
|
2018-06-22 20:21:03 +03:00
|
|
|
for rail in self.rails:
|
|
|
|
rail.motor_enable(print_time, 1)
|
2016-11-14 21:40:35 +03:00
|
|
|
self.need_motor_enable = False
|
2016-09-15 19:20:49 +03:00
|
|
|
def check_move(self, move):
|
|
|
|
end_pos = move.end_pos
|
2018-10-27 18:43:24 +03: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 06:17:32 +03:00
|
|
|
# Normal XY move
|
|
|
|
return
|
|
|
|
if self.need_home:
|
|
|
|
raise homing.EndstopMoveError(end_pos, "Must home first")
|
2018-10-27 18:43:24 +03:00
|
|
|
end_z = end_pos[2]
|
2017-02-13 06:17:32 +03:00
|
|
|
limit_xy2 = self.max_xy2
|
2018-10-27 18:43:24 +03: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]):
|
|
|
|
raise homing.EndstopMoveError(end_pos)
|
2018-10-27 18:54:17 +03:00
|
|
|
limit_xy2 = -1.
|
2016-12-02 00:04:48 +03:00
|
|
|
if move.axes_d[2]:
|
2017-02-13 06:17:32 +03:00
|
|
|
move.limit_speed(self.max_z_velocity, move.accel)
|
|
|
|
limit_xy2 = -1.
|
2017-02-13 23:04:29 +03:00
|
|
|
# Limit the speed/accel of this move if is is at the extreme
|
|
|
|
# end of the build envelope
|
2018-10-27 18:43:24 +03:00
|
|
|
extreme_xy2 = max(end_xy2, move.start_pos[0]**2 + move.start_pos[1]**2)
|
2017-02-13 23:04:29 +03:00
|
|
|
if extreme_xy2 > self.slow_xy2:
|
2017-02-21 19:18:56 +03:00
|
|
|
r = 0.5
|
|
|
|
if extreme_xy2 > self.very_slow_xy2:
|
|
|
|
r = 0.25
|
2017-02-13 23:04:29 +03:00
|
|
|
max_velocity = self.max_velocity
|
|
|
|
if move.axes_d[2]:
|
|
|
|
max_velocity = self.max_z_velocity
|
2017-02-21 19:18:56 +03:00
|
|
|
move.limit_speed(max_velocity * r, self.max_accel * r)
|
2017-02-13 23:04:29 +03:00
|
|
|
limit_xy2 = -1.
|
|
|
|
self.limit_xy2 = min(limit_xy2, self.slow_xy2)
|
2017-09-12 19:47:40 +03:00
|
|
|
def move(self, print_time, move):
|
2017-04-06 18:09:08 +03:00
|
|
|
if self.need_motor_enable:
|
2017-09-12 19:47:40 +03:00
|
|
|
self._check_motor_enable(print_time)
|
2018-06-22 18:44:25 +03:00
|
|
|
for rail in self.rails:
|
2018-07-13 17:52:40 +03:00
|
|
|
rail.step_itersolve(move.cmove)
|
2018-09-03 20:34:27 +03:00
|
|
|
# Helper function for DELTA_CALIBRATE script
|
2017-12-04 02:54:34 +03:00
|
|
|
def get_calibrate_params(self):
|
2018-07-16 05:05:13 +03:00
|
|
|
out = { 'radius': self.radius }
|
|
|
|
for i, axis in enumerate('abc'):
|
|
|
|
rail = self.rails[i]
|
|
|
|
out['angle_'+axis] = self.angles[i]
|
|
|
|
out['arm_'+axis] = self.arm_lengths[i]
|
2018-09-03 20:34:27 +03:00
|
|
|
out['endstop_'+axis] = rail.get_homing_info().position_endstop
|
|
|
|
out['stepdist_'+axis] = rail.get_steppers()[0].get_step_dist()
|
2018-07-14 18:35:13 +03:00
|
|
|
return out
|
2018-07-13 05:15:45 +03:00
|
|
|
|
|
|
|
def load_kinematics(toolhead, config):
|
|
|
|
return DeltaKinematics(toolhead, config)
|