Update unfold.py config to show temp in Kelvin.
[unfold-protein.git] / unfold_protein / afm.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 from pyafm.afm import AFM as _AFM
20 from pycomedi.channel import AnalogChannel as _AnalogChannel
21 from pycomedi.channel import DigitalChannel as _DigitalChannel
22 from pycomedi.device import Device as _Device
23 from pycomedi.subdevice import StreamingSubdevice as _StreamingSubdevice
24 from pypiezo.afm import AFMPiezo as _AFMPiezo
25 from stepper import Stepper as _Stepper
26 import pycomedi.constant as _pycomedi_constant
27 import pypiezo.base as _pypiezo_base
28 import pypiezo.config as _pypiezo_config
29
30 try:
31     from .temperature import Temperature as _Temperature
32 except ImportError, _temperature_import_error:
33     _Temperature = None
34
35
36 def get_afm(with_temperature=True, logger=None):
37     d = _Device('/dev/comedi0')
38     d.open()
39     s_in = d.find_subdevice_by_type(
40         _pycomedi_constant.SUBDEVICE_TYPE.ai, factory=_StreamingSubdevice)
41     s_out = d.find_subdevice_by_type(
42         _pycomedi_constant.SUBDEVICE_TYPE.ao, factory=_StreamingSubdevice)
43     z_axis_channel = s_out.channel(
44         0, factory=_AnalogChannel,
45         aref=_pycomedi_constant.AREF.ground)
46     x_axis_channel = s_out.channel(
47         1, factory=_AnalogChannel,
48         aref=_pycomedi_constant.AREF.ground)
49     input_channel = s_in.channel(
50         0, factory=_AnalogChannel,
51         aref=_pycomedi_constant.AREF.diff)
52     for chan in [z_axis_channel, x_axis_channel, input_channel]:
53         chan.range = chan.find_range(
54             unit=_pycomedi_constant.UNIT.volt, min=-10, max=10)
55     z_axis_config = _pypiezo_config.AxisConfig()
56     z_axis_config.update({'gain':20, 'sensitivity':6.5e-9})
57     z_axis_channel_config = _pypiezo_config.ChannelConfig()
58     z_axis_config['channel'] = z_axis_channel_config
59     z_axis_channel_config['name'] = 'z'
60     x_axis_config = _pypiezo_config.AxisConfig()
61     x_axis_config.update({'gain':20, 'sensitivity':6.5e-9})  # TODO: GET FROM CALIBRATION
62     x_axis_channel_config = _pypiezo_config.ChannelConfig()
63     x_axis_config['channel'] = x_axis_channel_config
64     x_axis_channel_config['name'] = 'x'
65     input_channel_config = _pypiezo_config.ChannelConfig()
66     input_channel_config['name'] = 'deflection'
67     z = _pypiezo_base.PiezoAxis(
68         config=z_axis_config, axis_channel=z_axis_channel)
69     z.setup_config()
70     x = _pypiezo_base.PiezoAxis(
71         config=x_axis_config, axis_channel=x_axis_channel)
72     x.setup_config()
73     inp = _pypiezo_base.InputChannel(
74         config=input_channel_config, channel=input_channel)
75     inp.setup_config()
76     piezo = _AFMPiezo(axes=[x, z], inputs=[inp])
77
78     s_d = d.find_subdevice_by_type(
79         _pycomedi_constant.SUBDEVICE_TYPE.dio)
80     d_channels = [s_d.channel(i, factory=_DigitalChannel)
81                   for i in (0, 1, 2, 3)]
82     for chan in d_channels:
83         chan.dio_config(
84             _pycomedi_constant.IO_DIRECTION.output)
85     def write(value):
86         s_d.dio_bitfield(bits=value, write_mask=2**4-1)
87     stepper = _Stepper(write=write, delay=5e-2)
88
89     if with_temperature:
90         if _Temperature is None:
91             raise _temperature_import_error
92         temperature = _Temperature()
93     else:
94         temperature = None
95
96     afm = _AFM(piezo, stepper, temperature=temperature)
97     return (afm, d)