From 2f2f2271e778d7de7f73157320a7cc095360413d Mon Sep 17 00:00:00 2001 From: KrauTech Date: Mon, 16 Dec 2024 19:57:37 +1100 Subject: [PATCH] [Touch][Threshold] Corrects Attempt After Max Retry & STD to MAX (#202) * should fix weird error after final attempt * remove useless else stamement * change weird result to all std_deviation * change back * changed std dev to max dev * increment to v0.0.5 --- scanner.py | 120 +++++++++++++++++++++++--------------------- scripts/firmware.py | 2 +- 2 files changed, 63 insertions(+), 59 deletions(-) diff --git a/scanner.py b/scanner.py index 91d89f5..1c76841 100644 --- a/scanner.py +++ b/scanner.py @@ -461,7 +461,6 @@ def cmd_SCANNER_TOUCH(self, gcmd: GCodeCommand): manual_z_offset = gcmd.get_float( "Z_OFFSET", self.scanner_touch_config["z_offset"], minval=0 ) - # Debugging information self.log_debug_info( vars["verbose"], @@ -561,7 +560,7 @@ def cmd_SCANNER_TOUCH(self, gcmd: GCodeCommand): result = self.start_touch(gcmd, touch_settings, vars["verbose"]) - standard_deviation = result["standard_deviation"] + max_deviation = result["max_deviation"] final_position = result["final_position"] retries = result["retries"] success = result["success"] @@ -577,7 +576,7 @@ def cmd_SCANNER_TOUCH(self, gcmd: GCodeCommand): self.log_debug_info( vars["verbose"], gcmd, - f"Standard Deviation: {standard_deviation:.4f}", + f"Maximum Deviation: {max_deviation:.4f}", ) if calibrate == 1: self._calibrate( @@ -610,6 +609,7 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool): randomize = touch_settings.randomize original_threshold = self.detect_threshold_z + deviation = None try: self.detect_threshold_z = test_threshold # Set the initial position for the toolhead @@ -623,6 +623,13 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool): original_position = initial_position[:] while len(samples) < num_samples: + if retries >= max_retries: + self.detect_threshold_z = original_threshold + self.trigger_method = TriggerMethod.SCAN + self._zhop() + raise gcmd.error( + f"Exceeded maximum attempts [{retries}/{int(max_retries)}]" + ) if randomize > 0 and new_retry: # Generate random offsets x_offset = random.uniform(-randomize, randomize) @@ -649,6 +656,7 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool): ) except self.printer.command_error as e: if self.printer.is_shutdown(): + self.detect_threshold_z = original_threshold self.trigger_method = TriggerMethod.SCAN raise self.printer.command_error( "Touch procedure interrupted due to printer shutdown" @@ -674,12 +682,6 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool): deviation = round(deviation, 4) if deviation > tolerance: - if retries >= max_retries: - self.trigger_method = TriggerMethod.SCAN - self._zhop() - raise gcmd.error( - f"Exceeded maximum attempts [{retries}/{int(max_retries)}]" - ) self.log_debug_info( verbose, gcmd, @@ -698,42 +700,45 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool): f"Deviation: {deviation:.4f}\nNew Average: {average:.4f}\nTolerance: {tolerance:.4f}", ) - std_dev = np.std(samples) - gcmd.respond_info( - f"Completed {len(samples)} touches with a standard deviation of {std_dev:.4f}" - ) - position_difference = initial_position[2] - self.toolhead.get_position()[2] - adjusted_difference = initial_position[2] - np.mean(samples) - self.log_debug_info( - verbose, - gcmd, - f"Position Difference: {position_difference:.4f}\nAdjusted Difference: {adjusted_difference:.4f}", - ) + if len(samples) == num_samples and deviation <= tolerance: + gcmd.respond_info( + f"Completed {len(samples)} touches with a max deviation of {deviation:.4f}" + ) + position_difference = ( + initial_position[2] - self.toolhead.get_position()[2] + ) + adjusted_difference = initial_position[2] - np.mean(samples) + self.log_debug_info( + verbose, + gcmd, + f"Position Difference: {position_difference:.4f}\nAdjusted Difference: {adjusted_difference:.4f}", + ) - initial_position[2] = float(adjusted_difference - position_difference) - formatted_position = [f"{coord:.2f}" for coord in initial_position] - self.log_debug_info( - verbose, gcmd, f"Updated Initial Position: {formatted_position}" - ) - if manual_z_offset > 0: - gcmd.respond_info(f"Offsetting by {manual_z_offset:.3f}") - initial_position[2] = initial_position[2] - manual_z_offset - self.toolhead.set_position(initial_position) - self.toolhead.wait_moves() - self.toolhead.flush_step_generation() - self.trigger_method = TriggerMethod.SCAN - self.previous_probe_success = 1 + initial_position[2] = float(adjusted_difference - position_difference) + formatted_position = [f"{coord:.2f}" for coord in initial_position] + self.log_debug_info( + verbose, gcmd, f"Updated Initial Position: {formatted_position}" + ) + if manual_z_offset > 0: + gcmd.respond_info(f"Offsetting by {manual_z_offset:.3f}") + initial_position[2] = initial_position[2] - manual_z_offset + self.toolhead.set_position(initial_position) + self.toolhead.wait_moves() + self.toolhead.flush_step_generation() + self.trigger_method = TriggerMethod.SCAN + self.previous_probe_success = 1 - # Return relevant data - self.detect_threshold_z = original_threshold + # Return relevant data + self.detect_threshold_z = original_threshold return { "samples": samples, - "standard_deviation": std_dev, + "max_deviation": deviation, "final_position": initial_position, "retries": retries, "success": self.previous_probe_success, } except self.printer.command_error: + self.detect_threshold_z = original_threshold self.trigger_method = TriggerMethod.SCAN if hasattr(kinematics, "note_z_not_homed"): kinematics.note_z_not_homed() @@ -791,7 +796,7 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): max_acceptable_retries = round( confirmation_retries * THRESHOLD_ACCEPTANCE_FACTOR ) - max_acceptable_std_dev = vars["target"] + max_acceptable_max_dev = vars["target"] verbose = vars["verbose"] @@ -876,8 +881,8 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): if result["success"]: # Check if this result meets "good" criteria if result["retries"] <= max_acceptable_retries and ( - result["standard_deviation"] is not None - and result["standard_deviation"] <= max_acceptable_std_dev + result["max_deviation"] is not None + and result["max_deviation"] <= max_acceptable_max_dev ): # Increase threshold_max by 3 steps above the current threshold, only if it hasn't been increased before if not has_increased_threshold_max: @@ -898,8 +903,7 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): gcmd, touch_settings, verbose ) if not repeat_result["success"] or ( - repeat_result["standard_deviation"] - > max_acceptable_std_dev + repeat_result["max_deviation"] > max_acceptable_max_dev ): gcmd.respond_info( f"Qualify attempt {attempt + 1} failed for threshold {current_threshold}" @@ -907,15 +911,15 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): consistent_results = False break gcmd.respond_info( - f"Qualify attempt {attempt + 1} successful with std dev: {repeat_result['standard_deviation']:.5f}" + f"Qualify attempt {attempt + 1} successful with max dev: {repeat_result['max_deviation']:.5f}" ) # Save only successful repeat attempts in results result["consistent_results"] = ( consistent_results # Mark if it passed repeatability checks ) - result["standard_deviation"] = ( - repeat_result["standard_deviation"] + result["max_deviation"] = ( + repeat_result["max_deviation"] if consistent_results else None ) @@ -946,13 +950,13 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): return # Exit as there's no best threshold to save if consistent_results: - # Find the best consistent result based on minimum retries and standard deviation + # Find the best consistent result based on minimum retries and max deviation best_result = min( consistent_results, key=lambda x: ( x["retries"], - x["standard_deviation"] - if x["standard_deviation"] is not None + x["max_deviation"] + if x["max_deviation"] is not None else float("inf"), ), ) @@ -964,8 +968,8 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): results, key=lambda x: ( x["retries"], - x["standard_deviation"] - if x["standard_deviation"] is not None + x["max_deviation"] + if x["max_deviation"] is not None else float("inf"), ), ) @@ -976,21 +980,21 @@ def cmd_SCANNER_THRESHOLD_SCAN(self, gcmd: GCodeCommand): self.detect_threshold_z = best_threshold self._save_threshold(best_threshold, vars["speed"]) - # Handle None for standard deviation by using a default message - std_dev_display = ( - f"{best_result['standard_deviation']:.5f}" - if best_result["standard_deviation"] is not None + # Handle None for max deviation by using a default message + max_dev_display = ( + f"{best_result['max_deviation']:.5f}" + if best_result["max_deviation"] is not None else "N/A" ) # Inform the user about the result if optimal_found: gcmd.respond_info( - f"Optimal Threshold Determined: {best_threshold} with standard deviation of {std_dev_display}" + f"Optimal Threshold Determined: {best_threshold} with max deviation of {max_dev_display}" ) else: gcmd.respond_info( - f"No fully optimal threshold found. Best attempt: {best_threshold} with standard deviation of {std_dev_display}" + f"No fully optimal threshold found. Best attempt: {best_threshold} with max deviation of {max_dev_display}" ) gcmd.respond_info( f"You can now {format_macro('SAVE_CONFIG')} to save your threshold." @@ -1098,7 +1102,7 @@ def start_threshold_scan(self, gcmd: GCodeCommand, touch_settings, verbose: bool f"Deviation: {deviation:.4f}\nNew Average: {average:.4f}\nTolerance: {tolerance:.4f}", ) - std_dev = np.std(samples) if samples else None + max_dev = np.std(samples) if samples else None if len(samples) == num_samples: success = True position_difference = ( @@ -1111,7 +1115,7 @@ def start_threshold_scan(self, gcmd: GCodeCommand, touch_settings, verbose: bool f"Position Difference: {position_difference:.4f}\nAdjusted Difference: {adjusted_difference:.4f}", ) else: - std_dev = None + max_dev = None success = False self.toolhead.wait_moves() @@ -1121,7 +1125,7 @@ def start_threshold_scan(self, gcmd: GCodeCommand, touch_settings, verbose: bool # Return relevant data return { "samples": samples, - "standard_deviation": std_dev, + "max_deviation": max_dev, "final_position": initial_position, "retries": retries, "success": success, diff --git a/scripts/firmware.py b/scripts/firmware.py index 198a05b..04c2c43 100755 --- a/scripts/firmware.py +++ b/scripts/firmware.py @@ -28,7 +28,7 @@ ClassVar, ) -FLASHER_VERSION: str = "0.0.4" +FLASHER_VERSION: str = "0.0.5" PAGE_WIDTH: int = 89 # Default global width