-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfieldfox_thvisa.py
More file actions
291 lines (216 loc) · 10.2 KB
/
fieldfox_thvisa.py
File metadata and controls
291 lines (216 loc) · 10.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
## general class for common mode independent fieldfox commands ##
Created on Sun May 02 2021
@author: thirschbuechler
"""
import time
import numpy as np # abscissa
# import class not whole module
# - works both internally (called as __main__ f. testing)
# - and externally (called as submodule)
#from thvisa import thInstr as thi
try:
import thvisa as thv # import common functions
from thvisa import thInstr # this should not do anything but otherwise calling subm fails as "name fieldfox not defined"
except:
try:
from thvisa import thvisa as thv # if called as module
except:
print("failed to import module directly or via submodule - mind adding them with underscores not operators (minuses aka dashes, etc.)")
if __name__ == '__main__': # test if called as executable, not as library, regular prints allowed
testing = True
else:
testing = False
class fieldfox(thv.thInstr):
# overwrite class inherited defaults
myprintdef = print
instrnamedef = "TCPIP::K-N9914A-71670.local::inst0::INSTR" # TCPIP: full name since no autodetection
instrnamedef = "USB0::10893::23576::MY57271670::0::INSTR" # USB: not fullname required but whatever
qdelaydef = 0.5
def __init__(self, instrname = instrnamedef, qdelay = qdelaydef, myprint = myprintdef):
## set defaults or overrides as given to init() ##
self.timeout = 10000 # "https://pyvisa.readthedocs.io/en/1.8/resources.html#timeout"
self.instrname=instrname
self.myprint=myprint
self.qdelay=qdelay
#self.role="bla" # NA=VNA, SA=speccy - both need be set by ff_VNA_thvisa or ff_speccy_thvisa children
self.abscissa = []
self.avgs=1 # default since always sent
self.title = None
## call parent init ##
# .. righthand stuff has to be "self." properties and unusually, has no ".self" prefix
super(fieldfox, self).__init__(myprint=myprint,instrname=instrname, qdelay=qdelay, timeout=10000)
self.do_command("*CLS")#clear any old errors
#self.reset() dependent on application - do in subclass or actual script
def __exit__(self, exc_type, exc_value, tb):# "with" context exit: call del
self.unlock()
if "TCPIP" in self.instrname:
self.instr.clear()#clear seems unsupported by USBtmc, wtf
super(fieldfox, self).__exit__( exc_type, exc_value, tb) # self.instr.close() and so on
## main shared functions ##
def setup(self, hard=True, numPoints = 1001, startFreq = 2.4E9, stopFreq = 2.5E9, avgs=1):
""" parent setup class
to be called and appended by ff_VNA or ff_speccy children"""
if hard:
# Preset the FieldFox
self.do_command("SYST:PRES")#"SYST:PRES;*OPC?" # docommand does opc inherited by fieldfox mainclass
# Set mode to VNA
self.do_command("INST:SEL '{}'".format(self.role))
# abscissa s2p files
self.numPoints = numPoints
self.startFreq = startFreq
self.stopFreq = stopFreq
## msr setup ##
self.do_command("SENS:SWE:POIN " + str(self.numPoints))
self.do_command("SENS:FREQ:START " + str(self.startFreq))
self.do_command("SENS:FREQ:STOP " + str(self.stopFreq))
self.set_avgs(avgs)
#self.setup_done = True # in childclasses
def set_avgs(self,avgs):
self.avgs = avgs
self.do_command("AVER:COUNt 1")#reset to invalidate old avgs
self.do_command("AVER:COUNt " + str(self.avgs))
def sweep_reset(self):
""" manually reset avg!
otherwise more than avg count required to get rid of old stuff"""
# self.do_command("INIT:REST")# does not work
# circumvent
#self.do_command("AVER:COUNt 1")# initate restart via reset to 0
#self.do_command("AVER:COUNt 5")
#self.do_command("INIT:IMM")#now dueto
self.set_avgs(self.avgs)#use the setter as it clears as well
def do_sweeps(self, continous="off"):
""" enable trigger and get data into instr memory """
# Set trigger mode to hold for trigger synchronization
#continous="off" # during measurement.. anyway
self.do_command("INIT:CONT "+str(continous)+"")
self.myprint("aquiring data "+str(self.avgs)+" times, acc. to avg")
if self.avgs > 1:
self.sweep_reset()
for i in range(self.avgs): # manually trigger each run for the averaging..
ret = self.do_command("INIT:IMM") # opc baked into do_command
#self.myprint("Single Trigger complete, *OPC? returned : " + ret)
self.myprint("Trig'd({}/{})".format(i+1,self.avgs)) #, *OPC? returned : " + ret)
def make_abscissa(self):
""" fetch f-axis stuff from VNA """
if not self.setup_done:
self.numPoints=self.do_query_string("SENS:SWE:POIN?")
self.startFreq=self.do_query_string("SENS:FREQ:START?")
self.stopFreq=self.do_query_string("SENS:FREQ:STOP?")
# build
self.abscissa = np.linspace(float(self.startFreq),float(self.stopFreq),int(self.numPoints))
# notes
#SCPI "SENSe:X?" probably unsupported
# however Read X-axis values possible via- [:SENSe]:FREQuency:DATA?
## other helpers ##
# frontpanel input access control
def lock(self,state=1):
if state:
self.do_command("INST:GTR")
else:
self.do_command("INST:GTL")
def unlock(self, state=1):
self.lock(not state)
# reset the instrument to the known default setup #
# same as check_instrument_errors()?
def errcheck(self):
myError = []
ErrorList = self.instr.query("SYST:ERR?").split(',')
Error = ErrorList[0]
if int(Error) == 0:
self.myprint ("+0, No Error!")
else:
while int(Error)!=0:
self.myprint ("Error #: " + ErrorList[0])
self.myprint ("Error Description: " + ErrorList[1])
myError.append(ErrorList[0])
myError.append(ErrorList[1])
ErrorList = self.instr.query("SYST:ERR?").split(',')
Error = ErrorList[0]
myError = list(myError)
return myError
def do_command(self, cmd, qdelay=None, OPC=1):
if qdelay==None:
qdelay=self.qdelay
#super(fieldfox, self).do_command(cmd)
if OPC: # 99% of all commands will cause "Query unterminated" without this
cmd+=";*OPC?"
self.instr.write(cmd)
time.sleep(qdelay)
return self.instr.read()
# $ test invinivision stuff
# $$ what happens to cal?
# alternatively, MMEMory:LOAD:STATe "autosave1.sta"
# or something like that
# store instrument setup state in script dir #
def store_setup(self, filename="setup.sta"):
#sSetup =self.do_query_ieee_block(":SYSTem:SETup?")
#f = open(filename, "wb")
#f.write(sSetup)
#f.close()
#self.myprint("Setup bytes saved: %d" % len(sSetup))
pass
# load instrument setup state from script dir #
def load_setup(self, filename="setup.sta"):
#f = open(filename, "rb")
#sSetup = f.read()
#f.close()
#self.do_command_ieee_block(":SYSTem:SETup", sSetup)
#self.myprint("Setup bytes restored: %d" % len(sSetup))
pass
def center_on_peak_marker(self, nr=1, trace=1, center=True, settlet=0.3):
""" make marker, goto peak, save values, center , ret values
options
- nr: marker nr
- trace (NA): UNTESTED which trace to perform on?
- settletime (s): 1x/10x sweep time (NA/SA) i guess, defaults 0.3s
"""
#make marker
self.do_command("CALC:MARK{}:ACTivate".format(nr)) # enough to create & activate
#self.do_command("CALC:MARK{}:Norm".format(nr)) # fails
#set type
#self.do_command("CALCulate[:SELected]:MARKer:FORMat <char>")
if self.role=="NA":# untested
self.do_command("CALC:MARK{}:TRAC {}".format(nr,trace))
#lock to maximum
time.sleep(settlet)#settle needed!! otherwise too fast sometimes
self.do_command("CALC:MARK{}:FUNC:MAX".format(nr))#(SA/NA no query)
#q = self.do_query_string("CALC:MARK:FUNC:PEXC?")# reads peak default id val
#read marker values x/y (pg 226)
X = self.do_query_string("CALC:MARK{}:X?".format(nr))
Y = self.do_query_string("CALC:MARK{}:Y?".format(nr))
if center:
#read new center freq from marker
self.do_command("CALC:MARK{}:SET:CENT".format(nr))
return X,Y
def ff_title(self, title=None):
if title!=None:
self.do_command(str("DISPlay:TITLe:DATA \'"+title+"\'"))
self.do_command("disp:TITL 1")
self.title = title
else:
self.do_command("disp:TITL 0")
def askandlog(self, thing):
return(thing+" "+self.do_query_string(thing))
def query_setup(self):
log=[]
log.append(self.askandlog("SENS:SWE:POIN?"))
log.append(self.askandlog("SENS:FREQ:START?"))
log.append(self.askandlog("SENS:FREQ:STOP?"))
log.append(self.askandlog("AVER:COUNt?"))
if self.role=="NA":
log.append(self.askandlog("BWID?"))
log.append(self.askandlog("SOUR:POW:ALC?"))
log.append(self.cal_str()) # ask usercal and report # HACK - calls fct defined in NA class variant (child)
else: # SA speccy
log.append(self.askandlog("SENS:FREQ:SPAN?"))
log.append("all properties of class:")
log.append(str(vars(self)))
return log
#### test this library using semi Unit Testing ####
if __name__ == '__main__': # test if called as executable, not as library, regular prints allowed
with fieldfox() as myff: # make object, autodispose afterward with-end, read IDN
myff.errcheck() # because why not
myff.ff_title("..testing general fieldfox class..")