scan: Approach the surface with stepper_approach() before looping
[unfold-protein.git] / unfold_protein / scan.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of unfold_protein.
4 #
5 # unfold_protein is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # unfold_protein is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # unfold_protein.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Define `UnfoldScanner` for sequential unfolding experiments."""
19
20 import signal as _signal
21
22 import pypiezo.base as _pypiezo_base
23
24 from . import LOG as _LOG
25 from .unfolder import ExceptionTooFar as _ExceptionTooFar
26 from .unfolder import ExceptionTooClose as _ExceptionTooClose
27
28 class UnfoldScanner (object):
29     def __init__(self, config, unfolder):
30         self.config = config
31         self.unfolder = unfolder
32         self._state = {'x direction': 1}
33
34     def run(self):
35         self._stop = False
36         _signal.signal(_signal.SIGTERM, self._handle_stop_signal)
37         self.unfolder.afm.move_away_from_surface()
38         self.stepper_approach()
39         for i in range(self.config['velocity']['num loops']):
40             _LOG.info('on loop {} of {}'.format(
41                     i, self.config['velocity']['num loops']))
42             for velocity in self.config['velocity']['unfolding velocities']:
43                 if self._stop:
44                     return
45                 self.unfolder.config['unfold']['velocity'] = velocity
46                 try:
47                     self.unfolder.run()
48                 except _ExceptionTooFar:
49                     self.stepper_approach()
50                 except _ExceptionTooClose:
51                     self.afm.move_far_from_surface()
52                     self.stepper_approach()
53                 else:
54                     self.position_scan_step()
55
56     def _handle_stop_signal(self, signal, frame):
57         self._stop = True
58
59     def stepper_approach(self):
60         config = self.unfolder.config['approach']
61         deflection = self.unfolder.read_deflection()
62         setpoint = deflection + config['relative setpoint']
63         def_config = self.unfolder.afm.piezo.config.select_config(
64             'inputs', 'deflection')
65         setpoint_bits = _pypiezo_base.convert_volts_to_bits(
66             def_config, setpoint)
67         self.unfolder.afm.stepper_approach(target_deflection=setpoint_bits)
68
69     def position_scan_step(self):
70         axis_name = 'x'
71         config = self.config['position'] 
72         axis_config = self.unfolder.afm.piezo.config.select_config(
73                 'axes', self.unfolder.afm.config['main-axis'],
74                 get_attribute=_pypiezo_base.get_axis_name
75                 )
76         pos = self.unfolder.afm.piezo.last_output[axis_name]
77         pos_m = _pypiezo_base.convert_bits_to_meters(axis_config, pos)
78         # HACK
79         try:
80             step = float(open('/home/wking/x-step', 'r').read())
81             _LOG.info('read step from file: {}'.format(step))
82         except Exception, e:
83             _LOG.warn('could not read step from file: {}'.format(e))
84             step = config['x step']
85         next_pos_m = pos_m + self._state['x direction']*step
86         if next_pos_m > config['x max']:
87             self._state['x direction'] = -1
88             next_pos_m = pos_m + self._state['x direction']*step
89         elif next_pos_m < config['x min']:
90             self._state['x direction'] = 1
91             next_pos_m = pos_m + self._state['x direction']*step
92         next_pos = _pypiezo_base.convert_meters_to_bits(
93             axis_config, next_pos_m)
94         _LOG.info('move {} from {:g} to {:g} bits'.format(
95                 axis_name, pos, next_pos))
96         self.unfolder.afm.piezo.jump(axis_name, next_pos)