-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalign_adcs.py
More file actions
executable file
·290 lines (246 loc) · 10.7 KB
/
align_adcs.py
File metadata and controls
executable file
·290 lines (246 loc) · 10.7 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
#!/usr/bin/python
import nuphase
import time
import numpy
NUM_TRIES = 10
verbose=True
align_status_file = '/home/nuphase/nuphase-python/output/align_status'
check_align_file = '/home/nuphase/nuphase-python/output/align_check'
def init(verbose=verbose, reset_shift_bytes=True):
sys = nuphase.Nuphase()
sys.boardInit(verbose=verbose)
current_atten_values = sys.getCurrentAttenValues(verbose=True)
sys.setAttenValues(numpy.zeros(10, dtype=int), readback=verbose)
sys.calPulser(True, readback=verbose) #turn on cal pulse feature
SHIFT_BYTES = numpy.zeros(16, dtype=int)
if reset_shift_bytes:
#set to 0 and read in current values for delay:
##master board
for i in range(4):
sys.write(sys.BUS_MASTER, [56+i,0,0,0])
readback = sys.readRegister(sys.BUS_MASTER, 56+i)
SHIFT_BYTES[2*i] = readback[3]
SHIFT_BYTES[2*i+1] = readback[2]
##slave board
for i in range(4):
sys.write(sys.BUS_SLAVE, [56+i,0,0,0])
readback = sys.readRegister(sys.BUS_SLAVE, 56+i)
SHIFT_BYTES[2*i+8] = readback[3]
SHIFT_BYTES[2*i+1+8] = readback[2]
return sys, SHIFT_BYTES, current_atten_values
def getPeaks(data, mode=1, channels=[0,15]):
location_of_peaks=[]
#SHOULD MAKE THIS MORE CONFIGURABLE:
pulse_threshold_master = 83
pulse_threshold_slave = 93
###########################################
pulse_thresholds = [pulse_threshold_master] * 8
pulse_thresholds.extend([pulse_threshold_slave]*8)
for chan in range(len(data)):
if chan < channels[0] or chan > channels[1]:
continue
##mode = 0, simply use numpy.argmax
if mode == 0:
location_of_peaks.append(numpy.argmax(data[chan]))
##mode = 1, a bit more complicated and board-specific
elif mode == 1:
location_of_peaks_temp = numpy.where(numpy.transpose(data[chan]) > pulse_thresholds[chan])[0]
if len(location_of_peaks_temp) < 1:
location_of_peaks.append(-1)
else:
location_of_peaks.append(location_of_peaks_temp[0])
return location_of_peaks
def alignInit(sys):
sys.boardInit()
print 'checking ADC data good flags..'
while (sys.getDataValid() != (1,1)):
time.sleep(1)
print '...good\n'
sys.boardInit()
sys.softwareTrigger()
sys.readSysEvent(save=False)
sys.boardInit()
def align(NUM_TRIES=10, only_master_board=False):
sys, SHIFT_BYTES, current_atten_values = init()
DELAY_SLAVE_BY_CLKCYCLE = False
DELAY_MASTER_BY_CLKCYCLE = False
alignInit(sys)
for i in range(NUM_TRIES):
sys.boardInit() #only deal with buffer 0
sys.calPulser(True)
sys.softwareTrigger()
time.sleep(0.001)
data = sys.readSysEvent(save=False)
if only_master_board:
location_of_peaks = getPeaks(data, channels=[0,7])
else:
location_of_peaks = getPeaks(data)
earliest_peak = min(location_of_peaks)
earliest_peak_ch = numpy.argmin(location_of_peaks)
latest_peak = max(location_of_peaks)
latest_peak_ch = numpy.argmax(location_of_peaks)
#condition if peaks caught from multiple pulses
if (latest_peak - earliest_peak) > 300:
if verbose:
print i, location_of_peaks, "skipping, trying again..."
continue
#condition if not within 16 samples, try a dclk reset
elif (latest_peak - earliest_peak) > 32:
sys.dclkReset()
if verbose:
print 'sending dclk reset to boards..'
time.sleep(2)
if verbose:
print sys.readRegister(1,8), sys.readRegister(0,8)
print i, location_of_peaks, 'sample diff', latest_peak-earliest_peak
time.sleep(1)
sys.boardInit()
sys.softwareTrigger()
#dump event
dat=sys.readSysEvent(save=False)
continue
elif (latest_peak - earliest_peak) > 16:
print 'applying clkcycle delay'
if earliest_peak_ch > 7:
DELAY_SLAVE_BY_CLKCYCLE = True
DELAY_MASTER_BY_CLKCYCLE = False
else:
DELAY_MASTER_BY_CLKCYCLE = True
DELAY_SLAVE_BY_CLKCYCLE = False
check_peak_location=True
sum_of_peak_location = 0
for i in range(2,len(location_of_peaks),2):
sum_of_peak_location = sum_of_peak_location + location_of_peaks[i-2]
if location_of_peaks[i] != location_of_peaks[i-2]:
check_peak_location=False
break
#if 'sum of peak location' is 0, found peaks in all channels at index=0, which might happen on startup (for some reason?)
if sum_of_peak_location == 0:
print 'found all zeros for peak index, trying again'
time.sleep(1)
continue
elif sum_of_peak_location < 0:
print 'issue found, no peaks, trying reset'
print location_of_peaks
print 'sending reset...'
sys.reset()
time.sleep(30)
alignInit(sys)
continue
#otherwise, try to align
#if location_of_peaks[0] == location_of_peaks[2] == location_of_peaks[4] ==\
# location_of_peaks[6] == location_of_peaks[8] == location_of_peaks[10]:
if check_peak_location:
if verbose:
print 'alignment successful: ', location_of_peaks
print 'SHIFT BYTES:', SHIFT_BYTES
sys.readSysEvent(save=True, filename='output/test_alignment.dat') #save pulse event for verification
close(sys, current_atten_values, verbose=verbose)
return 1
if verbose:
print location_of_peaks, 'sample diff', latest_peak-earliest_peak
#system has 12 channels = 6 ADC shift values
for j in range(4):
shift_value = latest_peak+DELAY_MASTER_BY_CLKCYCLE*16-location_of_peaks[2*j]+1
if shift_value != 0:
#should be identical for both channels within an ADC
SHIFT_BYTES[2*j] = 0x00 | DELAY_MASTER_BY_CLKCYCLE << 5 | True << 4 | (shift_value & 0xF)
SHIFT_BYTES[2*j+1] = 0x00 | DELAY_MASTER_BY_CLKCYCLE << 5 | True << 4 | (shift_value & 0xF)
sys.write(sys.BUS_MASTER, [56+j,0,SHIFT_BYTES[2*j+1], SHIFT_BYTES[2*j]])
if not only_master_board:
for j in range(4):
shift_value = latest_peak+DELAY_SLAVE_BY_CLKCYCLE*16-location_of_peaks[2*j+8]+1
if shift_value != 0:
#should be identical for both channels within an ADC
SHIFT_BYTES[2*j+8] = 0x00 | DELAY_SLAVE_BY_CLKCYCLE << 5 | True << 4 | (shift_value & 0xF)
SHIFT_BYTES[2*j+1+8] = 0x00 | DELAY_SLAVE_BY_CLKCYCLE << 5 | True << 4 | (shift_value & 0xF)
sys.write(sys.BUS_SLAVE, [56+j,0,SHIFT_BYTES[2*j+1+8],SHIFT_BYTES[2*j+8]])
close(sys, current_atten_values, verbose=verbose)
return 0
def close(sys, atten_values, verbose):
sys.boardInit(verbose=verbose)
sys.calPulser(False, readback=verbose)
sys.setAttenValues(atten_values, readback=verbose)
def checkAlignment(channels=[0,9], NUM_TRIES=50, verbose=False):
num_tests=0
num_success=0
num_almost_success=0
sys, _, current_atten_values = init(reset_shift_bytes=False)
for i in range(NUM_TRIES):
sys.boardInit() #only deal with buffer 0
sys.calPulser(True)
sys.softwareTrigger()
time.sleep(0.001)
data = sys.readSysEvent(save=False)
if verbose:
print sys.getMetaData()
location_of_peaks = getPeaks(data, channels=channels)
earliest_peak = min(location_of_peaks)
latest_peak = max(location_of_peaks)
#condition if peaks caught from multiple pulses
if (latest_peak - earliest_peak) > 300:
if verbose:
print i, location_of_peaks, "skipping, trying again..."
continue
num_tests=num_tests+1
check_peak_location=True
for i in range(2,len(location_of_peaks),2):
if location_of_peaks[i] != location_of_peaks[i-2]:
check_peak_location=False
break
#otherwise, try to align
#if location_of_peaks[0] == location_of_peaks[2] == location_of_peaks[4] ==\
# location_of_peaks[6] == location_of_peaks[8] == location_of_peaks[10]:
if check_peak_location:
if verbose:
print 'alignment successful: ', location_of_peaks
#sys.readSysEvent(save=True, filename='test_alignment.dat') #save pulse event for verification
num_success=num_success+1
else:
if verbose:
print i, 'DATA ARE NOT ALIGNED..', location_of_peaks, earliest_peak, latest_peak
if abs(latest_peak - earliest_peak) == 1:
num_almost_success = num_almost_success + 1
sys.boardInit()
sys.calPulser(False, readback=True)
print '-------------------------'
print 'RESULTS:', num_success, 'pure successes out of', num_tests, 'tries'
print 'RESULTS:', num_success+num_almost_success, '>= almost successes (+/-1) out of', num_tests, 'tries'
return num_success, num_tests
##--------------------
##run >>python align_adcs.py to align board timestreams
##run >>python align_adcs.py -c to verify alignment
if __name__=="__main__":
import sys
from optparse import OptionParser
parser = OptionParser()
usage = "usage: %prog [options]"
parser.add_option("-c", "--check", action="store_const", dest="check", const=True)
parser.add_option("-m", "--master-only", action="store_const", dest="master", const=True)
(options, args) = parser.parse_args()
if options.check:
num_tries = 50
#if len(sys.argv) == 3:
# num_tries = int(sys.argv[2])
if options.master:
success, tests = checkAlignment(NUM_TRIES=num_tries, channels=[0,7])
else:
success, tests = checkAlignment(NUM_TRIES=num_tries)
file = open(check_align_file, 'w')
file.write(str(success)+'\n'+str(tests))
file.close()
else:
file = open(align_status_file, 'w')
file.write("0")
file.close()
if options.master:
retval=align(only_master_board=True)
else:
retval=align()
if retval==0:
print 'problem here'
else:
file = open(align_status_file, 'w')
file.write("1")
file.close()
sys.exit(retval)