| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  | # Bed tilt compensation | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2018  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							|  |  |  | import logging | 
					
						
							| 
									
										
										
										
											2018-03-02 18:18:35 -05:00
										 |  |  | import probe, mathutil | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BedTilt: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         self.x_adjust = config.getfloat('x_adjust', 0.) | 
					
						
							|  |  |  |         self.y_adjust = config.getfloat('y_adjust', 0.) | 
					
						
							| 
									
										
										
										
											2018-08-18 11:28:42 -04:00
										 |  |  |         self.z_adjust = config.getfloat('z_adjust', 0.) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         if config.get('points', None) is not None: | 
					
						
							|  |  |  |             BedTiltCalibrate(config, self) | 
					
						
							|  |  |  |         self.toolhead = None | 
					
						
							| 
									
										
										
										
											2018-08-18 11:28:42 -04:00
										 |  |  |         # Register move transform with g-code class | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         gcode.set_move_transform(self) | 
					
						
							|  |  |  |     def printer_state(self, state): | 
					
						
							|  |  |  |         if state == 'connect': | 
					
						
							|  |  |  |             self.toolhead = self.printer.lookup_object('toolhead') | 
					
						
							|  |  |  |     def get_position(self): | 
					
						
							|  |  |  |         x, y, z, e = self.toolhead.get_position() | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |         return [x, y, z - x*self.x_adjust - y*self.y_adjust - self.z_adjust, e] | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |     def move(self, newpos, speed): | 
					
						
							|  |  |  |         x, y, z, e = newpos | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |         self.toolhead.move([x, y, z + x*self.x_adjust + y*self.y_adjust | 
					
						
							|  |  |  |                             + self.z_adjust, e], speed) | 
					
						
							| 
									
										
										
										
											2018-08-18 11:28:42 -04:00
										 |  |  |     def update_adjust(self, x_adjust, y_adjust, z_adjust): | 
					
						
							|  |  |  |         self.x_adjust = x_adjust | 
					
						
							|  |  |  |         self.y_adjust = y_adjust | 
					
						
							|  |  |  |         self.z_adjust = z_adjust | 
					
						
							|  |  |  |         configfile = self.printer.lookup_object('configfile') | 
					
						
							|  |  |  |         configfile.set('bed_tilt', 'x_adjust', "%.6f" % (x_adjust,)) | 
					
						
							|  |  |  |         configfile.set('bed_tilt', 'y_adjust', "%.6f" % (y_adjust,)) | 
					
						
							|  |  |  |         configfile.set('bed_tilt', 'z_adjust', "%.6f" % (z_adjust,)) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Helper script to calibrate the bed tilt | 
					
						
							|  |  |  | class BedTiltCalibrate: | 
					
						
							|  |  |  |     def __init__(self, config, bedtilt): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							| 
									
										
										
										
											2018-05-20 11:28:28 -04:00
										 |  |  |         self.bedtilt = bedtilt | 
					
						
							| 
									
										
										
										
											2018-09-26 10:32:57 -04:00
										 |  |  |         self.probe_helper = probe.ProbePointsHelper(config, self.probe_finalize) | 
					
						
							| 
									
										
										
										
											2018-05-20 11:28:28 -04:00
										 |  |  |         # Automatic probe:z_virtual_endstop XY detection | 
					
						
							| 
									
										
										
										
											2018-03-12 20:10:46 -04:00
										 |  |  |         self.z_position_endstop = None | 
					
						
							|  |  |  |         if config.has_section('stepper_z'): | 
					
						
							|  |  |  |             zconfig = config.getsection('stepper_z') | 
					
						
							|  |  |  |             self.z_position_endstop = zconfig.getfloat('position_endstop', None) | 
					
						
							| 
									
										
										
										
											2018-05-20 11:28:28 -04:00
										 |  |  |         # Register BED_TILT_CALIBRATE command | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         self.gcode = self.printer.lookup_object('gcode') | 
					
						
							|  |  |  |         self.gcode.register_command( | 
					
						
							|  |  |  |             'BED_TILT_CALIBRATE', self.cmd_BED_TILT_CALIBRATE, | 
					
						
							|  |  |  |             desc=self.cmd_BED_TILT_CALIBRATE_help) | 
					
						
							|  |  |  |     cmd_BED_TILT_CALIBRATE_help = "Bed tilt calibration script" | 
					
						
							|  |  |  |     def cmd_BED_TILT_CALIBRATE(self, params): | 
					
						
							| 
									
										
										
										
											2018-06-30 14:08:02 -04:00
										 |  |  |         self.gcode.run_script_from_command("G28") | 
					
						
							| 
									
										
										
										
											2018-09-26 13:07:18 -04:00
										 |  |  |         self.probe_helper.start_probe(params) | 
					
						
							| 
									
										
										
										
											2018-09-26 10:32:57 -04:00
										 |  |  |     def probe_finalize(self, offsets, positions): | 
					
						
							| 
									
										
										
										
											2018-08-18 12:25:57 -04:00
										 |  |  |         z_offset = offsets[2] | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Calculating bed_tilt with: %s", positions) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         params = { 'x_adjust': self.bedtilt.x_adjust, | 
					
						
							|  |  |  |                    'y_adjust': self.bedtilt.y_adjust, | 
					
						
							| 
									
										
										
										
											2018-03-17 14:00:37 -04:00
										 |  |  |                    'z_adjust': z_offset } | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Initial bed_tilt parameters: %s", params) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         def adjusted_height(pos, params): | 
					
						
							|  |  |  |             x, y, z = pos | 
					
						
							| 
									
										
										
										
											2018-02-05 14:52:38 -05:00
										 |  |  |             return (z - x*params['x_adjust'] - y*params['y_adjust'] | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |                     - params['z_adjust']) | 
					
						
							|  |  |  |         def errorfunc(params): | 
					
						
							|  |  |  |             total_error = 0. | 
					
						
							|  |  |  |             for pos in positions: | 
					
						
							|  |  |  |                 total_error += adjusted_height(pos, params)**2 | 
					
						
							|  |  |  |             return total_error | 
					
						
							| 
									
										
										
										
											2018-03-02 18:18:35 -05:00
										 |  |  |         new_params = mathutil.coordinate_descent( | 
					
						
							|  |  |  |             params.keys(), params, errorfunc) | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |         logging.info("Calculated bed_tilt parameters: %s", new_params) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         for pos in positions: | 
					
						
							| 
									
										
										
										
											2018-02-16 13:30:49 -05:00
										 |  |  |             logging.info("orig: %s new: %s", adjusted_height(pos, params), | 
					
						
							|  |  |  |                          adjusted_height(pos, new_params)) | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |         # Update current bed_tilt calculations | 
					
						
							| 
									
										
										
										
											2018-03-17 14:00:37 -04:00
										 |  |  |         z_diff = new_params['z_adjust'] - z_offset | 
					
						
							| 
									
										
										
										
											2018-08-18 11:28:42 -04:00
										 |  |  |         bed_tilt = self.printer.lookup_object('bed_tilt') | 
					
						
							|  |  |  |         bed_tilt.update_adjust(new_params['x_adjust'], new_params['y_adjust'], | 
					
						
							|  |  |  |                                z_diff) | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |         self.gcode.reset_last_position() | 
					
						
							|  |  |  |         # Report results back to user | 
					
						
							| 
									
										
										
										
											2018-03-12 20:10:46 -04:00
										 |  |  |         if self.z_position_endstop is not None: | 
					
						
							|  |  |  |             # Cartesian style robot | 
					
						
							|  |  |  |             z_extra = "" | 
					
						
							|  |  |  |             probe = self.printer.lookup_object('probe', None) | 
					
						
							|  |  |  |             if probe is not None: | 
					
						
							|  |  |  |                 last_home_position = probe.last_home_position() | 
					
						
							|  |  |  |                 if last_home_position is not None: | 
					
						
							|  |  |  |                     # Using z_virtual_endstop | 
					
						
							|  |  |  |                     home_x, home_y = last_home_position[:2] | 
					
						
							|  |  |  |                     z_diff -= home_x * new_params['x_adjust'] | 
					
						
							|  |  |  |                     z_diff -= home_y * new_params['y_adjust'] | 
					
						
							|  |  |  |                     z_extra = " (when Z homing at %.3f,%.3f)" % (home_x, home_y) | 
					
						
							|  |  |  |             z_adjust = "stepper_z position_endstop: %.6f%s\n" % ( | 
					
						
							|  |  |  |                 self.z_position_endstop - z_diff, z_extra) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Delta (or other) style robot | 
					
						
							|  |  |  |             z_adjust = "Add %.6f to endstop position\n" % (-z_diff,) | 
					
						
							|  |  |  |         msg = "%sx_adjust: %.6f y_adjust: %.6f" % ( | 
					
						
							|  |  |  |             z_adjust, new_params['x_adjust'], new_params['y_adjust']) | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |         self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg,)) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  |         self.gcode.respond_info( | 
					
						
							| 
									
										
										
										
											2018-04-03 12:02:17 -04:00
										 |  |  |             "%s\nThe above parameters have been applied to the current\n" | 
					
						
							| 
									
										
										
										
											2018-08-18 11:28:42 -04:00
										 |  |  |             "session. The SAVE_CONFIG command will update the printer\n" | 
					
						
							|  |  |  |             "config file and restart the printer." % (msg,)) | 
					
						
							| 
									
										
										
										
											2018-01-20 23:47:36 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return BedTilt(config) |