From b66711ace12d68d167017ef0cfd7bfcc7514d0c1 Mon Sep 17 00:00:00 2001
From: Marko Mecina <marko.mecina@univie.ac.at>
Date: Thu, 27 Jul 2023 18:25:46 +0200
Subject: [PATCH] update SXI thermal model & control

---
 Ccs/thermal_control.py | 22 ++++++++++++++--------
 Ccs/thermal_model.py   |  5 ++++-
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/Ccs/thermal_control.py b/Ccs/thermal_control.py
index 1ceb39c..a32b8eb 100644
--- a/Ccs/thermal_control.py
+++ b/Ccs/thermal_control.py
@@ -19,7 +19,7 @@ class ThermalController:
     VCTRLUPPERVOLT = 2.8
     MAXDELTAVOLTAGE = 0.2
 
-    def __init__(self, temp_ref, coeffP, coeffI, offset, exec_per, model=None):
+    def __init__(self, temp_ref, coeffP, coeffI, offset, exec_per, model=None, pi=True, deltaTmax=1.):
 
         self.tempRef = temp_ref
         self.coeffP = coeffP
@@ -37,6 +37,13 @@ class ThermalController:
         self.hctrl_par_exec_per = exec_per
         self._algo_active = False
 
+        if pi:
+            self.calc_vctrl = self.do_pi_vctrl
+        else:
+            self.calc_vctrl = self.on_off_vctrl
+
+        self.deltaTmax = deltaTmax
+
         self.model = model
         self.log = []
 
@@ -45,7 +52,7 @@ class ThermalController:
     def set_temp_ref(self, temp):
         self.tempRef = temp
 
-    def calculate_vctrl(self, temp):
+    def do_pi_vctrl(self, temp):
 
         deltaTime = self.hctrl_par_exec_per * self.ASW_PERIOD_MS / 1000
         integ = self.integOld + deltaTime * (self.tempRef - self.tempOld)
@@ -69,11 +76,11 @@ class ThermalController:
 
         self.voltCtrlUint16 = vctrl_ana_to_dig(self.voltCtrl)
 
-    def on_off_vctrl(self, temp, deltaTmax=1.):
+    def on_off_vctrl(self, temp):
 
-        if temp > self.tempRef + deltaTmax:
+        if temp > self.tempRef + self.deltaTmax:
             v = self.voltCtrl - self.MAXDELTAVOLTAGE
-        elif temp < self.tempRef - deltaTmax:
+        elif temp < self.tempRef - self.deltaTmax:
             v = self.voltCtrl + self.MAXDELTAVOLTAGE
         else:
             v = self.voltCtrl
@@ -121,14 +128,13 @@ class ThermalController:
     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.calc_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)
+            self.calc_vctrl(self.temp)
 
     def stop_algo(self):
         self._algo_active = False
diff --git a/Ccs/thermal_model.py b/Ccs/thermal_model.py
index c0fe87b..5ad00aa 100644
--- a/Ccs/thermal_model.py
+++ b/Ccs/thermal_model.py
@@ -140,6 +140,8 @@ class ThermalModel:
         self.htr_pwr = 0
         self.htr_cur = 0
 
+        self.inst_heat = 0  # additional, immediate heat input (e.g., from CCD, etc.)
+
         self._evolving = False
         self.record = record
         self.log = []
@@ -179,7 +181,8 @@ class ThermalModel:
 
     def heat(self):
         self.heat_pipe += self.htr_pwr * self.heat_distr
-        addheat = self.heat_pipe[0]
+        addheat = self.heat_pipe[0] + self.inst_heat
+        #print(self.heat_pipe, self.heat_pipe.sum(), self.htr_pwr,self.inst_heat)
         self.heat_pipe = np.roll(self.heat_pipe, -1)
         self.heat_pipe[-1] = 0
         return addheat
-- 
GitLab