From 3b8f9d823194b8e0103cab417654a2e2b812b257 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Tue, 2 May 2023 18:44:19 +0200
Subject: [PATCH] Rewrites TX voltage handling

---
 config_mb_2023_mux_2024.py                    |  3 ++-
 .../abstract_hardware_components.py           | 17 +++++++++++------
 hardware_components/ohmpi_card_3_15.py        | 19 ++++++++++++-------
 hardware_system.py                            |  7 +++++--
 4 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/config_mb_2023_mux_2024.py b/config_mb_2023_mux_2024.py
index 097d5fde..252974c8 100644
--- a/config_mb_2023_mux_2024.py
+++ b/config_mb_2023_mux_2024.py
@@ -22,7 +22,8 @@ HARDWARE_CONFIG = {
                    },
     'tx' : {'model' : 'ohmpi_card_3_15',
              'mcp_board_address': 0x20,
-             'current_max': 4800 / 50 / 2,  # Maximum current
+             'voltage_max': 12, # Maximum voltage [V]
+             'current_max': 4800 / 50 / 2,  # Maximum current [mA]
              'r_shunt': 2  # Shunt resistance in Ohms
             },
     'rx' : {'model': 'ohmpi_card_3_15',
diff --git a/hardware_components/abstract_hardware_components.py b/hardware_components/abstract_hardware_components.py
index da34d4d8..07b3ef15 100644
--- a/hardware_components/abstract_hardware_components.py
+++ b/hardware_components/abstract_hardware_components.py
@@ -18,6 +18,7 @@ class ControllerAbstract(ABC):
         self.exec_logger.debug(f'{self.board_name} Controller initialization')
         self._cpu_temp_available = False
         self.max_cpu_temp = np.inf
+
     @property
     def cpu_temperature(self):
         if not self._cpu_temp_available:
@@ -167,6 +168,9 @@ class TxAbstract(ABC):
         self._dps_state = 'off'
         self._adc_gain = 1.
         self.inj_time = inj_time
+        self._voltage = 0.
+        self.voltage_adjustable = True
+        self._current_adjustable = False
         self.exec_logger.debug(f'{self.board_name} TX initialization')
 
     @property
@@ -230,16 +234,17 @@ class TxAbstract(ABC):
         self._dps_state = 'on'
 
     @property
-    @abstractmethod
     def voltage(self):
-        # add actions to read the DPS voltage and return it
-        return None
+        return self._voltage
 
     @voltage.setter
-    @abstractmethod
     def voltage(self, value):
-        # add actions to set the DPS voltage
-        pass
+        assert isinstance(value, float)
+        if not self.voltage_adjustable:
+            self.exec_logger.warning(f'Voltage cannot be set on {self.board_name}...')
+        else:
+            self._voltage = value
+        # Add specifics to set DPS voltage
 
     @property
     @abstractmethod
diff --git a/hardware_components/ohmpi_card_3_15.py b/hardware_components/ohmpi_card_3_15.py
index e3914f8b..0057df53 100644
--- a/hardware_components/ohmpi_card_3_15.py
+++ b/hardware_components/ohmpi_card_3_15.py
@@ -74,6 +74,8 @@ class Tx(TxAbstract):
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         super().__init__(**kwargs)
         self._voltage = kwargs.pop('voltage', TX_CONFIG['default_voltage'])
+        self.voltage_adjustable = False
+        self.current_adjustable = False
         if self.controller is None:
             self.controller = controller_module.Controller()
 
@@ -164,13 +166,6 @@ class Tx(TxAbstract):
             self.pin1.value = False
         #time.sleep(0.001) # TODO: check max switching time of relays
 
-    @property
-    def voltage(self):
-        return self._voltage
-    @voltage.setter
-    def voltage(self, value):
-            self.exec_logger.debug(f'Voltage cannot be set on {self.board_name}...')
-
     def turn_off(self):
         pass
 
@@ -207,6 +202,16 @@ class Tx(TxAbstract):
         time.sleep(length)
         self.inject(state='off')
 
+
+    @property
+    def voltage(self):
+        return self._voltage
+    @voltage.setter
+    def voltage(self, value):
+        assert isinstance(value, float)
+        value = np.max(TX_CONFIG['voltage_min'], np.min(value, TX_CONFIG['voltage_max']))
+        super().voltage = value
+
 class Rx(RxAbstract):
     def __init__(self, **kwargs):
         kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
diff --git a/hardware_system.py b/hardware_system.py
index 177733cd..a4a9dc93 100644
--- a/hardware_system.py
+++ b/hardware_system.py
@@ -224,7 +224,10 @@ class OhmPiHardware:
             sampling_rate = RX_CONFIG['sampling_rate']
         if polarity is not None and polarity != self.tx.polarity:
             self.tx.polarity = polarity
-        self.tx.voltage = vab
+        if self.tx.voltage_adjustable:
+            self.tx.voltage = vab
+        else:
+            vab = self.tx.voltage
         injection = Thread(target=self._inject, kwargs={'duration':length})
         readings = Thread(target=self._read_values, kwargs={'sampling_rate': sampling_rate, 'append': append})
         # set gains automatically
@@ -289,7 +292,7 @@ class OhmPiHardware:
                     if mux not in mux_workers:
                         mux_workers.append(mux)
                 except KeyError:
-                    self.exec_logger.warning(f'({elec}, {roles[idx]} is not in cabling. It will be ignored...')
+                    self.exec_logger.warning(f'({elec}, {roles[idx]}) is not in cabling. It will be ignored...')
             mux_workers = list(set(mux_workers))
             b = Barrier(len(mux_workers)+1)
             self.mux_barrier = b
-- 
GitLab