185 lines
6.8 KiB
Python
185 lines
6.8 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# *****************************************************************************
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
# the terms of the GNU General Public License as published by the Free Software
|
|
# Foundation; either version 2 of the License, or (at your option) any later
|
|
# version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
# details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License along with
|
|
# this program; if not, write to the Free Software Foundation, Inc.,
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
#
|
|
# Module authors:
|
|
# Daniel Margineda <daniel.margineda@psi.ch>, Oksana Shliakhtun <oksana.shliakhtun@psi.ch>
|
|
# *****************************************************************************
|
|
"""Signal Recovery SR7270: lockin amplifier for AC susceptibility"""
|
|
|
|
from frappy.core import Readable, Parameter, FloatRange, TupleOf, \
|
|
HasIO, StringIO, IntRange, BoolType, Writable, EnumType
|
|
from frappy.errors import RangeError
|
|
|
|
|
|
class SR_IO(StringIO):
|
|
end_of_line = b'\x00'
|
|
identification = [('ID', r'.*')] # Identification; causes the lock-in amplifier to respond with the number 7270
|
|
|
|
def communicate(self, cmd): # remove dash from terminator
|
|
reply = super().communicate(cmd)
|
|
status = self._conn.readbytes(2, timeout=0.1) # get the 2 status bytes
|
|
return reply + ';%d;%d' % tuple(status)
|
|
|
|
|
|
class XY(HasIO, Readable):
|
|
value = Parameter('X, Y', datatype=TupleOf(FloatRange(unit='V'), FloatRange(unit='V')))
|
|
vmode = Parameter('control mode', EnumType(both_grounded=0, A=1, B=2, A_B_diff=3), readonly=False)
|
|
range = Parameter('sensitivity value', FloatRange(0.00, 1), unit='V', default=1)
|
|
autosen_on = Parameter('is auto sensitivity on', BoolType(), readonly=False)
|
|
noise_control = Parameter('is noise control mode on', BoolType(), readonly=False)
|
|
phase = Parameter('reference phase control', FloatRange(-360, 360), unit='deg', readonly=False)
|
|
frequency = Parameter('oscill. frequen. control', FloatRange(0.001, 250e3), unit='Hz', readonly=False,
|
|
group='frequency')
|
|
amplitude = Parameter('oscill. amplit. control', FloatRange(0.00, 5), unit='V_rms', readonly=False)
|
|
#filter = Parameter('line frequency filter', unit='Hz')
|
|
|
|
sen_range = {name: value + 1 for value, name in enumerate(
|
|
['2nV', '5nV', '10nV', '20nV', '50nV', '100nV', '200nV', '500nV', '1uV',
|
|
'2uV', '5uV', '10uV', '20uV', '50uV', '100uV', '200uV', '500uV', '1mV',
|
|
'2mV', '5mV', '10mV', '20mV', '50mV', '100mV', '200mV', '500mV', '1V']
|
|
)}
|
|
|
|
irange = Parameter('sensitivity index', EnumType('sensitivity index range', sen_range), readonly=False)
|
|
|
|
time_const = {name: value for value, name in enumerate(
|
|
['10us', '20us', '50us', '100us', '200us', '500us', '1ms', '2ms', '5ms', '10ms',
|
|
'20ms', '50ms', '100ms', '200ms', '500ms', '1s', '2s', '5s', '10s', '20s', '50s',
|
|
'100s', '200s', '500s', '1ks', '10ks', '20ks', '50ks', '100ks']
|
|
)}
|
|
|
|
tc = Parameter('time const. value', FloatRange(0.00005, 100000), unit='s', readonly=False)
|
|
itc = Parameter('time const. index', EnumType('time const. index range', time_const), readonly=False)
|
|
ioClass = SR_IO
|
|
|
|
def comparison(self, curr_value, new_value, value_dict):
|
|
c_ind = None # closest index
|
|
c_diff = None # closets difference
|
|
|
|
for index, value in value_dict.items():
|
|
if c_diff is None or diff < c_diff:
|
|
c_ind = index
|
|
c_diff = c_diff
|
|
|
|
if abs(curr_value - new_value) < c_diff:
|
|
return c_ind
|
|
else:
|
|
for index, value in value_dict.items():
|
|
diff = abs(new_value - value)
|
|
if c_diff is None or diff < c_diff:
|
|
c_ind = index
|
|
c_diff = diff
|
|
return c_ind
|
|
|
|
def comm(self, cmd):
|
|
reply, status, overload = self.communicate(cmd).split(';')
|
|
if overload != '0':
|
|
self.status = (self.Status.WARN, f'overload {overload}')
|
|
self.status = (self.Status.IDLE, '')
|
|
return reply
|
|
|
|
def read_vmode(self):
|
|
return self.comm('VMODE')
|
|
|
|
def write_vmode(self, vmode):
|
|
self.comm(f'IMODE {0}')
|
|
return self.comm(f'VMODE {vmode}')
|
|
|
|
def read_autosen_on(self):
|
|
return self.comm('AUTOMATIC')
|
|
|
|
def write_autosen_on(self, autosen_on):
|
|
return self.comm(f'AUTOMATIC {autosen_on}')
|
|
|
|
def read_irange(self):
|
|
return self.comm('SEN')
|
|
|
|
def write_irange(self, irange):
|
|
self.comm(f'IMODE {0}')
|
|
self.comm(f'SEN {irange}')
|
|
self.read_range()
|
|
return irange
|
|
|
|
def read_range(self):
|
|
return self.comm('SEN.') # range value
|
|
|
|
def write_range(self):
|
|
self.comm(f'IMODE 0')
|
|
curr_value = self.read_range()
|
|
new_value = self.value
|
|
c_ind = self.comparison(curr_value, new_value, self.sen_range)
|
|
return self.comm(f'SEN {c_ind}')
|
|
|
|
def read_noise_control(self):
|
|
return self.comm('NOISEMODE')
|
|
|
|
def write_noise_control(self, noise_control):
|
|
return self.comm(f'NOISEMODE {noise_control}')
|
|
|
|
def read_tc(self):
|
|
return self.comm('TC.')
|
|
|
|
def write_tc(self):
|
|
pass
|
|
|
|
def read_itc(self):
|
|
return self.comm('TC')
|
|
|
|
def write_itc(self, new_itc):
|
|
curr_value = self.read_itc()
|
|
new_value = self.time_const[self.itc]
|
|
c_ind = self.comparison(curr_value, new_value, self.time_const)
|
|
|
|
if abs(curr_value - new_value) < c_diff:
|
|
if self.read_noise_control() == 1 and (5e-4 <= self.time_const[new_itc] <= 1e-2):
|
|
raise RangeError('not allowed with noisemode=1')
|
|
return self.comm(f'TC {new_itc}')
|
|
|
|
def read_value(self):
|
|
reply = self.comm('XY.').split(b',')
|
|
x = float(reply[0])
|
|
y = float(reply[1])
|
|
return x, y
|
|
|
|
def write_value(self, value):
|
|
return self.comm(f'XY {value}')
|
|
|
|
def read_frequency(self):
|
|
return self.comm('OF.')
|
|
|
|
def write_frequency(self, frequency):
|
|
frequency = self.frequency
|
|
return self.comm(f'OF. {frequency}')
|
|
|
|
def read_amplitude(self):
|
|
return self.comm('OA.')
|
|
|
|
def write_amplitude(self, amplitude):
|
|
return self.comm(f'OA. {amplitude}')
|
|
|
|
# phase and autophase
|
|
def read_phase(self):
|
|
return self.comm('REFP.')
|
|
|
|
def write_phase(self, value):
|
|
self.comm(f'REFP {round(1000 * value)}')
|
|
return self.read_phase()
|
|
|
|
def aphase(self):
|
|
"""auto phase"""
|
|
self.read_phase()
|
|
return self.comm('AQN')
|