-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotator-VNA.py
executable file
·286 lines (230 loc) · 8.8 KB
/
rotator-VNA.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
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
# Control the rotator and download data from a VNA
# Andrew Temme
# temmeand@gmail.com
# updated 2015-11-4
# currently written for HP 8753
from __future__ import division
import visa
import numpy as np
from datetime import datetime
from os import path, makedirs
import serial
from time import sleep
from math import trunc
# import skrf
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
def openRotatorPort(portNum=0, timeout=5):
"""
Open a serial port for the rotator
Open commport ``portNum`` with a timeout of ``timeout``.
Parameters
----------
portNum : integer, default: 0
commport for the serial connection to the rotator
timeout : number, default: 5 sec
timeout for the commport
Returns
-------
port : serial port object thingy
object that you use to communicate now with the serial port.
"""
ser = serial.Serial(0,timeout=5)
return ser
def advanceRotator(deg):
"""
Advance the rotator ``deg`` degrees
Parameters
----------
deg : number
how many degrees to advance the rotator
Returns
-------
"""
stepsPerRotation = 508000
gearRatio = 6
stepsPerDeg = stepsPerRotation * gearRatio
steps = stepsPerDeg * deg
raise NotImplementedError("Write this function!")
def startRotatorTurning(rpm):
"""
Start the rotator turning at a set RPM
Parameters
----------
rpm : number
RPM for the rotator
Return
------
"""
# gear ratio for rotator in the antenna chamber and arch range
gearRatio = 6
# gear ratio for APS Design Competition turntable
# gearRatio=2.446
delay = 0.01
print "start"
speed=1/(rpm/60*400)/2.04e-6/gearRatio
print speed
speed = trunc(round(speed))
print speed
print str(speed)
ser.write('BD0\r')
sleep(delay)
ser.write('SH\r')
sleep(delay)
ser.write('SO\r')
sleep(delay)
ser.write('SA10\r')
sleep(delay)
ser.write('SM2000\r')
sleep(delay)
ser.write('SD'+str(speed)+'\r')
sleep(delay)
ser.write('H+\r')
def stopRotator():
"""
Stop the rotator
"""
ser.write('H0\r')
ser.close()
print "Port closed"
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
print "start"
# open the rotator
openRotatorPort(0, 5)
# start the rotator
# startRotatorTurning(16)
#------------------------------------------------------------------------------
dataPrefix = 'carpet-foam'
#------------------------------------------------------------------------------
# number of averages
numGrps = 4
# GPIB address for the VNA set below
ena = visa.instrument('GPIB::16', timeout = 120)
idn = ena.ask('*IDN?')
print idn
optLine = "# Hz S RI R 50"
cmd8753D = {\
'basicInit':'HOLD;DUACOFF;CHAN1;S11;LOGM;CONT;AUTO',\
'corrQ':'CORR?',\
'freqSpanQ':'SPAN?',\
'freqStartQ':'STAR?',\
'freqStopQ':'STOP?',\
'getImag':'IMAG;OUTPFORM',\
'getLinMag':'LINM;OUTPFORM',\
'getLogMag':'LOGM;OUTPFORM',\
'getPhase':'PHAS;OUTPFORM',\
'getReal':'REAL;OUTPFORM',\
'hold':'HOLD',\
'IDStr':'HEWLETT PACKARD,8753D,0,6.14',\
'ifbwQ':'IFBW?',\
'numPtsQ':'POIN?',\
'powerQ':'POWE?',\
'preset':'PRES',\
'numGroups':'NUMG',\
'polar':'POLA',\
's11':'S11',\
's21':'S21',\
's12':'S12',\
's22':'S22'\
}
cmdDict = cmd8753D
#------------------------------------------------------------------------------
ena.write('form4')
# number of points
ena.write('POIN1601')
numPts = ena.ask_for_values(cmdDict['numPtsQ'])[0]
freqStart = ena.ask_for_values(cmdDict['freqStartQ'])[0]
freqStop = ena.ask_for_values(cmdDict['freqStopQ'])[0]
freq = np.linspace(freqStart,freqStop,num=numPts,endpoint=True)
ifbw = ena.ask_for_values(cmdDict['ifbwQ'])[0]
pwr = ena.ask_for_values(cmdDict['powerQ'])[0]
corr = ena.ask(cmdDict['corrQ'])
dateString = datetime.now().strftime("%Y-%m-%d")
timeString = datetime.now().strftime("%H:%M:%S")
dataDir = 'Data/' + dateString
if not path.exists(dataDir):
makedirs(dataDir)
i = 0
#saveMeas = True
#for i in range(numMeasurements):
try:
ena.write('pola;numg' + str(numGrps))
# while saveMeas:
while True:
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
# move the rotator
# will need to decide about how to handle the first case. Do you advance
# the rotator and then measure or do you measure and then advance? The
# call to ``advanceRotator`` can be made at the end of the loop.
advanceRotator(15)
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
print('Starting Measurement Number: %d' % i)
# print "here"
s11polar = np.array(ena.ask_for_values(cmdDict['s11']+cmdDict['polar']+';'+cmdDict['numGroups'] + str(numGrps)+';outpform'))
# print "there"
s11polReal = s11polar[::2] # real values from the polar data
s11polImag = s11polar[1::2] # imaginary values from the polar data
print "s21"
s21polar = np.array(ena.ask_for_values(cmdDict['s21']+cmdDict['polar']+';'+cmdDict['numGroups'] + str(numGrps)+';outpform'))
s21polReal = s21polar[::2] # real values from the polar data
s21polImag = s21polar[1::2] # imaginary values from the polar data
print "s12"
s12polar = np.array(ena.ask_for_values(cmdDict['s12']+cmdDict['polar']+';'+cmdDict['numGroups'] + str(numGrps)+';outpform'))
s12polReal = s12polar[::2] # real values from the polar data
s12polImag = s12polar[1::2] # imaginary values from the polar data
print "s22"
s22polar = np.array(ena.ask_for_values(cmdDict['s22']+cmdDict['polar']+';'+cmdDict['numGroups'] + str(numGrps)+';outpform'))
s22polReal = s22polar[::2] # real values from the polar data
s22polImag = s22polar[1::2] # imaginary values from the polar data
saveData = np.concatenate(([freq],
[s11polReal],[s11polImag],
[s21polReal],[s21polImag],
[s12polReal],[s12polImag],
[s22polReal],[s22polImag])).T
timeAppend = datetime.now().strftime("-%Y-%m-%d-%H%M%S")
dataName = dataDir + '/' + dataPrefix + timeAppend
touchFileName = dataName + ".s2p"
print touchFileName
saveFile = open(touchFileName, "w")
saveFile.write("!"+idn+"\n")
saveFile.write("!Date: " + dateString + " " + timeString + "\n")
saveFile.write("!Data & Calibration Information:\n")
if corr == '0':
saveFile.write("!Freq S11 S21 S12 S22\n")
elif corr== '1':
saveFile.write("!Freq S11:Cal(ON) S21:Cal(ON) S12:Cal(ON) S22:Cal(ON)\n")
saveFile.write("!PortZ Port1:50+j0 Port2:50+j0\n")
saveFile.write(("!Above PortZ is port z conversion or system Z0 "
"setting when saving the data.\n"))
saveFile.write(("!When reading, reference impedance value at option "
"line is always used.\n"))
saveFile.write("!\n")
saveFile.write("!--Config file parameters\n")
saveFile.write("!start = " + str(freqStart) + "\n")
saveFile.write("!stop = " + str(freqStop) + "\n")
saveFile.write("!numPts = " + str(numPts) + "\n")
saveFile.write("!avgFact = " + str(numGrps) + "\n")
saveFile.write("!power = " + str(pwr) + "\n")
saveFile.write("!ifbw = " + str(ifbw) + "\n")
saveFile.write("!\n")
saveFile.write(optLine + "\n")
np.savetxt(saveFile,saveData,delimiter=" ")
saveFile.close()
i += 1
# balun = skrf.Network(touchFileName)
# balun.plot_s_db()
# legend(loc=0)
except KeyboardInterrupt:
print('Ctrl-C Interrupt')
finally:
print('Finally')
visa.vpp43.gpib_control_ren(ena.vi,2)
stopRotator()
print("Done")