2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Code for handling the kinematics of linear delta robots
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Copyright (C) 2016,2017  Kevin O'Connor <kevin@koconnor.net>
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# This file may be distributed under the terms of the GNU GPLv3 license.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import math, logging
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import stepper, homing
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								StepList = (0, 1, 2)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# Slow moves once the ratio of tower to XY movement exceeds SLOW_RATIO
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								SLOW_RATIO = 3.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								class DeltaKinematics:
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-03 15:17:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def __init__(self, toolhead, printer, config):
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        stepper_configs = [config.getsection('stepper_' + n)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                           for n in ['a', 'b', 'c']]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.steppers = [stepper.PrinterHomingStepper(printer, sconfig)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                         for sconfig in stepper_configs]
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.need_motor_enable = self.need_home = True
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        radius = config.getfloat('delta_radius', above=0.)
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        arm_length_a = stepper_configs[0].getfloat('arm_length', above=radius)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        arm_lengths = [sconfig.getfloat('arm_length', arm_length_a, above=radius)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                       for sconfig in stepper_configs]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        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 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.limit_xy2 = -1.
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-09 19:45:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.max_z = min([s.position_endstop for s in self.steppers])
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.limit_z = min([ep - arm
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                            for ep, arm in zip(self.endstops, arm_lengths)])
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        logging.info(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            "Delta max build height %.2fmm (radius tapered above %.2fmm)" % (
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                self.max_z, self.limit_z))
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-03 15:17:02 -04: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 00:48:01 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        max_halt_velocity = toolhead.get_max_axis_halt()
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-03 15:17:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for s in self.steppers:
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-12 00:48:01 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            s.set_max_jerk(max_halt_velocity, self.max_accel)
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-05 14:43:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Determine tower locations in cartesian space
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        angles = [sconfig.getfloat('angle', angle)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                  for sconfig, angle in zip(stepper_configs, [210., 330., 90.])]
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-05 14:43:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.towers = [(math.cos(math.radians(angle)) * radius,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        math.sin(math.radians(angle)) * radius)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                       for angle in angles]
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Find the point where an XY move could result in excessive
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # tower movement
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        half_min_step_dist = min([s.step_dist for s in self.steppers]) * .5
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        min_arm_length = min(arm_lengths)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-21 11:18:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        def ratio_to_dist(ratio):
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            return (ratio * math.sqrt(min_arm_length**2 / (ratio**2 + 1.)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-21 11:18:56 -05: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 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.max_xy2 = min(radius, min_arm_length - radius,
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-21 11:18:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                           ratio_to_dist(4. * SLOW_RATIO) - radius)**2
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        logging.info(
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-21 11:18:56 -05: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)))
							 | 
						
					
						
							
								
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.set_position([0., 0., 0.])
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-06 10:13:58 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def get_steppers(self):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return list(self.steppers)
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 13:09:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _cartesian_to_actuator(self, coord):
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return [math.sqrt(self.arm2[i] - (self.towers[i][0] - coord[0])**2
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:20:54 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                          - (self.towers[i][1] - coord[1])**2) + coord[2]
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                for i in StepList]
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 13:09:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _actuator_to_cartesian(self, pos):
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 10:42:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Find nozzle position using trilateration (see wikipedia)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        carriage1 = list(self.towers[0]) + [pos[0]]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        carriage2 = list(self.towers[1]) + [pos[1]]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        carriage3 = list(self.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)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        x = (self.arm2[0] - self.arm2[1] + d**2) / (2. * d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        y = (self.arm2[0] - self.arm2[2] - x**2 + (x-i)**2 + j**2) / (2. * j)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        z = -math.sqrt(self.arm2[0] - x**2 - y**2)
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 10:42:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        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)))
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def set_position(self, newpos):
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 13:09:40 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        pos = self._cartesian_to_actuator(newpos)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-19 11:34:42 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for i in StepList:
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-07 12:29:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.steppers[i].set_position(pos[i])
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.limit_xy2 = -1.
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 11:27:16 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def home(self, homing_state):
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # All axes are homed simultaneously
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-18 11:27:16 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_state.set_axes([0, 1, 2])
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-05 21:51:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        endstops = [es for s in self.steppers for es in s.get_endstops()]
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-19 10:36:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        s = self.steppers[0] # Assume homing speed same for all steppers
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.need_home = False
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Initial homing
							 | 
						
					
						
							
								
									
										
										
										
											2017-08-29 17:37:14 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_speed = s.get_homing_speed()
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-19 10:36:08 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homepos = [0., 0., self.max_z, None]
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        coord = list(homepos)
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        coord[2] = -1.5 * math.sqrt(max(self.arm2)-self.max_xy2)
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-05 21:51:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_state.home(coord, homepos, endstops, homing_speed)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Retract
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        coord[2] = homepos[2] - s.homing_retract_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-05 21:51:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_state.retract(coord, homing_speed)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        # Home again
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        coord[2] -= s.homing_retract_dist
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-05 21:51:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_state.home(coord, homepos, endstops,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                          homing_speed/2.0, second_home=True)
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        # Set final homed position
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        spos = [ep + s.get_homed_offset()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                for ep, s in zip(self.endstops, self.steppers)]
							 | 
						
					
						
							
								
									
										
										
										
											2017-06-09 19:45:44 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        homing_state.set_homed_position(self._actuator_to_cartesian(spos))
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def motor_off(self, print_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.limit_xy2 = -1.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for stepper in self.steppers:
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            stepper.motor_enable(print_time, 0)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.need_motor_enable = self.need_home = True
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def _check_motor_enable(self, print_time):
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-14 13:40:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for i in StepList:
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self.steppers[i].motor_enable(print_time, 1)
							 | 
						
					
						
							
								
									
										
										
										
											2016-11-14 13:40:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        self.need_motor_enable = False
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def check_move(self, move):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        end_pos = move.end_pos
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        xy2 = end_pos[0]**2 + end_pos[1]**2
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05: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 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if end_pos[2] > self.limit_z:
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            limit_xy2 = min(limit_xy2, (self.max_z - end_pos[2])**2)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if xy2 > limit_xy2 or end_pos[2] < 0. or end_pos[2] > self.max_z:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise homing.EndstopMoveError(end_pos)
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-01 16:04:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if move.axes_d[2]:
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-12 22:17:32 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            move.limit_speed(self.max_z_velocity, move.accel)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            limit_xy2 = -1.
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05: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 11:18:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            r = 0.5
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if extreme_xy2 > self.very_slow_xy2:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                r = 0.25
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            max_velocity = self.max_velocity
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if move.axes_d[2]:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                max_velocity = self.max_z_velocity
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-21 11:18:56 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            move.limit_speed(max_velocity * r, self.max_accel * r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-13 15:04:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            limit_xy2 = -1.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        self.limit_xy2 = min(limit_xy2, self.slow_xy2)
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def move(self, print_time, move):
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if self.need_motor_enable:
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            self._check_motor_enable(print_time)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        axes_d = move.axes_d
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        move_d = move.move_d
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-04 19:30:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        movexy_r = 1.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        movez_r = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        inv_movexy_d = 1. / move_d
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if not axes_d[0] and not axes_d[1]:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            # Z only move
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-04 19:30:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            movez_r = axes_d[2] * inv_movexy_d
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            movexy_r = inv_movexy_d = 0.
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-04 19:30:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elif axes_d[2]:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            # XY+Z move
							 | 
						
					
						
							
								
									
										
										
										
											2016-12-04 19:30:35 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            movexy_d = math.sqrt(axes_d[0]**2 + axes_d[1]**2)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            movexy_r = movexy_d * inv_movexy_d
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            movez_r = axes_d[2] * inv_movexy_d
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            inv_movexy_d = 1. / movexy_d
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        origx, origy, origz = move.start_pos[:3]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-04 19:03:04 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        accel = move.accel
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cruise_v = move.cruise_v
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        accel_d = move.accel_r * move_d
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        cruise_d = move.cruise_r * move_d
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        decel_d = move.decel_r * move_d
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for i in StepList:
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            # Calculate a virtual tower along the line of movement at
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            # the point closest to this stepper's tower.
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            towerx_d = self.towers[i][0] - origx
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            towery_d = self.towers[i][1] - origy
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            vt_startxy_d = (towerx_d*axes_d[0] + towery_d*axes_d[1])*inv_movexy_d
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            tangentxy_d2 = towerx_d**2 + towery_d**2 - vt_startxy_d**2
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 14:14:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            vt_arm_d = math.sqrt(self.arm2[i] - tangentxy_d2)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            vt_startz = origz
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            # Generate steps
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-07 12:12:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            step_delta = self.steppers[i].step_delta
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            move_time = print_time
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if accel_d:
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-07 12:12:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                step_delta(move_time, accel_d, move.start_v, accel,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                           vt_startz, vt_startxy_d, vt_arm_d, movez_r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                vt_startz += accel_d * movez_r
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                vt_startxy_d -= accel_d * movexy_r
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                move_time += move.accel_t
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if cruise_d:
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-07 12:12:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                step_delta(move_time, cruise_d, cruise_v, 0.,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                           vt_startz, vt_startxy_d, vt_arm_d, movez_r)
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                vt_startz += cruise_d * movez_r
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                vt_startxy_d -= cruise_d * movexy_r
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                move_time += move.cruise_t
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-07 11:47:24 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            if decel_d:
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-07 12:12:28 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								                step_delta(move_time, decel_d, cruise_v, -accel,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                           vt_startz, vt_startxy_d, vt_arm_d, movez_r)
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04: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 10:42:12 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								def matrix_add(m1, m2):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return [m1[0] + m2[0], m1[1] + m2[1], m1[2] + m2[2]]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-15 12:20:49 -04: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]
							 |