From e6021cb3299f3dbda53aa60f48aea9b181789309 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Mon, 9 Oct 2023 11:15:35 +0200
Subject: [PATCH] Updates ohmpi to handle no mux case

---
 configs/config_mb_2024_1_mux_2024.py |  6 +--
 dev/test_mb_2024_0_mux_2024.py       | 75 ++++++++++++++++++++++++++++
 ohmpi/ohmpi.py                       |  5 +-
 3 files changed, 82 insertions(+), 4 deletions(-)
 create mode 100644 dev/test_mb_2024_0_mux_2024.py

diff --git a/configs/config_mb_2024_1_mux_2024.py b/configs/config_mb_2024_1_mux_2024.py
index 1ce2e8cc..69393b24 100644
--- a/configs/config_mb_2024_1_mux_2024.py
+++ b/configs/config_mb_2024_1_mux_2024.py
@@ -20,14 +20,14 @@ OHMPI_CONFIG = {
 HARDWARE_CONFIG = {
     'ctl': {'model': 'raspberry_pi'},
     'pwr': {'model': 'pwr_batt', 'voltage': 12.},
-    'tx':  {'model': 'mb_2023_0_X',
+    'tx':  {'model': 'mb_2024_0_X',
              'voltage_max': 12.,  # Maximum voltage supported by the TX board [V]
              'adc_voltage_max': 4800.,  # Maximum voltage read by the current ADC on the TX board [mA]
              'r_shunt': 2.,  # Shunt resistance in Ohms
              'interface_name': 'i2c',
             },
-    'rx':  {'model': 'mb_2023_0_X',
-            'coef_p2': 2.50,  # slope for conversion for ADS, measurement in V/V
+    'rx':  {'model': 'mb_2024_0_X',
+            'coef_p2': 1.0,  # slope for conversion for ADS, measurement in V/V
             'sampling_rate': 50.,  # number of samples per second
             'interface_name': 'i2c',
             },
diff --git a/dev/test_mb_2024_0_mux_2024.py b/dev/test_mb_2024_0_mux_2024.py
new file mode 100644
index 00000000..5205f2bb
--- /dev/null
+++ b/dev/test_mb_2024_0_mux_2024.py
@@ -0,0 +1,75 @@
+import time
+from ohmpi.utils import change_config
+from ohmpi.plots import plot_exec_log
+import logging
+change_config('../configs/config_mb_2024.py', verbose=False)
+# from ohmpi.hardware_components.mux_2024_0_X import Mux
+from ohmpi.hardware_components import raspberry_pi as ctl_module
+from ohmpi.config import HARDWARE_CONFIG
+MUX_CONFIG = HARDWARE_CONFIG['mux']
+
+
+stand_alone_mux = False
+part_of_hardware_system = False
+within_ohmpi = True
+# Stand alone mux
+if stand_alone_mux:
+    mux_id = 'mux_00'
+    first = 24
+    print(MUX_CONFIG)
+    MUX_CONFIG.update(HARDWARE_CONFIG['mux']['boards'][mux_id])
+    MUX_CONFIG.update({'id': mux_id})
+    MUX_CONFIG['ctl'] = ctl_module.Ctl()
+    mux = Mux(**MUX_CONFIG)
+    mux.switch_one(elec=1+first, role='M', state='on')
+    time.sleep(1)
+    mux.switch_one(elec=1+first, role='M', state='off')
+    mux.switch({'A': [1], 'B': [2], 'M': [3], 'N': [4]}, state='on')
+    time.sleep(2)
+    # mux.switch({'A': [1], 'B': [4], 'M': [2], 'N': [3]}, state='off')
+    mux.reset()
+    mux.test({'A': [i+first for i in range(1, 9)], 'B': [i+first for i in range(1, 9)],
+              'M': [i+first for i in range(1, 9)], 'N': [i+first for i in range(1, 9)]}, activation_time=.1)
+
+# mux as part of a OhmPiHardware system
+if part_of_hardware_system:
+    from ohmpi.hardware_system import OhmPiHardware
+    print('Starting test of mux as part of a OhmPiHardware system.')
+
+    k = OhmPiHardware()
+    k.exec_logger.setLevel(logging.DEBUG)
+
+    # Test mux switching
+    k.reset_mux()
+    k.switch_mux(electrodes=[1, 4, 2, 3], roles=['A', 'B', 'M', 'N'], state='on')
+    time.sleep(1.)
+    k.switch_mux(electrodes=[1, 4, 2, 3], roles=['A', 'B', 'M', 'N'], state='off')
+
+if within_ohmpi:
+    from ohmpi.ohmpi import OhmPi
+    print('Starting test of mux within OhmPi.')
+    k = OhmPi()
+    #A, B, M, N = (32, 29, 31, 30)
+    k.reset_mux()
+    #k._hw.switch_mux([A, B, M, N], state='on')
+    #k._hw.vab_square_wave(12.,1., cycles=2)
+    #k._hw.switch_mux([A, B, M, N], state='off')
+    #k._hw.calibrate_rx_bias()  # electrodes 1 4 2 3 should be connected to a reference circuit
+    #k._hw.rx._bias = -1.38
+    #print(f'Resistance: {k._hw.last_rho :.2f} ohm, dev. {k._hw.last_dev:.2f} %, rx bias: {k._hw.rx._bias:.2f} mV')
+    # k._hw._plot_readings()
+    A, B, M, N = (28, 25, 27, 26)
+    # k._hw.switch_mux([A, B, M, N], state='on')
+    # k._hw.vab_square_wave(12., cycle_duration=10., cycles=3)
+    # k._hw.switch_mux([A, B, M, N], state='off')
+    # print(f'OhmPiHardware Resistance: {k._hw.last_rho :.2f} ohm, dev. {k._hw.last_dev:.2f} %, rx bias: {k._hw.rx._bias:.2f} mV')
+    # k._hw._plot_readings()
+    print('using OhmPi')
+    d = k.run_measurement([A, B, M, N], injection_duration=1., nb_stack=2, duty_cycle=0.5)
+    print(d)
+    #k._hw._plot_readings()
+    print(f'OhmPiHardware: Resistance: {k._hw.last_rho :.2f} ohm, dev. {k._hw.last_dev:.2f} %, sp: {k._hw.sp:.2f} mV, rx bias: {k._hw.rx._bias:.2f} mV')
+    print(f'OhmPi: Resistance: {d["R [ohm]"] :.2f} ohm, dev. {d["R_std [%]"]:.2f} %, rx bias: {k._hw.rx._bias:.2f} mV')
+    k._hw._plot_readings(save_fig=False)
+    # plot_exec_log('ohmpi/logs/exec.log')
+change_config('../configs/config_default.py', verbose=False)
\ No newline at end of file
diff --git a/ohmpi/ohmpi.py b/ohmpi/ohmpi.py
index c176d901..44c70738 100644
--- a/ohmpi/ohmpi.py
+++ b/ohmpi/ohmpi.py
@@ -789,7 +789,10 @@ class OhmPi(object):
             self.exec_logger.debug(f'tx pwr voltage: {self._hw.tx.pwr.voltage}, rx max voltage: {self._hw.rx._voltage_max}')
             return False
         else:
-            return self._hw.switch_mux(electrodes=quadrupole, state='on', bypass_check=bypass_check)
+            if quadrupole == [0, 0, 0, 0]:  # NOTE: No mux
+                return True
+            else:
+                return self._hw.switch_mux(electrodes=quadrupole, state='on', bypass_check=bypass_check)
 
     def switch_mux_off(self, quadrupole, cmd_id=None):
         """Switches off multiplexer relays for given quadrupole.
-- 
GitLab