From 6ca493b800822d914b1567658d21f542e12fff0c Mon Sep 17 00:00:00 2001
From: "rpi2.0" <rpi2.0>
Date: Tue, 14 Feb 2023 17:24:27 +0100
Subject: [PATCH] update relay switch

---
 config.py |  2 +-
 ohmpi.py  | 17 ++++++-----
 test.py   | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 101 insertions(+), 9 deletions(-)
 create mode 100644 test.py

diff --git a/config.py b/config.py
index d32bf2b6..3dcfa8c0 100644
--- a/config.py
+++ b/config.py
@@ -22,7 +22,7 @@ OHMPI_CONFIG = {
     'max_elec': 64,
     'board_addresses': {'A': 0x73, 'B': 0x72, 'M': 0x71, 'N': 0x70},  # CHECK IF YOUR BOARDS HAVE THESE ADDRESSES
     'settings': 'ohmpi_settings.json',  # INSERT YOUR FAVORITE SETTINGS FILE HERE
-    'board_version': 'mb.2023.0.0'
+    'board_version': '22.10'
 }  # TODO: add a dictionary with INA models and associated gain values
 
 # SET THE LOGGING LEVELS, MQTT BROKERS AND MQTT OPTIONS ACCORDING TO YOUR NEEDS
diff --git a/ohmpi.py b/ohmpi.py
index b69c4349..cfda3d71 100644
--- a/ohmpi.py
+++ b/ohmpi.py
@@ -744,7 +744,7 @@ class OhmPi(object):
             self.pin7.value = False
             
                 
-            if self.sequence == None :
+            if self.sequence is None :
                 self.pin2 = self.mcp.get_pin(2) # dsp +
                 self.pin2.direction = Direction.OUTPUT
                 self.pin2.value = True
@@ -761,9 +761,9 @@ class OhmPi(object):
             self.pin7 = self.mcp.get_pin(7) #IHM on mesaurement
             self.pin7.direction = Direction.OUTPUT
             self.pin7.value = False           
-            
-            if self.DPS.read_register(0x05,2) < 11:
-                self.pin7.value = True# max current allowed (100 mA for relays) #voltage
+            if self.idps: 
+                if self.DPS.read_register(0x05,2) < 11:
+                    self.pin7.value = True# max current allowed (100 mA for relays) #voltage
             
             # get best voltage to inject AND polarity
             if self.idps:
@@ -996,6 +996,7 @@ class OhmPi(object):
                     "Tx [V]": tx_volt if not out_of_range else 0.,
                     "CPU temp [degC]": CPUTemperature().temperature,
                     "Nb samples [-]": self.nb_samples,
+                    "fulldata": fulldata,
                 }
 
 
@@ -1019,7 +1020,7 @@ class OhmPi(object):
         dd['cmd_id'] = str(cmd_id)
         self.data_logger.info(dd)
         self.pin5.value = False #IHM led on measurement off 
-        if self.sequence == None :
+        if self.sequence is None :
             self.pin2.value = False # DSP + off
             self.pin3.value = False # DSP - off
         return d
@@ -1304,12 +1305,12 @@ class OhmPi(object):
             if i2c_address is not None:
                 # select the MCP23017 of the selected MUX board
                 mcp2 = MCP23017(tca[i2c_address])
-                mcp2.get_pin(relay_nr - 1).direction = digitalio.Direction.OUTPUT
+                mcp2.get_pin(relay_nr).direction = digitalio.Direction.OUTPUT
 
                 if state == 'on':
-                    mcp2.get_pin(relay_nr - 1).value = True
+                    mcp2.get_pin(relay_nr).value = True
                 else:
-                    mcp2.get_pin(relay_nr - 1).value = False
+                    mcp2.get_pin(relay_nr).value = False
 
                 self.exec_logger.debug(f'Switching relay {relay_nr} '
                                        f'({str(hex(self.board_addresses[role]))}) {state} for electrode {electrode_nr}')
diff --git a/test.py b/test.py
new file mode 100644
index 00000000..136de2a8
--- /dev/null
+++ b/test.py
@@ -0,0 +1,91 @@
+from ohmpi import OhmPi
+import matplotlib.pyplot as plt
+import numpy as np
+
+a = np.arange(13) + 49
+b = a + 3
+m = a + 1
+n = a + 2
+seq = np.c_[a, b, m, n]
+
+k = OhmPi(idps=False)
+k.settings['injection_duration'] = 1
+k.settings['nb_stack'] = 1
+k.settings['nbr_meas'] = 1
+k.sequence = seq
+k.reset_mux()
+# k.sequence = np.array([[1,4,2,3]])
+k.run_sequence()
+#k.measure(strategy='vmax')
+#print('vab', k.compute_tx_volt(strategy='vmin'))
+#k.rs_check()
+#out = k.run_measurement(quad=[3, 3, 3, 3], nb_stack=1, tx_volt=12, strategy='constant', autogain=True)
+
+#k.rs_check(tx_volt=12)
+
+# x = []
+#for i in range(3):
+#    out = k.run_measurement(injection_duration=2, nb_stack=2, strategy='constant', tx_volt=5, autogain=False)
+    #x.append(out['R [ohm]'])
+    #k.append_and_save('out.csv', out)
+
+data = out['fulldata']
+inan = ~np.isnan(data[:,0])
+
+if True:
+    fig, axs = plt.subplots(2, 1, sharex=True)
+    ax = axs[0]
+    ax.plot(data[inan,2], data[inan,0], 'r.-', label='current [mA]')
+    ax.set_ylabel('Current AB [mA]')
+    ax = axs[1]
+    ax.plot(data[inan,2], data[inan,1], '.-', label='voltage [mV]')
+    ax.set_ylabel('Voltage MN [mV]')
+    ax.set_xlabel('Time [s]')
+    plt.show()
+    
+#     fig,ax=plt.subplots()
+#     
+#    
+#     ax.plot(data[inan,2], data[inan,0],  label='current [mA]', marker="o")
+#     ax2=ax.twinx()
+#     ax2.plot(data[inan,2], data[inan,1],'r.-' , label='current [mV]')
+#     ax2.set_ylabel('Voltage [mV]', color='r')
+#     ymin=-50
+#     ymax=50
+#     ymin1=-4500
+#     ymax1= 4500
+#     ax.set_ylim([ymin,ymax])
+#     ax2.set_ylim([ymin1,ymax1])
+#     
+#     plt.show()
+    
+
+
+if False:
+    from numpy.fft import fft, ifft
+
+    x = data[inan, 1][10:300]
+    t = np.linspace(0, len(x)*4, len(x))
+    sr = 1/0.004
+
+    X = fft(x)
+    N = len(X)
+    n = np.arange(N)
+    T = N/sr
+    freq = n/T 
+
+    plt.figure(figsize = (12, 6))
+    plt.subplot(121)
+
+    plt.stem(freq, np.abs(X), 'b', \
+             markerfmt=" ", basefmt="-b")
+    plt.xlabel('Freq (Hz)')
+    plt.ylabel('FFT Amplitude |X(freq)|')
+    #plt.xlim(0, 10)
+
+    plt.subplot(122)
+    plt.plot(t, ifft(X), 'r')
+    plt.xlabel('Time (s)')
+    plt.ylabel('Amplitude')
+    plt.tight_layout()
+    plt.show()
-- 
GitLab