From 7f8322253b569b1f7b476baf0b5f9334ffa44e5c Mon Sep 17 00:00:00 2001
From: Arnaud Watlet <arnaud.watlet@umons.ac.be>
Date: Thu, 7 Dec 2023 12:23:42 +0100
Subject: [PATCH] Tries inject_current

---
 ohmpi/hardware_components/mb_2024_0_2.py | 22 ++++++++++++++++++++++
 ohmpi/hardware_components/pwr_dps5005.py |  4 ++--
 ohmpi/hardware_system.py                 |  8 +++++++-
 3 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/ohmpi/hardware_components/mb_2024_0_2.py b/ohmpi/hardware_components/mb_2024_0_2.py
index cc5dbc21..5deb8127 100644
--- a/ohmpi/hardware_components/mb_2024_0_2.py
+++ b/ohmpi/hardware_components/mb_2024_0_2.py
@@ -130,6 +130,28 @@ class Tx(Tx_mb_2023):
             self.exec_logger.debug(f'Switching DPS off')
             self._pwr_state = 'off'
 
+    def current_pulse(self, current=None, length=None, polarity=1):
+        """ Generates a square voltage pulse
+
+        Parameters
+        ----------
+        voltage: float, optional
+            Voltage to apply in volts, tx_v_def is applied if omitted.
+        length: float, optional
+            Length of the pulse in seconds
+        polarity: 1,0,-1
+            Polarity of the pulse
+        """
+        self.exec_logger.event(f'{self.model}\ttx_current_pulse\tbegin\t{datetime.datetime.utcnow()}')
+        # self.exec_logger.info(f'injection_duration: {length}')  # TODO: delete me
+        if length is None:
+            length = self.injection_duration
+        if current is not None:
+            self.pwr.current = current
+        self.exec_logger.debug(f'Current pulse of {polarity*self.pwr.voltage:.3f} V for {length:.3f} s')
+        self.inject(polarity=polarity, injection_duration=length)
+        self.exec_logger.event(f'{self.model}\ttx_current_pulse\tend\t{datetime.datetime.utcnow()}')
+
 
 class Rx(Rx_mb_2023):
     def __init__(self, **kwargs):
diff --git a/ohmpi/hardware_components/pwr_dps5005.py b/ohmpi/hardware_components/pwr_dps5005.py
index 81371d35..8d0f2963 100644
--- a/ohmpi/hardware_components/pwr_dps5005.py
+++ b/ohmpi/hardware_components/pwr_dps5005.py
@@ -87,8 +87,8 @@ class Pwr(PwrAbstract):
 
     @current.setter
     def current(self, value, **kwargs):
-        value = value  # To set DPS max current slightly above (20%) the limit to avoid regulation artefacts
-        self.connection.write_register(0x0001, int(value), 3)
+        # value = value  # To set DPS max current slightly above (20%) the limit to avoid regulation artefacts
+        self.connection.write_register(0x0001, np.round((value), 3), 3)
         self._current = value
         # self.exec_logger.debug(f'Current cannot be set on {self.model}')
 
diff --git a/ohmpi/hardware_system.py b/ohmpi/hardware_system.py
index 97abc7ad..c24d7685 100644
--- a/ohmpi/hardware_system.py
+++ b/ohmpi/hardware_system.py
@@ -241,6 +241,11 @@ class OhmPiHardware:
         self.tx.voltage_pulse(length=injection_duration, polarity=polarity)
         self.exec_logger.event(f'OhmPiHardware\tinject\tend\t{datetime.datetime.utcnow()}')
 
+    def _inject_current(self, polarity=1, injection_duration=None):  # TODO: deal with voltage or current pulse
+        self.exec_logger.event(f'OhmPiHardware\tinject\tbegin\t{datetime.datetime.utcnow()}')
+        self.tx.current_pulse(length=injection_duration, polarity=polarity)
+        self.exec_logger.event(f'OhmPiHardware\tinject\tend\t{datetime.datetime.utcnow()}')
+
     def _set_mux_barrier(self):
         self.mux_barrier = Barrier(len(self.mux_boards) + 1)
         for mux in self.mux_boards:
@@ -639,6 +644,7 @@ class OhmPiHardware:
         if self.tx.pwr.voltage_adjustable:
             if self.tx.voltage != vab:
                 self.tx.voltage = vab
+                self.tx.current = 0.02
         else:
             vab = self.tx.voltage
 
@@ -648,7 +654,7 @@ class OhmPiHardware:
             self.tx.pwr.pwr_state = 'on'
             switch_pwr_off = True
         # reads current and voltage during the pulse
-        injection = Thread(target=self._inject, kwargs={'injection_duration': duration, 'polarity': polarity})
+        injection = Thread(target=self._inject_current, kwargs={'injection_duration': duration, 'polarity': polarity})
         readings = Thread(target=self._read_values, kwargs={'sampling_rate': sampling_rate, 'append': append})
         readings.start()
         injection.start()
-- 
GitLab