From 21eaf172808e296b25e696dae43a573e70556d2f Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Wed, 26 Jul 2023 18:27:23 +0200
Subject: [PATCH] update SXI thermal control

---
 Ccs/thermal_control.py | 55 ++++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 15 deletions(-)

diff --git a/Ccs/thermal_control.py b/Ccs/thermal_control.py
index 0a2814e..1ceb39c 100644
--- a/Ccs/thermal_control.py
+++ b/Ccs/thermal_control.py
@@ -15,24 +15,24 @@ class ThermalController:
 
     ASW_PERIOD_MS = 125.
 
-    VCTRLLOWERVOLT = 0.2
-    VCTRLUPPERVOLT = 2.9
-    MAXDELTAVOLTAGE = 0.25
+    VCTRLLOWERVOLT = 0.4
+    VCTRLUPPERVOLT = 2.8
+    MAXDELTAVOLTAGE = 0.2
 
-    def __init__(self, temp_ref, cp, ci, offset, exec_per, model=None):
+    def __init__(self, temp_ref, coeffP, coeffI, offset, exec_per, model=None):
 
         self.tempRef = temp_ref
-        self.coeffP = cp
-        self.coeffI = ci
+        self.coeffP = coeffP
+        self.coeffI = coeffI
         self.offset = offset
 
-        self.temp = 0
-        self.voltCtrl = 0
+        self.temp = temp_ref
+        self.voltCtrl = self.VCTRLLOWERVOLT
         self.voltCtrlUint16 = vctrl_ana_to_dig(self.voltCtrl)
 
         self.tempOld = temp_ref
         self.integOld = 0
-        self.voltCtrlOld = 0
+        self.voltCtrlOld = self.VCTRLLOWERVOLT
 
         self.hctrl_par_exec_per = exec_per
         self._algo_active = False
@@ -69,6 +69,22 @@ class ThermalController:
 
         self.voltCtrlUint16 = vctrl_ana_to_dig(self.voltCtrl)
 
+    def on_off_vctrl(self, temp, deltaTmax=1.):
+
+        if temp > self.tempRef + deltaTmax:
+            v = self.voltCtrl - self.MAXDELTAVOLTAGE
+        elif temp < self.tempRef - deltaTmax:
+            v = self.voltCtrl + self.MAXDELTAVOLTAGE
+        else:
+            v = self.voltCtrl
+
+        if v > self.VCTRLUPPERVOLT:
+            self.voltCtrl = self.VCTRLUPPERVOLT
+        elif v < self.VCTRLLOWERVOLT:
+            self.voltCtrl = self.VCTRLLOWERVOLT
+        else:
+            self.voltCtrl = v
+
     def start_algo(self):
 
         if self._thread is not None and self._thread.is_alive():
@@ -87,14 +103,10 @@ class ThermalController:
                 t1 = time.time()
 
                 if self.model is not None:
-                    self.temp = self.model.T_noisy
-                    self.calculate_vctrl(self.temp)
-                    self.model.set_heater_power(vctrl=self.voltCtrl)
-
-                    self.log.append((t1, self.tempRef, self.temp, self.voltCtrl, self.coeffI*self.integOld, self.coeffP * (self.tempRef - self.temp)))
+                    self._step(t1, with_model=True)
 
                 else:
-                    self.calculate_vctrl(self.temp)
+                    self._step(t1, with_model=False)
 
                 dt = (self.ASW_PERIOD_MS / 1000 * self.hctrl_par_exec_per) - (time.time() - t1)
                 if dt > 0:
@@ -106,8 +118,21 @@ class ThermalController:
 
         print('TTC algo stopped')
 
+    def _step(self, t1, with_model=True, glitch=0):
+        if with_model:
+            self.temp = self.model.T_noisy + glitch
+            self.calculate_vctrl(self.temp)
+            # self.on_off_vctrl(self.temp)
+            self.model.set_heater_power(vctrl=self.voltCtrl)
+
+            self.log.append((t1, self.tempRef, self.temp, self.voltCtrl, self.coeffI * self.integOld, self.coeffP * (self.tempRef - self.temp)))
+            
+        else:
+            self.calculate_vctrl(self.temp)
+
     def stop_algo(self):
         self._algo_active = False
 
     def save_log(self, fname):
         np.savetxt(fname, np.array(self.log), header='time\tT_ref\tT\tV_ctrl\tcI*integ\tcP*(T_ref-T)')
+
-- 
GitLab