aboutsummaryrefslogtreecommitdiffstats
path: root/klippy
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2018-03-02 18:18:35 -0500
committerKevin O'Connor <kevin@koconnor.net>2018-03-04 09:56:50 -0500
commitfa07be93463d9ce0262ae383259e68eea625bc4d (patch)
treeec028add97cd891fe9ebd85b140c1219e062eaaa /klippy
parent7290ed5f73f4c649099fec5b13cd7c3f7a29b2d2 (diff)
downloadkutter-fa07be93463d9ce0262ae383259e68eea625bc4d.tar.gz
kutter-fa07be93463d9ce0262ae383259e68eea625bc4d.tar.xz
kutter-fa07be93463d9ce0262ae383259e68eea625bc4d.zip
mathutil: Move coordinate_descent() to new file
Add a new python file (mathutil.py) and move the coordinate_descent() code to it. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy')
-rw-r--r--klippy/extras/bed_tilt.py5
-rw-r--r--klippy/extras/delta_calibrate.py4
-rw-r--r--klippy/extras/probe.py35
-rw-r--r--klippy/mathutil.py40
4 files changed, 45 insertions, 39 deletions
diff --git a/klippy/extras/bed_tilt.py b/klippy/extras/bed_tilt.py
index f7e61052..a6254a29 100644
--- a/klippy/extras/bed_tilt.py
+++ b/klippy/extras/bed_tilt.py
@@ -4,7 +4,7 @@
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
-import probe
+import probe, mathutil
class BedTilt:
def __init__(self, config):
@@ -73,7 +73,8 @@ class BedTiltCalibrate:
for pos in positions:
total_error += adjusted_height(pos, params)**2
return total_error
- new_params = probe.coordinate_descent(params.keys(), params, errorfunc)
+ new_params = mathutil.coordinate_descent(
+ params.keys(), params, errorfunc)
logging.info("Calculated bed_tilt parameters: %s", new_params)
for pos in positions:
logging.info("orig: %s new: %s", adjusted_height(pos, params),
diff --git a/klippy/extras/delta_calibrate.py b/klippy/extras/delta_calibrate.py
index b65a7815..57ae8f09 100644
--- a/klippy/extras/delta_calibrate.py
+++ b/klippy/extras/delta_calibrate.py
@@ -4,7 +4,7 @@
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import math, logging
-import probe, delta
+import probe, delta, mathutil
class DeltaCalibrate:
def __init__(self, config):
@@ -51,7 +51,7 @@ class DeltaCalibrate:
x, y, z = delta.get_position_from_stable(spos, params)
total_error += (z - self.probe_z_offset)**2
return total_error
- new_params = probe.coordinate_descent(
+ new_params = mathutil.coordinate_descent(
adj_params, params, delta_errorfunc)
logging.info("Calculated delta_calibrate parameters: %s", new_params)
for spos in positions:
diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py
index 70cc2a1d..4113a263 100644
--- a/klippy/extras/probe.py
+++ b/klippy/extras/probe.py
@@ -3,7 +3,6 @@
# Copyright (C) 2017-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
-import logging
import pins, homing
class PrinterProbe:
@@ -137,39 +136,5 @@ class ProbePointsHelper:
if success:
self.callback.finalize(self.results)
-# Helper code that implements coordinate descent
-def coordinate_descent(adj_params, params, error_func):
- # Define potential changes
- params = dict(params)
- dp = {param_name: 1. for param_name in adj_params}
- # Calculate the error
- best_err = error_func(params)
-
- threshold = 0.00001
- rounds = 0
-
- while sum(dp.values()) > threshold and rounds < 10000:
- rounds += 1
- for param_name in adj_params:
- orig = params[param_name]
- params[param_name] = orig + dp[param_name]
- err = error_func(params)
- if err < best_err:
- # There was some improvement
- best_err = err
- dp[param_name] *= 1.1
- continue
- params[param_name] = orig - dp[param_name]
- err = error_func(params)
- if err < best_err:
- # There was some improvement
- best_err = err
- dp[param_name] *= 1.1
- continue
- params[param_name] = orig
- dp[param_name] *= 0.9
- logging.info("Coordinate descent best_err: %s rounds: %d", best_err, rounds)
- return params
-
def load_config(config):
return PrinterProbe(config)
diff --git a/klippy/mathutil.py b/klippy/mathutil.py
new file mode 100644
index 00000000..d8df3539
--- /dev/null
+++ b/klippy/mathutil.py
@@ -0,0 +1,40 @@
+# Simple math helper functions
+#
+# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
+#
+# This file may be distributed under the terms of the GNU GPLv3 license.
+import logging
+
+# Helper code that implements coordinate descent
+def coordinate_descent(adj_params, params, error_func):
+ # Define potential changes
+ params = dict(params)
+ dp = {param_name: 1. for param_name in adj_params}
+ # Calculate the error
+ best_err = error_func(params)
+
+ threshold = 0.00001
+ rounds = 0
+
+ while sum(dp.values()) > threshold and rounds < 10000:
+ rounds += 1
+ for param_name in adj_params:
+ orig = params[param_name]
+ params[param_name] = orig + dp[param_name]
+ err = error_func(params)
+ if err < best_err:
+ # There was some improvement
+ best_err = err
+ dp[param_name] *= 1.1
+ continue
+ params[param_name] = orig - dp[param_name]
+ err = error_func(params)
+ if err < best_err:
+ # There was some improvement
+ best_err = err
+ dp[param_name] *= 1.1
+ continue
+ params[param_name] = orig
+ dp[param_name] *= 0.9
+ logging.info("Coordinate descent best_err: %s rounds: %d", best_err, rounds)
+ return params