From e3f9ff6701159d8447364121587717208ec521eb Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Sat, 17 Mar 2018 14:00:37 -0400
Subject: [PATCH] probe: Add z_offset parameter

Move the probe_z_offset parameter from delta_calibrate and
bed_tilt_calibrate to a z_offset parameter within the probe config
section.  It's easier to understand the z offset setting when it is in
the probe config section.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
---
 config/example-delta.cfg         | 3 ---
 config/example-extras.cfg        | 6 +++---
 klippy/extras/bed_tilt.py        | 7 +++----
 klippy/extras/delta_calibrate.py | 5 ++---
 klippy/extras/probe.py           | 8 +++++++-
 5 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/config/example-delta.cfg b/config/example-delta.cfg
index 76d0cfc07..291916d37 100644
--- a/config/example-delta.cfg
+++ b/config/example-delta.cfg
@@ -121,9 +121,6 @@ radius: 50
 #horizontal_move_z: 5
 #   The height (in mm) that the head should be commanded to move to
 #   just prior to starting a probe operation. The default is 5.
-#probe_z_offset: 0
-#   The Z height (in mm) of the head when the probe triggers. The
-#   default is 0.
 #manual_probe:
 #   If true, then DELTA_CALIBRATE will perform manual probing. If
 #   false, then a PROBE command will be run at each probe
diff --git a/config/example-extras.cfg b/config/example-extras.cfg
index a548d86e3..38076f6ee 100644
--- a/config/example-extras.cfg
+++ b/config/example-extras.cfg
@@ -17,6 +17,9 @@
 #[probe]
 #pin: ar15
 #   Probe detection pin. This parameter must be provided.
+#z_offset:
+#   The distance (in mm) between the bed and the nozzle when the probe
+#   triggers. This parameter must be provided.
 #speed: 5.0
 #   Speed (in mm/s) of the Z axis when probing. The default is 5mm/s.
 #activate_gcode:
@@ -52,9 +55,6 @@
 #horizontal_move_z: 5
 #   The height (in mm) that the head should be commanded to move to
 #   just prior to starting a probe operation. The default is 5.
-#probe_z_offset: 0
-#   The Z height (in mm) of the head when the probe triggers. The
-#   default is 0.
 #manual_probe:
 #   If true, then BED_TILT_CALIBRATE will perform manual probing. If
 #   false, then a PROBE command will be run at each probe
diff --git a/klippy/extras/bed_tilt.py b/klippy/extras/bed_tilt.py
index 675097fa5..c4c5b506e 100644
--- a/klippy/extras/bed_tilt.py
+++ b/klippy/extras/bed_tilt.py
@@ -43,7 +43,6 @@ class BedTiltCalibrate:
             raise config.error("Need at least 3 points for bed_tilt_calibrate")
         self.speed = config.getfloat('speed', 50., above=0.)
         self.horizontal_move_z = config.getfloat('horizontal_move_z', 5.)
-        self.probe_z_offset = config.getfloat('probe_z_offset', 0.)
         self.z_position_endstop = None
         if config.has_section('stepper_z'):
             zconfig = config.getsection('stepper_z')
@@ -64,11 +63,11 @@ class BedTiltCalibrate:
     def get_position(self):
         kin = self.printer.lookup_object('toolhead').get_kinematics()
         return kin.get_position()
-    def finalize(self, positions):
+    def finalize(self, z_offset, positions):
         logging.info("Calculating bed_tilt with: %s", positions)
         params = { 'x_adjust': self.bedtilt.x_adjust,
                    'y_adjust': self.bedtilt.y_adjust,
-                   'z_adjust': self.probe_z_offset }
+                   'z_adjust': z_offset }
         logging.info("Initial bed_tilt parameters: %s", params)
         def adjusted_height(pos, params):
             x, y, z = pos
@@ -85,7 +84,7 @@ class BedTiltCalibrate:
         for pos in positions:
             logging.info("orig: %s new: %s", adjusted_height(pos, params),
                          adjusted_height(pos, new_params))
-        z_diff = new_params['z_adjust'] - self.probe_z_offset
+        z_diff = new_params['z_adjust'] - z_offset
         if self.z_position_endstop is not None:
             # Cartesian style robot
             z_extra = ""
diff --git a/klippy/extras/delta_calibrate.py b/klippy/extras/delta_calibrate.py
index 57ae8f093..ac9d13aa9 100644
--- a/klippy/extras/delta_calibrate.py
+++ b/klippy/extras/delta_calibrate.py
@@ -14,7 +14,6 @@ class DeltaCalibrate:
         self.radius = config.getfloat('radius', above=0.)
         self.speed = config.getfloat('speed', 50., above=0.)
         self.horizontal_move_z = config.getfloat('horizontal_move_z', 5.)
-        self.probe_z_offset = config.getfloat('probe_z_offset', 0.)
         self.manual_probe = config.getboolean('manual_probe', None)
         if self.manual_probe is None:
             self.manual_probe = not config.has_section('probe')
@@ -38,7 +37,7 @@ class DeltaCalibrate:
     def get_position(self):
         kin = self.printer.lookup_object('toolhead').get_kinematics()
         return kin.get_stable_position()
-    def finalize(self, positions):
+    def finalize(self, z_offset, positions):
         kin = self.printer.lookup_object('toolhead').get_kinematics()
         logging.info("Calculating delta_calibrate with: %s", positions)
         params = kin.get_calibrate_params()
@@ -49,7 +48,7 @@ class DeltaCalibrate:
             total_error = 0.
             for spos in positions:
                 x, y, z = delta.get_position_from_stable(spos, params)
-                total_error += (z - self.probe_z_offset)**2
+                total_error += (z - z_offset)**2
             return total_error
         new_params = mathutil.coordinate_descent(
             adj_params, params, delta_errorfunc)
diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py
index a8c2fa9be..3691266f5 100644
--- a/klippy/extras/probe.py
+++ b/klippy/extras/probe.py
@@ -16,6 +16,7 @@ class PrinterProbe:
     def __init__(self, config):
         self.printer = config.get_printer()
         self.speed = config.getfloat('speed', 5.0)
+        self.z_offset = config.getfloat('z_offset')
         # Infer Z position to move to during a probe
         if config.has_section('stepper_z'):
             zconfig = config.getsection('stepper_z')
@@ -137,6 +138,7 @@ class ProbePointsHelper:
         self.probe_points = probe_points
         self.horizontal_move_z = horizontal_move_z
         self.speed = speed
+        self.manual_probe = manual_probe
         self.callback = callback
         self.toolhead = self.printer.lookup_object('toolhead')
         self.results = []
@@ -177,7 +179,11 @@ class ProbePointsHelper:
         self.gcode.reset_last_position()
         self.gcode.register_command('NEXT', None)
         if success:
-            self.callback.finalize(self.results)
+            z_offset = 0.
+            if not self.manual_probe:
+                probe = self.printer.lookup_object('probe')
+                z_offset = probe.z_offset
+            self.callback.finalize(z_offset, self.results)
 
 def load_config(config):
     return PrinterProbe(config)