-
Notifications
You must be signed in to change notification settings - Fork 2
/
awg_data.py
265 lines (219 loc) · 8.96 KB
/
awg_data.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
import numpy as np
import os
class AwgDefaults:
def __init__(self, uut_name):
self.defs = "DATA/{}.npy".format(uut_name)
def read_defaults(self):
print("read_defaults {}".format(self.defs))
with open(self.defs, 'r') as fp:
current = np.load(fp)
print("read_defaults {} {}".format(self.defs, current))
return current
def store_defaults(self, current):
print("store_defaults {} {}".format(self.defs, current))
with open(self.defs, 'w') as fp:
np.save(fp, current)
class RunsFiles:
def __init__(self, uut, files, run_forever=False):
self.uut = uut
self.files = files
self.run_forever = run_forever
def load(self, autorearm = False):
for ii in range(99999 if self.run_forever else 1):
for f in self.files:
with open(f, mode='rb') as fp:
self.uut.load_awg(fp.read(), autorearm = autorearm)
yield f
class SinGen:
NCYCLES = 5
def sin(self):
nsam = self.nsam
NCYCLES = self.NCYCLES
return np.sin(np.array(range(nsam))*NCYCLES*2*np.pi/nsam) # sin, amplitude of 1 (volt)
class AllFullScale(SinGen):
def __init__(self, uut, nchan, nsam, run_forever=False):
self.uut = uut
self.nchan = nchan
self.nsam = nsam
self.run_forever = run_forever
self.sw = self.sin()
self.aw = np.zeros((nsam,nchan))
for ch in range(nchan):
self.aw[:,ch] = self.sw
def load(self, autorearm = False):
for ii in range(99999 if self.run_forever else 1):
for ch in range(self.nchan):
self.uut.load_awg((self.aw*(2**15-1)).astype(np.int16), autorearm = autorearm)
print("loaded array ", self.aw.shape)
yield ch
class RainbowGen:
NCYCLES = 5
def offset(self, ch):
return -9.0 + 8.0*ch/self.nchan;
def rainbow(self, ch):
return np.add(self.sw, self.offset(ch))
def sin(self):
nsam = self.nsam
NCYCLES = self.NCYCLES
return np.sin(np.array(range(nsam))*NCYCLES*2*np.pi/nsam) # sin, amplitude of 1 (volt)
def sinc(self, ch):
nsam = self.nsam
nchan = self.nchan
NCYCLES = self.NCYCLES
xoff = ch*100
xx = np.array(range(-nsam/2-xoff,nsam/2-xoff))*NCYCLES*2*np.pi/nsam
return [ np.sin(x)/x if x != 0 else 1 for x in xx ]
def __init__(self, uut, nchan, nsam, run_forever=False, ao0 = 0):
self.uut = uut
self.nchan = nchan
self.nsam = nsam
self.ao0 = ao0
self.run_forever = run_forever
self.sw = self.sin()
self.aw = np.zeros((nsam,nchan))
self.defs = AwgDefaults(uut.uut)
self.gain = 1.0
try:
self.current = self.defs.read_defaults()
print("self.current len {} self.nchan {}".format(len(self.current), self.nchan))
for ch in range(0, len(self.current)):
self.aw[:,self.ao0+ch] = self.current[ch]
except IOError:
self.current = np.zeros(self.nchan)
print("no defaults")
for ch in range(nchan):
self.aw[:,ch] = self.rainbow(ch)
def load(self, autorearm = False):
for ii in range(99999 if self.run_forever else 1):
for ch in range(self.nchan):
aw1 = np.copy(self.aw)
aw1[:,ch] = np.add(np.multiply(self.sinc(ch),5),2)
print("loading array ", aw1.shape)
awr = (aw1*(2**15-1)/10)/self.gain
for chx in range(len(self.current)):
awr[:,self.ao0+chx] += self.current[chx]
self.uut.load_awg(awr.astype(np.int16), autorearm = autorearm)
print("loaded array ", aw1.shape)
yield ch
class Pulse:
def generate(self):
zset = np.zeros(self.interval)
pset = zset
pset[self.interval-1-self.flat_top:] = 1
for seg in range(1, self.nsam/self.interval):
x1 = seg*self.interval
x2 = x1 + self.interval
for ch in range(self.nchan):
if seg%self.nchan == ch:
self.aw[x1:x2,ch] = pset
def __init__(self, uut, nchan, nsam, args = (1000,10)):
self.uut = uut
self.nchan = nchan
self.nsam = nsam
(self.interval, self.flat_top) = [ int(u) for u in args ]
print( "self.interval {}".format(self.interval))
self.aw = np.zeros((nsam,nchan))
self.generate()
def load(self, autorearm = False):
self.uut.load_awg((self.aw*(2**15-1)/10).astype(np.int16), autorearm = autorearm)
yield self
class ZeroOffset:
def __init__(self, uut, nchan, nsam, target=0, run_forever=False, gain = 0.1, passvalue = 1, aochan = 0, ao0 = 0):
print("ZeroOffset")
self.uut = uut
self.nchan = nchan
self.nsam = nsam
self.target = float(target)
self.run_forever = run_forever
if aochan == 0:
aochan = nchan
self.aw = np.zeros((nsam,aochan))
for ch in range(0, aochan):
self.aw[:,ch] = ch
self.aw.astype('int16').tofile("awg.dat")
self.current = np.zeros(nchan)
self.finished = 0
self.in_bounds = False
self.KFB = gain
self.passvalue = float(passvalue/gain)
self.identity_pattern = bool(int(os.getenv("IDENTITY_PATTERN", 0)))
self.verbose = int(os.getenv("VERBOSE", 0))
self.ao0 = ao0
self.user_quit = False
self.defs = AwgDefaults(uut.uut)
# offsets compensate channel geometry when AWG disabled
self.apply_geometry = bool(int(os.getenv("AO_CORRECT_GEOMETRY", 0)))
self.geometry = [
-2*3.3, 0, 0, -2*3.3, 0, 0, 0, 6*3.3,
0, 0, 0, 0, 0, -3*3.3, 0, 0,
-2*3.3, 0, 0, -2*3.3, 0, 0, 0, 6*3.3,
0, 0, 0, 0, 0, -3*3.3, 0, 0
]
try:
print("self.identity_pattern {}".format(self.identity_pattern))
if not self.identity_pattern:
self.current = self.defs.read_defaults()
for ch in range(0, self.nchan):
self.aw[:,self.ao0+ch] = self.current[ch]
except IOError:
print("no defaults")
def vprint(self, str):
if self.verbose > 0:
print(str)
def feedback(self, fb_data):
actual = np.mean(fb_data[50:,:], axis=0)
error = actual - self.target
errmax = max(abs(error))
if errmax < self.passvalue:
print("maximum error {} is within bounds {}, save it".format(errmax, self.passvalue))
self.defs.store_defaults(self.current)
self.in_bounds = True
else:
print("maximum error {}".format(errmax))
self.current = np.mean(self.aw, axis=0)[self.ao0:self.ao0+self.nchan]
self.newset = self.current + (self.target - actual) * self.KFB
self.newset = np.clip(self.newset, -32768, 32767)
if np.max(self.newset) >= 32767 or np.min(self.newset) <= 32768:
print("Hit rails good idea to quit")
passcount=0
railcount=0
for e in np.nditer(error):
if abs(e) < self.passvalue:
passcount += 1
elif abs(e) >= 32767:
railcount +=1
if passcount+railcount > len(error)/2:
self.in_bounds = True
if self.verbose or self.in_bounds:
np.set_printoptions(linewidth=200, precision=3)
print("target {}".format(self.target))
print("current {}".format(self.current))
print("actual {}".format(actual))
print("error {}".format(error))
print("errma {}".format(errmax))
print("gain {}".format(self.KFB))
print("step {}".format((self.target - actual) * self.KFB))
print("newset {}".format(self.newset))
if not self.identity_pattern:
for ch in range(0, self.nchan):
self.aw[:,self.ao0+ch] = self.newset[ch]
self.aw.astype('int16').tofile("awg.dat")
def load(self, autorearm = False):
self.vprint("load 01")
yy = self
while not self.finished:
self.vprint("load 10")
if self.finished and self.apply_geometry:
print("apply_geometry")
for ch in range(0, self.nchan):
self.aw[:,ch] += self.geometry[ch]
yy = None
self.uut.load_awg(self.aw.astype(np.int16), autorearm = autorearm)
print("loaded array ", self.aw.shape)
if self.in_bounds:
# plot this one, drop out next time
print("Target achieved, quit any time")
self.finished = True
self.vprint("load 66")
yield yy
self.vprint("load 99")