From 5c01110ce5baf8edd1eacfb37a8976bd5414ad12 Mon Sep 17 00:00:00 2001 From: rtan Date: Tue, 27 Aug 2024 15:51:24 +0200 Subject: [PATCH] FIXED:C bug polar_to_cartesian --- 20240709SerdarModScript.py | 149 +++++++++++++++++++++++++++++++------ 1 file changed, 125 insertions(+), 24 deletions(-) diff --git a/20240709SerdarModScript.py b/20240709SerdarModScript.py index 2709b28..1c83686 100644 --- a/20240709SerdarModScript.py +++ b/20240709SerdarModScript.py @@ -576,9 +576,83 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float, wl = np.array(loaded_files.wavelength) np.savetxt("Wavelength.txt", wl) +# TODO: old function, DELETE LATER +# def polar_to_cartesian(radius, start_angle, end_angle, step_size): +# """_summary_ -def polar_to_cartesian(radius, start_angle, end_angle, step_size): - # TODO: docs +# 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] + +def polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True): + # TODO: DOCS """_summary_ Args: @@ -586,6 +660,7 @@ def polar_to_cartesian(radius, start_angle, end_angle, step_size): start_angle (_type_): _description_ end_angle (_type_): _description_ step_size (_type_): _description_ + clockwise (bool, optional): _description_. Defaults to True. Returns: _type_: _description_ @@ -598,15 +673,10 @@ def polar_to_cartesian(radius, start_angle, end_angle, step_size): 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 + if clockwise: + # Clockwise rotation current_angle = start_angle - while current_angle >= end_angle: + while True: # Append the current angle to the angles list angles.append(current_angle % 360) @@ -620,16 +690,18 @@ def polar_to_cartesian(radius, start_angle, end_angle, step_size): # 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: + # Check if we've reached the end_angle (handling wrap-around) (current_angle - step_size) % 360 == end_angle or + if current_angle % 360 == end_angle: break # Decrement the current angle by the step size current_angle -= step_size + if current_angle < 0: + current_angle += 360 else: - # Counterclockwise is shorter + # Counterclockwise rotation current_angle = start_angle - while current_angle <= end_angle: + while True: # Append the current angle to the angles list angles.append(current_angle % 360) @@ -643,12 +715,14 @@ def polar_to_cartesian(radius, start_angle, end_angle, step_size): # 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: + # Check if we've reached the end_angle (handling wrap-around) (current_angle + step_size) % 360 == end_angle or + if current_angle % 360 == end_angle: break # Increment the current angle by the step size current_angle += step_size + if current_angle >= 360: + current_angle -= 360 return [angles, coordinates] @@ -695,19 +769,13 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R # 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 + angles, cartesian_coords = polar_to_cartesian(Babs, startangle, endangle, angle_stepsize) # create 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) + # reverse sweep limits and directions for the clockwise rotation 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 # the max recommended current. init_range_lst1 = list(sep_num_from_units(el) for el in query_no_echo(instr1, 'RATE? 0;RATE? 1;RATE? 2').split(';')) @@ -722,8 +790,41 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R 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') + # wait for Babs to be reached by the Bx field + actual_bval = sep_num_from_units(query_no_echo(instr1, 'IMAG?'))[0]*0.1 # convert kG to T + print(f'Actual magnet strength (Bx): {actual_bval} T,', f'Target magnet strength: {Babs} T') + while abs(actual_bval - Babs) > 0.0001: + time.sleep(5) # little break + 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') + # 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 + # TODO: polar_to_cartesian does not work when the angle is 0°!!!! fix bug + # TODO: write the for loop for the rotation here + for idx, angle in enumerate(angles): + pass + + # # we acquire with the LF + # acquire_name_spe = f'{base_file_name}_{bval}T' + # AcquireAndLock(acquire_name_spe) #this creates a .spe file with the scan name. + + # # read the .spe file and get the data as loaded_files + # cwd = os.getcwd() # save original directory + # os.chdir(path_save) #change directory + # loaded_files = sl.load_from_files([acquire_name_spe + '.spe']) # get the .spe file as a variable + # os.chdir(cwd) # go back to original directory + + # # Delete the created .spe file from acquiring after getting necessary info + # spe_file_path = os.path.join(path_save, acquire_name_spe + '.spe') + # os.remove(spe_file_path) + + # points_left = total_points - i - 1 # TODO: SEE IF THIS IS CORRECT + # print('Points left in the scan: ', points_left) + + # #append the intensity data as it is (so after every #of_wl_points, the spectrum of the next point begins) + # intensity_data.append(loaded_files.data[0][0][0]) ################################################################# END OF FUNCTION DEFS ###########################################################################################