aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-10-15 18:52:28 -0400
committerKevin O'Connor <kevin@koconnor.net>2018-10-15 19:01:19 -0400
commit368703fd78ae7256d8989acdc164b524e27cd320 (patch)
tree76cf2f589744a85d0c1dc6ed2fad1b55e7f1348c
parent46355f903eb8d13448633045346051bc68c1279b (diff)
downloadkutter-368703fd78ae7256d8989acdc164b524e27cd320.tar.gz
kutter-368703fd78ae7256d8989acdc164b524e27cd320.tar.xz
kutter-368703fd78ae7256d8989acdc164b524e27cd320.zip
delta_calibrate: Perform coordinate descent in a background process
Run the coordinate descent in a background process so that the main thread does not block. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--klippy/extras/delta_calibrate.py9
-rw-r--r--klippy/mathutil.py29
2 files changed, 30 insertions, 8 deletions
diff --git a/klippy/extras/delta_calibrate.py b/klippy/extras/delta_calibrate.py
index b30ffaf2..db8509a7 100644
--- a/klippy/extras/delta_calibrate.py
+++ b/klippy/extras/delta_calibrate.py
@@ -216,12 +216,7 @@ class DeltaCalibrate:
adj_params += ('arm_a', 'arm_b', 'arm_c')
z_weight = len(distances) / (MEASURE_WEIGHT * len(probe_positions))
# Perform coordinate descent
- call_count = [0]
def delta_errorfunc(params):
- call_count[0] += 1
- if not call_count[0] % 1000:
- self.gcode.respond_info("Working on calibration...")
- self.printer.get_reactor().pause(0.)
# Build new delta_params for params under test
delta_params = build_delta_params(params)
# Calculate z height errors
@@ -237,8 +232,8 @@ class DeltaCalibrate:
d = math.sqrt((x1-x2)**2 + (y1-y2)**2 + (z1-z2)**2)
total_error += (d - dist)**2
return total_error
- new_params = mathutil.coordinate_descent(
- adj_params, params, delta_errorfunc)
+ new_params = mathutil.background_coordinate_descent(
+ self.printer, adj_params, params, delta_errorfunc)
# Log and report results
logging.info("Calculated delta_calibrate parameters: %s", new_params)
new_delta_params = build_delta_params(new_params)
diff --git a/klippy/mathutil.py b/klippy/mathutil.py
index 0801eba5..81a949ad 100644
--- a/klippy/mathutil.py
+++ b/klippy/mathutil.py
@@ -3,7 +3,7 @@
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
-import math, logging
+import math, logging, multiprocessing
######################################################################
@@ -45,6 +45,33 @@ def coordinate_descent(adj_params, params, error_func):
logging.info("Coordinate descent best_err: %s rounds: %d", best_err, rounds)
return params
+# Helper to run the coordinate descent function in a background
+# process so that it does not block the main thread.
+def background_coordinate_descent(printer, adj_params, params, error_func):
+ parent_conn, child_conn = multiprocessing.Pipe()
+ def wrapper():
+ res = coordinate_descent(adj_params, params, error_func)
+ child_conn.send(res)
+ child_conn.close()
+ # Start a process to perform the calculation
+ calc_proc = multiprocessing.Process(target=wrapper)
+ calc_proc.daemon = True
+ calc_proc.start()
+ # Wait for the process to finish
+ reactor = printer.get_reactor()
+ gcode = printer.lookup_object("gcode")
+ eventtime = last_report_time = reactor.monotonic()
+ while calc_proc.is_alive():
+ if eventtime > last_report_time + 5.:
+ last_report_time = eventtime
+ gcode.respond_info("Working on calibration...")
+ eventtime = reactor.pause(eventtime + .1)
+ # Return results
+ res = parent_conn.recv()
+ calc_proc.join()
+ parent_conn.close()
+ return res
+
######################################################################
# Trilateration