Finished b_field_rotation

This commit is contained in:
ryantan 2024-08-30 12:04:17 +02:00
parent 54a2948d0b
commit 4450fd1e76

View File

@ -415,7 +415,7 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
instr_info = query_no_echo(instr, '*IDN?') instr_info = query_no_echo(instr, '*IDN?')
instr_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element tuple of tuples containing the set unit, llim and ulim instr_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element list of lists containing the set unit, llim and ulim
if instr_bsettings[0][0] == 'T': if instr_bsettings[0][0] == 'T':
instr_bsettings[1][0] = instr_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T instr_bsettings[1][0] = instr_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T
@ -654,10 +654,8 @@ def polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True
return [angles, coordinates] return [angles, coordinates]
# TODO: write a function that simultaneously controls the two power supplies and perform the rotation of the B-field. => Threading
# in function head should be func(instr1, instr2, args1, kwargs1, args2, kwargs2)
def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Resource, def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Resource,
Babs:float, startangle:float, endangle:float, angle_stepsize:float, clockwise=True, path_save='', base_file_name='', sweepdown=False)->None: Babs:float, startangle:float, endangle:float, angle_stepsize:float, Settings:str, clockwise=True, path_save='', base_file_name='', zerowhenfin_bool=False)->None:
# TODO: update docs # TODO: update docs
"""Rotation of the b-field in discrete steps, spectrum is measured at each discrete step in the rotation. Scan angle is """Rotation of the b-field in discrete steps, spectrum is measured at each discrete step in the rotation. Scan angle is
defined as the angle between the x-axis and the current B-field vector, i.e., in the anticlockwise direction. defined as the angle between the x-axis and the current B-field vector, i.e., in the anticlockwise direction.
@ -670,11 +668,12 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
endangle (float): end angle in degrees endangle (float): end angle in degrees
angle_stepsize (float): angle step size in degrees angle_stepsize (float): angle step size in degrees
clockwise (bool): determines the direction of rotation of the B-field. Defaults to True. clockwise (bool): determines the direction of rotation of the B-field. Defaults to True.
sweepdown (bool, optional): after finishing the rotation, both B-field components should be set to 0 T. Defaults to False. zerowhenfin_bool (bool, optional): after finishing the rotation, both B-field components should be set to 0 T. Defaults to False.
""" """
# TODO: possibly rename instr1 and instr2 to the dual and single power supplies respectively??
if path_save =='': if path_save =='':
path_save = datetime.datetime.now().strftime("%Y_%m_%d_%H%M_hrs_") # TODO: add path_save, base_file_name in the function header path_save = datetime.datetime.now().strftime("%Y_%m_%d_%H%M_hrs_")
if base_file_name =='': if base_file_name =='':
base_file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H.%M') base_file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H.%M')
@ -689,13 +688,23 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
intensity_data = [] # To store data from each scan intensity_data = [] # To store data from each scan
cwd = os.getcwd() # save original directory cwd = os.getcwd() # save original directory
# TODO: find which one is the dual power supply, then, ramp B_x to Babs value # find which one is the dual power supply, then, ramp B_x to Babs value
if '2301034' in idnstr1: # serial no. the dual power supply if '2301034' in idnstr1: # serial no. the dual power supply
pass pass
elif '2101034' in idnstr2: elif '2101034' in idnstr2:
# swap instruments, instr 1 to be the dual power supply (^= x-axis) # swap instruments, instr 1 to be the dual power supply (^= x-axis)
instr1, instr2 = instr2, instr1 instr1, instr2 = instr2, instr1
# save initial low and high sweep limits of each device, and set them back after the rotation
instr1_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr1, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element tuple of tuples containing the set unit, llim and ulim
instr2_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr2, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element tuple of tuples containing the set unit, llim and ulim
if instr1_bsettings[0][0] == 'T':
instr1_bsettings[1][0] = instr1_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T
instr1_bsettings[2][0] = instr1_bsettings[2][0]*0.1
if instr2_bsettings[0][0] == 'T':
instr2_bsettings[1][0] = instr2_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T
instr2_bsettings[2][0] = instr2_bsettings[2][0]*0.1
# initialise the sweep angle list as well as the sweep limits and directions for each instrument # initialise the sweep angle list as well as the sweep limits and directions for each instrument
instr1_lim, instr2_lim = 'LLIM', 'ULIM' instr1_lim, instr2_lim = 'LLIM', 'ULIM'
instr1_sweep, instr2_sweep = 'DOWN', 'UP' instr1_sweep, instr2_sweep = 'DOWN', 'UP'
@ -730,11 +739,7 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
actual_bval = sep_num_from_units(query_no_echo(instr1, 'IMAG?'))[0]*0.1 actual_bval = sep_num_from_units(query_no_echo(instr1, 'IMAG?'))[0]*0.1
print(f'Actual magnet strength (Bx): {actual_bval} T,', f'Target magnet strength: {Babs} T') print(f'Actual magnet strength (Bx): {actual_bval} T,', f'Target magnet strength: {Babs} T')
# TODO: begin the rotation of the B-field, saving the spectrum for each angle, including the start- and end angles
# NOTE: implement PID control, possibly best option to manage the b field DO THIS LATER ON, WE DO DISCRETE B VALUES RN # NOTE: implement PID control, possibly best option to manage the b field DO THIS LATER ON, WE DO DISCRETE B VALUES RN
# TODO: define the helper functions for threading here, write the commented code inside this function and call the function below + the other
# functionalities in b_val_sweep later on....
# SEE ThreadTest2.py for the helper functions
# Helper function that listens to a device # Helper function that listens to a device
def listen_to_device(device_id, target_value, shared_values, lock, all_targets_met_event): def listen_to_device(device_id, target_value, shared_values, lock, all_targets_met_event):
while not all_targets_met_event.is_set(): # Loop until the event is set while not all_targets_met_event.is_set(): # Loop until the event is set
@ -749,7 +754,7 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
shared_values[device_id] = value shared_values[device_id] = value
# Check if both devices have met their targets # Check if both devices have met their targets
if all(shared_values.get(device) is not None and abs(shared_values[device] - target_value[device]) <= 0.0001 if all(shared_values.get(device) is not None and abs(shared_values[device] - target_value[device]) <= 0.0001
for device in shared_values): # TODO: MODIFY THE IF CONDITIONAL HERE TO THAT IN B VAL SWEEP for device in shared_values):
print(f"Both devices reached their target values: {shared_values}") print(f"Both devices reached their target values: {shared_values}")
all_targets_met_event.set() # Signal that both targets are met all_targets_met_event.set() # Signal that both targets are met
@ -805,13 +810,44 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
print(f"Threads for iteration {iteration+1} closed.\n") print(f"Threads for iteration {iteration+1} closed.\n")
#prints total time the mapping lasted
end_time = time.time()
elapsed_time = (end_time - start_time) / 60
print('Scan time: ', elapsed_time, 'minutes')
# reset both devices to original sweep limits
write_no_echo(instr1, f'LLIM {instr1_bsettings[1][0]*10};ULIM {instr1_bsettings[2][0]*10}') # reset the initial limits of the device after the scan
write_no_echo(instr2, f'LLIM {instr2_bsettings[1][0]*10};ULIM {instr2_bsettings[2][0]*10}') # reset the initial limits of the device after the scan
# reset both devices' initial rates for each range
write_no_echo(instr1, f'RANGE 0 {init_range_lst1[0][0]};RANGE 1 {init_range_lst1[1][0]};RANGE 2 {init_range_lst1[2][0]}') # reset the initial limits of the device after the scan
write_no_echo(instr2, f'RANGE 0 {init_range_lst2[0][0]};RANGE 1 {init_range_lst2[1][0]}') # reset the initial limits of the device after the scan
if zerowhenfin_bool:
write_no_echo(instr1, 'SWEEP ZERO') # if switched on, discharges the magnet after performing the measurement loop above
write_no_echo(instr2, 'SWEEP ZERO')
#save intensity & WL data as .txt
os.chdir('C:/Users/localadmin/Desktop/Users/Lukas')
# creates new folder for MAP data
new_folder_name = "Test_Map_" + f"{datetime.datetime.now().strftime('%Y_%m_%d_%H.%M')}"
os.mkdir(new_folder_name)
# Here the things will be saved in a new folder under user Lukas !
# IMPORTANT last / has to be there, otherwise data cannot be saved and will be lost!!!!!!!!!!!!!!!!
os.chdir('C:/Users/localadmin/Desktop/Users/Lukas/'+ new_folder_name)
intensity_data = np.array(intensity_data)
np.savetxt(Settings + f'{angles[0]}°_to_{angles[-1]}°' + experiment_name +'.txt', intensity_data)
# TODO: remove/edit experiment_name in line above, as well in sweep_b_val func, rn takes a global variable below
wl = np.array(loaded_files.wavelength)
np.savetxt("Wavelength.txt", wl)
# modify cartesian_coords to suite the required data struct in monitor_devices # modify cartesian_coords to suite the required data struct in monitor_devices
cartesian_coords = [{'2301034': t[0], '2101014': t[1]} for t in cartesian_coords] cartesian_coords = [{'2301034': t[0], '2101014': t[1]} for t in cartesian_coords]
# call the helper function to carry out the rotation/measurement of spectrum
monitor_devices(cartesian_coords, angles) monitor_devices(cartesian_coords, angles)
# TODO: add the end functionalities for saving the intensity data (see b-val_sweep), modify if necessary
# SEE LINES 554 TO 577 in code (version on 28.08.2024, 12.49 hrs)
################################################################# END OF FUNCTION DEFS ########################################################################################### ################################################################# END OF FUNCTION DEFS ###########################################################################################