-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeithley6221.py
executable file
·253 lines (191 loc) · 6.85 KB
/
Keithley6221.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Sub programs for operating some Keithley instruments
author : Eoin O'Farrell
email : phyoec@nus.edu.sg
last edited : July 2013
Classes for:
Keithley 6221
InitializeInstruments
ScanInstruments
InitializeDataFile
WriteDataFile
CloseDataFile
GraphData
"""
import rpyc
import visa as visa
import VisaSubs as VisaSubs
import string as string
import re as re
from collections import namedtuple
import time
import math
import numpy as np
import threading
import Queue
######################################################
# At the moment each of the instruments we use is a
# seperate class
#####################################################
class k6221:
def __init__(self, address, compliance = 0.1, analogFilter = True, autorange = True, setupOption = "SAV0", doSetup = False, mode = "Wave", wave = "SIN", frequency = 9.2, amplitude = 10e-8):
# The setup option sets the setup that we use if doSetup is True
self.Address = address
self.Visa = VisaSubs.InitializeGPIB(address,0,term_chars = "\\n")
# Other 6430 properties
self.Compliance = compliance
self.AnalogFilter = analogFilter
self.AutoRange = autorange
self.CurrentRange = currentRange
self.Mode = mode
self.Wave = wave
self.DoSetup = doSetup
self.SetupOption = SetupOption
self.Output = False
self.Frequency = frequency
self.Amplitude = amplitude
if doSetup:
self.Visa.write("*RST")
self.Visa.write("".join(("SYST:POS ",setupOption)))
######################################
# Initialization i.e. writing a load of SCPI
#######################################
def Initialize(self):
# Assume that the source is in Sine mode and that there is no
# offset
# Determine if the output is on or off
Reply = self.Visa.ask("OUTP:STAT?")
self.Output = bool(Reply)
time.sleep(.1)
# if the output is on we now determine the parameters
# amplitude, frequency, compliance ...
if self.Output:
Reply = self.Visa.ask("SOUR:CURR:COMP?")
self.Compliance = float(Reply)
Reply = self.Visa.ask("SOUR:CURR:FILT?")
self.AnalogFilter = bool(Reply)
Reply = self.Visa.ask("SOUR:CURR:RANG:AUTO?")
self.AutoRange = bool(Reply)
if not self.AutoRange:
Reply = self.Visa.ask("SOUR:CURR:RANG?")
self.CurrentRange = float(Reply)
self.Visa.write("".join((":SOUR:FUNC:MODE ",self.Source)))
# Configure the auto zero (reference)
self.Visa.write(":SYST:AZER:STAT ON")
self.Visa.write(":SYST:AZER:CACH:STAT 1")
self.Visa.write(":SYST:AZER:CACH:RES")
# Disable concurrent mode, measure I and V (not R)
self.Visa.write(":SENS:FUNC:CONC 1")
if self.Source == "VOLT":
self.Sense = "CURR"
elif self.Source == "CURR":
self.Sense = "VOLT"
self.Visa.write("".join((":SENS:FUNC:ON ","\"%s\"," % self.Source,"\"%s\"" % self.Sense)))
self.Visa.write("".join((":FORM:ELEM ","%s," % self.Source,"%s" % self.Sense)))
self.Visa.write("".join((":SENS:",self.Sense,":RANG:AUTO 0")))
# Set the complicance
if not SkipCompliance:
self.Visa.write("".join((":SENS:",self.Sense,":RANG 105e-9")))
self.Visa.write("".join((":SENS:",self.Sense,":PROT:LEV %.3e" % self.Compliance)))
# # Set some filters
self.Visa.write("".join((":SENS:",self.Sense,":NPLC %.2f" % self.Integration)))
if not SkipMath:
self.Visa.write(":SENS:AVER:REP:COUN %d" % self.Repetition)
self.Visa.write(":SENS:MED:RANK %d" % self.Median)
self.Visa.write(":SOUR:DEL %.4f" % self.Delay)
self.Visa.write(":TRIG:DEL %.4f" % self.Trigger)
pass
###########################################
# Set the range and compliance
#######################################
def SetRangeCompliance(self, Range = 105, Compliance = 105):
self.Compliance = Compliance
self.Visa.write("".join((":SENS:",self.Sense,":PROT:LEV %.3e" % self.Compliance)))
if Range:
self.Visa.write("".join((":SENS:",self.Sense,":RANG ","%.2e" % Range)))
else:
self.Visa.write("".join((":SENS:",self.Sense,":RANG:AUTO 1")))
pass
##################################################
# Read data
################################################
def ReadWave(self):
Reply = self.Visa.ask("SOUR:WAVE:FREQ?")
self.Frequency = float(Reply)
Reply = self.Visa.ask("SOUR:WAVE:AMPL?")
self.Amplitude = float(Reply)
pass
##################################################
# Set source
##################################################
def SetWave(self,Amp,Freq):
self.Visa.write("SOUR:WAVE:CURR %.4e" % Amp)
self.Visa.write("SOUR:WAVE:FREQ %.4e" % Amp)
pass
#################################################
# Switch the output
###############################################
def SwitchOutput(self):
self.Output = not self.Output
self.Visa.write("".join((":OUTP:STAT ","%d" % self.Output)))
pass
#################################################
# Switch the wave
###############################################
def SwitchWave(self):
self.Output = not self.Output
if self.Output:
self.Visa.write("SOUR:WAVE:ARM")
self.Visa.write("SOUR:WAVE:INIT")
else:
self.Visa.write("SOUR:WAVE:ABOR")
pass
######################################################
# Manual sweep, this sweep will be run as a separate process
# so it doesn't block the program
##################################################
def RunSweep(self,Start,Stop,Step,Wait,Mode = "linear",**kwargs):
#self.Visa.write("".join((":SOUR:",self.Source,":MODE FIX")))
Targets = [Start, Stop]
for kw in kwargs.keys():
if kw == "mid":
Mid = kwargs[kw]
for i in Mid:
Targets.insert(len(Targets)-1,i)
Voltage = [Start]
for i in range(1,len(Targets)):
Points = int(1+abs(Targets[i]-Targets[i-1])/Step)
if Mode == "linear":
Voltage = np.hstack([Voltage,np.linspace(Targets[i-1],Targets[i],num = Points)[1:Points]])
if Mode == "log":
Voltage = np.hstack([Voltage,np.linspace(Targets[i-1],Targets[i],num = Points)[1:Points]])
# self.Visa.write("".join((":SOUR:",self.Source," %.4e" % Voltage[0])))
return Voltage
###################################################
# Print a description string
################################################
def Description(self):
DescriptionString = "Keithley6221"
for item in vars(self).items():
if item[0] == "Frequency" or item[0] == "Amplitude" or item[0] == "Address":
DescriptionString = ", ".join((DescriptionString,"%s = %.3f" % item))
DescriptionString = "".join((DescriptionString,"\n"))
return DescriptionString
############################################
######### Ramp the source to a final value
#########################################
def Ramp(self,Finish):
if self.Output:
self.ReadData()
VStart = self.Data[0]
N = max(100,int(abs(Finish-VStart)/0.1))
VSweep = np.linspace(VStart,Finish,num=N+1)
if not self.Output:
self.SwitchOutput()
for i in range(len(VSweep)):
self.SetSource(VSweep[i])
time.sleep(0.05)
self.ReadData()
return