forked from python-ivi/python-ivi
/
rigolDSSource.py
435 lines (366 loc) · 18.8 KB
/
rigolDSSource.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
"""
Python Interchangeable Virtual Instrument Library
Copyright (c) 2017 Alex Forencich
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy as np
import struct
from .. import ivi
from .. import fgen
OutputMode = set(['function', 'arbitrary'])
OperationMode = set(['continuous'])
StandardWaveformMapping = {
'sine': 'sin',
'square': 'squ',
'triangle': 'ramp',
'ramp_up': 'ramp',
'ramp_down': 'ramp',
'dc': 'dc',
'pulse': 'puls',
'noise': 'nois',
'sinc': 'sinc',
'exprise': 'expr',
'expfall': 'expf',
'cardiac': 'ecg',
'gaussian': 'gaus',
'lorentz': 'lor',
'haversine': 'hav'
}
class rigolDSSource(fgen.Base, fgen.StdFunc, fgen.ArbWfm, fgen.ArbFrequency,
fgen.ArbChannelWfm):
"Rigol DSO internal source IVI function generator driver"
def __init__(self, *args, **kwargs):
self.__dict__.setdefault('_instrument_id', '')
self._output_standard_waveform_symmetry = list()
super(rigolDSSource, self).__init__(*args, **kwargs)
# Internal source
self._output_count = 2
self._arbitrary_sample_rate = 0
self._arbitrary_waveform_number_waveforms_max = 0
self._arbitrary_waveform_size_max = 16384
self._arbitrary_waveform_size_min = 2
self._arbitrary_waveform_quantum = 1
self._add_property('outputs[].standard_waveform.symmetry',
self._get_output_standard_waveform_symmetry,
self._set_output_standard_waveform_symmetry,
None,
"""
Specifies the symmetry for a ramp or triangle waveform. This attribute
affects function generator behavior only when the Waveform attribute is
set to Waveform Triangle, Ramp Up, or Ramp Down. The value is expressed
as a percentage.
""")
self._identity_description = "Rigol DSO internal source IVI function generator driver"
self._identity_supported_instrument_models = []
self._init_outputs()
def _init_outputs(self):
try:
super(rigolDSSource, self)._init_outputs()
except AttributeError:
pass
self._output_name = list()
self._output_operation_mode = list()
self._output_enabled = list()
self._output_impedance = list()
self._output_mode = list()
self._output_reference_clock_source = list()
self._output_standard_waveform_ramp_symmetry = list()
for i in range(self._output_count):
self._output_name.append("source%d" % (i+1))
self._output_operation_mode.append('continuous')
self._output_enabled.append(False)
self._output_impedance.append(50)
self._output_mode.append('function')
self._output_reference_clock_source.append('internal')
self._output_standard_waveform_symmetry.append(50.0)
self.outputs._set_list(self._output_name)
# AFG option
def _get_output_operation_mode(self, index):
index = ivi.get_index(self._output_name, index)
return self._output_operation_mode[index]
def _set_output_operation_mode(self, index, value):
index = ivi.get_index(self._output_name, index)
if value not in OperationMode:
raise ivi.ValueNotSupportedException()
self._output_operation_mode[index] = value
def _get_output_enabled(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:output:state?" % self._output_name[index])
self._output_enabled[index] = resp == 'ON'
self._set_cache_valid(index=index)
return self._output_enabled[index]
def _set_output_enabled(self, index, value):
index = ivi.get_index(self._output_name, index)
value = bool(value)
if not self._driver_operation_simulate:
self._write(":%s:output:state %d" % (self._output_name[index], value))
self._output_enabled[index] = value
self._set_cache_valid(index=index)
def _get_output_impedance(self, index):
index = ivi.get_index(self._analog_channel_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
val = self._ask(":%s:output:impedance?" % self._output_name[index])
if val == 'HIGHZ':
self._output_impedance[index] = 1000000
elif val == 'FIF':
self._output_impedance[index] = 50
self._set_cache_valid(index=index)
return self._output_impedance[index]
def _set_output_impedance(self, index, value):
value = float(value)
index = ivi.get_index(self._analog_channel_name, index)
if value != 50 and value != 1000000:
raise Exception('Invalid impedance selection')
if not self._driver_operation_simulate:
if value == 1000000:
self._write(":%s:output:impedance highz" % self._output_name[index])
elif value == 50:
self._write(":%s:output:impedance fifty" % self._output_name[index])
self._output_impedance[index] = value
self._set_cache_valid(index=index)
self._set_cache_valid(False, 'output_standard_waveform_amplitude', index)
def _get_output_mode(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:function?" % self._output_name[index]).lower()
if resp == 'ext':
self._output_mode[index] = 'arbitrary'
else:
self._output_mode[index] = 'function'
self._set_cache_valid(index=index)
return self._output_mode[index]
def _set_output_mode(self, index, value):
index = ivi.get_index(self._output_name, index)
if value not in OutputMode:
raise ivi.ValueNotSupportedException()
if not self._driver_operation_simulate:
if value == 'arbitrary':
self._write(":%s:function ext" % self._output_name[index])
else:
if self._get_cache_valid('output_standard_waveform_waveform', index=index):
self._set_output_standard_waveform_waveform(index, self._output_standard_waveform_waveform[index])
else:
self._set_output_standard_waveform_waveform(index, 'sine')
self._output_mode[index] = value
self._set_cache_valid(index=index)
def _get_output_reference_clock_source(self, index):
index = ivi.get_index(self._output_name, index)
return self._output_reference_clock_source[index]
def _set_output_reference_clock_source(self, index, value):
index = ivi.get_index(self._output_name, index)
value = 'internal'
self._output_reference_clock_source[index] = value
def abort_generation(self):
pass
def initiate_generation(self):
pass
def _get_output_standard_waveform_amplitude(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:voltage:amplitude?" % self._output_name[index])
self._output_standard_waveform_amplitude[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_amplitude[index]
def _set_output_standard_waveform_amplitude(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value)
if value < 0.01 or value > 5.0:
raise ivi.OutOfRangeException()
if not self._driver_operation_simulate:
self._write(":%s:voltage:amplitude %e" % (self._output_name[index], value))
self._output_standard_waveform_amplitude[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_dc_offset(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:voltage:offset?" % self._output_name[index])
self._output_standard_waveform_dc_offset[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_dc_offset[index]
def _set_output_standard_waveform_dc_offset(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value)
if not self._driver_operation_simulate:
self._write(":%s:voltage:offset %e" % (self._output_name[index], value))
self._output_standard_waveform_dc_offset[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_duty_cycle_high(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:pulse:dcycle?" % self._output_name[index])
self._output_standard_waveform_duty_cycle_high[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_duty_cycle_high[index]
def _set_output_standard_waveform_duty_cycle_high(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value)
if value < 10.0 or value > 90.0:
raise ivi.OutOfRangeException()
if not self._driver_operation_simulate:
self._write(":%s:pulse:dcycle %e" % (self._output_name[index], value))
self._output_standard_waveform_duty_cycle_high[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_symmetry(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:function:ramp:symmetry?" % self._output_name[index])
self._output_standard_waveform_symmetry[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_symmetry[index]
def _set_output_standard_waveform_symmetry(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value)
if value < 0.0 or value > 100.0:
raise ivi.OutOfRangeException()
if not self._driver_operation_simulate:
self._write(":%s:function:ramp:symmetry %e" % (self._output_name[index], value))
self._output_standard_waveform_symmetry[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_start_phase(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:phase?" % self._output_name[index])
self._output_standard_waveform_start_phase[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_start_phase[index]
def _set_output_standard_waveform_start_phase(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value) % 360
if value < 0 or value > 360.0:
raise ivi.OutOfRangeException()
if not self._driver_operation_simulate:
self._write(":%s:phase %e" % (self._output_name[index], value))
self._output_standard_waveform_start_phase[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_frequency(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:frequency?" % self._output_name[index])
self._output_standard_waveform_frequency[index] = float(resp)
self._set_cache_valid(index=index)
return self._output_standard_waveform_frequency[index]
def _set_output_standard_waveform_frequency(self, index, value):
index = ivi.get_index(self._output_name, index)
value = float(value)
if value < 0.1 or value > 25e6:
raise ivi.OutOfRangeException()
if not self._driver_operation_simulate:
self._write(":%s:frequency %e" % (self._output_name[index], value))
self._output_standard_waveform_frequency[index] = value
self._set_cache_valid(index=index)
def _get_output_standard_waveform_waveform(self, index):
index = ivi.get_index(self._output_name, index)
if not self._driver_operation_simulate and not self._get_cache_valid(index=index):
resp = self._ask(":%s:function?" % self._output_name[index]).lower()
if resp == 'arbitrary':
resp = 'sine'
resp = [k for k,v in StandardWaveformMapping.items() if v==resp][0]
if resp == 'ramp_up':
if self._get_output_standard_waveform_symmetry(index) <= 10.0:
resp = 'ramp_down'
elif self._get_output_standard_waveform_symmetry(index) >= 90.0:
resp = 'ramp_up'
else:
resp = 'triangle'
self._output_standard_waveform_waveform[index] = resp
self._set_cache_valid(index=index)
return self._output_standard_waveform_waveform[index]
def _set_output_standard_waveform_waveform(self, index, value):
index = ivi.get_index(self._output_name, index)
if value not in StandardWaveformMapping:
raise ivi.ValueNotSupportedException()
if not self._driver_operation_simulate:
self._write(":%s:function %s" % (self._output_name[index], StandardWaveformMapping[value]))
if value == 'triangle':
if self._get_output_standard_waveform_symmetry(index) <= 10.0 or self._get_output_standard_waveform_symmetry(index) >= 90:
self._set_output_standard_waveform_symmetry(index, 50.0)
elif value == 'ramp_up':
self._set_output_standard_waveform_symmetry(index, 100.0)
elif value == 'ramp_down':
self._set_output_standard_waveform_symmetry(index, 0.0)
self._output_standard_waveform_waveform[index] = value
self._set_cache_valid(index=index)
self._output_mode[index] = 'function'
self._set_cache_valid(True, 'output_mode', index=index)
def _get_output_arbitrary_gain(self, index):
return self._get_output_standard_waveform_amplitude(index)
def _set_output_arbitrary_gain(self, index, value):
self._set_output_standard_waveform_amplitude(index, value)
def _get_output_arbitrary_offset(self, index):
return self._get_output_standard_waveform_dc_offset(index)
def _set_output_arbitrary_offset(self, index, value):
self._set_output_standard_waveform_dc_offset(index, value)
def _get_output_arbitrary_waveform(self, index):
index = ivi.get_index(self._output_name, index)
return self._output_arbitrary_waveform[index]
def _set_output_arbitrary_waveform(self, index, value):
index = ivi.get_index(self._output_name, index)
value = str(value)
self._output_arbitrary_waveform[index] = value
def _get_arbitrary_sample_rate(self):
return self._arbitrary_sample_rate
def _set_arbitrary_sample_rate(self, value):
value = float(value)
self._arbitrary_sample_rate = value
def _get_arbitrary_waveform_number_waveforms_max(self):
return self._arbitrary_waveform_number_waveforms_max
def _get_arbitrary_waveform_size_max(self):
return self._arbitrary_waveform_size_max
def _get_arbitrary_waveform_size_min(self):
return self._arbitrary_waveform_size_min
def _get_arbitrary_waveform_quantum(self):
return self._arbitrary_waveform_quantum
def _arbitrary_waveform_clear(self, handle):
pass
def _arbitrary_waveform_configure(self, index, handle, gain, offset):
self._set_output_arbitrary_waveform(index, handle)
self._set_output_arbitrary_gain(index, gain)
self._set_output_arbitrary_offset(index, offset)
def _arbitrary_waveform_create(self, data):
return "handle"
def _get_output_arbitrary_frequency(self, index):
return self._get_output_standard_waveform_frequency(index)
def _set_output_arbitrary_frequency(self, index, value):
self._set_output_standard_waveform_frequency(index, value)
def _arbitrary_waveform_create_channel_waveform(self, index, data):
y = None
x = None
if type(data) == list and type(data[0]) == float:
# list
y = array(data)
elif type(data) == np.ndarray and len(data.shape) == 1:
# 1D array
y = data
elif type(data) == np.ndarray and len(data.shape) == 2 and data.shape[0] == 1:
# 2D array, hieght 1
y = data[0]
elif type(data) == np.ndarray and len(data.shape) == 2 and data.shape[1] == 1:
# 2D array, width 1
y = data[:,0]
else:
x, y = ivi.get_sig(data)
if len(y) % self._arbitrary_waveform_quantum != 0:
raise ivi.ValueNotSupportedException()
# clip on [-1,1] and rescale to [0,1]
yc = (y.clip(-1, 1)+1)/2
# scale to 14 bits
yb = np.rint(yc * ((1 << 14)-1)).astype(int) & 0x00003fff
raw_data = yb.astype('<i2').tobytes()
# space required before IEEE block due to Rigol firmware bug wrt. data alignment in scope memory
self._write_ieee_block(raw_data, ':trace%d:data:dac volatile, ' % (index+1))
return self._output_name[index]