Add AFM._check_target_deflection to avoid approaching forever.
authorW. Trevor King <wking@drexel.edu>
Mon, 23 Jan 2012 20:41:52 +0000 (15:41 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 23 Jan 2012 20:41:52 +0000 (15:41 -0500)
Also use the deflection channel config to convert `setpoint` to bits,
instead of the output z channel config.

pyafm/afm.py

index 0c89df9da1e8fae7e2d026800956f3cf4cb96653..a73b59792e2190af458713d378683c373d17fd9e 100644 (file)
@@ -83,9 +83,11 @@ class AFM (object):
         stepper_tolerance = 2*self.stepper.step_size
 
         axis = self.piezo.axis_by_name(self.axis_name)
         stepper_tolerance = 2*self.stepper.step_size
 
         axis = self.piezo.axis_by_name(self.axis_name)
+        def = self._deflection_channel()
 
         zero = _convert_volts_to_bits(axis.config['channel'], 0)
 
         zero = _convert_volts_to_bits(axis.config['channel'], 0)
-        target_def = _convert_volts_to_bits(axis.config['channel'], setpoint)
+        target_def = _convert_volts_to_bits(def.config, setpoint)
+        self._check_target_deflection(target_def)
 
         _LOG.debug('zero the %s piezo output' % self.axis_name)
         self.piezo.jump(axis_name=self.axis_name, position=zero)
 
         _LOG.debug('zero the %s piezo output' % self.axis_name)
         self.piezo.jump(axis_name=self.axis_name, position=zero)
@@ -148,6 +150,14 @@ class AFM (object):
             'positioned %g m into the surface at stepper %d, piezo %d (%g m)'
             % (depth, self.stepper.position, target, target_m))
 
             'positioned %g m into the surface at stepper %d, piezo %d (%g m)'
             % (depth, self.stepper.position, target, target_m))
 
+    def _check_target_deflection(self, target_deflection):
+        def = self._deflection_channel()
+        max_def = def.get_maxdata():
+        if target_deflection > max_def:
+            _LOG.error(('requested setpoint ({:g} V = {:d} bits) is larger '
+                        'than the maximum deflection value of {:d} bits'
+                        ).format(setpoint, target_deflection, max_def))
+            raise ValueError(setpoint)
 
     def _stepper_approach_again(self, target_deflection, min_slope_ratio, far):
         _LOG.info('back off %d half steps and approach until deflection > %g'
 
     def _stepper_approach_again(self, target_deflection, min_slope_ratio, far):
         _LOG.info('back off %d half steps and approach until deflection > %g'
@@ -173,6 +183,7 @@ class AFM (object):
     def stepper_approach(self, target_deflection):
         _LOG.info('approach with stepper until deflection > {}'.format(
                 target_deflection))
     def stepper_approach(self, target_deflection):
         _LOG.info('approach with stepper until deflection > {}'.format(
                 target_deflection))
+        self._check_target_deflection(deflection=target_deflection)
         cd = self.piezo.read_deflection()  # cd = current deflection in bits
         _LOG.debug('single stepping approach')
         while cd < target_deflection:
         cd = self.piezo.read_deflection()  # cd = current deflection in bits
         _LOG.debug('single stepping approach')
         while cd < target_deflection: