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