| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  | # Delta calibration support | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2017-2018  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							|  |  |  | import math, logging | 
					
						
							| 
									
										
										
										
											2018-07-12 22:15:45 -04:00
										 |  |  | import probe, mathutil, kinematics.delta | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DeltaCalibrate: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         if config.getsection('printer').get('kinematics') != 'delta': | 
					
						
							|  |  |  |             raise config.error("Delta calibrate is only for delta printers") | 
					
						
							| 
									
										
										
										
											2018-05-20 11:28:28 -04:00
										 |  |  |         # Calculate default probing points | 
					
						
							|  |  |  |         radius = config.getfloat('radius', above=0.) | 
					
						
							|  |  |  |         points = [(0., 0.)] | 
					
						
							|  |  |  |         scatter = [.95, .90, .85, .70, .75, .80] | 
					
						
							|  |  |  |         for i in range(6): | 
					
						
							|  |  |  |             r = math.radians(90. + 60. * i) | 
					
						
							|  |  |  |             dist = radius * scatter[i] | 
					
						
							|  |  |  |             points.append((math.cos(r) * dist, math.sin(r) * dist)) | 
					
						
							|  |  |  |         self.probe_helper = probe.ProbePointsHelper( | 
					
						
							|  |  |  |             config, self, default_points=points) | 
					
						
							|  |  |  |         # Register DELTA_CALIBRATE command | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         self.gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         self.gcode.register_command( | 
					
						
							|  |  |  |             'DELTA_CALIBRATE', self.cmd_DELTA_CALIBRATE, | 
					
						
							|  |  |  |             desc=self.cmd_DELTA_CALIBRATE_help) | 
					
						
							|  |  |  |     cmd_DELTA_CALIBRATE_help = "Delta calibration script" | 
					
						
							|  |  |  |     def cmd_DELTA_CALIBRATE(self, params): | 
					
						
							| 
									
										
										
										
											2018-06-30 14:08:02 -04:00
										 |  |  |         self.gcode.run_script_from_command("G28") | 
					
						
							| 
									
										
										
										
											2018-05-20 11:28:28 -04:00
										 |  |  |         self.probe_helper.start_probe() | 
					
						
							| 
									
										
										
										
											2018-06-22 15:33:12 -04:00
										 |  |  |     def get_probed_position(self): | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         kin = self.printer.lookup_object('toolhead').get_kinematics() | 
					
						
							|  |  |  |         return kin.get_stable_position() | 
					
						
							| 
									
										
										
										
											2018-03-17 14:00:37 -04:00
										 |  |  |     def finalize(self, z_offset, positions): | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         kin = self.printer.lookup_object('toolhead').get_kinematics() | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Calculating delta_calibrate with: %s", positions) | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         params = kin.get_calibrate_params() | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Initial delta_calibrate parameters: %s", params) | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius', | 
					
						
							|  |  |  |                       'angle_a', 'angle_b') | 
					
						
							| 
									
										
										
										
											2018-07-12 22:15:45 -04:00
										 |  |  |         get_position_from_stable = kinematics.delta.get_position_from_stable | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         def delta_errorfunc(params): | 
					
						
							|  |  |  |             total_error = 0. | 
					
						
							|  |  |  |             for spos in positions: | 
					
						
							| 
									
										
										
										
											2018-07-12 22:15:45 -04:00
										 |  |  |                 x, y, z = get_position_from_stable(spos, params) | 
					
						
							| 
									
										
										
										
											2018-03-17 14:00:37 -04:00
										 |  |  |                 total_error += (z - z_offset)**2 | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |             return total_error | 
					
						
							| 
									
										
										
										
											2018-03-02 18:18:35 -05:00
										 |  |  |         new_params = mathutil.coordinate_descent( | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |             adj_params, params, delta_errorfunc) | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Calculated delta_calibrate parameters: %s", new_params) | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         for spos in positions: | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |             logging.info("orig: %s new: %s", | 
					
						
							| 
									
										
										
										
											2018-07-12 22:15:45 -04:00
										 |  |  |                          get_position_from_stable(spos, params), | 
					
						
							|  |  |  |                          get_position_from_stable(spos, new_params)) | 
					
						
							| 
									
										
										
										
											2017-12-03 18:54:34 -05:00
										 |  |  |         self.gcode.respond_info( | 
					
						
							|  |  |  |             "stepper_a: position_endstop: %.6f angle: %.6f\n" | 
					
						
							|  |  |  |             "stepper_b: position_endstop: %.6f angle: %.6f\n" | 
					
						
							|  |  |  |             "stepper_c: position_endstop: %.6f angle: %.6f\n" | 
					
						
							|  |  |  |             "radius: %.6f\n" | 
					
						
							|  |  |  |             "To use these parameters, update the printer config file with\n" | 
					
						
							|  |  |  |             "the above and then issue a RESTART command" % ( | 
					
						
							|  |  |  |                 new_params['endstop_a'], new_params['angle_a'], | 
					
						
							|  |  |  |                 new_params['endstop_b'], new_params['angle_b'], | 
					
						
							|  |  |  |                 new_params['endstop_c'], new_params['angle_c'], | 
					
						
							|  |  |  |                 new_params['radius'])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return DeltaCalibrate(config) |