db1172344222ba6debbbd415261fd76bf91bea03
[pyafm.git] / pyafm.py
1 """Tools for controlling atomic force microscopes.
2
3 Provides control of AFM postition using both short-range (piezo) and
4 long range (stepper) vertical positioning.  There are separate modules
5 for controlling the piezo (`pypiezo`) and stepper (`stepper`), this
6 module only contains methods that require the capabilities of both.
7 """
8
9 import logging as _logging
10
11 from pypiezo.base import convert_volts_to_bits as _convert_volts_to_bits
12 from pypiezo.base import convert_meters_to_bits as _convert_meters_to_bits
13 from pypiezo.base import convert_bits_to_meters as _convert_bits_to_meters
14 from pypiezo.surface import SurfaceError as _SurfaceError
15 from pypiezo.surface import FlatFit as _FlatFit
16
17
18 __version__ = '0.1'
19
20
21 LOG = _logging.getLogger('pyafm')
22 "pyafm logger"
23
24 LOG.setLevel(_logging.DEBUG)
25 h = _logging.StreamHandler()
26 h.setLevel(_logging.WARN)
27 f = _logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
28 h.setFormatter(f)
29 LOG.addHandler(h)
30 del h, f
31
32
33 class AFM (object):
34     """Atomic force microscope positioning.
35
36     Uses a short range `piezo` and a long range `stepper` to position
37     an AFM tip relative to the surface.
38
39     Parameters
40     ----------
41     piezo | pypiezo.afm.AFMpiezo instance
42         Fine positioning and deflection measurements.
43     stepper | stepper.Stepper instance
44         Coarse positioning.
45     """
46     def __init__(self, piezo, stepper, axis_name='z'):
47         self.piezo = piezo
48         self.stepper = stepper
49         self.axis_name = axis_name
50
51     def move_just_onto_surface(self, depth=-50e-9, setpoint=2, far=200):
52         """Position the AFM tip close to the surface.
53
54         Uses `.piezo.get_surface_position()` to pinpoint the position
55         of the surface.  Adjusts the stepper position as required via
56         `.stepper.step_relative()` to get within
57         `2*.stepper.step_size` meters of the surface.  Then adjusts
58         the piezo to place the cantilever `depth` meters onto the
59         surface.  Negative `depth`\s place the tip off the surface
60
61         If `.piezo.get_surface_position()` fails to find the surface,
62         backs off `far` half steps (for safety) and steps in (without
63         moving the zpiezo) until deflection voltage is greater than
64         `setpoint`.
65         """
66         LOG.info('moving to %g onto the surface' % depth)
67
68         stepper_tolerance = 2*self.stepper.step_size
69
70         axis = self.piezo.axis_by_name(self.axis_name)
71
72         zero = _convert_volts_to_bits(axis.axis_channel_config, 0)
73         target_def = _convert_volts_to_bits(axis.axis_channel_config, setpoint)
74
75         LOG.debug('zero the %s piezo output' % self.axis_name)
76         self.piezo.jump(axis_name=self.axis_name, position=zero)
77
78         LOG.debug("see if we're starting near the surface")
79         try:
80             pos = self.piezo.get_surface_position(
81                 axis_name=self.axis_name, max_deflection=target_def)
82         except _FlatFit, e:
83             LOG.info(e)
84             pos = self._stepper_approach_again(
85                 target_deflection=target_def, far=far)
86         except _SurfaceError, e:
87             LOG.info(e)
88             pos = self._stepper_approach_again(
89                 target_deflection=target_def, far=far)
90
91         pos_m = _convert_bits_to_meters(
92             axis.axis_channel_config, axis.axis_config, pos)
93         LOG.debug('located surface at stepper %d, piezo %d (%g m)'
94                   % (self.stepper.position, pos, pos_m))
95
96         LOG.debug('fine tune the stepper position')
97         while pos_m < -stepper_tolerance:  # step back if we need to
98             LOG.debug('step back')
99             self.stepper.step_relative(-1, backlash_safe=True)
100             pos = self.piezo.get_surface_position(
101                 axis_name=self.axis_name, max_deflection=target_def)
102             pos_m = _convert_bits_to_meters(
103                 axis.axis_channel_config, axis.axis_config, pos)
104             LOG.debug('located surface at stepper %d, piezo %d (%g m)'
105                       % (self.stepper.position, pos, pos_m))
106         while pos_m > stepper_tolerance:  # step forward if we need to
107             LOG.debug('step forward')
108             self.stepper.step_relative(1)
109             pos = self.piezo.get_surface_position(
110                 axis_name=self.axis_name, max_deflection=target_def)
111             pos_m = _convert_bits_to_meters(
112                 axis.axis_channel_config, axis.axis_config, pos)
113             LOG.debug('located surface at stepper %d, piezo %d (%g m)'
114                       % (self.stepper.position, pos, pos_m))
115
116         LOG.debug('adjust the %s piezo to place us just onto the surface'
117                   % self.axis_name)
118         target_m = pos_m + depth
119         target = _convert_meters_to_bits(
120             axis.axis_channel_config, axis.axis_config, target_m)
121         self.piezo.jump(self.axis_name, target)
122
123         LOG.debug(
124             'positioned %g m into the surface at stepper %d, piezo %d (%g m)'
125             % (depth, self.stepper.position, target, target_m))
126
127
128     def _stepper_approach_again(target_deflection, far):
129         LOG.info('back off %d half steps and approach until deflection > %g'
130                  % (far, target_deflection))
131
132         # back away
133         self.stepper.step_relative(-far, backlash_safe=True)
134
135         cd = self.pyiezo.read_deflection()  # cd = current deflection in bits
136         LOG.debug('single stepping approach')
137         while cd < target_deflection:
138             LOG.debug('deflection %g < setpoint %g.  step closer'
139                       % (cd, target_deflection))
140             self.stepper.single_step(1)  # step in
141             cd = self.pyiezo.read_deflection()
142
143         for i in range(2*max(1, self.stepper.backlash)):
144             LOG.debug('additional surface location attempt')
145             try:
146                 pos = self.piezo.get_surface_position(
147                     axis_name=self.axis_name, max_deflection=target_def)
148                 return pos
149             except _SurfaceError, e:
150                 LOG.info(e)
151             self.stepper.single_step(-1)  # step out
152         LOG.debug('giving up on finding the surface')
153         LOG.warn(e)
154         raise e