From a66cef020696c5d96b6a09ef7fbd70d91bfeaed6 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Tue, 17 Nov 2026 15:02:24 +0100 Subject: [PATCH] zustand vom 17.nov --- frappy_psi/ccu4.py | 12 +++- frappy_psi/cryostat.py | 143 +++++++++++++++++++------------------ frappy_psi/lakeshore.py | 62 +++++++++++----- frappy_psi/lakeshore340.py | 33 +++++---- 4 files changed, 150 insertions(+), 100 deletions(-) diff --git a/frappy_psi/ccu4.py b/frappy_psi/ccu4.py index 502e7346..85ffe55e 100644 --- a/frappy_psi/ccu4.py +++ b/frappy_psi/ccu4.py @@ -371,6 +371,11 @@ class HasFilter: class Pressure(HasFilter, Base, Readable): + + def writeInitParams(self): + super().writeInitParams() + self.command(fav=1) + value = Parameter(unit='mbar') mbar_offset = Parameter('offset in mbar', FloatRange(unit='mbar'), default=0.8, readonly=False) filter_time = Parameter('filter time', FloatRange(unit='sec'), readonly=False, default=3) @@ -385,6 +390,11 @@ def Table(miny=None, maxy=None): class NeedleValveFlow(HasStates, Base, Drivable): # + + def writeInitParams(self): + super().writeInitParams() + self.command(mav=1) + flow_sensor = Attached(Readable, mandatory=False) pressure = Attached(Pressure, mandatory=False) use_pressure = Parameter('flag (use pressure instead of flow meter)', BoolType(), @@ -583,7 +593,6 @@ class NeedleValveFlow(HasStates, Base, Drivable): # self.command(mp=step) print('step for motor is set to the value of', step, minstep) self._speed_sum += step - print('2demfall isch do jett aber ebbis id hose') return Retry # still approaching difmax = max(difdir) @@ -592,7 +601,6 @@ class NeedleValveFlow(HasStates, Base, Drivable): # sm.ref_dif = difmax # self.log.info('new ref %g', sm.ref_dif) sm.last_progress = sm.now - print('1wieso zom gugus isch jetzt do au ebbis id hose?') return Retry # progressing: no pulse needed @status_code(IDLE) diff --git a/frappy_psi/cryostat.py b/frappy_psi/cryostat.py index 51d328b3..731394ec 100644 --- a/frappy_psi/cryostat.py +++ b/frappy_psi/cryostat.py @@ -45,7 +45,7 @@ class Cryostat(HasConvergence, Drivable): ArrayOf(FloatRange(0, unit='K')), readonly=False, value=(0, 2, 4, 8)) flowpars = Parameter( 'flows for lowering T', - ArrayOf(FloatRange(0, unit='ln/min')), readonly=False, default=(0, 1, 5, 20)) + ArrayOf(FloatRange(0, unit='ln/min')), readonly=False, default=(0, 5, 10, 20)) sample_weight = Parameter( 'weight of sample for averaging', FloatRange(0,1), readonly=False, default=0.5) @@ -54,7 +54,7 @@ class Cryostat(HasConvergence, Drivable): BoolType(), readonly=False, default=True) dblctrl = Parameter( 'True: correct vti target according to VTI - sample difference', - BoolType(), readonly=False, default=False) + BoolType(), readonly=False, default=True) #andern!!!!!!!!!!!!!!!! auto_flow = Parameter( 'set to True for automatic flow', BoolType(), readonly=False, default=False) @@ -71,9 +71,9 @@ class Cryostat(HasConvergence, Drivable): # FloatRange(unit='K'), default=0) avtime = Parameter('average time from simsam', FloatRange(unit='s'), readonly=False, default=30) - _Ta_now = Parameter('first order lowpass', FloatRange(unit='K'), readonly=False, default=0) - _Tb_now = Parameter('second order lowpass', FloatRange(unit='K'), readonly=False, default=0) - _Tc_now = Parameter('thirth order lowpass', FloatRange(unit='K'), readonly=False, default=0) + simvalue_1 = Parameter('Simulation 1 value', FloatRange(unit='K'), readonly=False, default=0) + simvalue_2 = Parameter('Simulation 2 value', FloatRange(unit='K'), readonly=False, default=0) + tc = Parameter('Timeconstant simulation 1', FloatRange(), readonly=False, default=50) _hist_range = None _dif_range = 0 @@ -102,10 +102,13 @@ class Cryostat(HasConvergence, Drivable): index_flow = None undershot = False #Ta_now = None - Tm_now = None - tc = 10 + _Tm_now = None _checkdiff = [] offset_dblctr = 0 + _simulation_1 = [] + _simulation_2 = [] + last_sim = 0 + manual = True def initModule(self): super().initModule() @@ -159,6 +162,21 @@ class Cryostat(HasConvergence, Drivable): return offsets[3], offsets[-4] return None + def _simulation(self, w, parameters): + last_sim = None + for ordr in range(len(parameters) - 1): + last_sim_new = parameters[ordr + 1] + if last_sim is None: + last_sim = self.last_sim + parameters[0] = self.vti.read_value() + parameters[ordr + 1] = parameters[ordr + 1] + w * (parameters[ordr] - parameters[ordr + 1]) #war - last_sim + last_sim = last_sim_new + print(parameters) + self.last_sim = parameters[0] + #tm, ta, tb, tc + + + def doPoll(self): super().doPoll() vti = self.vti #temp regeler @@ -203,18 +221,17 @@ class Cryostat(HasConvergence, Drivable): if self.overshoot and not self.undershot and mean_t > self.target: #idee ist, dass die TDifferenz erhoht wird um dadurch langer auf hohen flowwerten zu bleiben. #ausserdem soll ein geiwsser mindestwet immer erhalten bleiben, solange das target nicht erreicht resp unterboten wird - dif_t = max(mean_t - self.target + 5, 2 * (mean_t - self.target)) + dif_t = max(vti.value - self.target + (self.deltatemp[-2] + 0.3), 2 * (vti.value - self.target)) + print('overshootmeldung', dif_t, mean_t) else: self.undershot = True #otherwise hysteresis might not ork anymore dif_t = mean_t - self.target #ndif = len(self.deltatemp) flow_range = self._flow_range index_flow = self.index_flow - print(index_flow, self.index_flow, dif_t, self.flowpars, mean_t) if index_flow is None and dif_t != 0: - print('inside') try: for element in reversed(self.deltatemp): if element < dif_t: @@ -224,18 +241,24 @@ class Cryostat(HasConvergence, Drivable): print('Fehler indexing:', repr(e)) try: + if self.overshoot: + dif_flow = dif_t + else: + dif_flow = vti.value - self.target for _ in range(len(self.flowpars)): #falls die temperatur extrem schnell sinkt, ansonsten hinkt der wert nach upper = clamp(0, index_flow + 1, len(self.flowpars) - 1) down = clamp(0, index_flow - 1, len(self.flowpars) - 1) - if dif_t < self.deltatemp[down]: + if dif_flow < self.deltatemp[down]: if index_flow > 0: index_flow -= 1 - elif dif_t > self.deltatemp[upper]: + elif dif_flow > self.deltatemp[upper]: if index_flow < len(self.flowpars) - 1: index_flow += 1 else: break + #self.log.info('dif_flow %r index_flow %r', dif_flow, index_flow) except Exception as e: + self.log.error('autoflow %r', e) print('Fehler:', repr(e)) target = self.target @@ -245,60 +268,36 @@ class Cryostat(HasConvergence, Drivable): # target = self.target - sample_vti * (1 - self.sample_weight) if self.dblctrl: - - if self._Tm_now is None: - simulation_1 = [self.vti.read_value(), self.vti.read_value(), self.vti.read_value(), self.vti.read_value()] - simulation_1 = [self.vti.read_value(), self.vti.read_value(), self.vti.read_value(), self.vti.read_value()] - #tm, ta, tb, tc - + self._Tm_now = self.vti.read_value() - - def simulation(order, w, parameters): - last_sim = None - for ordr in range(order): - last_sim_new = parameters[ordr + 1] - if last_sim is None: - last_sim = parameters[0] - parameters[0] = self.vti.read_value() - parameters[ordr + 1] = parameters[ordr + 1] + w * (parameters[ordr] - last_sim) - last_sim = last_sim_new - return parameters - - simulation_1 = simulation(1, 5/self.tc, simulation_1) - simulation_2 = simulation(1, 5/self.tc, simulation_2) - - range = sorted([self.sample.read_value() - simulation_1[1], self.sample.read_value() - simulation_2[1]]) #temp differenz - - if len(self._checkdiff) < math.ceil(self.tc/5): #built list mit abweichungen - self._checkdiff.append(self._Tm_now - self.vti.target) +#timeconstants for simulations + def timeconstant(sim, const, tc): + return float(const * 2**(sim[-1]/tc) * 100) +#simulation + self._simulation(5/timeconstant(self._simulation_1, 1.15, self.tc), self._simulation_1) #original: 1 + self._simulation(5/timeconstant(self._simulation_2, 0.45, self.tc), self._simulation_2) #original:0.5 + self.simvalue_1 = self._simulation_1[-1] + self.simvalue_2 = self._simulation_2[-1] +#build tolerancearray + if len(self._checkdiff) < self.tc/5: #built list mit abweichungen + self._checkdiff.append(abs(self._Tm_now - self.vti.target)) else: - del self._checkdiff[0] - self._checkdiff.append(self._Tm_now - self.vti.target). + del self._checkdiff[0] + self._checkdiff.append(abs(self._Tm_now - self.vti.target)) - if np.median(self._checkdiff) < self._toll: - #schauen, ob intervalle sich uberschneiden mit beruck. von offset, das nur tun wenn abweichung klein ist - if max(simulation_1[0], simulation_2[0]) < min([self._Tm_now + self.offset_dblctr - self._tol, self._Tm_now + self.offset_dblctr + self._tol]): - self.offset_dblctr = max(simulation_1[1], simulation_2[1]) + self._tol - self._Tm_now #index anpassen je nach ordnung - target += self.offset_dblctr - self.__vti_write_target(target) - elif min(simulation_1[0], simulation_2[0]) > max([self._Tm_now + self.offset_dblctr - self._tol, self._Tm_now + self.offset_dblctr + self._tol]): - self.offset_dblctr = min(simulation_1[1], simulation_2[1]) - self._tol - self._Tm_now #index anpassen je nach ordnung - target += self.offset_dblctr - self.__vti_write_target(target) - - - - -# if self.offset > range[1]: -# self.offset = range[1] -# target += self.offset -# self.__vti_write_target(target) -# elif self.offset < range[0]: -# self.offset = range[0] -# target += self.offset -# self.__vti_write_target(target) + _std_abweich = math.sqrt(sum(x ** 2 for x in self._checkdiff) / len(self._checkdiff)) + sample_value = self.sample.read_value() + _offsetrange = [sample_value - self.offset_dblctr - _std_abweich, sample_value + self.offset_dblctr + _std_abweich] +#change vti target + if _offsetrange[0] > max(self.simvalue_1, self.simvalue_2): #sample is over vti temp + self.offset_dblctr = sample_value - _std_abweich - max(self.simvalue_1, self.simvalue_2) #offset is absolutevalue of Tsim - Ts + target = self.tartet - self.offset_dblctr + self.__vti_write_target(target) + elif _offsetrange[-1] < min(self.simvalue_1, self.simvalue_2): #sample is under vti temp + self.offset_dblctr = min(self.simvalue_1, self.simvalue_2) - sample_value - _std_abweich + target = self.target + self.offset_dblctr + self.__vti_write_target(target) - # if self.dblctrl: @@ -352,11 +351,11 @@ class Cryostat(HasConvergence, Drivable): self._count_since_change = 0 self._half_dif = abs(target - self.target) self._avsize = None # trigger new calculation - if self.dblctrl: - if target < self.target: - # reduce offset proportionally - self.offset *= target / self.target - target += self.offset +# if self.dblctrl: +# if target < self.target: +# # reduce offset proportionally +# self.offset *= target / self.target +# target += self.offset vti = self.vti # tol = max(self.target * 0.01, abs(vti.value - target) * 0.2) # if hasattr(vti, 'reduce_pid_i'): @@ -367,6 +366,14 @@ class Cryostat(HasConvergence, Drivable): self.undershot = False #is needed otherwise after restart weirdelemts in list might distract autoregulation of nv self._Tm_now = None + self.last_sim = self.vti.read_value() + #self._simulation_1 = [self.vti.read_value(), self.vti.read_value(), self.vti.read_value()] + #self._simulation_2 = [self.vti.read_value(), self.vti.read_value(), self.vti.read_value()] + if self.manual: + self._simulation_1 = [self.sample.read_value(), self.sample.read_value(), self.sample.read_value()] + self._simulation_2 = [self.sample.read_value(), self.sample.read_value(), self.sample.read_value()] + self.offset_dblctr = 0 + self.manual = True def write_avtime(self, value): @@ -381,4 +388,4 @@ class AvtimeTable(Table): class PidTable(Table): logarithmic = True pid_p = Curve() - pid_i = Curve() \ No newline at end of file + pid_i = Curve() diff --git a/frappy_psi/lakeshore.py b/frappy_psi/lakeshore.py index 7d7de0d9..7558ce01 100644 --- a/frappy_psi/lakeshore.py +++ b/frappy_psi/lakeshore.py @@ -70,11 +70,12 @@ class IO(StringIO): class HasLscIO(HasIO): ioClass = IO - def query(self, msg, *converters): + def query(self, msg, *converters, force_list=False): """send a query and parse result :param msg: the query message, contains a '?' :param converters: converters for the result values + :param force_list: return list even when only one converter is given :return: the converted value(s) when more than one converter is given, this is a list """ @@ -87,7 +88,7 @@ class HasLscIO(HasIO): f'expected {len(converters)} values,' f' got {values} as reply to {msg}') result = [c(v.strip()) for (c, v) in zip(converters, values)] - return result[0] if len(converters) == 1 else result + return result if force_list or len(converters) > 1 else result[0] def command(self, msghead, *args): """send a command to change parameters and query the replies @@ -264,13 +265,13 @@ class Device(HasLscIO, Module): raise ValueError('no empty curves available') return curve_no - def get_calib_state(self, sensor): + def get_calib_state(self, sensor, name): if not self.io.is_connected: return CommunicationFailedError('no connection') req = self._requests.get(sensor.calcurve) if req: if req.loading: - return ImpossibleError(f'can not read T while {req.status[1]}') + return ImpossibleError(f'can not read {name} while {req.status[1]}') if req.status[0] >= ERROR: return ImpossibleError('error while loading calibration curve') return None @@ -638,6 +639,7 @@ class SensorBase(Base, Readable): STATUS_BIT_LABELS = 'invalid_reading old_reading b2 b3 t_under t_over units_zero units_overrange'.split() _value_error = None _raw_error = None + _curve_installed = 0 # TODO: implement alarms def initModule(self): @@ -648,6 +650,11 @@ class SensorBase(Base, Readable): def doPoll(self): self.read_status() # polls also value and raw + def get_curve_state(self, name): + if self._curve_installed: + return None + return ImpossibleError('no curve installed') + def get_data(self): """get reading status, raw and Kelvin value with minimal delay""" status = IDLE, '' @@ -655,7 +662,10 @@ class SensorBase(Base, Readable): value_error = None if self.enabled: rdgst, raw, value = self.get_internal_data() - self.log.info('rdgst %r, raw %r value %r', rdgst, raw, value) + value_error = self.get_curve_state('T') + if value_error: + status = ERROR, str(value_error) + # self.log.info('rdgst %r, raw %r value %r', rdgst, raw, value) rdgst &= 0xfd # suppress old reading if rdgst: statuslist = formatStatusBits(rdgst, self.STATUS_BIT_LABELS) @@ -665,10 +675,6 @@ class SensorBase(Base, Readable): statuslist.pop() if statuslist: raw_error = HardwareError(statuslist[-1]) - elif self._curve_handling: - value_error = self.device.get_calib_state(self) - if value_error: - status = ERROR, str(value_error) else: value_error = raw_error = DisabledError('disabled') value = raw = 0 @@ -678,6 +684,16 @@ class SensorBase(Base, Readable): self._raw_error = raw_error return status, value, raw + def get_enabled(self): + if not self.query(f'INTYPE?{self.channel}', int): + return False + return self.enabled + + def read_enabled(self): + self._curve_installed = self.query(f'INCRV?{self.channel}', int) + self.log.info('curve_installed %r', self._curve_installed) + return self.get_enabled() + def read_filter(self): on, settle = self.query(f'FILTER?{self.channel}', int, int) return settle if on else 0 @@ -734,6 +750,14 @@ class Sensor(SensorBase): if not self._curve_handling: self.parameters['calcurve'].setProperty('export', False) + def get_curve_state(self, name): + error = None + if self._curve_handling: + error = self.device.get_calib_state(self, name) + if self._curve_installed: + return error + return error or HardwareError('no curve installed') + def get_internal_data(self): ch = self.channel rdgst, raw, value = self.query(f'RDGST?{ch};SRDG?{ch};KRDG?{ch}', int, float, float) @@ -775,11 +799,6 @@ class Sensor(SensorBase): else: self.device.disable_channel(self.channel) - def read_enabled(self): - if not self.query(f'INTYPE?{self.channel}', int): - return False - return self.enabled - def get_curve_type(self, calcurve): unit = calcurve.options.get('unit', 'Ohm') logformat = False @@ -798,13 +817,19 @@ class Sensor(SensorBase): self.intype = (4, 0, 0, 1, 1) return logformat, range_limit - def install_sensor(self): - if self.query(f'INTYPE?{self.channel}', *(type(v) for v in self.intype)) != self.intype: + def install_sensor(self, range_pos=2): + previous = self.query(f'INTYPE?{self.channel}', *(type(v) for v in self.intype), force_list=True) + if range_pos and len(previous) > range_pos and previous[1] == 1: + # do not check range when autorange is on + previous[range_pos] = self.intype[range_pos] + if tuple(previous) != self.intype: + self._curve_installed = 0 self.command(f'INTYPE {self.channel}', *self.intype) def install_curve(self): if self.query(f'INCRV?{self.channel}', int) != self.curve_no: self.command(f'INCRV {self.channel}', self.curve_no) + self._curve_installed = self.curve_no class Output(Base, HasControlledBy, Writable): @@ -1069,6 +1094,9 @@ class Loop(HasConvergence, HasOutputModule, Sensor): return self.command(f'SETP{self.loop()}', float(value)) def get_target(self): + error = self.get_curve_state('setpoint') + if error: + raise HardwareError(error) return self.query(f'SETP?{self.loop()}', float) def write_control_active(self, active): @@ -1114,4 +1142,4 @@ class Device2(Device): -# \ No newline at end of file +# diff --git a/frappy_psi/lakeshore340.py b/frappy_psi/lakeshore340.py index 4b103e12..abb870e8 100644 --- a/frappy_psi/lakeshore340.py +++ b/frappy_psi/lakeshore340.py @@ -64,11 +64,18 @@ class Device(ls.Device): class RawSensor(ls.RawSensor): model = 340 + def get_enabled(self): + if not self.query(f'INSET?{self.channel}', int): + return False + return self.enabled + class Sensor(ls.Sensor): model = 340 intype = None # arguments for the intype command + get_enabled = RawSensor.get_enabled + def get_curve_type(self, calcurve): unit = calcurve.options.get('unit', 'Ohm') logformat = False @@ -98,17 +105,17 @@ class Sensor(ls.Sensor): self.intype = (12,) return logformat, range_limit - def install_sensor(self): - super().install_sensor() + def install_sensor(self, range_pos=None): + super().install_sensor(range_pos) if self.query(f'INSET?{self.channel}', int, int) != (1, 1): self.command(f'INSET {self.channel}', 1, 1) -class Loop340(ls.Loop, Sensor): +class Loop(ls.Loop, Sensor): pass -class MainOutput340(ls.MainOutput): +class MainOutput(ls.MainOutput): ioClass = IO model = 340 output_no = Parameter(datatype=IntRange(1, 1)) @@ -128,7 +135,7 @@ class MainOutput340(ls.MainOutput): SETPOINTLIMS = 1500.0 sorted_factors = sorted([(fhtr * fcur ** 2, (i, h)) for i, fcur in max_currents.items() - for h, fhtr in MainOutput.heater_ranges.items() + for h, fhtr in ls.MainOutput.heater_ranges.items() ]) def get_status(self): @@ -143,7 +150,7 @@ class MainOutput340(ls.MainOutput): self._power_scale = self.max_currents[icurrent] ** 2 * self.heater_ranges[htr_range] / 1e4 self.command(f'CLIMIT {self.output_no}', self.SETPOINTLIMS, 0.0, 0.0, icurrent, htr_range) self._htr_range = htr_range - if self._control_loop is None: + if self.get_control_loop() is None: #TODO: dg changee!!!!!! was self._control_loop mode = self.query(f'CMODE?{self.output_no}', int) if mode != 3: # open loop self.command(f'CSET {self.output_no}', '0', 1, 0, 0) # control off @@ -151,10 +158,10 @@ class MainOutput340(ls.MainOutput): self.command('RANGE', self._htr_range) self.put_manual_power(self._manual_output) else: - self.command(f'CSET {self.output_no}', self._control_loop.channel, 1, 1, 0) # control on + self.command(f'CSET {self.output_no}', self.get_control_loop().channel, 1, 1, 0) # control on self.command(f'CMODE {self.output_no}', 1) # pid self.command('RANGE', self._htr_range) - self.put_manual_power(self._control_loop.power_offset) + self.put_manual_power(self.get_control_loop().power_offset) self.command(f'CDISP {self.output_no}', 1, self.resistance, 1, 0) def fix_heater_range(self): @@ -179,19 +186,19 @@ class MainOutput340(ls.MainOutput): return self.query('HTR?', float) -class AnalogOutput340(ls.AnalogOutput): +class AnalogOutput(ls.AnalogOutput): model = 340 output_no = Parameter(datatype=IntRange(2, 2), default=2) def configure(self): - if self._control_loop is not None: + if self.get_control_loop() is not None: self.put_manual_power(self.target) else: - self.command('ANALOG 2', 0, 3, self._control_loop.channel) - self.put_manual_power(self._control_loop.power_offset) + self.command('ANALOG 2', 0, 3, self.get_control_loop().channel) + self.put_manual_power(self.get_control_loop().power_offset) def put_manual_power(self, value): - if self._control_loop is None: + if self.get_control_loop() is None: self.command('ANALOG 2', 0, 2, 0, 0, 0, 0, 0, self.calc_percent(value)) else: self.command(f'MOUT {self.output_no}', self.calc_percent(value))