Oops, fix min_slope_position/min_slope_postion -> min_slope_ratio typo.
[pyafm.git] / pyafm / afm.py
index 8ae61fa524e76e068091f90fdfd014eecba8d95d..0c89df9da1e8fae7e2d026800956f3cf4cb96653 100644 (file)
@@ -26,6 +26,8 @@ module only contains methods that require the capabilities of both.
 from pypiezo.base import convert_bits_to_meters as _convert_bits_to_meters
 from pypiezo.base import convert_meters_to_bits as _convert_meters_to_bits
 from pypiezo.base import convert_volts_to_bits as _convert_volts_to_bits
+from pypiezo.surface import FlatFit as _FlatFit
+from pypiezo.surface import SurfaceError as _SurfaceError
 
 from . import LOG as _LOG
 
@@ -57,10 +59,11 @@ class AFM (object):
         Return the sample temperature in Kelvin or `None` if such a
         measurement is not possible.
         """
-        if hasattr(self.temperature, 'get'):
+        if hasattr(self.temperature, 'get_temperature'):
             return self.temperature.get_temperature()
 
-    def move_just_onto_surface(self, depth=-50e-9, setpoint=2, far=200):
+    def move_just_onto_surface(self, depth=-50e-9, setpoint=2,
+                               min_slope_ratio=10, far=200):
         """Position the AFM tip close to the surface.
 
         Uses `.piezo.get_surface_position()` to pinpoint the position
@@ -81,8 +84,8 @@ class AFM (object):
 
         axis = self.piezo.axis_by_name(self.axis_name)
 
-        zero = _convert_volts_to_bits(axis.axis_channel_config, 0)
-        target_def = _convert_volts_to_bits(axis.axis_channel_config, setpoint)
+        zero = _convert_volts_to_bits(axis.config['channel'], 0)
+        target_def = _convert_volts_to_bits(axis.config['channel'], setpoint)
 
         _LOG.debug('zero the %s piezo output' % self.axis_name)
         self.piezo.jump(axis_name=self.axis_name, position=zero)
@@ -90,18 +93,20 @@ class AFM (object):
         _LOG.debug("see if we're starting near the surface")
         try:
             pos = self.piezo.get_surface_position(
-                axis_name=self.axis_name, max_deflection=target_def)
+                axis_name=self.axis_name, max_deflection=target_def,
+                min_slope_ratio=min_slope_ratio)
         except _FlatFit, e:
             _LOG.info(e)
             pos = self._stepper_approach_again(
-                target_deflection=target_def, far=far)
+                target_deflection=target_def, min_slope_ratio=min_slope_ratio,
+                far=far)
         except _SurfaceError, e:
             _LOG.info(e)
             pos = self._stepper_approach_again(
-                target_deflection=target_def, far=far)
+                target_deflection=target_def, min_slope_ratio=min_slope_ratio,
+                far=far)
 
-        pos_m = _convert_bits_to_meters(
-            axis.axis_channel_config, axis.axis_config, pos)
+        pos_m = _convert_bits_to_meters(axis.config, pos)
         _LOG.debug('located surface at stepper %d, piezo %d (%g m)'
                   % (self.stepper.position, pos, pos_m))
 
@@ -111,12 +116,12 @@ class AFM (object):
             self.stepper.step_relative(-1, backlash_safe=True)
             try:
                 pos = self.piezo.get_surface_position(
-                    axis_name=self.axis_name, max_deflection=target_def)
+                    axis_name=self.axis_name, max_deflection=target_def,
+                    min_slope_ratio=min_slope_ratio)
             except _FlatFit, e:
                 _LOG.debug(e)
                 continue
-            pos_m = _convert_bits_to_meters(
-                axis.axis_channel_config, axis.axis_config, pos)
+            pos_m = _convert_bits_to_meters(axis.config, pos)
             _LOG.debug('located surface at stepper %d, piezo %d (%g m)'
                       % (self.stepper.position, pos, pos_m))
         while pos_m > stepper_tolerance:  # step forward if we need to
@@ -124,20 +129,19 @@ class AFM (object):
             self.stepper.step_relative(1)
             try:
                 pos = self.piezo.get_surface_position(
-                    axis_name=self.axis_name, max_deflection=target_def)
+                    axis_name=self.axis_name, max_deflection=target_def,
+                    min_slope_ratio=min_slope_ratio)
             except _FlatFit, e:
                 _LOG.debug(e)
                 continue
-            pos_m = _convert_bits_to_meters(
-                axis.axis_channel_config, axis.axis_config, pos)
+            pos_m = _convert_bits_to_meters(axis.config, pos)
             _LOG.debug('located surface at stepper %d, piezo %d (%g m)'
                       % (self.stepper.position, pos, pos_m))
 
         _LOG.debug('adjust the %s piezo to place us just onto the surface'
                   % self.axis_name)
         target_m = pos_m + depth
-        target = _convert_meters_to_bits(
-            axis.axis_channel_config, axis.axis_config, target_m)
+        target = _convert_meters_to_bits(axis.config, target_m)
         self.piezo.jump(self.axis_name, target)
 
         _LOG.debug(
@@ -145,27 +149,19 @@ class AFM (object):
             % (depth, self.stepper.position, target, target_m))
 
 
-    def _stepper_approach_again(self, target_deflection, far):
+    def _stepper_approach_again(self, target_deflection, min_slope_ratio, far):
         _LOG.info('back off %d half steps and approach until deflection > %g'
                  % (far, target_deflection))
-
         # back away
         self.stepper.step_relative(-far, backlash_safe=True)
-
-        cd = self.piezo.read_deflection()  # cd = current deflection in bits
-        _LOG.debug('single stepping approach')
-        while cd < target_deflection:
-            _LOG.debug('deflection %g < setpoint %g.  step closer'
-                      % (cd, target_deflection))
-            self.stepper.single_step(1)  # step in
-            cd = self.piezo.read_deflection()
-
+        self.stepper_approach(target_deflection=target_deflection)
         for i in range(2*max(1, self.stepper.backlash)):
             _LOG.debug(
                 'additional surface location attempt (stepping backwards)')
             try:
                 pos = self.piezo.get_surface_position(
-                    axis_name=self.axis_name, max_deflection=target_deflection)
+                    axis_name=self.axis_name, max_deflection=target_deflection,
+                    min_slope_ratio=min_slope_ratio)
                 return pos
             except _SurfaceError, e:
                 _LOG.info(e)
@@ -173,3 +169,14 @@ class AFM (object):
         _LOG.debug('giving up on finding the surface')
         _LOG.warn(e)
         raise e
+
+    def stepper_approach(self, target_deflection):
+        _LOG.info('approach with stepper until deflection > {}'.format(
+                target_deflection))
+        cd = self.piezo.read_deflection()  # cd = current deflection in bits
+        _LOG.debug('single stepping approach')
+        while cd < target_deflection:
+            _LOG.debug('deflection {} < setpoint {}.  step closer'.format(
+                    cd, target_deflection))
+            self.stepper.single_step(1)  # step in
+            cd = self.piezo.read_deflection()