如何在一个命令中有效地设置电压扫描? Python

How to set a voltage sweep effectively in just one command? Python

我正在尝试使用 PyVisa 和 Keithley 6430 进行电流电压扫描,做一个完整的电压循环,从零到正值和负值:从 0 到 +5,回到 0,从 0 到-5 并再次回到 0。

为此,我基本上使用了 4 个不同的范围和 4 个不同的 for 循环,如下所示:

 #Voltage values
 low = -5
 middle = 0
 high = 5
 step = 1

 voltage_range1 = np.arange(middle,high,step)
 voltage_range2 = np.arange(high,middle,-step)
 voltage_range3 = np.arange(middle,low,-step)
 voltage_range4 = np.arange(low,middle+1,step)

 data = [] 

 for voltage in voltage_range1:
 keithley.write('source:voltage:level {}'.format(voltage)) 
 data.append(keithley.query_ascii_values('read?'))

 for voltage in voltage_range2:
 keithley.write('source:voltage:level {}'.format(voltage)) 
 data.append(keithley.query_ascii_values('read?'))

 for voltage in voltage_range3:
 keithley.write('source:voltage:level {}'.format(voltage)) 
 data.append(keithley.query_ascii_values('read?'))

 for voltage in voltage_range4:
 keithley.write('source:voltage:level {}'.format(voltage)) 
 data.append(keithley.query_ascii_values('read?'))

该程序运行正常,但我找不到更实用的方法来执行此操作,例如将整个过程设置为一个 voltage_range 并避免使用4个循环。你有什么想法?

我认为这可行:

voltage_range = np.array([]) # initialize

for array in (voltage_range1,
              voltage_range2,
              voltage_range3,
              voltage_range4)
    voltage_range = np.append(voltage_range, array)

然后你只有一个循环遍历 voltage_range。您也可以直接将 voltage_rangeN 替换为 np.arange() 并且基本上您可以通过更改括号中的位置来创建您想要的任何值序列。

所以修改后你的程序看起来像这样:

# Voltage values
low = -5
middle = 0
high = 5
step = 1

data = [] 
voltage_range = np.array([]) # initialize

for array in (np.arange(middle,high,step),
              np.arange(high,middle,-step),
              np.arange(middle,low,-step),
              np.arange(low,middle+1,step)):
    voltage_range = np.append(voltage_range, array)

for voltage in voltage_range:
    keithley.write('source:voltage:level {}'.format(voltage)) 
    data.append(keithley.query_ascii_values('read?'))

您也可以合并范围 2 和 3:

for array in (np.arange(middle,high,step),
              np.arange(high,low-1,-step),
              np.arange(low,middle+1,step)):