diff --git a/20240709SerdarModScript.py b/20240709SerdarModScript.py index 4b8d01b..9897f88 100644 --- a/20240709SerdarModScript.py +++ b/20240709SerdarModScript.py @@ -7,6 +7,7 @@ Lightfield + Positioner ############################################ # Packages from Ryan import re +import math from threading import Thread import pyvisa # from pyvisa import ResourceManager, constants @@ -303,6 +304,7 @@ def sep_num_from_units(powerbox_output :str)->list: else: return [powerbox_output,] + def query_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0)->str: """helper function for the Attocube APS100 that queries a function to the device, removing the echo. @@ -327,6 +329,7 @@ def query_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0)->st print(f"Error communicating with instrument: {e}") return None + def write_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0)->str: """helper function for the Attocube APS100 that writes a function to the device, removing the echo. @@ -353,11 +356,12 @@ def write_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0)->st except pyvisa.VisaIOError as e: print(f"Error communicating with instrument: {e}") + # receive values in units of T, rescale in kg to talk with the power supplyy. 1T = 10kG # NOTE: removed singlepowersupply_bool, reading serial-nr. of the device instead. # old save folder: "C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test" def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float, - res:float, magnet_coil:str, Settings:str, base_file_name='', path_save=None, + res:float, magnet_coil:str, Settings:str, base_file_name='', path_save='', reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False)->None: # TODO: update docs in the end """ this function performs a sweep of the B field of the chosen magnet coil. It creates a list o B values from the given min and max values, @@ -401,7 +405,7 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float, else: raise TypeError('Please input a list!') - if path_save is None: + if path_save =='': path_save = datetime.datetime.now().strftime("%Y_%m_%d_%H%M_hrs_") if base_file_name =='': @@ -573,34 +577,114 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float, np.savetxt("Wavelength.txt", wl) +def create_discrete_b_field_list(radius, start_angle, end_angle, step_size): + # TODO: docs + """_summary_ + + Args: + radius (_type_): _description_ + start_angle (_type_): _description_ + end_angle (_type_): _description_ + step_size (_type_): _description_ + + Returns: + _type_: _description_ + """ """""" + # Initialize lists to hold angles and (x, y) pairs + angles = [] + coordinates = [] + + # Normalize angles to the range [0, 360) + start_angle = start_angle % 360 + end_angle = end_angle % 360 + + # Calculate the clockwise and counterclockwise differences + clockwise_diff = (start_angle - end_angle) % 360 + counterclockwise_diff = (end_angle - start_angle) % 360 + + # Determine the shorter path + if clockwise_diff <= counterclockwise_diff: + # Clockwise is shorter + current_angle = start_angle + while current_angle >= end_angle: + # Append the current angle to the angles list + angles.append(current_angle % 360) + + # Convert the current angle to radians + current_angle_rad = math.radians(current_angle % 360) + + # Convert polar to Cartesian coordinates + x = radius * math.cos(current_angle_rad) + y = radius * math.sin(current_angle_rad) + + # Append the (x, y) pair to the list + coordinates.append((x, y)) + + # Check if we've reached the end_angle + if (current_angle ) % 360 == end_angle: + break + + # Decrement the current angle by the step size + current_angle -= step_size + else: + # Counterclockwise is shorter + current_angle = start_angle + while current_angle <= end_angle: + # Append the current angle to the angles list + angles.append(current_angle % 360) + + # Convert the current angle to radians + current_angle_rad = math.radians(current_angle % 360) + + # Convert polar to Cartesian coordinates + x = radius * math.cos(current_angle_rad) + y = radius * math.sin(current_angle_rad) + + # Append the (x, y) pair to the list + coordinates.append((x, y)) + + # Check if we've reached the end_angle + if (current_angle ) % 360 == end_angle: + break + + # Increment the current angle by the step size + current_angle += step_size + + 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, - Babs:float, startangle:float, endangle:float, angle_stepsize:float, path_save:str, base_file_name:str, anticlockwise=True, sweepdown=False)->None: - """_summary_ + Babs:float, startangle:float, endangle:float, angle_stepsize:float, path_save='', base_file_name='', sweepdown=False)->None: + """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. Args: instr1 (pyvisa.resources.Resource): _description_ instr2 (pyvisa.resources.Resource): _description_ Babs (float): absolute B-field value in T - startangle (float): _description_ - endangle (float): _description_ - angle_stepsize (float): _description_ - anticlockwise (bool, optional): _description_. Defaults to True. - sweepdown (bool, optional): _description_. Defaults to False. + startangle (float): start angle in degrees + endangle (float): end angle in degrees + angle_stepsize (float): angle step size in degrees + sweepdown (bool, optional): after finishing the rotation, both B-field components should be set to 0 T. Defaults to False. """ - if path_save is None: + 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 - if base_file_name =='': base_file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H.%M') start_time = time.time() # start of the scan function - idnstr1 = query_no_echo(instr1, '*IDN?') + startangle = startangle % 360 + endangle = endangle % 360 # ensures that the angles are within [0,360) + idnstr1 = query_no_echo(instr1, '*IDN?') idnstr2 = query_no_echo(instr1, '*IDN?') + intensity_data = [] # To store data from each scan + cwd = os.getcwd() # save original directory + # TODO: 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 pass @@ -608,6 +692,20 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R # swap instruments, instr 1 to be the dual power supply instr1, instr2 = instr2, instr1 + # initialise the sweep angle list as well as the sweep limits and directions for each instrument + instr1_lim, instr2_lim = 'LLIM', 'ULIM' + instr1_sweep, instr2_sweep = 'DOWN', 'UP' + angles_lst = np.arange(startangle, endangle + angle_stepsize, angle_stepsize) # TODO: check to see if this considers 0° crossover or not + # TODO: this does not consider 0° crossover, try to fix + anticlockwise_bool = True + + if startangle > endangle: + # reverse list of angles, as well as the sweep limits and directions, for clockwise rotation + angles_lst = np.arange(startangle, endangle - angle_stepsize, - angle_stepsize) + instr1_lim, instr2_lim = instr2_lim, instr1_lim + instr1_sweep, instr2_sweep = instr2_sweep, instr1_sweep + anticlockwise_bool = False + # TODO: compare which device has the lower rates, save initial rates lists, then set both devices to the lower rates for each range # then, set the initial values back # list of rates (with units) for diff ranges of each device, only up to Range 1 for single power supply as that is already @@ -617,14 +715,14 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R min_range_lst = [min(el1[0], el2[0]) for el1,el2 in zip(init_range_lst1, init_range_lst2)] # min rates for each given range + # set both devices to the min rates write_no_echo(instr1, f'RATE 0 {min_range_lst[0]};RATE 1 {min_range_lst[1]}') write_no_echo(instr2, f'RATE 0 {min_range_lst[0]};RATE 1 {min_range_lst[1]}') - # TODO: check the device rates and ranges in the lab tmrw or friday - write_no_echo(instr1, f'CHAN 2;ULIM {Babs*10};SWEEP UP') # sets to B_x, the B_x upper limit and sweeps the magnet field to the upper limit + print(f'SWEEPING B-X TO {Babs} T NOW') + - # TODO: include the functionalities located in the function above, update function header with the new parameters of path_save, base_file_name ################################################################# END OF FUNCTION DEFS ###########################################################################################