Commit d100b692 authored by Arnaud WATLET's avatar Arnaud WATLET
Browse files

Merge branch 'code_refactor' of https://gitlab.irstea.fr/reversaal/OhmPi into code_refactor

Showing with 21 additions and 11 deletions
+21 -11
......@@ -53,7 +53,7 @@ class PwrAbstract(ABC):
self._state = 'off'
self._current_min = kwargs.pop('current_min', 0.)
self._current_max = kwargs.pop('current_max', 0.)
self._current_min = kwargs.pop('voltage_min', 0.)
self._voltage_min = kwargs.pop('voltage_min', 0.)
self._voltage_max = kwargs.pop('voltage_max', 0.)
@property
......@@ -139,7 +139,7 @@ class MuxAbstract(ABC):
def reset(self):
pass
def switch(self, elec_dict=None, state='off'): # TODO: generalize for other roles
def switch(self, elec_dict=None, state='off', bypass_check=False): # TODO: generalize for other roles
"""Switch a given list of electrodes with different roles.
Electrodes with a value of 0 will be ignored.
......@@ -149,6 +149,8 @@ class MuxAbstract(ABC):
Dictionary of the form: {role: [list of electrodes]}.
state : str, optional
Either 'on' or 'off'.
bypass_check: bool, optional
Bypasses checks for A==M or A==M or B==M or B==N (i.e. used for rs-check)
"""
status = True
if elec_dict is not None:
......@@ -166,7 +168,7 @@ class MuxAbstract(ABC):
# as to prevent burning the MN part which cannot take
# the full voltage of the DPS
if 'A' in elec_dict.keys() and 'B' in elec_dict.keys() and 'M' in elec_dict.keys() and 'N' in elec_dict.keys():
if (np.in1d(elec_dict['M'], elec_dict['A']).any() # noqa
if not bypass_check and (np.in1d(elec_dict['M'], elec_dict['A']).any() # noqa
or np.in1d(elec_dict['M'], elec_dict['B']).any() # noqa
or np.in1d(elec_dict['N'], elec_dict['A']).any() # noqa
or np.in1d(elec_dict['N'], elec_dict['B']).any()) and state=='on': # noqa
......@@ -174,6 +176,8 @@ class MuxAbstract(ABC):
'This could create an over-voltage in the RX! Switching aborted.')
status = False
return status
elif bypass_check:
self.exec_logger.debug('Bypassing switching check')
# if all ok, then wait for the barrier to open, then switch the electrodes
self.exec_logger.debug(f'{self.board_id} waiting to switch.')
......@@ -328,6 +332,7 @@ class RxAbstract(ABC):
self.board_name = kwargs.pop('board_name', 'unknown RX hardware')
self._sampling_rate = kwargs.pop('sampling_rate', 1) # ms
self.exec_logger.debug(f'{self.board_name} RX initialization')
self._voltage_max = kwargs.pop('voltage_max', 0.)
self._adc_gain = 1.
self._max_sampling_rate = np.inf
self._bias = 0.
......
......@@ -225,6 +225,7 @@ class Rx(RxAbstract):
address=self._ads_voltage_address)
self._ads_voltage.mode = Mode.CONTINUOUS
self._coef_p2 = kwargs.pop('coef_p2', RX_CONFIG['coef_p2'])
self._voltage_max = kwargs.pop('voltage_max', RX_CONFIG['voltage_max'])
self._sampling_rate = kwargs.pop('sampling_rate', sampling_rate)
self.exec_logger.event(f'{self.board_name}\trx_init\tend\t{datetime.datetime.utcnow()}')
......
......@@ -21,14 +21,12 @@ pwr_module = importlib.import_module(f'ohmpi.hardware_components.{HARDWARE_CONFI
tx_module = importlib.import_module(f'ohmpi.hardware_components.{HARDWARE_CONFIG["tx"]["model"]}')
rx_module = importlib.import_module(f'ohmpi.hardware_components.{HARDWARE_CONFIG["rx"]["model"]}')
MUX_CONFIG = {}
# mux_boards = []
for mux_id, mux_config in HARDWARE_CONFIG['mux']['boards'].items():
mux_module = importlib.import_module(f'ohmpi.hardware_components.{mux_config["model"]}')
MUX_CONFIG[mux_id] = mux_module.MUX_CONFIG
MUX_CONFIG[mux_id].update(mux_config)
MUX_CONFIG[mux_id].update({'id': mux_id})
# mux_boards.append(mux_id)
TX_CONFIG = tx_module.TX_CONFIG
RX_CONFIG = rx_module.RX_CONFIG
......@@ -370,7 +368,7 @@ class OhmPiHardware:
self._vab_pulse(self, length=lengths[i], sampling_rate=sampling_rate, polarity=polarities[i],
append=True)
def switch_mux(self, electrodes, roles=None, state='off'):
def switch_mux(self, electrodes, roles=None, state='off', **kwargs):
"""Switches on multiplexer relays for given quadrupole.
Parameters
......@@ -407,8 +405,8 @@ class OhmPiHardware:
for idx, mux in enumerate(mux_workers):
# Create a new thread to perform some work
self.mux_boards[mux].barrier = b
mux_workers[idx] = Thread(target=self.mux_boards[mux].switch, kwargs={'elec_dict': elec_dict,
'state': state})
kwargs.update({'elec_dict': elec_dict, 'state': state})
mux_workers[idx] = Thread(target=self.mux_boards[mux].switch, kwargs=kwargs)
mux_workers[idx].start()
self.mux_barrier.wait()
for mux_worker in mux_workers:
......
......@@ -697,7 +697,7 @@ class OhmPi(object):
# measure all quad of the RS sequence
for i in range(0, quads.shape[0]):
quad = quads[i, :] # quadrupole
self.switch_mux_on(quad) # put before raising the pins (otherwise conflict i2c)
self.switch_mux_on(quad, bypass_check=True) # put before raising the pins (otherwise conflict i2c)
d = self.run_measurement(quad=quad, nb_stack=1, injection_duration=0.2, tx_volt=tx_volt, autogain=False)
if self._hw.tx.voltage_adjustable:
......@@ -754,7 +754,7 @@ class OhmPi(object):
self.exec_logger.warning(f'Unable to set sequence: {e}')
status = False
def switch_mux_on(self, quadrupole, cmd_id=None):
def switch_mux_on(self, quadrupole, bypass_check=False, cmd_id=None):
"""Switches on multiplexer relays for given quadrupole.
Parameters
......@@ -763,9 +763,15 @@ class OhmPi(object):
Unique command identifier
quadrupole : list of 4 int
List of 4 integers representing the electrode numbers.
bypass_check: bool, optional
Bypasses checks for A==M or A==M or B==M or B==N (i.e. used for rs-check)
"""
assert len(quadrupole) == 4
return self._hw.switch_mux(electrodes=quadrupole, state='on')
if self._hw.tx.pwr.voltage > self._hw.rx._voltage_max and bypass_check:
self.exec_logger.warning('Cannot bypass checking electrode roles because tx voltage is over rx maximum voltage')
return False
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.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment