| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  | # Helper script to adjust parameters based on Z level | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2019  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							|  |  |  | import math, logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 14:58:17 -04:00
										 |  |  | CANCEL_Z_DELTA=2.0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  | class TuningTower: | 
					
						
							|  |  |  |     def __init__(self, config): | 
					
						
							|  |  |  |         self.printer = config.get_printer() | 
					
						
							|  |  |  |         self.normal_transform = None | 
					
						
							|  |  |  |         self.last_position = [0., 0., 0., 0.] | 
					
						
							|  |  |  |         self.last_z = self.start = self.factor = self.band = 0. | 
					
						
							| 
									
										
										
										
											2020-01-07 19:21:54 -05:00
										 |  |  |         self.command_fmt = "" | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         # Register command | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object("gcode") | 
					
						
							|  |  |  |         gcode.register_command("TUNING_TOWER", self.cmd_TUNING_TOWER, | 
					
						
							|  |  |  |                                desc=self.cmd_TUNING_TOWER_help) | 
					
						
							|  |  |  |     cmd_TUNING_TOWER_help = "Tool to adjust a parameter at each Z height" | 
					
						
							| 
									
										
										
										
											2020-04-24 23:09:12 -04:00
										 |  |  |     def cmd_TUNING_TOWER(self, gcmd): | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         if self.normal_transform is not None: | 
					
						
							|  |  |  |             self.end_test() | 
					
						
							|  |  |  |         # Get parameters | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object("gcode") | 
					
						
							| 
									
										
										
										
											2020-04-24 23:09:12 -04:00
										 |  |  |         command = gcmd.get('COMMAND') | 
					
						
							|  |  |  |         parameter = gcmd.get('PARAMETER') | 
					
						
							|  |  |  |         self.start = gcmd.get_float('START', 0.) | 
					
						
							|  |  |  |         self.factor = gcmd.get_float('FACTOR') | 
					
						
							|  |  |  |         self.band = gcmd.get_float('BAND', 0., minval=0.) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         # Enable test mode | 
					
						
							| 
									
										
										
										
											2020-01-07 19:21:54 -05:00
										 |  |  |         if gcode.is_traditional_gcode(command): | 
					
						
							|  |  |  |             self.command_fmt = "%s %s%%.9f" % (command, parameter) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.command_fmt = "%s %s=%%.9f" % (command, parameter) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         self.normal_transform = gcode.set_move_transform(self, force=True) | 
					
						
							|  |  |  |         self.last_z = -99999999.9 | 
					
						
							|  |  |  |         gcode.reset_last_position() | 
					
						
							|  |  |  |         self.get_position() | 
					
						
							| 
									
										
										
										
											2020-04-24 23:09:12 -04:00
										 |  |  |         gcmd.respond_info("Starting tuning test (start=%.6f factor=%.6f)" | 
					
						
							|  |  |  |                           % (self.start, self.factor)) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |     def get_position(self): | 
					
						
							|  |  |  |         pos = self.normal_transform.get_position() | 
					
						
							| 
									
										
										
										
											2019-12-04 20:11:54 -05:00
										 |  |  |         self.last_position = list(pos) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         return pos | 
					
						
							|  |  |  |     def calc_value(self, z): | 
					
						
							| 
									
										
										
										
											2020-05-24 17:33:09 +02:00
										 |  |  |         # Compensate Z-Offset | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object("gcode") | 
					
						
							|  |  |  |         z_offset = gcode.get_status(None)['base_zpos'] | 
					
						
							|  |  |  |         z = max(0, z - z_offset) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         if self.band: | 
					
						
							|  |  |  |             z = (math.floor(z / self.band) + .5) * self.band | 
					
						
							|  |  |  |         return self.start + z * self.factor | 
					
						
							|  |  |  |     def move(self, newpos, speed): | 
					
						
							|  |  |  |         normal_transform = self.normal_transform | 
					
						
							|  |  |  |         if (newpos[3] > self.last_position[3] and newpos[2] != self.last_z | 
					
						
							|  |  |  |             and newpos[:3] != self.last_position[:3]): | 
					
						
							|  |  |  |             # Extrusion move at new z height | 
					
						
							|  |  |  |             z = newpos[2] | 
					
						
							| 
									
										
										
										
											2019-10-04 14:58:17 -04:00
										 |  |  |             if z < self.last_z - CANCEL_Z_DELTA: | 
					
						
							|  |  |  |                 # Extrude at a lower z height - probably start of new print | 
					
						
							|  |  |  |                 self.end_test() | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |                 # Process update | 
					
						
							|  |  |  |                 oldval = self.calc_value(self.last_z) | 
					
						
							|  |  |  |                 newval = self.calc_value(z) | 
					
						
							|  |  |  |                 self.last_z = z | 
					
						
							|  |  |  |                 if newval != oldval: | 
					
						
							|  |  |  |                     gcode = self.printer.lookup_object("gcode") | 
					
						
							| 
									
										
										
										
											2020-01-07 19:21:54 -05:00
										 |  |  |                     gcode.run_script_from_command(self.command_fmt % (newval,)) | 
					
						
							| 
									
										
										
										
											2019-01-26 13:26:14 -05:00
										 |  |  |         # Forward move to actual handler | 
					
						
							|  |  |  |         self.last_position[:] = newpos | 
					
						
							|  |  |  |         normal_transform.move(newpos, speed) | 
					
						
							|  |  |  |     def end_test(self): | 
					
						
							|  |  |  |         gcode = self.printer.lookup_object("gcode") | 
					
						
							|  |  |  |         gcode.respond_info("Ending tuning test mode") | 
					
						
							|  |  |  |         gcode.set_move_transform(self.normal_transform, force=True) | 
					
						
							|  |  |  |         self.normal_transform = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def load_config(config): | 
					
						
							|  |  |  |     return TuningTower(config) |