From 65e2ee0fb569d05e6e3a909463af8f825694ba32 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Thu, 15 Jun 2023 17:41:29 +0200
Subject: [PATCH] Tries to tackle issue #110

---
 .../abstract_hardware_components.py                    | 10 ++++++++--
 ohmpi/hardware_components/mux_2024_rev_0_0.py          | 10 +++++++---
 ohmpi/hardware_system.py                               |  7 ++++++-
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/ohmpi/hardware_components/abstract_hardware_components.py b/ohmpi/hardware_components/abstract_hardware_components.py
index c6cd8b27..9e3a40ad 100644
--- a/ohmpi/hardware_components/abstract_hardware_components.py
+++ b/ohmpi/hardware_components/abstract_hardware_components.py
@@ -119,6 +119,8 @@ class MuxAbstract(ABC):
         self.exec_logger.debug(f'{self.board_id} cabling: {self.cabling}')
         self.addresses = kwargs.pop('addresses', None)
         self._barrier = kwargs.pop('barrier', Barrier(1))
+        self._activation_delay = kwargs.pop('activation_delay', 0.)  # in s
+        self._release_delay = kwargs.pop('release_delay', 0.)  # in s
 
     @abstractmethod
     def _get_addresses(self):
@@ -154,7 +156,7 @@ class MuxAbstract(ABC):
             # check to prevent A == B (SHORT-CIRCUIT)
             if 'A' in elec_dict.keys() and 'B' in elec_dict.keys():
                 out = np.in1d(elec_dict['A'], elec_dict['B'])
-                if out.any() and state=='on':  # noqa
+                if out.any() and state == 'on':  # noqa
                     self.exec_logger.error('Trying to switch on some electrodes with both A and B roles. '
                                            'This would create a short-circuit! Switching aborted.')
                     status = False
@@ -183,13 +185,17 @@ class MuxAbstract(ABC):
                             self.switch_one(elec, role, state)
                             status &= True
                         else:
-                            self.exec_logger.warning(f'{self.board_id} skipping switching {(elec, role)} because it '
+                            self.exec_logger.debug(f'{self.board_id} skipping switching {(elec, role)} because it '
                                                    f'is not in board cabling.')
                             status = False
             self.exec_logger.debug(f'{self.board_id} switching done.')
         else:
             self.exec_logger.warning(f'Missing argument for {self.board_name}.switch: elec_dict is None.')
             status = False
+        if state == 'on':
+            time.sleep(self._activation_delay)
+        elif state == 'off':
+            time.sleep(self._release_delay)
         return status
 
     @abstractmethod
diff --git a/ohmpi/hardware_components/mux_2024_rev_0_0.py b/ohmpi/hardware_components/mux_2024_rev_0_0.py
index 309f3458..250a5852 100644
--- a/ohmpi/hardware_components/mux_2024_rev_0_0.py
+++ b/ohmpi/hardware_components/mux_2024_rev_0_0.py
@@ -10,7 +10,7 @@ from digitalio import Direction  # noqa
 
 MUX_CONFIG = HARDWARE_CONFIG['mux'].pop('default', {})
 MUX_CONFIG.update({'voltage_max': 50., 'current_max': 3.})  # board default values that overwrite system default values
-MUX_CONFIG.update({'activation_delay': 0.01, 'release_delay': 0.005}) # s
+MUX_CONFIG.update({'activation_delay': 0.01, 'release_delay': 0.005})  # s
 default_mux_cabling = {(elec, role) : ('mux_1', elec) for role in ['A', 'B', 'M', 'N'] for elec in range(1,9)} # defaults to 4 roles cabling electrodes from 1 to 8
 
 
@@ -57,6 +57,10 @@ class Mux(MuxAbstract):
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         if 'cabling' not in kwargs.keys() or kwargs['cabling']=={}:
             kwargs.update({'cabling': default_mux_cabling})
+        if 'activation_delay' not in kwargs:
+            kwargs.update({'activation_delay': MUX_CONFIG['activation_delay']})
+        if 'release_delay' not in kwargs:
+            kwargs.update({'release_delay': MUX_CONFIG['release_delay']})
         super().__init__(**kwargs)
         self.exec_logger.debug(f'configuration: {MUX_CONFIG}')
         tca_address = kwargs.pop('tca_address', None)
@@ -106,7 +110,7 @@ class Mux(MuxAbstract):
         d = self.addresses[elec, role]
         if state == 'on':
             activate_relay(self._mcp[d['MCP']], d['MCP_GPIO'], True)
-            time.sleep(MUX_CONFIG['activation_delay'])
+            # time.sleep(MUX_CONFIG['activation_delay'])  # NOTE: moved to MuxAbstract switch
         if state == 'off':
             activate_relay(self._mcp[d['MCP']], d['MCP_GPIO'], False)
-            time.sleep(MUX_CONFIG['release_delay'])
\ No newline at end of file
+            # time.sleep(MUX_CONFIG['release_delay'])  # NOTE: moved to MuxAbstract switch
diff --git a/ohmpi/hardware_system.py b/ohmpi/hardware_system.py
index f5b3ae46..2ec85791 100644
--- a/ohmpi/hardware_system.py
+++ b/ohmpi/hardware_system.py
@@ -173,7 +173,12 @@ class OhmPiHardware:
                               self.rx.voltage])
             sample += 1
             sleep_time = self._start_time + datetime.timedelta(seconds=sample * sampling_rate / 1000) - lap
-            time.sleep(np.max([0., sleep_time.total_seconds()]))  # TODO set readings to nan if sleep time <0 and skip the sample (sample +=1)
+            if sleep_time < 0.:
+                _readings.append([elapsed_seconds(self._start_time), self._pulse, self.tx.polarity, np.nan, np.nan])  # TODO:
+                sample += 1
+            else:
+                time.sleep(np.max([0., sleep_time.total_seconds()]))  # TODO: set readings to nan if sleep time <0 and skip the sample (sample +=1)
+
         self.exec_logger.warning(f'pulse {self._pulse}: elapsed time {(lap-self._start_time).total_seconds()} s')  # TODO: Set to debug level
         self.exec_logger.warning(f'pulse {self._pulse}: total samples {len(_readings)}')  # TODO: Set to debug level
         self.readings = np.array(_readings)
-- 
GitLab