From 5e57dc0ac1556c40fa6d6f6283a8d644079de93a Mon Sep 17 00:00:00 2001
From: sldesnoo-Delft <s.l.desnoo@tudelft.nl>
Date: Thu, 2 Feb 2023 09:20:57 +0100
Subject: [PATCH] Calculate and set timeout for acquisition

---
 CHANGELOG.md                          | 1 +
 pulse_lib/keysight/M3202A_uploader.py | 6 ++++++
 pulse_lib/qblox/pulsar_uploader.py    | 8 ++++++--
 pulse_lib/tests/acquire/test_read.py  | 9 +++++++++
 pulse_lib/tests/mock_m3102a.py        | 3 +++
 5 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7db1f391..a7a0d593 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ All notable changes to Pulselib will be documented in this file.
 
 - Fixed setpoints of measurment parameter for time traces > 2 seconds.
 - Fixed simultaneous pulses with indexing of segments on looping parameter.
+- Calculate timeout for get_acquisition_data from sequence.
 
 ## \[1.6.7] - 2023-02-01
 
diff --git a/pulse_lib/keysight/M3202A_uploader.py b/pulse_lib/keysight/M3202A_uploader.py
index da3f3031..af26465f 100644
--- a/pulse_lib/keysight/M3202A_uploader.py
+++ b/pulse_lib/keysight/M3202A_uploader.py
@@ -155,6 +155,10 @@ class M3202A_Uploader:
             acquisition_mode: set externally
             scale, impedance: set externally
         '''
+        n_rep = job.n_rep if job.n_rep else 1
+        total_seconds = job.playback_time * n_rep * 1e-9
+        timeout = int(total_seconds*1.1) + 3
+
         enabled_channels = {}
         channels = job.acquisition_conf.channels
         sample_rate = job.acquisition_conf.sample_rate
@@ -178,6 +182,8 @@ class M3202A_Uploader:
             dig = self.digitizers[dig_name]
             dig.set_operating_mode(2) # HVI
             dig.set_active_channels(channel_nums)
+            if hasattr(dig, 'set_timeout'):
+                dig.set_timeout(timeout)
 
         self.acq_description = AcqDescription(job.seq_id, job.index, channels,
                                               job.acquisitions, enabled_channels,
diff --git a/pulse_lib/qblox/pulsar_uploader.py b/pulse_lib/qblox/pulsar_uploader.py
index a31965bb..e35d31b6 100644
--- a/pulse_lib/qblox/pulsar_uploader.py
+++ b/pulse_lib/qblox/pulsar_uploader.py
@@ -242,8 +242,12 @@ class PulsarUploader:
 
         logging.info(f'Play {index}')
 
-        self.q1instrument.run_program(job.program) # sync
-#        self.q1instrument.start_program(job.program) # @@@ async
+        n_rep = job.n_rep if job.n_rep else 1
+        total_seconds = job.playback_time * n_rep * 1e-9
+        timeout_minutes = int(total_seconds*1.1 / 60) + 1
+
+        self.q1instrument.start_program(job.program)
+        self.q1instrument.wait_stopped(timeout_minutes=timeout_minutes)
 
         if release_job:
             job.release()
diff --git a/pulse_lib/tests/acquire/test_read.py b/pulse_lib/tests/acquire/test_read.py
index 0f7a3637..020b241c 100644
--- a/pulse_lib/tests/acquire/test_read.py
+++ b/pulse_lib/tests/acquire/test_read.py
@@ -24,8 +24,17 @@ def test3(iq_mode='I+Q'):
 
     return context.run('read_iq_'+iq_mode, dc_param)
 
+def test4():
+    # takes 200 seconds to run !!!
+    pulse = context.init_pulselib(n_gates=0, n_sensors=2, rf_sources=False)
+
+    dc_param = read_channels(pulse, 200e9, sample_rate=100) # 100 Hz, 200 seconds
+
+    return context.run('read', dc_param)
+
 #%%
 if __name__ == '__main__':
     ds1 = test1()
     ds2 = test2()
     ds3 = test3()
+    ds4 = test4()
diff --git a/pulse_lib/tests/mock_m3102a.py b/pulse_lib/tests/mock_m3102a.py
index f4cff035..da10a5c1 100644
--- a/pulse_lib/tests/mock_m3102a.py
+++ b/pulse_lib/tests/mock_m3102a.py
@@ -45,6 +45,9 @@ class MockM3102A(Instrument):
         t_downsampling = downsampling_factor * 10
         return max(1, round(t_measure/t_downsampling))
 
+    def set_timeout(self, seconds):
+        self.timeout_seconds = seconds
+
     def set_daq_settings(self, channel, n_cycles, t_measure, downsampled_rate=None):
         self.measure._active_channels.add(channel)
         properties = self.measure._ch_properties[channel]
-- 
GitLab