Skip to content

Commit

Permalink
changed std dev to max dev
Browse files Browse the repository at this point in the history
  • Loading branch information
krautech committed Dec 16, 2024
1 parent f24ffe7 commit 8d16dbf
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,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"]
Expand All @@ -576,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(
Expand Down Expand Up @@ -609,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
Expand Down Expand Up @@ -698,10 +699,10 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool):
gcmd,
f"Deviation: {deviation:.4f}\nNew Average: {average:.4f}\nTolerance: {tolerance:.4f}",
)
std_dev = np.std(samples)
if retries < max_retries and std_dev <= tolerance:

if len(samples) == num_samples and deviation <= tolerance:
gcmd.respond_info(
f"Completed {len(samples)} touches with a standard deviation of {std_dev:.4f}"
f"Completed {len(samples)} touches with a max deviation of {deviation:.4f}"
)
position_difference = (
initial_position[2] - self.toolhead.get_position()[2]
Expand Down Expand Up @@ -731,7 +732,7 @@ def start_touch(self, gcmd: GCodeCommand, touch_settings, verbose: bool):
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,
Expand Down Expand Up @@ -795,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"]

Expand Down Expand Up @@ -880,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:
Expand All @@ -902,24 +903,23 @@ 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}"
)
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
)
Expand Down Expand Up @@ -950,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"),
),
)
Expand All @@ -968,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"),
),
)
Expand All @@ -980,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."
Expand Down Expand Up @@ -1102,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 = (
Expand All @@ -1115,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()
Expand All @@ -1125,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,
Expand Down

0 comments on commit 8d16dbf

Please sign in to comment.