-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsampling_scope.py
339 lines (277 loc) · 10.8 KB
/
sampling_scope.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright (C) 2007 Matthew Neeley, Max Hofheinz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### BEGIN NODE INFO
[info]
name = Sampling Scope
version = 2.1
description =
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 5
### END NODE INFO
"""
import struct
import re
import numpy as np
from labrad import types as T, errors, util
from labrad.server import setting
from labrad.gpib import GPIBManagedServer, GPIBDeviceWrapper
from twisted.internet.defer import inlineCallbacks, returnValue
__QUERY__ = 'ENC WAV:BIN;BYT. LSB;OUT TRA%d;WAV?'
class NotConnectedError(errors.Error):
"""You need to connect"""
code = 10
class InvalidChannelError(errors.Error):
"""Only channels 1 through 8 are valid"""
code = 10
class MeasurementError(errors.Error):
"""Scope returned error"""
code = 10
class OutofrangeError(errors.Error):
"""Signal is out of range"""
code = 10
class SendTraceError(errors.Error):
"""StrList needs to have either 3 or 4 elements"""
code = 11
TIMEOUT = 120
class SamplingScopeDevice(GPIBDeviceWrapper):
@inlineCallbacks
def initialize(self):
yield self.timeout(TIMEOUT)
class SamplingScope(GPIBManagedServer):
name = 'Sampling Scope'
deviceName = 'Tektronix 11801C'
deviceWrapper = SamplingScopeDevice
deviceIdentFunc = 'identify_device'
@setting(1000, server='s', address='s', idn='s')
def identify_device(self, c, server, address, idn):
if idn == '\xff':
return self.deviceName
@setting(10, 'Get Trace',
trace=[': Query TRACE1',
'w: Specify trace to query: 1, 2, or 3'],
returns=['*v: y-values', 'v: x-increment'])
def get_trace(self, c, trace=1):
"""Returns the y-values of the current trace from the sampling scope.
First element: offset time
Second element: time step
Third to last element: trace
"""
dev = self.selectedDevice(c)
if trace < 1 or trace > 3:
raise NotConnectedError()
yield dev.write('COND TYP:AVG')
while True:
if int((yield dev.query('COND? REMA'))[18:]) == 0:
break
yield util.wakeupCall(2)
resp = yield dev.query(__QUERY__ % trace, bytes=20000L)
ofs, incr, vals = _parseBinaryData(resp)
returnValue([T.Value(v, 'V') for v in np.hstack(([ofs, incr], vals))])
@setting(99, 'Multi Trace',
cstar='w', cend='w',
returns='v[s]{time offset} v[s]{time step} *2v[V]{channel}')
def multi_trace(self, c, cstar=1, cend=2):
"""Returns the y-values of the current traces from the sampling scope in a tuple.
(offset, timestep, 2-D array of traces)
"""
dev = self.selectedDevice(c)
if cstar < 1 or cstar > 8:
raise Exception('cstar out of range')
if cend < 1 or cend > 8:
raise Exception('cend out of range')
if cstar > cend:
raise Exception('must have cend >= cstar')
yield dev.write('COND TYP:AVG')
while True:
if int((yield dev.query('COND? REMA'))[18:]) == 0:
break
yield util.wakeupCall(2)
resp = yield dev.query('ENC WAV:BIN;BYT. LSB;OUT TRA%dTOTRA%d;WAV?' % (cstar, cend), bytes=20000L)
splits = resp.split(";WFMPRE")
traces = []
ofs = 0
incr = 0
for trace in splits:
ofs, incr, vals = _parseBinaryData(trace)
traces.append(vals)
#ofs1, incr1, vals1 = _parseBinaryData(t1)
#ofs2, incr2, vals2 = _parseBinaryData(t2)
#traces = np.vstack((vals1, vals2))
returnValue((ofs, incr, traces))
@setting(241, 'Send Trace To Data Vault',
server=['s'], session=['*s'], dataset=['s'], trace=['w'],
returns=['*s s: Dataset Name'])
def send_trace(self, c, server, session, dataset, trace=1):
"""Send the current trace to the data vault.
"""
dev = self.selectedDevice(c)
resp = yield dev.query(__QUERY__ % trace, bytes=20000L)
vals = _parseBinaryData(resp)
startx = vals[0]
stepx = vals[1]
vals = vals[2:]
for _ in np.shape(vals)[:-1]:
vals = vals[0]
out = [[(startx + i*stepx)*1e9, d] for i, d in enumerate(vals)]
p = self.client[server].packet()
p.cd(session,True)
p.new(dataset,[('time', 'ns')],[('amplitude','trace %d' % trace, 'V')])
p.add(out)
resp = yield p.send()
name = resp.new
returnValue(name)
@setting(101, 'Record Length',
data=['w: Record Length 512, 1024, 2048 or 4096, 5120'],
returns=['w: Record Length'])
def record_length(self, c, data):
"""Sets the start time of the trace."""
dev = self.selectedDevice(c)
yield dev.write('TBM LEN:%d' % data)
returnValue(data)
@setting(102, 'Mean',
channel=['w: Trace number', ': Trace 1'],
returns=['v[V]: Time average of the trace'])
def mean(self, c, channel=1):
dev = self.selectedDevice(c)
s = yield dev.query('COND TYP:AVG;SEL TRA%d;COND WAIT;MEAN?' % channel)
if s[-2:] in ['GT', 'LT', 'OR']:
raise OutofrangeError()
returnValue(T.Value(float(s[5:-3]), 'V'))
@setting(103, 'Amplitude',
channel=['w: Trace number', ': Trace 1'],
returns=['v[V]: Time average of the trace'])
def amplitude(self, c, channel=1):
dev = self.selectedDevice(c)
s = yield dev.query('SEL TRA%d;PP?' % channel) # 'COND TYP:AVG;SEL TRA%d;COND WAIT;AMP?'
if s[-2:] in ['GT', 'LT', 'OR']:
raise OutofrangeError()
returnValue(T.Value(float(s[3:-3]), 'V'))
@setting(11, 'Start Time',
data=['v[s]: Set Start Time',''],
returns=['v[s]: Start Time'])
def start_time(self, c, data=None):
"""Sets the start time of the trace."""
dev = self.selectedDevice(c)
if data is not None:
dataS = data['s']
yield dev.write('MAINP %g' % dataS)
resp = yield dev.query('MAINP?')
print resp
returnValue(data)
@setting(12, 'Time Step',
data=['v[s]: Set Time Step'],
returns=['v[s]: Time Step'])
def time_step(self, c, data):
"""Sets the time/div for of the trace."""
dev = self.selectedDevice(c)
dataS = data['s']
yield dev.write('TBM TIM:%g' % dataS)
returnValue(data)
@setting(112, 'Offset',
data=['v[V]: Set offset (voltage at screen center)'],
returns=['v[V]: offset'])
def offset(self, c, data):
"""Set offset, i.e. the voltage at the center of the screen."""
dev = self.selectedDevice(c)
dataV = data['V']
yield dev.write('CHM%d OFFS:%g' % (self.getchannel(c), dataV))
returnValue(data)
@setting(13, 'Sensitivity',
data=['v[V]: Set V/div'],
returns=['v[V]: Sensitivity'])
def sensitivity(self, c, data):
"""Set sensitivity (V/div)."""
dev = self.selectedDevice(c)
dataV = data['V']
yield dev.write('CHM%d SENS:%g' % (self.getchannel(c), dataV))
returnValue(data)
def getchannel(self, c):
return c.get('Channel', 1)
@setting(113, 'Channel',
data=[': Select Channel 1',
'w: Channel (1 to 8)'],
returns=['w: Sensitivity'])
def channel(self, c, data=1):
"""Select channel."""
if data < 1 or data > 8:
raise InvalidChannelError()
c['Channel'] = data
return data
@setting(114, 'Average', averages=['w'], returns=['w'])
def average(self, c, averages=1):
"""Set number of averages."""
dev = self.selectedDevice(c)
yield dev.write('AVG OFF')
if averages > 1:
yield dev.write('NAV %d' % averages)
yield dev.write('AVG ON')
returnValue(averages)
@setting(14, 'trace',
trace=[': Attach selected channel to trace 1',
'w: Attach selected channel to a trace'],
returns=['w: Trace'])
def trace(self, c, trace=1):
"""Define a trace."""
dev = self.selectedDevice(c)
yield dev.write("TRA%d DES:'M%d'" % (trace, self.getchannel(c)))
yield dev.write('SEL TRA%d' % trace)
returnValue(trace)
@setting(15, 'Trigger Level',
data=['v[V]: Set trigger level'],
returns=['v[V]: Trigger level'])
def trigger_level(self, c, data):
"""Set trigger level."""
dev = self.selectedDevice(c)
dataV = data['V']
yield dev.write('TRI LEV:%g' % dataV)
returnValue(data)
@setting(16, 'Trigger positive', returns=[''])
def trigger_positive(self, c):
"""Trigger on positive slope."""
dev = self.selectedDevice(c)
yield dev.write('TRI SLO:PLU')
@setting(17, 'Trigger negative', returns=[''])
def trigger_negative(self, c):
"""Trigger on negative slope."""
dev = self.selectedDevice(c)
yield dev.write('TRI SLO:NEG')
@setting(18, 'Reset', returns=[''])
def reset(self, c):
"""Reset to default state."""
dev = self.selectedDevice(c)
yield dev.write('INI')
_xzero = re.compile('XZERO:(-?\d*.?\d+E?-?\+?\d*),')
_xincr = re.compile('XINCR:(-?\d*.?\d+E?-?\+?\d*),')
_yzero = re.compile('YZERO:(-?\d*.?\d+E?-?\+?\d*),')
_ymult = re.compile('YMULT:(-?\d*.?\d+E?-?\+?\d*),')
def _parseBinaryData(data):
"""Parse the data coming back from the scope"""
hdr, dat = data.split(';CURVE')
dat = dat[dat.find('%')+3:-1]
dat = np.array(struct.unpack('h'*(len(dat)/2), dat))
xzero = float(_xzero.findall(hdr)[0])
xincr = float(_xincr.findall(hdr)[0])
yzero = float(_yzero.findall(hdr)[0])
ymult = float(_ymult.findall(hdr)[0])
return xzero, xincr, dat*ymult + yzero
__server__ = SamplingScope()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)