From 26c1bb46fffd11e057c21594accb819809e7e83f Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Tue, 10 Oct 2023 15:05:29 +0200
Subject: [PATCH] Searches for run_measurement error

---
 .../abstract_hardware_components.py               | 15 ++++++++++-----
 ohmpi/hardware_components/mb_2023_0_X.py          |  7 ++++++-
 ohmpi/hardware_components/mb_2024_0_2.py          |  2 +-
 ohmpi/hardware_system.py                          |  4 ++--
 4 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/ohmpi/hardware_components/abstract_hardware_components.py b/ohmpi/hardware_components/abstract_hardware_components.py
index 27308832..6aa90ef2 100644
--- a/ohmpi/hardware_components/abstract_hardware_components.py
+++ b/ohmpi/hardware_components/abstract_hardware_components.py
@@ -289,7 +289,7 @@ class TxAbstract(ABC):
         pass
 
     @abstractmethod
-    def inject(self, polarity=1, injection_duration=None):
+    def inject(self, polarity=1, injection_duration=None, switch_pwr=False):
         """
         Abstract method to define injection
         Parameters
@@ -303,15 +303,20 @@ class TxAbstract(ABC):
         if injection_duration is None:
             injection_duration = self._injection_duration
         if np.abs(polarity) > 0:
-            self.pwr.turn_on()
+            if switch_pwr:
+                self.pwr.turn_on()
             self.tx_sync.set()
             time.sleep(injection_duration)
-            self.pwr.turn_off()
+            self.tx_sync.clear()
+            if switch_pwr:
+                self.pwr.turn_off()
         else:
             self.tx_sync.set()
-            self.pwr.turn_off()
+            if switch_pwr:
+                self.pwr.turn_off()
             time.sleep(injection_duration)
-        self.tx_sync.clear()
+            self.tx_sync.clear()
+
 
     @property
     def injection_duration(self):
diff --git a/ohmpi/hardware_components/mb_2023_0_X.py b/ohmpi/hardware_components/mb_2023_0_X.py
index f6a47d93..4269fd54 100644
--- a/ohmpi/hardware_components/mb_2023_0_X.py
+++ b/ohmpi/hardware_components/mb_2023_0_X.py
@@ -10,6 +10,7 @@ import numpy as np
 import os
 from ohmpi.hardware_components import TxAbstract, RxAbstract
 from ohmpi.utils import enforce_specs
+import inspect
 
 # hardware characteristics and limitations
 # voltages are given in mV, currents in mA, sampling rates in Hz and data_rate in S/s
@@ -36,6 +37,7 @@ SPECS = {'rx': {'sampling_rate': {'min': 2., 'default': 10., 'max': 100.},
 
 # TODO: move low_battery spec in pwr
 
+
 def _ads_1115_gain_auto(channel):  # Make it a class method ?
     """Automatically sets the gain on a channel
 
@@ -161,7 +163,10 @@ class Tx(TxAbstract):
     def polarity(self, polarity):
         assert polarity in [-1, 0, 1]
         self._polarity = polarity
-        print(f'asserted polarity: {self.polarity}')
+        # debugging code
+        curframe = inspect.currentframe()
+        calframe = inspect.getouterframes(curframe, 2)
+        print(f'polarity called from: {calframe}')
 
         if polarity == 1:
             print('pin0')
diff --git a/ohmpi/hardware_components/mb_2024_0_2.py b/ohmpi/hardware_components/mb_2024_0_2.py
index e8e64de6..0278228f 100644
--- a/ohmpi/hardware_components/mb_2024_0_2.py
+++ b/ohmpi/hardware_components/mb_2024_0_2.py
@@ -33,6 +33,7 @@ SPECS = {'rx': {'sampling_rate': {'min': 2., 'default': 10., 'max': 100.},
 
 # TODO: move low_battery spec in pwr
 
+
 def _ads_1115_gain_auto(channel):  # Make it a class method ?
     """Automatically sets the gain on a channel
 
@@ -124,7 +125,6 @@ class Rx(Rx_mb_2023):
             self.pin_DG1.value = True  # closed gain 1 active
             self.pin_DG2.value = False  # open gain 0.5 inactive
 
-
     def gain_auto(self):
         self._dg411_gain_auto()
 
diff --git a/ohmpi/hardware_system.py b/ohmpi/hardware_system.py
index 214729f9..79845010 100644
--- a/ohmpi/hardware_system.py
+++ b/ohmpi/hardware_system.py
@@ -178,13 +178,13 @@ class OhmPiHardware:
         rx_gains = []
         for pol in polarities:
             # self.tx.polarity = pol
-            # self.tx_sync.wait()
             # set gains automatically
             injection = Thread(target=self._inject, kwargs={'injection_duration': 0.2, 'polarity': pol})
             # readings = Thread(target=self._read_values)
             get_tx_gain = Thread(target=self.tx.gain_auto)
             get_rx_gain = Thread(target=self.rx.gain_auto)
             injection.start()
+            self.tx_sync.wait()
             get_tx_gain.start()  # TODO: add a barrier to synchronize?
             get_rx_gain.start()
             get_tx_gain.join()
@@ -404,7 +404,7 @@ class OhmPiHardware:
         assert 0. <= duty_cycle <= 1.
         if duty_cycle < 1.:
             durations = [cycle_duration/2 * duty_cycle, cycle_duration/2*(1.-duty_cycle)] * 2 * cycles
-            pol = [-int(1. * np.heaviside(i % 2, -1.)) for i in range(2 * cycles)]
+            pol = [-int(polarity * np.heaviside(i % 2, -1.)) for i in range(2 * cycles)]
             # pol = [-int(self.tx.polarity * np.heaviside(i % 2, -1.)) for i in range(2 * cycles)]
             polarities = [0] * (len(pol) * 2)
             polarities[0::2] = pol
-- 
GitLab