Commit 6b69d763 authored by Arnaud WATLET's avatar Arnaud WATLET
Browse files

creates switch_dps function

Showing with 81 additions and 41 deletions
+81 -41
...@@ -142,13 +142,7 @@ class OhmPi(object): ...@@ -142,13 +142,7 @@ class OhmPi(object):
# current injection module # current injection module
if self.idps: if self.idps:
self.pin2 = self.MCP_board.get_pin(2) # dsp + self.switch_dps('on')
self.pin2.direction = Direction.OUTPUT
self.pin2.value = True
self.pin3 = self.MCP_board.get_pin(3) # dsp -
self.pin3.direction = Direction.OUTPUT
self.pin3.value = True
time.sleep(4)
self.DPS = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1) # port name, address (decimal) self.DPS = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1) # port name, address (decimal)
self.DPS.serial.baudrate = 9600 # Baud rate 9600 as listed in doc self.DPS.serial.baudrate = 9600 # Baud rate 9600 as listed in doc
self.DPS.serial.bytesize = 8 # self.DPS.serial.bytesize = 8 #
...@@ -157,12 +151,13 @@ class OhmPi(object): ...@@ -157,12 +151,13 @@ class OhmPi(object):
self.DPS.serial.parity = 'N' # No parity self.DPS.serial.parity = 'N' # No parity
self.DPS.mode = minimalmodbus.MODE_RTU # RTU mode self.DPS.mode = minimalmodbus.MODE_RTU # RTU mode
self.DPS.write_register(0x0001, 1000, 0) # max current allowed (100 mA for relays) self.DPS.write_register(0x0001, 1000, 0) # max current allowed (100 mA for relays)
print(self.DPS.read_register(0x05,2 )) # max current allowed (100 mA for relays) #voltage
self.pin2.value = False
self.pin3.value = False
# (last number) 0 is for mA, 3 is for A # (last number) 0 is for mA, 3 is for A
#self.soh_logger.debug(f'Battery voltage: {self.DPS.read_register(0x05,2 ):.3f}') TODO: SOH logger
print(self.DPS.read_register(0x05,2 ))
self.switch_dps('off')
# injection courant and measure (TODO check if it works, otherwise back in run_measurement()) # injection courant and measure (TODO check if it works, otherwise back in run_measurement())
self.pin0 = self.MCP_board.get_pin(0) self.pin0 = self.MCP_board.get_pin(0)
self.pin0.direction = Direction.OUTPUT self.pin0.direction = Direction.OUTPUT
...@@ -328,6 +323,30 @@ class OhmPi(object): ...@@ -328,6 +323,30 @@ class OhmPi(object):
if strategy == 'constant': if strategy == 'constant':
vab = volt vab = volt
self.DPS.write_register(0x0000, volt, 2)
self.DPS.write_register(0x09, 1) # DPS5005 on
time.sleep(best_tx_injtime) # inject for given tx time
# autogain
self.ads_current = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_current_address)
self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_voltage_address)
gain_current = self._gain_auto(AnalogIn(self.ads_current, ads.P0))
gain_voltage0 = self._gain_auto(AnalogIn(self.ads_voltage, ads.P0))
gain_voltage2 = self._gain_auto(AnalogIn(self.ads_voltage, ads.P2))
gain_voltage = np.min([gain_voltage0, gain_voltage2]) # TODO: separate gain for P0 and P2
self.ads_current = ads.ADS1115(self.i2c, gain=gain_current, data_rate=860, address=self.ads_current_address)
self.ads_voltage = ads.ADS1115(self.i2c, gain=gain_voltage, data_rate=860, address=self.ads_voltage_address)
# we measure the voltage on both A0 and A2 to guess the polarity
I = AnalogIn(self.ads_current, ads.P0).voltage * 1000. / 50 / self.r_shunt # noqa measure current
U0 = AnalogIn(self.ads_voltage, ads.P0).voltage * 1000. # noqa measure voltage
U2 = AnalogIn(self.ads_voltage, ads.P2).voltage * 1000. # noqa
# check polarity
polarity = 1 # by default, we guessed it right
vmn = U0
if U0 < 0: # we guessed it wrong, let's use a correction factor
polarity = -1
vmn = U2
elif strategy == 'vmax': elif strategy == 'vmax':
# implement different strategy # implement different strategy
...@@ -357,9 +376,12 @@ class OhmPi(object): ...@@ -357,9 +376,12 @@ class OhmPi(object):
self.ads_current = ads.ADS1115(self.i2c, gain=gain_current, data_rate=860, address=self.ads_current_address) self.ads_current = ads.ADS1115(self.i2c, gain=gain_current, data_rate=860, address=self.ads_current_address)
self.ads_voltage = ads.ADS1115(self.i2c, gain=gain_voltage, data_rate=860, address=self.ads_voltage_address) self.ads_voltage = ads.ADS1115(self.i2c, gain=gain_voltage, data_rate=860, address=self.ads_voltage_address)
# we measure the voltage on both A0 and A2 to guess the polarity # we measure the voltage on both A0 and A2 to guess the polarity
I = AnalogIn(self.ads_current, ads.P0).voltage * 1000. / 50 / self.r_shunt # noqa measure current for i in range(10):
U0 = AnalogIn(self.ads_voltage, ads.P0).voltage * 1000. # noqa measure voltage I = AnalogIn(self.ads_current, ads.P0).voltage * 1000. / 50 / self.r_shunt # noqa measure current
U2 = AnalogIn(self.ads_voltage, ads.P2).voltage * 1000. # noqa U0 = AnalogIn(self.ads_voltage, ads.P0).voltage * 1000. # noqa measure voltage
U2 = AnalogIn(self.ads_voltage, ads.P2).voltage * 1000. # noqa
time.sleep(best_tx_injtime)
print(U0,U2,I)
# check polarity # check polarity
polarity = 1 # by default, we guessed it right polarity = 1 # by default, we guessed it right
...@@ -374,7 +396,6 @@ class OhmPi(object): ...@@ -374,7 +396,6 @@ class OhmPi(object):
volt = volt - 2 volt = volt - 2
self.DPS.write_register(0x0000, volt, 2) self.DPS.write_register(0x0000, volt, 2)
#self.DPS.write_register(0x09, 1) # DPS5005 on #self.DPS.write_register(0x09, 1) # DPS5005 on
#time.sleep(best_tx_injtime)
I = AnalogIn(self.ads_current, ads.P0).voltage * 1000. / 50 / self.r_shunt I = AnalogIn(self.ads_current, ads.P0).voltage * 1000. / 50 / self.r_shunt
U0 = AnalogIn(self.ads_voltage, ads.P0).voltage * 1000. U0 = AnalogIn(self.ads_voltage, ads.P0).voltage * 1000.
U2 = AnalogIn(self.ads_voltage, ads.P2).voltage * 1000. U2 = AnalogIn(self.ads_voltage, ads.P2).voltage * 1000.
...@@ -394,7 +415,6 @@ class OhmPi(object): ...@@ -394,7 +415,6 @@ class OhmPi(object):
factor = factor_vmn factor = factor_vmn
#print('factor', factor_I, factor_vmn) #print('factor', factor_I, factor_vmn)
vab = factor * volt #* 0.8 vab = factor * volt #* 0.8
print(factor, volt, vab)
if vab > tx_max: if vab > tx_max:
vab = tx_max vab = tx_max
...@@ -411,10 +431,11 @@ class OhmPi(object): ...@@ -411,10 +431,11 @@ class OhmPi(object):
break break
# set voltage for test # set voltage for test
self.DPS.write_register(0x0000, volt, 2)
if count==1: if count==1:
self.DPS.write_register(0x09, 1) # DPS5005 on self.DPS.write_register(0x09, 1) # DPS5005 on
time.sleep(best_tx_injtime) # inject for given tx time time.sleep(best_tx_injtime) # inject for given tx time
self.DPS.write_register(0x0000, volt, 2)
# autogain # autogain
self.ads_current = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_current_address) self.ads_current = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_current_address)
self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_voltage_address) self.ads_voltage = ads.ADS1115(self.i2c, gain=2 / 3, data_rate=860, address=self.ads_voltage_address)
...@@ -462,8 +483,8 @@ class OhmPi(object): ...@@ -462,8 +483,8 @@ class OhmPi(object):
# print('polarity', polarity) # print('polarity', polarity)
self.pin0.value = False self.pin0.value = False
self.pin1.value = False self.pin1.value = False
# compute constant # # compute constant
c = vmn / I # c = vmn / I
Rab = (volt * 1000.) / I # noqa Rab = (volt * 1000.) / I # noqa
self.exec_logger.debug(f'Rab = {Rab:.2f} Ohms') self.exec_logger.debug(f'Rab = {Rab:.2f} Ohms')
...@@ -819,16 +840,10 @@ class OhmPi(object): ...@@ -819,16 +840,10 @@ class OhmPi(object):
self.pin7.direction = Direction.OUTPUT self.pin7.direction = Direction.OUTPUT
self.pin7.value = False self.pin7.value = False
if self.sequence is None : if self.sequence is None:
if self.idps: if self.idps:
self.pin2 = self.MCP_board.get_pin(2) # dsp + self.switch_dps('on')
self.pin2.direction = Direction.OUTPUT
self.pin2.value = True
self.pin3 = self.MCP_board.get_pin(3) # dsp -
self.pin3.direction = Direction.OUTPUT
self.pin3.value = True
time.sleep(5)
self.pin5 = self.MCP_board.get_pin(5) #IHM on mesaurement self.pin5 = self.MCP_board.get_pin(5) #IHM on mesaurement
self.pin5.direction = Direction.OUTPUT self.pin5.direction = Direction.OUTPUT
self.pin5.value = True self.pin5.value = True
...@@ -1145,8 +1160,8 @@ class OhmPi(object): ...@@ -1145,8 +1160,8 @@ class OhmPi(object):
self.data_logger.info(dd) self.data_logger.info(dd)
self.pin5.value = False #IHM led on measurement off self.pin5.value = False #IHM led on measurement off
if self.sequence is None : if self.sequence is None :
self.pin2.value = False # DSP + off self.switch_dps('off')
self.pin3.value = False # DSP - off
return d return d
def run_multiple_sequences(self, cmd_id=None, sequence_delay=None, nb_meas=None, **kwargs): def run_multiple_sequences(self, cmd_id=None, sequence_delay=None, nb_meas=None, **kwargs):
...@@ -1234,15 +1249,17 @@ class OhmPi(object): ...@@ -1234,15 +1249,17 @@ class OhmPi(object):
# call the switch_mux function to switch to the right electrodes # call the switch_mux function to switch to the right electrodes
self.switch_mux_on(quad) self.switch_mux_on(quad)
self.mcp = MCP23008(self.i2c, address=0x24)
self.pin2 = self.MCP_board.get_pin(2) # dsp -
self.pin2.direction = Direction.OUTPUT
self.pin2.value = True
self.pin3 = self.MCP_board.get_pin(3) # dsp -
self.pin3.direction = Direction.OUTPUT
self.pin3.value = True
time.sleep(4)
# switch on DPS
self.switch_dps('on')
# self.MCP_board = MCP23008(self.i2c, address=0x24)
# self.pin2 = self.MCP_board.get_pin(2) # dsp +
# self.pin2.direction = Direction.OUTPUT
# self.pin2.value = True
# self.pin3 = self.MCP_board.get_pin(3) # dsp -
# self.pin3.direction = Direction.OUTPUT
# self.pin3.value = True
# time.sleep(4)
# run a measurement # run a measurement
if self.on_pi: if self.on_pi:
acquired_data = self.run_measurement(quad, **kwargs) acquired_data = self.run_measurement(quad, **kwargs)
...@@ -1278,8 +1295,8 @@ class OhmPi(object): ...@@ -1278,8 +1295,8 @@ class OhmPi(object):
# save data and print in a text file # save data and print in a text file
self.append_and_save(filename, acquired_data) self.append_and_save(filename, acquired_data)
self.exec_logger.debug(f'quadrupole {i + 1:d}/{n:d}') self.exec_logger.debug(f'quadrupole {i + 1:d}/{n:d}')
self.pin2.value = False
self.pin3.value = False self.switch_dps('off')
self.status = 'idle' self.status = 'idle'
def run_sequence_async(self, cmd_id=None, **kwargs): def run_sequence_async(self, cmd_id=None, **kwargs):
...@@ -1448,6 +1465,29 @@ class OhmPi(object): ...@@ -1448,6 +1465,29 @@ class OhmPi(object):
else: else:
self.exec_logger.warning(f'Unable to address electrode nr {electrode_nr}') self.exec_logger.warning(f'Unable to address electrode nr {electrode_nr}')
def switch_dps(self,state='off'):
"""Switches DPS on or off.
Parameters
----------
state : str
'on', 'off'
"""
self.pin2 = self.MCP_board.get_pin(2) # dsp -
self.pin2.direction = Direction.OUTPUT
self.pin3 = self.MCP_board.get_pin(3) # dsp -
self.pin3.direction = Direction.OUTPUT
if state == 'on':
self.pin2.value = True
self.pin3.value = True
self.exec_logger.debug(f'Switching DPS on')
time.sleep(4)
elif state == 'off':
self.pin2.value = False
self.pin3.value = False
self.exec_logger.debug(f'Switching DPS off')
def switch_mux_on(self, quadrupole, cmd_id=None): def switch_mux_on(self, quadrupole, cmd_id=None):
"""Switches on multiplexer relays for given quadrupole. """Switches on 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