implemented basic logging helper functions for the rotation and angle sweep functions

This commit is contained in:
ryantan 2025-04-23 14:05:04 +02:00
parent 510857d3f2
commit f89e543beb
3 changed files with 52 additions and 11 deletions

View File

@ -1012,14 +1012,12 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R
# call the helper function to carry out the rotation/measurement of spectrum # call the helper function to carry out the rotation/measurement of spectrum
monitor_devices(device_target_values, angles, intensity_data) monitor_devices(device_target_values, angles, intensity_data)
# TODO: implement b_sweep at an arbitrary angle in the Bx-By plane
# copy the functionality of loading the powerboxsupplies from b_rotation and the other sweep functionalities of
# b_sweep_val and implement it in this function
# CHECK: B_abs <= BX_MAX or BY_MAX
def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Resource, def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Resource,
min_bval:float, max_bval:float, res:float, angle:float, min_bval:float, max_bval:float, res:float, angle:float,
Settings:str, clockwise=True, base_file_name='', Settings:str, clockwise=True, base_file_name='',
reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False): reversescan_bool=False, zerowhenfin_bool=False, loopscan_bool=False,
log_folder_path:str=""):
# check if the given limits exceed the max limits of the device # check if the given limits exceed the max limits of the device
if (abs(min_bval) > min(BX_MAX, BY_MAX)) or (abs(max_bval) > min(BX_MAX, BY_MAX)): if (abs(min_bval) > min(BX_MAX, BY_MAX)) or (abs(max_bval) > min(BX_MAX, BY_MAX)):
@ -1060,9 +1058,10 @@ def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Reso
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[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 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 # NOTE: this code block unused, remove later
instr1_lim, instr2_lim = 'LLIM', 'ULIM' # # initialise the sweep angle list as well as the sweep limits and directions for each instrument
instr1_sweep, instr2_sweep = 'DOWN', 'UP' # instr1_lim, instr2_lim = 'LLIM', 'ULIM'
# instr1_sweep, instr2_sweep = 'DOWN', 'UP'
# TODO: see later parts of b_field_rotation from line 820 onwards, and see if same logic can be applied here # TODO: see later parts of b_field_rotation from line 820 onwards, and see if same logic can be applied here
# acquire coordinates along the fixed axis, threading, sweep both supplies till desired value (with lock) # acquire coordinates along the fixed axis, threading, sweep both supplies till desired value (with lock)
@ -1084,9 +1083,6 @@ def sweep_b_angle(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.Reso
write_no_echo(instr2, 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: mod copied code (from b_field_rotation) forsweep_b_angle
# NEED TO MOD THE LOGIC OF listen_to_device FUNCTION LOGIC
# TO SWEEP FROM LOWER TO HIGHER ANGLES, SEE SWEEP_B_VAL FUNCTION
# 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
# Helper function that listens to a device # Helper function that listens to a device

39
Test2.py Normal file
View File

@ -0,0 +1,39 @@
import os
import datetime
# List to accumulate measurement data
measurement_data = []
def append_measurement(target_b_abs, b_x, b_y, measurement_data=measurement_data):
"""Append a single measurement to the global list."""
measurement = {
"Target B_abs": target_b_abs,
"Datetime": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"B_x": b_x,
"B_y": b_y
}
measurement_data.append(measurement)
def save_measurements_to_file(relative_directory, measurement_data=measurement_data):
"""Save accumulated measurements to a file in the specified directory."""
script_dir = os.path.dirname(os.path.abspath(__file__))
directory = os.path.join(script_dir, relative_directory)
os.makedirs(directory, exist_ok=True)
filename = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M") + ".txt"
file_path = os.path.join(directory, filename)
# Write header and data
with open(file_path, 'w') as f:
f.write("Target B_abs, Datetime, B_x, B_y\n")
for entry in measurement_data:
line = f"{entry['Target B_abs']}, {entry['Datetime']}, {entry['B_x']}, {entry['B_y']}\n"
f.write(line)
# Example usage
for i in range(5):
append_measurement(target_b_abs=0.5 + i, b_x=1.0 * i, b_y=2.0 * i)
save_measurements_to_file("Test_Map_" + f"{datetime.datetime.now().strftime('%Y_%m_%d_%H.%M')}")
# print(datetime.datetime.now())

View File

@ -0,0 +1,6 @@
Target B_abs, Datetime, B_x, B_y
0.5, 2025-04-23 14:03:43, 0.0, 0.0
1.5, 2025-04-23 14:03:43, 1.0, 2.0
2.5, 2025-04-23 14:03:43, 2.0, 4.0
3.5, 2025-04-23 14:03:43, 3.0, 6.0
4.5, 2025-04-23 14:03:43, 4.0, 8.0