diff --git a/Mag_Field_Sweep_2024_10_21.py b/Mag_Field_Sweep_2024_10_21.py index f75e83b..2099103 100644 --- a/Mag_Field_Sweep_2024_10_21.py +++ b/Mag_Field_Sweep_2024_10_21.py @@ -643,9 +643,12 @@ 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) np.savetxt("B_Values.txt", bval_lst) + +def generate_coord_list_fixed_angle(angle, b_abs, b_abs_step_size): + # TODO: write a function that returns a list of coordinates (x,y) for a fixed angle and a given b_val, b_val_step_size + pass - -def polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True): +def generate_angle_coord_list(radius, start_angle, end_angle, step_size, clockwise=True): # TODO: DOCS """Creates a list of discrete cartesian coordinates (x,y), given the radius, start- and end angles, the angle step size, and the direction of rotation. Function then returns a list of two lists: list of angles and list of cartesian coordinates (x,y coordinates in a tuple). @@ -742,7 +745,8 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R # TODO: possibly rename instr1 and instr2 to the dual and single power supplies respectively?? # defines the folder, in which the data from the spectrometer is temporarily stored in - temp_folder_path = "C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test" + temp_folder_path = "C:/Users/localadmin/Desktop/Users/Lukas/B_Field_Dump" + # temp_folder_path = "C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test" if base_file_name =='': base_file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H.%M') @@ -780,7 +784,7 @@ def b_field_rotation(instr1:pyvisa.resources.Resource, instr2:pyvisa.resources.R instr1_sweep, instr2_sweep = 'DOWN', 'UP' # create lists of angles and discrete Cartesian coordinates - angles, cartesian_coords = polar_to_cartesian(Babs, startangle, endangle, angle_stepsize, clockwise=clockwise) + angles, cartesian_coords = generate_angle_coord_list(Babs, startangle, endangle, angle_stepsize, clockwise=clockwise) if clockwise: # NOTE: old conditional was: startangle > endangle see if this works.... # reverse sweep limits and directions for the clockwise rotation diff --git a/Test.py b/Test.py new file mode 100644 index 0000000..a5f4eff --- /dev/null +++ b/Test.py @@ -0,0 +1,89 @@ +import math + +def polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True): + # TODO: DOCS + """Creates a list of discrete cartesian coordinates (x,y), given the radius, start- and end angles, the angle step size, and the direction of rotation. + Function then returns a list of two lists: list of angles and list of cartesian coordinates (x,y coordinates in a tuple). + + Args: + radius (_type_): _description_ + start_angle (_type_): _description_ + end_angle (_type_): _description_ + step_size (_type_): _description_ + clockwise (bool, optional): _description_. Defaults to True. + + 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 + + if clockwise: + # Clockwise rotation + current_angle = start_angle + while True: + # 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 (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 rotation + current_angle = start_angle + while True: + # 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 (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] + +if __name__=="__main__": + # Example usage + radius = 5 + start_angle = 0 + end_angle = 0 + step_size = 10 + + angles, coordinates = polar_to_cartesian(radius, start_angle, end_angle, step_size, clockwise=True) + + print('\n', "Angles:", angles, '\n') + print("Coordinates:", coordinates, '\n',) \ No newline at end of file diff --git a/ThreadTest.py b/ThreadTest.py new file mode 100644 index 0000000..8d7ff7e --- /dev/null +++ b/ThreadTest.py @@ -0,0 +1,83 @@ +import threading +import time +import random + +# Shared values +device_values = [0, 0] +value_lock = threading.Lock() + +# Per-device pause controls +device_events = [threading.Event(), threading.Event()] +device_events[0].set() # Start as running +device_events[1].set() + +# Tolerance threshold +TOLERANCE = 20 + +# Stop flag +stop_event = threading.Event() + +def is_within_tolerance(val_a, val_b): + return abs(val_a - val_b) <= TOLERANCE + +# Device thread +def device_thread(device_id): + other_id = 1 - device_id + while not stop_event.is_set(): + device_events[device_id].wait() # Pause if needed + + # Simulate value from device + new_value = random.randint(0, 100) + + with value_lock: + device_values[device_id] = new_value + my_val = device_values[device_id] + other_val = device_values[other_id] + + print(f"Device {device_id} => {my_val} | Device {other_id} => {other_val}") + + if not is_within_tolerance(my_val, other_val): + print(f"Device {device_id} is out of tolerance! Pausing...") + device_events[device_id].clear() + + time.sleep(0.1) # Faster check interval + +# Watcher thread +def tolerance_watcher(): + while not stop_event.is_set(): + with value_lock: + val0, val1 = device_values + if is_within_tolerance(val0, val1): + for i, event in enumerate(device_events): + if not event.is_set(): + print(f"Resuming Device {i}") + event.set() + + time.sleep(0.05) # Fast response + +# Start threads +threads = [ + threading.Thread(target=device_thread, args=(0,)), + threading.Thread(target=device_thread, args=(1,)), + threading.Thread(target=tolerance_watcher) +] + +for t in threads: + t.start() + +# Run loop (press Ctrl+C to stop) +try: + while True: + time.sleep(0.1) +except KeyboardInterrupt: + print("Stopping...") + +stop_event.set() +for event in device_events: + event.set() + +for t in threads: + t.join() + +print("All threads stopped.") +