From: W. Trevor King Date: Thu, 12 Jul 2012 22:01:20 +0000 (-0400) Subject: Add matplotlib config option and stepper_approach plot. X-Git-Tag: v0.4~1 X-Git-Url: http://git.tremily.us/?p=pyafm.git;a=commitdiff_plain;h=ec18beb90683ee76713c74bf5749234596a8747e Add matplotlib config option and stepper_approach plot. --- diff --git a/pyafm/afm.py b/pyafm/afm.py index bda92d5..fc8dacb 100644 --- a/pyafm/afm.py +++ b/pyafm/afm.py @@ -22,6 +22,14 @@ for controlling the piezo (`pypiezo`) and stepper (`stepper`), this 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 @@ -30,6 +38,7 @@ from pypiezo.surface import FlatFit as _FlatFit 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 @@ -454,6 +463,10 @@ class AFM (object): 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') @@ -462,6 +475,20 @@ class AFM (object): 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. diff --git a/pyafm/config.py b/pyafm/config.py index 6b3fa0e..5c72396 100644 --- a/pyafm/config.py +++ b/pyafm/config.py @@ -24,7 +24,12 @@ import pypiezo.config as _pypiezo_config 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):