2016-09-15 18:20:49 +02:00
|
|
|
# Code for handling the kinematics of linear delta robots
|
|
|
|
#
|
2017-04-07 17:47:24 +02:00
|
|
|
# Copyright (C) 2016,2017 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
|
2018-06-09 03:30:38 +02:00
|
|
|
import stepper, homing, chelper
|
2016-09-15 18:20:49 +02:00
|
|
|
|
|
|
|
StepList = (0, 1, 2)
|
|
|
|
|
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):
|
2017-10-29 19:14:12 +01:00
|
|
|
stepper_configs = [config.getsection('stepper_' + n)
|
|
|
|
for n in ['a', 'b', 'c']]
|
2018-05-19 01:34:17 +02:00
|
|
|
stepper_a = stepper.PrinterHomingStepper(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[0], need_position_minmax = False)
|
2017-11-02 02:21:37 +01:00
|
|
|
stepper_b = stepper.PrinterHomingStepper(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[1], need_position_minmax = False,
|
2018-05-19 01:34:17 +02:00
|
|
|
default_position_endstop=stepper_a.position_endstop)
|
2017-11-02 02:21:37 +01:00
|
|
|
stepper_c = stepper.PrinterHomingStepper(
|
2018-06-21 19:47:39 +02:00
|
|
|
stepper_configs[2], need_position_minmax = False,
|
2018-05-19 01:34:17 +02:00
|
|
|
default_position_endstop=stepper_a.position_endstop)
|
2017-11-02 02:21:37 +01:00
|
|
|
self.steppers = [stepper_a, stepper_b, stepper_c]
|
2017-02-13 04:17:32 +01:00
|
|
|
self.need_motor_enable = self.need_home = True
|
2017-12-04 00:54:34 +01:00
|
|
|
self.radius = radius = config.getfloat('delta_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]
|
|
|
|
self.endstops = [s.position_endstop + math.sqrt(arm2 - radius**2)
|
|
|
|
for s, arm2 in zip(self.steppers, self.arm2)]
|
2016-09-15 18:20:49 +02:00
|
|
|
self.limit_xy2 = -1.
|
2017-06-10 01:45:44 +02:00
|
|
|
self.max_z = min([s.position_endstop for s in self.steppers])
|
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
|
|
|
|
for ep, arm in zip(self.endstops, arm_lengths)])
|
2017-02-13 21:04:29 +01:00
|
|
|
logging.info(
|
|
|
|
"Delta max build height %.2fmm (radius tapered above %.2fmm)" % (
|
|
|
|
self.max_z, self.limit_z))
|
2017-09-03 21:17:02 +02: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)
|
2017-10-12 06:48:01 +02:00
|
|
|
max_halt_velocity = toolhead.get_max_axis_halt()
|
2017-09-03 21:17:02 +02:00
|
|
|
for s in self.steppers:
|
2017-10-12 06:48:01 +02:00
|
|
|
s.set_max_jerk(max_halt_velocity, self.max_accel)
|
2017-06-05 20:43:16 +02:00
|
|
|
# Determine tower locations in cartesian space
|
2017-12-04 00:54:34 +01:00
|
|
|
self.angles = [sconfig.getfloat('angle', angle)
|
|
|
|
for sconfig, angle in zip(stepper_configs,
|
|
|
|
[210., 330., 90.])]
|
2017-06-05 20:43:16 +02:00
|
|
|
self.towers = [(math.cos(math.radians(angle)) * radius,
|
|
|
|
math.sin(math.radians(angle)) * radius)
|
2017-12-04 00:54:34 +01:00
|
|
|
for angle in self.angles]
|
2018-06-09 03:30:38 +02:00
|
|
|
# Setup iterative solver
|
|
|
|
ffi_main, ffi_lib = chelper.get_ffi()
|
|
|
|
self.cmove = ffi_main.gc(ffi_lib.move_alloc(), ffi_lib.free)
|
|
|
|
self.move_fill = ffi_lib.move_fill
|
|
|
|
for s, a, t in zip(self.steppers, self.arm2, self.towers):
|
|
|
|
sk = ffi_main.gc(ffi_lib.delta_stepper_alloc(a, t[0], t[1]),
|
|
|
|
ffi_lib.free)
|
|
|
|
s.setup_itersolve(sk)
|
2017-02-13 21:04:29 +01:00
|
|
|
# Find the point where an XY move could result in excessive
|
|
|
|
# tower movement
|
2018-06-21 20:38:39 +02:00
|
|
|
half_min_step_dist = min([s.get_step_dist() for s in self.steppers]) * .5
|
2017-10-29 19:14:12 +01:00
|
|
|
min_arm_length = min(arm_lengths)
|
2017-02-21 17:18:56 +01:00
|
|
|
def ratio_to_dist(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)
|
|
|
|
+ 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 19:14:12 +01:00
|
|
|
self.max_xy2 = min(radius, min_arm_length - radius,
|
2017-02-21 17:18:56 +01:00
|
|
|
ratio_to_dist(4. * SLOW_RATIO) - radius)**2
|
2017-02-13 21:04:29 +01:00
|
|
|
logging.info(
|
2017-02-21 17:18:56 +01:00
|
|
|
"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 00:58:41 +01:00
|
|
|
self.set_position([0., 0., 0.], ())
|
2017-12-06 07:00:33 +01:00
|
|
|
def get_steppers(self, flags=""):
|
2017-12-06 16:13:58 +01:00
|
|
|
return list(self.steppers)
|
2016-12-08 19:09:40 +01:00
|
|
|
def _cartesian_to_actuator(self, coord):
|
2017-10-29 19:14:12 +01:00
|
|
|
return [math.sqrt(self.arm2[i] - (self.towers[i][0] - coord[0])**2
|
2017-04-05 01:20:54 +02:00
|
|
|
- (self.towers[i][1] - coord[1])**2) + coord[2]
|
2016-09-15 18:20:49 +02:00
|
|
|
for i in StepList]
|
2016-12-08 19:09:40 +01:00
|
|
|
def _actuator_to_cartesian(self, pos):
|
2017-12-04 00:54:34 +01:00
|
|
|
return actuator_to_cartesian(self.towers, self.arm2, pos)
|
2017-12-06 07:00:33 +01:00
|
|
|
def get_position(self):
|
|
|
|
spos = [s.mcu_stepper.get_commanded_position() for s in self.steppers]
|
|
|
|
return self._actuator_to_cartesian(spos)
|
2018-01-17 00:58:41 +01:00
|
|
|
def set_position(self, newpos, homing_axes):
|
2016-12-08 19:09:40 +01:00
|
|
|
pos = self._cartesian_to_actuator(newpos)
|
2016-11-19 17:34:42 +01:00
|
|
|
for i in StepList:
|
2017-11-07 18:29:51 +01:00
|
|
|
self.steppers[i].set_position(pos[i])
|
2017-02-13 04:17:32 +01:00
|
|
|
self.limit_xy2 = -1.
|
2018-01-17 00:58:41 +01:00
|
|
|
if tuple(homing_axes) == StepList:
|
|
|
|
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])
|
2017-12-06 03:51:44 +01:00
|
|
|
endstops = [es for s in self.steppers for es in s.get_endstops()]
|
2017-02-19 16:36:08 +01:00
|
|
|
s = self.steppers[0] # Assume homing speed same for all steppers
|
2016-09-15 18:20:49 +02:00
|
|
|
# Initial homing
|
2017-12-20 20:11:38 +01:00
|
|
|
homing_speed = min(s.homing_speed, self.max_z_velocity)
|
2017-02-19 16:36:08 +01:00
|
|
|
homepos = [0., 0., self.max_z, None]
|
2016-09-15 18:20:49 +02:00
|
|
|
coord = list(homepos)
|
2017-10-29 19:14:12 +01:00
|
|
|
coord[2] = -1.5 * math.sqrt(max(self.arm2)-self.max_xy2)
|
2017-12-06 03:51:44 +01:00
|
|
|
homing_state.home(coord, homepos, endstops, homing_speed)
|
2016-09-15 18:20:49 +02:00
|
|
|
# Retract
|
|
|
|
coord[2] = homepos[2] - s.homing_retract_dist
|
2017-12-06 03:51:44 +01:00
|
|
|
homing_state.retract(coord, homing_speed)
|
2016-09-15 18:20:49 +02:00
|
|
|
# Home again
|
|
|
|
coord[2] -= s.homing_retract_dist
|
2017-12-06 03:51:44 +01:00
|
|
|
homing_state.home(coord, homepos, endstops,
|
|
|
|
homing_speed/2.0, second_home=True)
|
2016-12-09 00:12:20 +01:00
|
|
|
# Set final homed position
|
2017-10-29 19:14:12 +01:00
|
|
|
spos = [ep + s.get_homed_offset()
|
|
|
|
for ep, s in zip(self.endstops, self.steppers)]
|
2017-06-10 01:45:44 +02:00
|
|
|
homing_state.set_homed_position(self._actuator_to_cartesian(spos))
|
2017-09-12 18:47:40 +02:00
|
|
|
def motor_off(self, print_time):
|
2016-09-15 18:20:49 +02:00
|
|
|
self.limit_xy2 = -1.
|
|
|
|
for stepper in self.steppers:
|
2017-09-12 18:47:40 +02:00
|
|
|
stepper.motor_enable(print_time, 0)
|
2017-02-13 04:17:32 +01:00
|
|
|
self.need_motor_enable = self.need_home = True
|
2017-09-12 18:47:40 +02:00
|
|
|
def _check_motor_enable(self, print_time):
|
2016-11-14 19:40:35 +01:00
|
|
|
for i in StepList:
|
2017-09-12 18:47:40 +02:00
|
|
|
self.steppers[i].motor_enable(print_time, 1)
|
2016-11-14 19:40:35 +01:00
|
|
|
self.need_motor_enable = False
|
2016-09-15 18:20:49 +02:00
|
|
|
def check_move(self, move):
|
|
|
|
end_pos = move.end_pos
|
|
|
|
xy2 = end_pos[0]**2 + end_pos[1]**2
|
2017-02-13 04:17:32 +01:00
|
|
|
if xy2 <= self.limit_xy2 and not move.axes_d[2]:
|
|
|
|
# Normal XY move
|
|
|
|
return
|
|
|
|
if self.need_home:
|
|
|
|
raise homing.EndstopMoveError(end_pos, "Must home first")
|
|
|
|
limit_xy2 = self.max_xy2
|
2016-09-15 18:20:49 +02:00
|
|
|
if end_pos[2] > self.limit_z:
|
2017-02-13 04:17:32 +01:00
|
|
|
limit_xy2 = min(limit_xy2, (self.max_z - end_pos[2])**2)
|
2017-12-23 03:44:56 +01:00
|
|
|
if xy2 > limit_xy2 or end_pos[2] < self.min_z or end_pos[2] > self.max_z:
|
2017-02-13 04:17:32 +01:00
|
|
|
raise homing.EndstopMoveError(end_pos)
|
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
|
|
|
|
extreme_xy2 = max(xy2, move.start_pos[0]**2 + move.start_pos[1]**2)
|
|
|
|
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)
|
2017-09-12 18:47:40 +02:00
|
|
|
def move(self, print_time, move):
|
2017-04-06 17:09:08 +02:00
|
|
|
if self.need_motor_enable:
|
2017-09-12 18:47:40 +02:00
|
|
|
self._check_motor_enable(print_time)
|
2018-06-09 03:30:38 +02:00
|
|
|
self.move_fill(
|
|
|
|
self.cmove, print_time,
|
|
|
|
move.accel_t, move.cruise_t, move.decel_t,
|
|
|
|
move.start_pos[0], move.start_pos[1], move.start_pos[2],
|
|
|
|
move.axes_d[0], move.axes_d[1], move.axes_d[2],
|
|
|
|
move.start_v, move.cruise_v, move.accel)
|
|
|
|
for stepper in self.steppers:
|
|
|
|
stepper.step_itersolve(self.cmove)
|
2017-12-04 00:54:34 +01:00
|
|
|
# Helper functions for DELTA_CALIBRATE script
|
|
|
|
def get_stable_position(self):
|
|
|
|
return [int((ep - s.mcu_stepper.get_commanded_position())
|
|
|
|
/ s.mcu_stepper.get_step_dist() + .5)
|
|
|
|
* s.mcu_stepper.get_step_dist()
|
|
|
|
for ep, s in zip(self.endstops, self.steppers)]
|
|
|
|
def get_calibrate_params(self):
|
|
|
|
return {
|
|
|
|
'endstop_a': self.steppers[0].position_endstop,
|
|
|
|
'endstop_b': self.steppers[1].position_endstop,
|
|
|
|
'endstop_c': self.steppers[2].position_endstop,
|
|
|
|
'angle_a': self.angles[0], 'angle_b': self.angles[1],
|
|
|
|
'angle_c': self.angles[2], 'radius': self.radius,
|
|
|
|
'arm_a': self.arm_lengths[0], 'arm_b': self.arm_lengths[1],
|
|
|
|
'arm_c': self.arm_lengths[2] }
|
2016-09-15 18:20:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# Matrix helper functions for 3x1 matrices
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def matrix_cross(m1, m2):
|
|
|
|
return [m1[1] * m2[2] - m1[2] * m2[1],
|
|
|
|
m1[2] * m2[0] - m1[0] * m2[2],
|
|
|
|
m1[0] * m2[1] - m1[1] * m2[0]]
|
|
|
|
|
|
|
|
def matrix_dot(m1, m2):
|
|
|
|
return m1[0] * m2[0] + m1[1] * m2[1] + m1[2] * m2[2]
|
|
|
|
|
|
|
|
def matrix_magsq(m1):
|
|
|
|
return m1[0]**2 + m1[1]**2 + m1[2]**2
|
|
|
|
|
2017-10-29 15:42:12 +01:00
|
|
|
def matrix_add(m1, m2):
|
|
|
|
return [m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2]]
|
|
|
|
|
2016-09-15 18:20:49 +02:00
|
|
|
def matrix_sub(m1, m2):
|
|
|
|
return [m1[0] - m2[0], m1[1] - m2[1], m1[2] - m2[2]]
|
|
|
|
|
|
|
|
def matrix_mul(m1, s):
|
|
|
|
return [m1[0]*s, m1[1]*s, m1[2]*s]
|
2017-12-04 00:54:34 +01:00
|
|
|
|
|
|
|
def actuator_to_cartesian(towers, arm2, pos):
|
|
|
|
# Find nozzle position using trilateration (see wikipedia)
|
|
|
|
carriage1 = list(towers[0]) + [pos[0]]
|
|
|
|
carriage2 = list(towers[1]) + [pos[1]]
|
|
|
|
carriage3 = list(towers[2]) + [pos[2]]
|
|
|
|
|
|
|
|
s21 = matrix_sub(carriage2, carriage1)
|
|
|
|
s31 = matrix_sub(carriage3, carriage1)
|
|
|
|
|
|
|
|
d = math.sqrt(matrix_magsq(s21))
|
|
|
|
ex = matrix_mul(s21, 1. / d)
|
|
|
|
i = matrix_dot(ex, s31)
|
|
|
|
vect_ey = matrix_sub(s31, matrix_mul(ex, i))
|
|
|
|
ey = matrix_mul(vect_ey, 1. / math.sqrt(matrix_magsq(vect_ey)))
|
|
|
|
ez = matrix_cross(ex, ey)
|
|
|
|
j = matrix_dot(ey, s31)
|
|
|
|
|
|
|
|
x = (arm2[0] - arm2[1] + d**2) / (2. * d)
|
|
|
|
y = (arm2[0] - arm2[2] - x**2 + (x-i)**2 + j**2) / (2. * j)
|
|
|
|
z = -math.sqrt(arm2[0] - x**2 - y**2)
|
|
|
|
|
|
|
|
ex_x = matrix_mul(ex, x)
|
|
|
|
ey_y = matrix_mul(ey, y)
|
|
|
|
ez_z = matrix_mul(ez, z)
|
|
|
|
return matrix_add(carriage1, matrix_add(ex_x, matrix_add(ey_y, ez_z)))
|
|
|
|
|
|
|
|
def get_position_from_stable(spos, params):
|
|
|
|
angles = [params['angle_a'], params['angle_b'], params['angle_c']]
|
|
|
|
radius = params['radius']
|
|
|
|
radius2 = radius**2
|
|
|
|
towers = [(math.cos(angle) * radius, math.sin(angle) * radius)
|
|
|
|
for angle in map(math.radians, angles)]
|
|
|
|
arm2 = [a**2 for a in [params['arm_a'], params['arm_b'], params['arm_c']]]
|
|
|
|
endstops = [params['endstop_a'], params['endstop_b'], params['endstop_c']]
|
|
|
|
pos = [es + math.sqrt(a2 - radius2) - p
|
|
|
|
for es, a2, p in zip(endstops, arm2, spos)]
|
|
|
|
return actuator_to_cartesian(towers, arm2, pos)
|