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

Updates compute_tx_volt kwargs

Showing with 121 additions and 121 deletions
+121 -121
...@@ -403,8 +403,9 @@ class OhmPiHardware: ...@@ -403,8 +403,9 @@ class OhmPiHardware:
# polarity = 1 # polarity = 1
# return vab, polarity, rab # return vab, polarity, rab
def _compute_tx_volt(self, pulse_duration=0.1, strategy='vmax', tx_volt=5., def _compute_tx_volt(self, pulse_duration=0.1, strategy='vmax', tx_volt=5., vab_max=voltage_max,
vab_max=voltage_max, vmn_min=voltage_min, polarities=(1, -1), delay=0.050): iab_max=current_max, vmn_max = 5., vmn_min=voltage_min, polarities=(1, -1), delay=0.050):
# TODO: Optimise how to pass iab_max, vab_max, vmn_min
"""Estimates best Tx voltage based on different strategies. """Estimates best Tx voltage based on different strategies.
At first a half-cycle is made for a short duration with a fixed At first a half-cycle is made for a short duration with a fixed
known voltage. This gives us Iab and Rab. We also measure Vmn. known voltage. This gives us Iab and Rab. We also measure Vmn.
...@@ -443,127 +444,126 @@ class OhmPiHardware: ...@@ -443,127 +444,126 @@ class OhmPiHardware:
rab : float rab : float
Resistance between injection electrodes Resistance between injection electrodes
""" """
# Get those values from components
p_max = 2.5
vmn_max = 5.
# define a sill
diff_vab_lim = 2.5
n_steps = 4
# Set gain at min
self.rx.reset_gain()
vab_max = np.abs(vab_max)
vmn_min = np.abs(vmn_min)
vab = np.min([np.abs(tx_volt), vab_max])
# self.tx.turn_on()
switch_pwr_off, switch_tx_pwr_off = False, False # TODO: check if these should be moved in kwargs
if self.tx.pwr_state == 'off':
self.tx.pwr_state = 'on'
switch_tx_pwr_off = True
if self.tx.pwr.pwr_state == 'off':
self.tx.pwr.pwr_state = 'on'
switch_pwr_off = True
if 1. / self.rx.sampling_rate > pulse_duration:
sampling_rate = 1. / pulse_duration # TODO: check this...
else:
sampling_rate = self.tx.sampling_rate
current, voltage = 0., 0.
if self.tx.pwr.voltage_adjustable: if self.tx.pwr.voltage_adjustable:
self.tx.pwr.voltage = vab # Get those values from components
if self.tx.pwr.pwr_state == 'off': p_max = vab_max * iab_max
self.tx.pwr.pwr_state = 'on'
switch_pwr_off = True # define a sill
k = 0 diff_vab_lim = 2.5
diff_vab = np.inf n_steps = 4
while (k < n_steps) and (diff_vab > diff_vab_lim):
vabs = [] # Set gain at min
for pol in polarities: self.rx.reset_gain()
# self.tx.polarity = pol
# set gains automatically vab_max = np.abs(vab_max)
injection = Thread(target=self._inject, kwargs={'injection_duration': 0.2, 'polarity': pol}) vmn_min = np.abs(vmn_min)
readings = Thread(target=self._read_values) vab = np.min([np.abs(tx_volt), vab_max])
injection.start() # self.tx.turn_on()
self.tx_sync.wait() switch_pwr_off, switch_tx_pwr_off = False, False # TODO: check if these should be moved in kwargs
readings.start() if self.tx.pwr_state == 'off':
readings.join() self.tx.pwr_state = 'on'
injection.join() switch_tx_pwr_off = True
v = np.where((self.readings[:, 0] > delay) & (self.readings[:, 2] != 0))[0] # NOTE : discard data aquired in the first x ms if self.tx.pwr.pwr_state == 'off':
iab = self.readings[v, 3] self.tx.pwr.pwr_state = 'on'
vmn = self.readings[v, 4] * self.readings[v, 2] switch_pwr_off = True
iab_mean = np.mean(iab) if 1. / self.rx.sampling_rate > pulse_duration:
iab_std = np.std(iab) sampling_rate = 1. / pulse_duration # TODO: check this...
vmn_mean = np.mean(vmn) else:
vmn_std = np.std(vmn) sampling_rate = self.tx.sampling_rate
print(f'iab: ({iab_mean:.5f}, {iab_std:5f}), vmn: ({vmn_mean:.4f}, {vmn_std:.4f})') current, voltage = 0., 0.
# bounds on iab if self.tx.pwr.voltage_adjustable:
iab_upper_bound = iab_mean + 2 * iab_std self.tx.pwr.voltage = vab
iab_lower_bound = np.max([0.00001, iab_mean - 2 * iab_std]) if self.tx.pwr.pwr_state == 'off':
# bounds on vmn self.tx.pwr.pwr_state = 'on'
vmn_upper_bound = vmn_mean + 2 * vmn_std switch_pwr_off = True
vmn_lower_bound = np.max([0.000001, vmn_mean - 2 * vmn_std]) k = 0
# ax.plot([vab[k]] * 2, [vmn_lower_bound, vmn_upper_bound], '-b') diff_vab = np.inf
# ax.plot([0, vab_max], [0, vmn_upper_bound * vab_max / vab[k]], '-r', alpha=(k + 1) / n_steps) while (k < n_steps) and (diff_vab > diff_vab_lim):
# ax.plot([0, vab_max], [0, vmn_lower_bound * vab_max / vab[k]], '-g', alpha=(k + 1) / n_steps) vabs = []
# bounds on rab for pol in polarities:
rab_lower_bound = vab[k] / iab_upper_bound # self.tx.polarity = pol
rab_upper_bound = vab[k] / iab_lower_bound # set gains automatically
# bounds on r injection = Thread(target=self._inject, kwargs={'injection_duration': 0.2, 'polarity': pol})
r_lower_bound = vmn_lower_bound / iab_upper_bound readings = Thread(target=self._read_values)
r_upper_bound = vmn_upper_bound / iab_lower_bound injection.start()
# conditions for vab update self.tx_sync.wait()
cond_vmn_max = rab_lower_bound / r_upper_bound * vmn_max readings.start()
cond_p_max = np.sqrt(p_max * rab_lower_bound) readings.join()
new_vab = np.min([vab_max, cond_vmn_max, cond_p_max]) injection.join()
diff_vab = np.abs(new_vab - vab[k]) v = np.where((self.readings[:, 0] > delay) & (self.readings[:, 2] != 0))[0] # NOTE : discard data aquired in the first x ms
print(f'Rab: [{rab_lower_bound:.1f}, {rab_upper_bound:.1f}], R: [{r_lower_bound:.1f},{r_upper_bound:.1f}]') iab = self.readings[v, 3]
print( vmn = self.readings[v, 4] * self.readings[v, 2]
f'{k}: [{vab_max:.1f}, {cond_vmn_max:.1f}, {cond_p_max:.1f}], new_vab: {new_vab}, diff_vab: {diff_vab}\n') iab_mean = np.mean(iab)
if new_vab == vab_max: iab_std = np.std(iab)
print('Vab bounded by Vab max') vmn_mean = np.mean(vmn)
elif new_vab == cond_p_max: vmn_std = np.std(vmn)
print('Vab bounded by Pmax') print(f'iab: ({iab_mean:.5f}, {iab_std:5f}), vmn: ({vmn_mean:.4f}, {vmn_std:.4f})')
elif new_vab == cond_vmn_max: # bounds on iab
print('Vab bounded by Vmn max') iab_upper_bound = iab_mean + 2 * iab_std
else: iab_lower_bound = np.max([0.00001, iab_mean - 2 * iab_std])
print('Next step') # bounds on vmn
vab[k + 1] = new_vab vmn_upper_bound = vmn_mean + 2 * vmn_std
k = k + 1 vmn_lower_bound = np.max([0.000001, vmn_mean - 2 * vmn_std])
vabs.append(new_vab) # ax.plot([vab[k]] * 2, [vmn_lower_bound, vmn_upper_bound], '-b')
if diff_vab < diff_vab_lim: # ax.plot([0, vab_max], [0, vmn_upper_bound * vab_max / vab[k]], '-r', alpha=(k + 1) / n_steps)
print('stopped on vab increase too small') # ax.plot([0, vab_max], [0, vmn_lower_bound * vab_max / vab[k]], '-g', alpha=(k + 1) / n_steps)
else: # bounds on rab
print('stopped on maximum number of steps reached') rab_lower_bound = vab[k] / iab_upper_bound
rab_upper_bound = vab[k] / iab_lower_bound
# bounds on r
r_lower_bound = vmn_lower_bound / iab_upper_bound
r_upper_bound = vmn_upper_bound / iab_lower_bound
# conditions for vab update
cond_vmn_max = rab_lower_bound / r_upper_bound * vmn_max
cond_p_max = np.sqrt(p_max * rab_lower_bound)
new_vab = np.min([vab_max, cond_vmn_max, cond_p_max])
diff_vab = np.abs(new_vab - vab[k])
print(f'Rab: [{rab_lower_bound:.1f}, {rab_upper_bound:.1f}], R: [{r_lower_bound:.1f},{r_upper_bound:.1f}]')
print(
f'{k}: [{vab_max:.1f}, {cond_vmn_max:.1f}, {cond_p_max:.1f}], new_vab: {new_vab}, diff_vab: {diff_vab}\n')
if new_vab == vab_max:
print('Vab bounded by Vab max')
elif new_vab == cond_p_max:
print('Vab bounded by Pmax')
elif new_vab == cond_vmn_max:
print('Vab bounded by Vmn max')
else:
print('Next step')
vab[k + 1] = new_vab
k = k + 1
vabs.append(new_vab)
if diff_vab < diff_vab_lim:
print('stopped on vab increase too small')
else:
print('stopped on maximum number of steps reached')
print(f'Selected Vab: {vab:.2f}')
vab = np.min(vabs)
print(f'Selected Vab: {vab:.2f}') print(f'Selected Vab: {vab:.2f}')
vab = np.min(vabs)
print(f'Selected Vab: {vab:.2f}') # if strategy == 'vmax':
# # implement different strategies
# if strategy == 'vmax': # if vab < vab_max and iab < current_max:
# # implement different strategies # vab = vab * np.min([0.9 * vab_max / vab, 0.9 * current_max / iab]) # TODO: check if setting at 90% of max as a safety margin is OK
# if vab < vab_max and iab < current_max: # self.tx.exec_logger.debug(f'vmax strategy: setting VAB to {vab} V.')
# vab = vab * np.min([0.9 * vab_max / vab, 0.9 * current_max / iab]) # TODO: check if setting at 90% of max as a safety margin is OK # elif strategy == 'vmin':
# self.tx.exec_logger.debug(f'vmax strategy: setting VAB to {vab} V.') # if vab <= vab_max and iab < current_max:
# elif strategy == 'vmin': # vab = vab * np.min([0.9 * vab_max / vab, vmn_min / np.abs(vmn), 0.9 * current_max / iab]) # TODO: check if setting at 90% of max as a safety margin is OK
# if vab <= vab_max and iab < current_max: # elif strategy != 'constant':
# vab = vab * np.min([0.9 * vab_max / vab, vmn_min / np.abs(vmn), 0.9 * current_max / iab]) # TODO: check if setting at 90% of max as a safety margin is OK # self.tx.exec_logger.warning(f'Unknown strategy {strategy} for setting VAB! Using {vab} V')
# elif strategy != 'constant': # else:
# self.tx.exec_logger.warning(f'Unknown strategy {strategy} for setting VAB! Using {vab} V') # self.tx.exec_logger.debug(f'Constant strategy for setting VAB, using {vab} V')
# else: # # self.tx.turn_off()
# self.tx.exec_logger.debug(f'Constant strategy for setting VAB, using {vab} V') # if switch_pwr_off:
# # self.tx.turn_off() # self.tx.pwr.pwr_state = 'off'
# if switch_pwr_off: # if switch_tx_pwr_off:
# self.tx.pwr.pwr_state = 'off' # self.tx.pwr_state = 'off'
# if switch_tx_pwr_off: # rab = (np.abs(vab) * 1000.) / iab
# self.tx.pwr_state = 'off' # self.exec_logger.debug(f'RAB = {rab:.2f} Ohms')
# rab = (np.abs(vab) * 1000.) / iab # if vmn < 0:
# self.exec_logger.debug(f'RAB = {rab:.2f} Ohms') # polarity = -1 # TODO: check if we really need to return polarity
# if vmn < 0: # else:
# polarity = -1 # TODO: check if we really need to return polarity # polarity = 1
# else: return vab
# polarity = 1
return vab, None, None
def _plot_readings(self, save_fig=False): def _plot_readings(self, save_fig=False):
# Plot graphs # Plot graphs
......
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