From 04940b1a0b9c32023e28b11a2b29b1ee8baba077 Mon Sep 17 00:00:00 2001 From: Oksana Shliakhtun Date: Wed, 28 Jun 2023 13:03:21 +0200 Subject: [PATCH] write_range, write_tc, string_to_value method Change-Id: I6f81db72e852d2670e0a774a621c8382680bb93a --- frappy_psi/SR.py | 65 +++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/frappy_psi/SR.py b/frappy_psi/SR.py index 229350f..5a54736 100644 --- a/frappy_psi/SR.py +++ b/frappy_psi/SR.py @@ -16,10 +16,12 @@ # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Module authors: -# Daniel Margineda , Oksana Shliakhtun +# Daniel Margineda +# Oksana Shliakhtun # ***************************************************************************** """Signal Recovery SR7270: lockin amplifier for AC susceptibility""" +import re from frappy.core import Readable, Parameter, FloatRange, TupleOf, \ HasIO, StringIO, BoolType, EnumType from frappy.errors import RangeError @@ -57,7 +59,7 @@ class XY(HasIO, Readable): '100s', '200s', '500s', '1ks', '2ks', '5ks', '10ks', '20ks', '50ks', '100ks'] )} - tc = Parameter('time const. value', FloatRange(0.00005, 100000), unit='s', readonly=False) + tc = Parameter('time const. value', FloatRange(0.00001, 100000), unit='s', readonly=False) itc = Parameter('time const. index', EnumType('time const. index range', time_const), readonly=False) nm = Parameter('noise mode on', BoolType(), readonly=False) phase = Parameter('reference phase control', FloatRange(-360, 360), unit='deg', readonly=False) @@ -70,6 +72,14 @@ class XY(HasIO, Readable): ioClass = SR_IO + def string_to_value(self, value): + value_with_unit = re.compile(r'(\d+)([pnumkMG]?)') + value, pfx = value_with_unit.match(value).groups() + pfx_dict = {'p': 1e-12, 'n': 1e-9, 'u': 1e-6, 'm': 1e-3, 'k': 1e3, 'M': 1e6, 'G':1e9} + if pfx in pfx_dict: + value = round(float(value) * pfx_dict[pfx], 12) + return float(value) + def comm(self, cmd): reply, status, overload = self.communicate(cmd).split(';') reply = reply.rstrip('\n') @@ -146,22 +156,18 @@ class XY(HasIO, Readable): reply = self.comm('SEN.') # range value return float(reply) - # def write_range(self, val): - # curr_val = self.read_range - # curr_idx = self.read_irange - # - # close_val = None - # min_diff = None - # - # for idx, value in self.sen_range.items(): - # diff = abs(value - val) - # close_idx = self.comparison(curr_val, value, self.sen_range) - # if close_idx ?: - # close_val = value - # close_idx = close_idx - # min_diff = abs(curr_val - close_val) - # - # return self.comm(f'SEN {close_idx}') + def write_range(self, target): + cl_idx = None + cl_diff = float('inf') + + for name, idx in self.sen_range.items(): + value = self.string_to_value(name) + diff = abs(value - target) + if diff < cl_diff: + cl_idx = idx + cl_diff = diff + self.write_irange(cl_idx) + return self.read_range() def read_nm(self): reply = self.comm('NOISEMODE') @@ -176,15 +182,22 @@ class XY(HasIO, Readable): reply = self.comm('TC.') return float(reply) - def write_tc(self, new_tc): - curr_value = self.read_tc() - new_value = self.time_const[self.itc] - c_ind = self.comparison(curr_value, new_value, self.time_const) + def write_tc(self, target): + cl_idx = None + cl_diff = float('inf') - if abs(curr_value - new_value) < c_ind: - if self.read_nm() == 1 and (5e-4 <= self.time_const[new_tc] <= 1e-2): - raise RangeError('Not allowed with noisemode=1') - return self.comm(f'TC {new_tc}') + for name, idx in self.time_const.items(): + value = self.string_to_value(name) + diff = abs(value - target) # range is the actual value, like SEN. + if diff < cl_diff: + cl_idx = idx + cl_diff = diff + if self.nm: + if cl_idx < 5 or cl_idx > 9: + idx = self.read_tc() + raise RangeError('Not allowed with noisemode=1') + self.comm(f'TC {cl_idx}') + return self.read_tc() def read_itc(self): return int(self.comm('TC'))