From 2969541799d290702607f16f78accee6e980d79e Mon Sep 17 00:00:00 2001 From: ryantan Date: Wed, 16 Apr 2025 13:07:35 +0200 Subject: [PATCH] implemented b_rotation_test fully --- b_rotation_test.py | 142 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 124 insertions(+), 18 deletions(-) diff --git a/b_rotation_test.py b/b_rotation_test.py index 2c28750..5721690 100644 --- a/b_rotation_test.py +++ b/b_rotation_test.py @@ -267,7 +267,7 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R # TODO: see if this is the desired process: to always start from the x-axis ASK LUKAS if Babs <= BX_MAX: # 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') + print(f'SWITCHED TO BX, SWEEPING B-X TO {Babs} T NOW') else: raise ValueError(f'{Babs=}T value exceeds the max limit of the Bx field {BX_MAX}T!') @@ -336,6 +336,7 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R thread = threading.Thread(target=listen_to_device, args=(device_id, target, shared_values, lock, all_targets_met_event)) threads.append(thread) thread.start() + print(f"======================\nThread started for device {device_id}\n======================") # Wait until both devices meet their target values all_targets_met_event.wait() @@ -345,26 +346,29 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R thread.join() print(f"Threads for iteration {iteration+1} closed.\n") + + print(f'COLLECTING SPECTRUM FOR ANGLE {angles_lst[iteration]}°\n') # Perform some action after both targets are met # we acquire with the LF - acquire_name_spe = f'{base_file_name}_{angles_lst[iteration]}°' # NOTE: save each intensity file with the given angle - AcquireAndLock(acquire_name_spe) #this creates a .spe file with the scan name. + # acquire_name_spe = f'{base_file_name}_{angles_lst[iteration]}°' # NOTE: save each intensity file with the given angle + # 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(temp_folder_path) #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 + # cwd = os.getcwd() # save original directory + # os.chdir(temp_folder_path) #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(temp_folder_path, acquire_name_spe + '.spe') - os.remove(spe_file_path) + # spe_file_path = os.path.join(temp_folder_path, acquire_name_spe + '.spe') + # os.remove(spe_file_path) points_left = len(angles) - iteration - 1 + 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]) + # intensity_data.append(loaded_files.data[0][0][0]) #prints total time the mapping lasted end_time = time.time() @@ -392,22 +396,124 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R #save intensity & WL data as .txt os.chdir('C:/Users/localadmin/Desktop/Users/Ryan') # 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) + # 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/Ryan/'+ new_folder_name) + # os.chdir('C:/Users/localadmin/Desktop/Users/Ryan/'+ new_folder_name) - intensity_data = np.array(intensity_data) - np.savetxt(Settings + f'{angles[0]}°_to_{angles[-1]}°' + experiment_name +'.txt', intensity_data) + # 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) + # wl = np.array(loaded_files.wavelength) + # np.savetxt("Wavelength.txt", wl) # NOTE: data struct of device_target_values is a list of dictionaries, where each dictionary contains the target values for each device device_target_values = [{'2301034': bval[0], '2101014': bval[1]} for bval in cartesian_coords] # call the helper function to carry out the rotation/measurement of spectrum monitor_devices(device_target_values, angles, intensity_data) - \ No newline at end of file + + +################################################################# END OF FUNCTION DEFS ########################################################################################### + +# NOTE: RYAN INTRODUCED SOME FUNCTIONS HERE TO PERFORM THE SCAN + +# Initialise PYVISA ResourceManager +rm = pyvisa.ResourceManager() +# print(rm.list_resources()) +# 'ASRL8::INSTR' for dual power supply, 'ASRL9::INSTR' for single power supply (online PC) +# 'ASRL10::INSTR' for dual power supply, 'ASRL12::INSTR' for single power supply (offline PC) + +try: + # Open the connection with the APS100 dual power supply + powerbox_dualsupply = rm.open_resource('ASRL10::INSTR', + baud_rate=9600, + data_bits=8, + parity= pyvisa.constants.Parity.none, + stop_bits= pyvisa.constants.StopBits.one, + timeout=10000)# 5000 ms timeout + write_no_echo(powerbox_dualsupply, 'REMOTE') # turn on the remote mode + + # # select axis for the dual supply, either z-axis(CHAN 1 ^= Supply A) or x-axis(CHAN 2 ^= Supply B) + write_no_echo(powerbox_dualsupply, 'CHAN 2') + # # #for dual until here + + # Open the connection with the APS100 single power supply + powerbox_singlesupply = rm.open_resource('ASRL12::INSTR', + baud_rate=9600, + data_bits=8, + parity= pyvisa.constants.Parity.none, + stop_bits= pyvisa.constants.StopBits.one, + timeout=10000)# 5000 ms timeout + write_no_echo(powerbox_singlesupply, 'REMOTE') # turn on the remote mode + #for single until here + # TODO: uncomment AMC connection code later, when moving the probe in cryostat is needed. + # Setup connection to AMC + # amc = AMC.Device(IP) + # amc.connect() + + # # Internally, axes are numbered 0 to 2 + # amc.control.setControlOutput(0, True) + # amc.control.setControlOutput(1, True) + + + # auto = Automation(True, List[String]()) + # experiment = auto.LightFieldApplication.Experiment + # acquireCompleted = AutoResetEvent(False) + + # experiment.Load("2025_03_28_Priyanka_CrSBr_DR_Sweep") + # experiment.ExperimentCompleted += experiment_completed # we are hooking a listener. + # experiment.SetValue(SpectrometerSettings.GratingSelected, '[750nm,1200][0][0]') + # InitializerFilenameParams() + + + #set scan range and resolution in nanometers + range_x = 20000 + range_y = 20000 + resolution = 1000 + # set B-field scan range and resolution (all in T) + set_llim_bval = -0.3 + set_ulim_bval = 0.3 + set_res_bval = 0.003 + + #Here you can specify the filename of the map e.g. put experiment type, exposure time, used filters, etc.... + # 'PL_SP_700_LP_700_HeNe_52muW_exp_2s_Start_' + # experiment_settings = 'PL_X_1859.2_Y_3918.3_HeNe_10.4muW_H_a-axis_LP_SP_650_exp_180s_600g_cwl_930_det_b-axis_Pol_90_l2_45' + experiment_settings = 'DR_white_6th spot_Power_G600_exp_25s_l1_40_l2_262_det_b_mag_b' + #The program adds the range of the scan as well as the resolution and the date and time of the measurement + # f"{set_llim_bval}T_to_{set_ulim_bval}T_{set_res_bval}T_{datetime.datetime.now().strftime('%Y_%m_%d_%H%M')}" + experiment_name = f"{set_llim_bval}T_to_{set_ulim_bval}T_stepsize_{set_res_bval}T" + + # this moves the probe in xy-direction and measures spectrum there + # move_scan_xy(range_x, range_y, resolution, experiment_settings, experiment_name) + + # ramp_b_val(powerbox_singlesupply, 0, 'y-axis') + # ramp_b_val(powerbox_dualsupply, 0, 'z-axis') + + + # for single/ dual replace and vice versa all the way down + # sweep_b_val(powerbox_singlesupply, set_llim_bval, set_ulim_bval, set_res_bval, 'y-axis', + # experiment_settings, experiment_name, zerowhenfin_bool=True, reversescan_bool=False, loopscan_bool=True) + b_field_rotation(powerbox_dualsupply, powerbox_singlesupply, Babs=0.1, startangle=0, endangle=3, + angle_stepsize=1, Settings=experiment_settings, zerowhenfin_bool=True + ) + + write_no_echo(powerbox_dualsupply, 'LOCAL') # turn off the remote mode + write_no_echo(powerbox_singlesupply, 'LOCAL') # turn off the remote mode + + time.sleep(0.5) + # powerbox_dualsupply.close() + powerbox_singlesupply.close() + +except Exception as e: + print(e) + # Internally, axes are numbered 0 to 2 + + write_no_echo(powerbox_dualsupply, 'LOCAL') # turn off the remote mode + write_no_echo(powerbox_singlesupply, 'LOCAL') # turn off the remote mode + + time.sleep(0.5) + powerbox_dualsupply.close() + powerbox_singlesupply.close() \ No newline at end of file