Add matplotlib config option and stepper_approach plot.
authorW. Trevor King <wking@tremily.us>
Thu, 12 Jul 2012 22:01:20 +0000 (18:01 -0400)
committerW. Trevor King <wking@tremily.us>
Thu, 12 Jul 2012 22:01:20 +0000 (18:01 -0400)
pyafm/afm.py
pyafm/config.py

index bda92d53ea6a4271b144fae81f515e8a70f02beb..fc8dacbefc4f3d8e37c2776241e3206e6016b7dd 100644 (file)
@@ -22,6 +22,14 @@ for controlling the piezo (`pypiezo`) and stepper (`stepper`), this
 module only contains methods that require the capabilities of both.
 """
 
 module only contains methods that require the capabilities of both.
 """
 
+try:
+    import matplotlib as _matplotlib
+    import matplotlib.pyplot as _matplotlib_pyplot
+    import time as _time  # for timestamping lines on plots
+except (ImportError, RuntimeError), e:
+    _matplotlib = None
+    _matplotlib_import_error = e
+
 from pypiezo.afm import AFMPiezo as _AFMPiezo
 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.afm import AFMPiezo as _AFMPiezo
 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
@@ -30,6 +38,7 @@ from pypiezo.surface import FlatFit as _FlatFit
 from pypiezo.surface import SurfaceError as _SurfaceError
 
 from . import LOG as _LOG
 from pypiezo.surface import SurfaceError as _SurfaceError
 
 from . import LOG as _LOG
+from . import package_config as _package_config
 from .stepper import Stepper as _Stepper
 from .temperature import Temperature as _Temperature
 
 from .stepper import Stepper as _Stepper
 from .temperature import Temperature as _Temperature
 
@@ -454,6 +463,10 @@ 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))
+        record_data = _package_config['matplotlib']
+        if record_data:
+            position = []
+            deflection = []
         self._check_target_deflection(deflection=target_deflection)
         cd = self.piezo.read_deflection()  # cd = current deflection in bits
         _LOG.debug('single stepping approach')
         self._check_target_deflection(deflection=target_deflection)
         cd = self.piezo.read_deflection()  # cd = current deflection in bits
         _LOG.debug('single stepping approach')
@@ -462,6 +475,20 @@ class AFM (object):
                     cd, target_deflection))
             self.stepper.single_step(1)  # step in
             cd = self.piezo.read_deflection()
                     cd, target_deflection))
             self.stepper.single_step(1)  # step in
             cd = self.piezo.read_deflection()
+            if record_data:
+                position.append(self.stepper.position)
+                deflection.append(cd)
+        if _package_config['matplotlib']:
+            figure = _matplotlib_pyplot.figure()
+            axes = figure.add_subplot(1, 1, 1)
+            axes.hold(False)
+            timestamp = _time.strftime('%H-%M-%S')
+            axes.set_title('stepper approach {}'.format(timestamp))
+            plot = axes.plot(position, deflection, 'b.-')
+            figure.canvas.draw()
+            figure.show()
+            if not _matplotlib.is_interactive():
+                _matplotlib_pyplot.show()
 
     def move_toward_surface(self, distance):
         """Step in approximately `distance` meters.
 
     def move_toward_surface(self, distance):
         """Step in approximately `distance` meters.
index 6b3fa0e0a23e0712396e495a61f431c33d011ff2..5c72396f8810704d72386ec37f022b33596befa8 100644 (file)
@@ -24,7 +24,12 @@ import pypiezo.config as _pypiezo_config
 
 class PackageConfig (_h5config_tools.PackageConfig):
     "Configure `pyafm` module operation"
 
 class PackageConfig (_h5config_tools.PackageConfig):
     "Configure `pyafm` module operation"
-    pass
+    settings = _h5config_tools.PackageConfig.settings + [
+        _config.BooleanSetting(
+            name='matplotlib',
+            help='Plot pyafm actions using `matplotlib`.',
+            default=False),
+        ]
 
 
 class _TemperatureUnit (object):
 
 
 class _TemperatureUnit (object):