Compare commits

..

2 Commits

Author SHA1 Message Date
11428120b9 delete test comment 2024-07-12 17:11:14 +02:00
034c1e4d33 Test 2024-07-12 17:10:17 +02:00

View File

@ -8,7 +8,6 @@ Lightfield + Positioner
# Packages from Ryan
import re
import pyvisa
import threading
# from pyvisa import ResourceManager, constants
# B Field Limits (in T)
@ -32,7 +31,6 @@ from System import String
import numpy as np
import matplotlib.pyplot as plt
import datetime
from typing import Union
#First choose your controller
@ -353,22 +351,19 @@ def write_no_echo(instr:pyvisa.resources.Resource, command:str, sleeptime=0.01)-
except pyvisa.VisaIOError as e:
print(f"Error communicating with instrument: {e}")
# TODO: implement the reverse scan and zero when finish functionality
# 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.
# TODO: add a param to allow the
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="C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test",
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,
with the given resolution. For each value, a measurement of the spectrum of the probe in the cryostat is made, using the LightField spectrometer.
res:float, Settings:str, base_file_name='', path_save="C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test",
singlepowersupply_bool=False, reversescan_bool=False, zerowhenfin_bool=False)->None:
""" 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, with the given resolution. For each value, a measurement of the spectrum
of the probe in the cryostat is made, using the LightField spectrometer.
Args:
instr (pyvisa.resources.Resource): chosen power supply device to connect to
min_bval (float): min B value of the scan (please input in units of Tesla)
max_bval (float): max B value of the scan (please input in units of Tesla)
res (float): resolution of the list of B values (please input in units of Tesla)
magnet_coil (str): select magnet coil to be used. String should be 'x-axis','y-axis' or 'z-axis'.
Settings (str): experiment settings, included in file name.
base_file_name (str, optional): base file name. Defaults to ''.
path_save (str, optional): file path where the file will be saved. Defaults to "C:/Users/localadmin/Desktop/Users/Lukas/2024_02_08_Map_test".
@ -380,69 +375,31 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
ValueError: when By limit is exceeded.
ValueError: when Bz limit is exceeded.
ValueError: when Bx limit is exceeded.
ConnectionError: when no device is connected.
""" ''''''
def pyramid_list(lst) -> Union[list, np.ndarray]:
"""reverses the list and removes the first element of reversed list. Then, this is appended to
the end of the original list and returned as the 'pyramid' list.
Args:
lst (list or np.ndarray):
Raises:
TypeError: if the input object isn't a list or np.ndarray
Returns:
Union[list, np.ndarray]: the pyramid list
""" ''''''
if isinstance(lst, list):
return lst + lst[-2::-1]
elif isinstance(lst, np.ndarray):
return np.append(lst, lst[-2::-1])
else:
raise TypeError('Please input a list!')
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
instr_info = query_no_echo(instr, '*IDN?')
start_time = time.time()
instr_bsettings = list(sep_num_from_units(el) for el in query_no_echo(instr, 'UNITS?;LLIM?;ULIM?').split(';')) # deliver a 3 element tuple of tuples containing the set unit, llim and ulim
if instr_bsettings[0][0] == 'T':
instr_bsettings[1][0] = instr_bsettings[1][0]*0.1 # rescale kG to T, device accepts values only in kG or A, eventho we set it to T
instr_bsettings[2][0] = instr_bsettings[2][0]*0.1
# if singlepowersupply_bool: # checks limits of Bx or By
# if (min_bval< -BY_MAX) or (max_bval > BY_MAX):
# raise ValueError('Input limits exceed that of the magnet By! Please input smaller limits.')
# elif '1' in query_no_echo(instr, 'CHAN?'): # check if its the coils for Bz
# if (min_bval < -BZ_MAX) or (max_bval > BZ_MAX):
# raise ValueError('Input limits exceed that of the magnet (Bz)! Please input smaller limits.')
# else: # checks limits of Bx
# if (min_bval< -BX_MAX) or (max_bval > BX_MAX):
# raise ValueError('Input limits exceed that of the magnet Bx! Please input smaller limits.')
if '2101014' in instr_info and (magnet_coil=='y-axis'): # single power supply
if singlepowersupply_bool: # checks limits of By
if (min_bval< -BY_MAX) or (max_bval > BY_MAX):
raise ValueError('Input limits exceed that of the magnet By! Please input smaller limits.')
elif '2301034' in instr_info: # dual power supply
if magnet_coil=='z-axis': # check if its the coils for Bz
if (min_bval < -BZ_MAX) or (max_bval > BZ_MAX):
raise ValueError('Input limits exceed that of the magnet (Bz)! Please input smaller limits.')
write_no_echo(instr, 'CHAN 1')
elif magnet_coil=='x-axis': # checks limits of Bx
if (min_bval< -BX_MAX) or (max_bval > BX_MAX):
raise ValueError('Input limits exceed that of the magnet Bx! Please input smaller limits.')
write_no_echo(instr, 'CHAN 2')
else:
raise ConnectionError('Device is not connected!')
elif '1' in query_no_echo(instr, 'CHAN?'): # check if its the coils for Bz
if (min_bval < -BZ_MAX) or (max_bval > BZ_MAX):
raise ValueError('Input limits exceed that of the magnet (Bz)! Please input smaller limits.')
else: # checks limits of Bx
if (min_bval< -BX_MAX) or (max_bval > BX_MAX):
raise ValueError('Input limits exceed that of the magnet Bx! Please input smaller limits.')
write_no_echo(instr, f'LLIM {min_bval*10};ULIM {max_bval*10}') # sets the given limits, must convert to kG for the device to read
bval_lst = np.arange(min_bval, max_bval + res, res) # creates list of B values to measure at, with given resolution, in T
# TODO: unused, see if can remove
# init_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1 # queries the initial B value of the coil, rescale from kG to T
init_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1 # queries the initial B value of the coil, rescale from kG to T
init_lim, subsequent_lim = 'LLIM', 'ULIM'
init_sweep, subsequent_sweep = 'DOWN', 'UP'
@ -460,18 +417,24 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
init_lim, subsequent_lim = subsequent_lim, init_lim
init_sweep, subsequent_sweep = subsequent_sweep, init_sweep
if loopscan_bool:
bval_lst = pyramid_list(bval_lst)
total_points = len(bval_lst)
middle_index_bval_lst = total_points // 2
intensity_data = [] # To store data from each scan
cwd = os.getcwd() # save original directory
#This gives a directory, in which the script will save the spectrum of each spot as spe
#However, it will open the spectrum, convert it to txt, add it to the intensity_data and delete the spe file
#scanning loop
for i, bval in enumerate(bval_lst):
# NOTE: helper function for the scanning loop
def helper_scan_func(idx, bval, instr=instr, init_lim=init_lim, init_sweep=init_sweep,
subsequent_lim=subsequent_lim, subsequent_sweep=subsequent_sweep, sleep=5):
if idx == 0: # for first iteration, sweep to one of the limits
# if init_bval == bval:
# # if initial bval is equal to the element of the given iteration from the bval_lst, then commence measuring the spectrum
# pass
# else:
# TODO: improve the conditional block later on... try to shorten the number of conditionals needed/flatten the nested conditionals
# else, travel to the lower or higher limit, depending on how far the init val is to each bound, and commence the measurement from there on
# if not reversescan_bool:
if i == 0: # for first iteration, sweep to one of the limits
write_no_echo(instr, f'{init_lim} {bval*10}') # convert back to kG
write_no_echo(instr, f'SWEEP {init_sweep}')
else:
@ -486,40 +449,6 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1
# update the actual bval
print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
#scanning loop
for i, bval in enumerate(bval_lst):
# if init_bval == bval:
# # if initial bval is equal to the element of the given iteration from the bval_lst, then commence measuring the spectrum
# pass
# else:
# NOTE: original code without the loop scan
################################################
# if i == 0: # for first iteration, sweep to one of the limits
# write_no_echo(instr, f'{init_lim} {bval*10}') # convert back to kG
# write_no_echo(instr, f'SWEEP {init_sweep}')
# else:
# write_no_echo(instr, f'{subsequent_lim} {bval*10}') # convert back to kG
# write_no_echo(instr, f'SWEEP {subsequent_sweep}')
# actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1 # convert kG to T
# print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
# while abs(actual_bval - bval) > 0.0001:
# time.sleep(5) # little break
# actual_bval = sep_num_from_units(query_no_echo(instr, 'IMAG?'))[0]*0.1
# # update the actual bval
# print(f'Actual magnet strength: {actual_bval} T,', f'Target magnet strength: {bval} T')
###############################################
if not loopscan_bool:
helper_scan_func(i, bval)
else:
if i <= middle_index_bval_lst:
helper_scan_func(i, bval)
else:
helper_scan_func(i, bval, instr=instr, init_lim=subsequent_lim, init_sweep=subsequent_sweep,
subsequent_lim=init_lim, subsequent_sweep=init_sweep, sleep=5)
time.sleep(5)
# we acquire with the LF
@ -547,8 +476,6 @@ def sweep_b_val(instr:pyvisa.resources.Resource, min_bval:float, max_bval:float,
elapsed_time = (end_time - start_time) / 60
print('Scan time: ', elapsed_time, 'minutes')
write_no_echo(instr, f'LLIM {instr_bsettings[1][0]*10};ULIM {instr_bsettings[2][0]*10}') # reset the initial limits of the device after the scan
if zerowhenfin_bool:
write_no_echo(instr, 'SWEEP ZERO') # if switched on, discharges the magnet after performing the measurement loop above
@ -587,9 +514,8 @@ powerbox_dualsupply = rm.open_resource('ASRL8::INSTR',
write_no_echo(powerbox_dualsupply, 'REMOTE') # turn on the remote mode
# TODO: test functionality of the magnet_coil param later on, should work... as this code below is basically implemented inside the scan func.
# 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 1')
write_no_echo(powerbox_dualsupply, 'CHAN 1')
# Setup connection to AMC
amc = AMC.Device(IP)
@ -624,13 +550,17 @@ experiment_settings = 'PL_SP_700_LP_700_HeNe_52muW_exp_2s_Start_'
#The program adds the range of the scan as well as the resolution and the date and time of the measurement
experiment_name = f"{set_llim_bval}T_to_{set_ulim_bval}T_{set_res_bval}T_{datetime.datetime.now().strftime('%Y_%m_%d_%H%M')}"
# # TODO: write the bval scan here
# for idx, bval in enumerate(bval_lst):
# write_no_echo(powerbox_dualsupply, '')
# this moves the probe in xy-direction and measures spectrum there
# move_scan_xy(range_x, range_y, resolution, experiment_settings, experiment_name)
# perform the B-field measurement for selected axis above
# sweep_b_val(powerbox_dualsupply, set_llim_bval, set_ulim_bval, set_res_bval, experiment_settings, experiment_name)
sweep_b_val(powerbox_dualsupply, set_llim_bval, set_ulim_bval, set_res_bval, 'z-axis',
experiment_settings, experiment_name, zerowhenfin_bool=True, reversescan_bool=False)
sweep_b_val(powerbox_dualsupply, set_llim_bval, set_ulim_bval, set_res_bval,
experiment_settings, experiment_name, singlepowersupply_bool=False, zerowhenfin_bool=True, reversescan_bool=False)
# Internally, axes are numbered 0 to 2