-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvxi-gps.py
executable file
·269 lines (232 loc) · 7.87 KB
/
vxi-gps.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
#!/usr/bin/env python
#
# coding: utf-8
import matplotlib.pyplot as plt
import sys
import os
import time
import h5py
import numpy as np
import glob
import vxi11
import ublox # pyUblox library
import util
import datetime
DEBUG = False
#DEBUG = True
def log(s):
if DEBUG:
print s
#------------------------------------------------------------------------------
# Delete data files if so requested
filename = 0
if (filename == 1):
for f in glob.iglob("./data/*.h5"): # delete all .h5 files
print 'Deleting', f
os.remove(f)
else:
print 'Not removing old files, as filename {0} is not 1.'.format(filename)
# Set the initial delay for frame capture
#start_wfd = 0.005 #0.05 #0.012
start_wfd = 0.02
wfd=start_wfd
#------------------------------------------------------------------------------
# This will need a rewrite
class TmcDriver:
def __init__(self, device):
print("Initializing connection to: " + device)
self.device = device
self.instr = vxi11.Instrument(device)
def write(self, command):
#print command
self.instr.write(command);
def read(self, length = 500):
return self.instr.read(length)
def read_raw(self, length = 500):
return self.instr.read_raw(length)
def getName(self):
self.write("*IDN?")
return self.read(300)
def ask(self, command):
return self.instr.ask(command)
def sendReset(self):
self.write("*RST") # Be carefull, this real resets an oscilloscope
def get_one_frame(thetime,ns):
global filename
global wfd
while True:
osc.write(':FUNC:WREC:OPER?') # finish recording?
reply = osc.read()
if reply == 'STOP':
run_time = round(time.time() - run_start_time, 2)
log(' Frame capture finished after %.2f seconds.' % run_time)
break
time.sleep(0.05)
for channel in ['CHAN1','CHAN2']:
if (osc.ask(':'+channel+':DISP?') == u'0'):
log(channel+' is not enabled')
else:
log('Reading out '+channel)
osc.write(':WAV:SOUR '+channel)
osc.write(':WAV:MODE NORM')
osc.write(':WAV:FORM BYTE')
osc.write(':WAV:POIN 1400')
print 'TIME:', thetime,
print 'NSPART:', ns,
osc.write(':WAV:XINC?')
xinc = float(osc.read(100))
print 'XINC:', xinc,
osc.write(':WAV:YINC?')
yinc = float(osc.read(100))
print 'YINC:', yinc,
osc.write(':TRIGger:EDGe:LEVel?')
trig = float(osc.read(100))
print 'TRIG:', trig,
osc.write(':WAVeform:YORigin?')
yorig = float(osc.read(100))
print 'YORIGIN:', yorig,
osc.write(':WAVeform:XORigin?')
xorig = float(osc.read(100))
print 'XORIGIN:', xorig,
osc.write(':WAVeform:XREFerence?')
xref = float(osc.read(100))
print 'XREF:', xref,
osc.write(':FUNC:WREP:FEND?') # get number of last frame
frames = int(osc.read(100))
print 'FRAMES:', frames, 'SUBRUN', filename
lastwave = bytearray()
with h5py.File('./data/data'+'{:02.0f}'.format(filename)+'_'+str(int(round(run_start_time,0)))+'-'+channel+'.h5', 'w') as hf:
hf.create_dataset('FRAMES', data=(frames)) # write number of frames
hf.create_dataset('XINC', data=(xinc)) # write axis parameters
hf.create_dataset('YINC', data=(yinc))
hf.create_dataset('TRIG', data=(trig))
hf.create_dataset('YORIGIN', data=(yorig))
hf.create_dataset('XORIGIN', data=(xorig))
hf.create_dataset('XREFERENCE', data=(xref))
hf.create_dataset('THETIME', data=(thetime))
hf.create_dataset('NSPART', data=(ns))
hf.create_dataset('CAPTURING', data=(run_time))
if (frames > 1):
osc.write(':FUNC:WREP:FCUR 1') # go to first frame
time.sleep(0.5)
for n in range(1,frames+1):
if (frames > 1):
osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
while True:
time.sleep(0.05)
fcur = osc.ask(':FUNC:WREP:FCUR?')
if (str(n) == fcur):
# Rigol returns correct current frame
sys.stdout.write(str(n))
break
else:
# Rigol has not yet made the seek, wait
# or consider extending the sleep above
print("Needwait: "+str(n)+' vs '+fcur)
reread_count = 0
while True:
time.sleep(wfd)
osc.write(':WAV:DATA?') # read data
time.sleep(wfd)
wave1 = bytearray(osc.read_raw(500))
wave2 = bytearray(osc.read_raw(500))
wave3 = bytearray(osc.read_raw(500))
#wave4 = bytearray(osc.read(500))
#wave = np.concatenate((wave1[11:],wave2[:(500-489)],wave3[:(700-489)]))
wave = np.concatenate((wave1[11:],wave2,wave3[:-1]))
if (np.array_equal(wave, lastwave)):
wfd = wfd + 0.005
print(' Same waveform, wait ' + str(wfd) + ' and reread')
reread_count = reread_count + 1
if (reread_count > 5):
print('------------ Wrong trigger level?')
else:
hf.create_dataset(str(n), data=wave)
lastwave = wave
sys.stdout.write('.')
wfd = start_wfd
break
print 'OK'
# End of channel recording
# Initialize ethernet link to oscilloscope
# For Ethernet
osc = TmcDriver("TCPIP::10.1.1.3::INSTR")
print(osc.ask("*IDN?"))
osc.write(':STOP') # go to STOP mode
time.sleep(0.5)
# Initialize GPS serial link
# If the device was disconnected briefly, this can jump to ACM1 or whatever
gpsport = '/dev/ttyACM0'
gpsbaudrate = 921600
# Open GPS port
dev = ublox.UBlox(gpsport, baudrate=gpsbaudrate, timeout=0)
print "# Configured to listen at " + gpsport, ' forcing trigger.'
# Prepare the oscilloscope
# Switch to single trigger mode and do one trigger
time.sleep(1)
osc.write(':SINGle')
time.sleep(1)
osc.write(':TFORce')
# ----------------------------
# Read GPS messages, if any
last_count = -1
first_count = 0
while True:
try:
msg = dev.receive_message()
except:
continue
if (msg is None):
pass#break
else:
#print msg.name()
if msg.name() == 'TIM_TM2':
#print('Got TM2 message')
#try:
msg.unpack()
if (last_count < 0):
first_count = msg.count
last_count = first_count
print('# May the force be with you. Setting first count to '+str(first_count))
run_start_time = time.time()
time.sleep(wfd)
log(' Starting single capture at time: %.2f' % run_start_time)
osc.write(':SINGle')
else:
# If this is not a duplicate message
if (msg.count > last_count):
timestring = '$HIT,'
# Edges since first count
timestring += str(msg.count-first_count)
timestring += ','
# Edges in this run
timestring += str(msg.count-last_count)
timestring += ','
# Computer time
timestring += str(datetime.datetime.utcnow())
timestring += ','
# This provides the rising edge time to microsecond precision
timestring += str(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR))
timestring += ','
timestring += str(datetime.datetime.utcfromtimestamp(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR)))
#timestring += str(datetime.datetime.utcfromtimestamp(util.gpsTimeToTime(msg.wnR, 1.0e6*(msg.towMsR % 1000 + msg.SubMsR)))
timestring += ','
# Tow sub millisecond fraction, in nanoseconds
timestring += str(msg.towMsR)
timestring += ','
# Tow sub millisecond fraction, in nanoseconds
timestring += str(msg.towSubMsR)
log(timestring)
sys.stdout.flush()
last_count = msg.count
# Get the data frame from oscilloscope
get_one_frame(util.gpsTimeToTime(msg.wnR, 1.0e-3*msg.towMsR + 1.0e-9*msg.towSubMsR), msg.towSubMsR)
filename = filename + 1
# and go again for single capture
time.sleep(0.5)
run_start_time = time.time()
log(' Run start time: %.2f' % run_start_time)
log(' Capturing...')
osc.write(':SINGle')
# else:
# print('# DUP')