#!/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: # Oksana Shliakhtun # ***************************************************************************** from math import log2 from frappy.core import Readable, Parameter, IntRange, EnumType, FloatRange, \ StringIO, HasIO, StringType, Property, Writable, Drivable, IDLE, ERROR, \ Attached, StructOf from frappy_psi.mixins import HasOutputModule, HasControlledBy class Ls340IO(StringIO): """communication with 340CT""" end_of_line = '\r' wait_before = 0.05 identification = [('*IDN?', r'LSCI,MODEL340,.*')] class LakeShore(HasIO): def set_par(self, cmd, *args): head = cmd + ','.join([str(a) for a in args]) tail = cmd.replace(' ', '? ') reply = self.communicate(f'{head};{tail}') return [float(num) for num in reply.split(',')] def get_par(self, cmd): reply = self.communicate(cmd) return [float(num) for num in reply.split(',')] class Sensor340(HasIO, Readable): """A channel of 340TC""" # define the communication class to create the IO module ioClass = Ls340IO channel = Property('lakeshore channel', StringType()) # define or alter the parameters # as Readable.value exists already, we give only the modified property 'unit' value = Parameter(unit='K') def read_value(self): reply = self.communicate(f'KRDG? {self.channel}') return float(reply) def read_status(self): c = int(self.communicate(f'RDGST? {self.channel}')) if c >= 128: return ERROR, 'units overrange' if c >= 64: return ERROR, 'units zero' if c >= 32: return ERROR, 'temperature overrange' if c >= 16: return ERROR, 'temperature underrange' if c >= 2: return ERROR, 'old reading' if c >= 1: return ERROR, 'invalid reading' return IDLE, '' class HeaterOutput(LakeShore, HasControlledBy, HasIO, Writable): loop = Property('lakeshore loop', IntRange(1, 2), default=1) channel = Property('attached channel', StringType()) max_power = Parameter('max heater power', datatype=FloatRange(0, 100), unit='W', readonly=False) value = Parameter('heater output', datatype=FloatRange(0, 100), unit='W') target = Parameter('manual heater output', datatype=FloatRange(0, 100), unit='W') resistance = Property('heater resistance', datatype=FloatRange(10, 1000)) current = Property('heater current', datatype=FloatRange(0, 2)) _range = 0 MAXCURRENTS = {1: 0.25, 2: 0.5, 3: 1.0, 4: 2.0} RANGES = {1: 1e4, 2: 1e3, 3: 1e2, 4: 1e1, 5: 1} SETPOINTLIMS = 999 max_current = 0 STATUS_MAP = { 0: (IDLE, ''), 1: (ERROR, 'Power supply over voltage'), 2: (ERROR, 'Power supply under voltage'), 3: (ERROR, 'Output digital-to-analog Converter error'), 4: (ERROR, 'Current limit digital-to-analog converter error'), 5: (ERROR, 'Open heater load'), 6: (ERROR, 'Heater load less than 10 ohms') } def earlyInit(self): super().earlyInit() self.CHOICES = sorted([(maxcurrent ** 2 * factor, icurrent, irange) for irange, factor in self.RANGES.items() for icurrent, maxcurrent in self.MAXCURRENTS.items()]) def write_max_power(self, max_power): prev = 0 for i, (factor, icurrent, irange) in enumerate(self.CHOICES): power = min(factor * self.resistance, 2500 / self.resistance) if power >= max_power: if prev >= max_power * 0.9 or prev == power: icurrent, irange = self.CHOICES[i - 1][1:3] break prev = power self._range = irange self.communicate(f'CLIMIT {self.loop},{self.SETPOINTLIMS},0,0,{icurrent},{irange};' f'RANGE {irange};' f'CDISP {self.loop},1,{self.resistance},0;RANGE?') return self.read_max_power() def read_max_power(self): setplimit, _, _, icurrent, irange = [ float(s) for s in self.communicate(f'CLIMIT? {self.loop}').split(',')] # max_power from codes disregarding voltage limit: self._max_power = self.MAXCURRENTS[icurrent] ** 2 * self.RANGES[irange] * self.resistance # voltage limit = 50V: max_power = min(self._max_power, 2500 / self.resistance) return max_power def set_range(self): self.set_par('RANGE ', {self._range}) def percent_to_power(self, percent): return min((percent / 100) ** 2 * self._max_power, 2500 / self.resistance) def power_to_percent(self, power): return (power / self._max_power) ** (1 / 2) * 100 # limit def read_value(self): return self.percent_to_power(float(self.communicate(f'HTR?'))) def read_status(self): return self.STATUS_MAP[int(self.communicate(f'HTRST?'))] def write_target(self, target): self.self_controlled() self.write_max_power(self.max_power) self.set_heater_mode(3) self.set_range() percent = self.power_to_percent(target) reply = self.set_par(f'MOUT {self.loop}, {percent:g}') return self.percent_to_power(float(reply)) def set_heater_mode(self, mode): self.communicate(f'CSET {self.loop},{self.channel},1,1,0') self.set_par(f'CMODE {self.loop}, {int(mode)}') class TemperatureLoop340(HasOutputModule, Sensor340, Drivable, LakeShore): target = Parameter(unit='K') ctrlpars = Parameter('PID parameters', StructOf(p=FloatRange(0, 1000), i=FloatRange(0, 1000), d=FloatRange(0, 1000)), readonly=False) loop = Property('lakeshore loop', IntRange(1, 2), default=1) def write_target(self, target): out = self.output_module if out.controlled_by != self.name: out.write_target(0) out.set_heater_mode(1) out.write_max_power(out.max_power) self.activate_output() out.set_range() reply = self.set_par(f'SETP {self.loop},{target}') return float(reply) def read_target(self): return float(self.communicate(f'SETP? {self.loop}')) def write_ctrlpars(self, ctrlpars): p, i, d = self.set_par(f'PID {self.loop}', ctrlpars['p'], ctrlpars['i'], ctrlpars['d']) return {'p': p, 'i': i, 'd': d} def read_ctrlpars(self): p, i, d = self.get_par(f'PID? {self.loop}') return {'p': p, 'i': i, 'd': d}