From 04e6f6350258b09a5849b7c204b4b3fac2ae65bd Mon Sep 17 00:00:00 2001
From: Martin Killenberg <martin.killenberg@desy.de>
Date: Thu, 12 Jan 2023 16:39:32 +0100
Subject: [PATCH] introduce wait_until so the readout interval is not affected
 by the communication time

---
 Python_script/prototype.py              | 14 ++++++++++++--
 Python_script/test_stand_parameter.json |  2 +-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Python_script/prototype.py b/Python_script/prototype.py
index 0ab7c2e..25d6fa1 100755
--- a/Python_script/prototype.py
+++ b/Python_script/prototype.py
@@ -92,6 +92,9 @@ class Measurements:
                         number_of_soaking_reads = next_soaking / self.sleep_time + 1
 
                         do_another_measurement = True
+                        #next_read_time is the starttime of the second read (i.e. read after this one). The time of
+                        #this read (i.e. the first read in the measurement) is now().
+                        next_read_time = self.clock.time() + self.sleep_time
                         while do_another_measurement:
                             # wait until set point is reached (+soaking time)
                             magnitudes_queue = []
@@ -148,7 +151,8 @@ class Measurements:
                                     print('SOAKING FINISHED!')
                                     break
                                 else:
-                                    self.clock.sleep(self.sleep_time)
+                                    self.sleep_until(next_read_time)
+                                    next_read_time += self.sleep_time
 
                             # perform the configured number of measurements and check that they are really stable
                             # It started running after everything become stable
@@ -171,7 +175,8 @@ class Measurements:
                                 if (self.temperature_stable and self.humidity_stable and self.magnitude_stable and
                                         self.phase_stable):
                                     print('Stable measurement ' + str(i+1) + '/' + str(next_reads))
-                                    self.clock.sleep(self.sleep_time)
+                                    self.sleep_until(next_read_time)
+                                    next_read_time += self.sleep_time
                                 else:
                                     all_measurements_stable = False
                                     print('Measurement not stable. Retrying.')
@@ -193,6 +198,11 @@ class Measurements:
             standby_response = self.chamber.set_mode('STANDBY')
             print(standby_response)
 
+    def sleep_until(self, wakeup_time):
+        remaining_sleep_time = wakeup_time - self.clock.time()
+        if remaining_sleep_time > 0:
+            self.clock.sleep(remaining_sleep_time)
+
     def read_data(self):
         [temp, hum, mode, alarms] = self.chamber.read_monitor().split(',')
         power = self.vna.get_current_power()
diff --git a/Python_script/test_stand_parameter.json b/Python_script/test_stand_parameter.json
index 5c76f21..00eadda 100644
--- a/Python_script/test_stand_parameter.json
+++ b/Python_script/test_stand_parameter.json
@@ -1 +1 @@
-{"delta_temp": 0.1, "delta_hum": 1, "delta_mag": 0.1 , "delta_phase": 0.02, "sleep_time": 1.0, "frequency": 1300000000, "vna_config_file": "climate-lab.znxml"}
+{"delta_temp": 0.1, "delta_hum": 1, "delta_mag": 0.1 , "delta_phase": 0.02, "sleep_time": 10.0, "frequency": 1300000000, "vna_config_file": "climate-lab.znxml"}
-- 
GitLab