Debian Codegraph @

tests/instruments/agilent/test_agilent33500_with_device.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import pytest
26 from pymeasure.instruments.agilent.agilent33500 import Agilent33500
27 from math import pi, sin
28
29
30 pytest.skip('Only works with connected hardware', allow_module_level=True)
31 # from pyvisa.errors import VisaIOError
32
33 ############
34 # FIXTURES #
35 ############
36
37
38 @pytest.fixture(scope="session")
39 def generator():
40 try:
41 generator = Agilent33500("TCPIP::192.168.225.208::inst0::INSTR")
42 except IOError:
43 print("Not able to connect to waveform generator")
44 assert False
45
46 yield generator
47 for channel in 1, 2:
48 generator.channels[channel].output = "off"
49 generator.channels[channel].amplitude = 0.1
50 generator.channels[channel].offset = 0.1
51 generator.channels[channel].frequency = 60
52
53
54 ########
55 # DATA #
56 ########
57 BOOLEANS = [True, False]
58 CHANNELS = [1, 2]
59 WF_SHAPES = ["SIN", "SQU", "TRI", "RAMP", "PULS", "PRBS", "NOIS", "ARB", "DC"]
60 AMPLITUDE_RANGE = [0.01, 10]
61 PHASE_RANGE = [-360, 360]
62 AMPLITUDE_UNIT = ["VPP", "VRMS", "DBM"]
63 OFFSET_RANGE = [-4.995, 4.995]
64 VOLTAGE_HIGH_RANGE = [-4.999, 5]
65 VOLTAGE_LOW_RANGE = [-5, 4.999]
66 SQUARE_DUTYCYCLE_RANGE = [0.01, 99.98]
67 RAMP_SYMMETRY_RANGE = [0, 100]
68 PULSE_PERIOD_RANGE = [33e-9, 1e6]
69 PULSE_HOLD_RANGE = [["WIDT", "WIDT"], ["WIDTH", "WIDT"], ["DCYC", "DCYC"], ["DCYCLE", "DCYC"]]
70 PULSE_WIDTH_RANGE = [16e-9, 1e6]
71 PULSE_DUTYCYCLE_RANGE = [0.1, 100]
72 PULSE_TRANSITION_RANGE = [8.4e-9, 1e-6]
73 BURST_MODES = [["TRIG", "TRIG"], ["TRIGGERED", "TRIG"], ["GAT", "GAT"], ["GATED", "GAT"]]
74 BURST_PERIOD = [1e-6, 8000]
75 BURST_NCYCLES = [1, 99999]
76 SRATE = [1e-6, 1.6e8]
77
78
79 def generate_simple_harmonic_waveform(harmonic, distortion):
80 """
81 Generates a waveform with onlhy one harmonic
82 :param: harmonic: Harmonic number
83 :param: distortion: Distortion of the harmonic in %
84 """
85 samples_per_cycle = 2048
86 number_of_cycles = 4
87 frequency = 60
88 fundamental_amplitude = 0.707 # in ptp
89 time_step = (1 / frequency) / samples_per_cycle
90 waveform = []
91 time = 0
92
93 while time < ((1 / frequency) * number_of_cycles):
94 fundamental = fundamental_amplitude * sin(2 * pi * frequency * time)
95 harmonic_amplitude = (
96 (distortion / 100) *
97 fundamental_amplitude *
98 sin(harmonic * 2 * pi * frequency * time)
99 )
100 waveform.append(fundamental + harmonic_amplitude)
101 time += time_step
102
103 return waveform
104
105 #########
106 # TESTS #
107 #########
108
109 #####################
110 # NON-CHANNEL TESTS #
111 #####################
112
113
114 def test_get_instrument_id(generator):
115 assert "Agilent Technologies" in generator.id
116
117
118 def test_turn_on(generator):
119 generator.output = "on"
120 assert generator.output
121
122
123 @pytest.mark.parametrize("shape", WF_SHAPES)
124 def test_shape(generator, shape):
125 generator.shape = shape
126 assert shape == generator.shape
127
128
129 @pytest.mark.parametrize("frequency", [0.1, 1, 10, 100, 1000])
130 def test_frequency(generator, frequency):
131 generator.frequency = frequency
132 assert frequency == pytest.approx(generator.frequency, 0.01)
133
134
135 @pytest.mark.parametrize("amplitude", AMPLITUDE_RANGE)
136 def test_amplitude(generator, amplitude):
137 generator.amplitude = amplitude
138 assert amplitude == pytest.approx(generator.amplitude, 0.01)
139
140
141 @pytest.mark.xfail
142 @pytest.mark.parametrize("amplitude_unit", AMPLITUDE_UNIT)
143 def test_amplitude_unit(generator, amplitude_unit):
144 generator.amplitude_unit = amplitude_unit
145 assert amplitude_unit == generator.amplitude_unit
146
147
148 @pytest.mark.parametrize("offset", OFFSET_RANGE)
149 def test_offset(generator, offset):
150 generator.offset = offset
151 assert offset == pytest.approx(generator.offset, 0.01)
152
153
154 @pytest.mark.parametrize("voltage_high", VOLTAGE_HIGH_RANGE)
155 def test_voltage_high(
156 generator,
157 voltage_high,
158 ):
159 generator.voltage_high = voltage_high
160 assert voltage_high == pytest.approx(generator.voltage_high, 0.01)
161
162
163 @pytest.mark.parametrize("voltage_low", VOLTAGE_LOW_RANGE)
164 def test_voltage_low(
165 generator,
166 voltage_low,
167 ):
168 generator.voltage_low = voltage_low
169 assert voltage_low == pytest.approx(generator.voltage_low, 0.01)
170
171
172 @pytest.mark.parametrize("phase", range(PHASE_RANGE[0], PHASE_RANGE[1], 10))
173 def test_phase(generator, phase):
174 generator.phase = phase
175 assert phase == pytest.approx(generator.phase, 0.01)
176
177
178 @pytest.mark.parametrize("square_dutycycle", SQUARE_DUTYCYCLE_RANGE)
179 def test_square_dutycycle(generator, square_dutycycle):
180 generator.square_dutycycle = square_dutycycle
181 assert square_dutycycle == pytest.approx(generator.square_dutycycle, 0.01)
182
183
184 @pytest.mark.parametrize("ramp_symmetry", RAMP_SYMMETRY_RANGE)
185 def test_ramp_symmetry(generator, ramp_symmetry):
186 generator.ramp_symmetry = ramp_symmetry
187 assert ramp_symmetry == pytest.approx(generator.ramp_symmetry, 0.01)
188
189
190 @pytest.mark.xfail # seems like my device 33500B only supports a min of 5e-08 range period
191 @pytest.mark.parametrize("pulse_period", PULSE_PERIOD_RANGE)
192 def test_pulse_period(generator, pulse_period):
193 generator.pulse_period = pulse_period
194 assert pulse_period == pytest.approx(generator.pulse_period, 0.01)
195
196
197 @pytest.mark.parametrize("pulse_hold, expected", PULSE_HOLD_RANGE)
198 def test_pulse_hold(generator, pulse_hold, expected):
199 generator.pulse_hold = pulse_hold
200 assert expected == generator.pulse_hold
201
202
203 @pytest.mark.parametrize("pulse_width", PULSE_WIDTH_RANGE)
204 def test_pulse_width(generator, pulse_width):
205 generator.pulse_width = pulse_width
206 assert pulse_width == pytest.approx(generator.pulse_width, 0.01)
207
208
209 # 33500B minimum dutycycle seems to be 0.1
210 @pytest.mark.parametrize("pulse_dutycycle", PULSE_DUTYCYCLE_RANGE)
211 def test_pulse_dutycycle(generator, pulse_dutycycle):
212 generator.pulse_dutycycle = pulse_dutycycle
213 assert pulse_dutycycle == pytest.approx(generator.pulse_dutycycle, 0.1)
214
215
216 @pytest.mark.parametrize("pulse_transition", PULSE_TRANSITION_RANGE)
217 def test_pulse_transition(generator, pulse_transition):
218 generator.pulse_transition = pulse_transition
219 assert pulse_transition == pytest.approx(generator.pulse_transition, 0.1)
220
221
222 @pytest.mark.parametrize("output_load", [1, 10000, "INF"])
223 def test_output_load(generator, output_load):
224 generator.output_load = output_load
225 if output_load == "INF":
226 assert generator.output_load == 9.9e37
227 else:
228 assert output_load == generator.output_load
229
230
231 @pytest.mark.xfail
232 @pytest.mark.parametrize("boolean", BOOLEANS)
233 def test_burst_state(generator, boolean):
234 generator.burst_state = boolean
235 assert boolean == generator.burst_state
236
237
238 @pytest.mark.parametrize("burst_mode, expected", BURST_MODES)
239 def test_burst_mode(generator, burst_mode, expected):
240 generator.burst_mode = burst_mode
241 assert expected == generator.burst_mode
242
243
244 @pytest.mark.parametrize("burst_period", BURST_PERIOD)
245 def test_burst_period(generator, burst_period):
246 generator.burst_period = burst_period
247 assert burst_period == generator.burst_period
248
249
250 @pytest.mark.parametrize("burst_ncycles", BURST_NCYCLES)
251 def test_burst_ncycles(generator, burst_ncycles):
252 generator.burst_ncycles = burst_ncycles
253 assert burst_ncycles == generator.burst_ncycles
254
255
256 def test_arb_file_should_not_be_empty(generator):
257 file = generator.arb_file
258 assert file != ""
259
260
261 @pytest.mark.parametrize("srate", SRATE)
262 def test_arb_srate(generator, srate):
263 generator.arb_srate = srate
264 assert srate == generator.arb_srate
265
266
267 def test_uploaded_arb_file(generator):
268 waveform = generate_simple_harmonic_waveform(3, 10)
269 generator.shape = "ARB"
270 generator.data_arb("test", waveform, data_format="float")
271 generator.arb_file = "test"
272 assert '"TEST"' == generator.arb_file
273
274
275 #################
276 # CHANNEL TESTS #
277 #################
278
279
280 @pytest.mark.parametrize("channel", CHANNELS)
281 def test_turn_on_channel(generator, channel):
282 generator.channels[channel].output = "on"
283 assert generator.channels[channel].output
284
285
286 @pytest.mark.parametrize("shape", WF_SHAPES)
287 @pytest.mark.parametrize("channel", CHANNELS)
288 def test_shape_channel(generator, shape, channel):
289 generator.channels[channel].shape = shape
290 assert shape == generator.channels[channel].shape
291
292
293 @pytest.mark.parametrize("frequency", [0.1, 1, 10, 100, 1000])
294 @pytest.mark.parametrize("channel", CHANNELS)
295 def test_frequency_channel(generator, frequency, channel):
296 generator.channels[channel].frequency = frequency
297 assert frequency == pytest.approx(generator.channels[channel].frequency, 0.01)
298
299
300 @pytest.mark.parametrize("amplitude", AMPLITUDE_RANGE)
301 @pytest.mark.parametrize("channel", CHANNELS)
302 def test_amplitude_channel(generator, amplitude, channel):
303 generator.channels[channel].amplitude = amplitude
304 assert amplitude == pytest.approx(generator.channels[channel].amplitude, 0.01)
305
306
307 @pytest.mark.xfail
308 @pytest.mark.parametrize("amplitude_unit", AMPLITUDE_UNIT)
309 @pytest.mark.parametrize("channel", CHANNELS)
310 def test_amplitude_unit_channel(generator, amplitude_unit, channel):
311 generator.channels[channel].amplitude_unit = amplitude_unit
312 assert amplitude_unit == generator.channels[channel].amplitude_unit
313
314
315 @pytest.mark.parametrize("offset", OFFSET_RANGE)
316 @pytest.mark.parametrize("channel", CHANNELS)
317 def test_offset_channel(generator, offset, channel):
318 generator.channels[channel].offset = offset
319 assert offset == pytest.approx(generator.channels[channel].offset, 0.01)
320
321
322 @pytest.mark.parametrize("voltage_high", VOLTAGE_HIGH_RANGE)
323 @pytest.mark.parametrize("channel", CHANNELS)
324 def test_voltage_high_channel(generator, voltage_high, channel):
325 generator.channels[channel].voltage_high = voltage_high
326 assert voltage_high == pytest.approx(generator.channels[channel].voltage_high, 0.01)
327
328
329 @pytest.mark.parametrize("voltage_low", VOLTAGE_LOW_RANGE)
330 @pytest.mark.parametrize("channel", CHANNELS)
331 def test_voltage_low_channel(generator, voltage_low, channel):
332 generator.channels[channel].voltage_low = voltage_low
333 assert voltage_low == pytest.approx(generator.channels[channel].voltage_low, 0.01)
334
335
336 @pytest.mark.parametrize("phase", range(PHASE_RANGE[0], PHASE_RANGE[1], 10))
337 @pytest.mark.parametrize("channel", CHANNELS)
338 def test_phase_channel(generator, phase, channel):
339 generator.channels[channel].phase = phase
340 assert phase == pytest.approx(generator.channels[channel].phase, 0.01)
341
342
343 @pytest.mark.parametrize("square_dutycycle", SQUARE_DUTYCYCLE_RANGE)
344 @pytest.mark.parametrize("channel", CHANNELS)
345 def test_square_dutycycle_channel(generator, square_dutycycle, channel):
346 generator.channels[channel].square_dutycycle = square_dutycycle
347 assert square_dutycycle == pytest.approx(generator.channels[channel].square_dutycycle, 0.01)
348
349
350 @pytest.mark.parametrize("ramp_symmetry", RAMP_SYMMETRY_RANGE)
351 @pytest.mark.parametrize("channel", CHANNELS)
352 def test_ramp_symmetry_channel(generator, ramp_symmetry, channel):
353 generator.channels[channel].ramp_symmetry = ramp_symmetry
354 assert ramp_symmetry == pytest.approx(generator.channels[channel].ramp_symmetry, 0.01)
355
356
357 @pytest.mark.xfail # seems like my device 33500B only supports a min of 5e-08 range period
358 @pytest.mark.parametrize("pulse_period", PULSE_PERIOD_RANGE)
359 @pytest.mark.parametrize("channel", CHANNELS)
360 def test_pulse_period_channel(generator, pulse_period, channel):
361 generator.channels[channel].pulse_period = pulse_period
362 assert pulse_period == pytest.approx(generator.channels[channel].pulse_period, 0.01)
363
364
365 @pytest.mark.parametrize("pulse_hold, expected", PULSE_HOLD_RANGE)
366 @pytest.mark.parametrize("channel", CHANNELS)
367 def test_pulse_hold_channel(generator, pulse_hold, expected, channel):
368 generator.channels[channel].pulse_hold = pulse_hold
369 assert expected == generator.channels[channel].pulse_hold
370
371
372 @pytest.mark.parametrize("pulse_width", PULSE_WIDTH_RANGE)
373 @pytest.mark.parametrize("channel", CHANNELS)
374 def test_pulse_width_channel(generator, pulse_width, channel):
375 generator.channels[channel].pulse_width = pulse_width
376 assert pulse_width == pytest.approx(generator.channels[channel].pulse_width, 0.01)
377
378
379 # 33500B minimum dutycycle seems to be 0.1
380 @pytest.mark.parametrize("pulse_dutycycle", PULSE_DUTYCYCLE_RANGE)
381 @pytest.mark.parametrize("channel", CHANNELS)
382 def test_pulse_dutycycle_channel(generator, pulse_dutycycle, channel):
383 generator.channels[channel].pulse_dutycycle = pulse_dutycycle
384 assert pulse_dutycycle == pytest.approx(generator.channels[channel].pulse_dutycycle, 0.1)
385
386
387 @pytest.mark.parametrize("pulse_transition", PULSE_TRANSITION_RANGE)
388 @pytest.mark.parametrize("channel", CHANNELS)
389 def test_pulse_transition_channel(generator, pulse_transition, channel):
390 generator.channels[channel].pulse_transition = pulse_transition
391 assert pulse_transition == pytest.approx(generator.channels[channel].pulse_transition, 0.1)
392
393
394 @pytest.mark.parametrize("output_load", [1, 10000, "INF"])
395 @pytest.mark.parametrize("channel", CHANNELS)
396 def test_output_load_channel(generator, channel, output_load):
397 generator.channels[channel].output_load = output_load
398 if output_load == "INF":
399 assert generator.channels[channel].output_load == 9.9e37
400 else:
401 assert output_load == generator.channels[channel].output_load
402
403
404 @pytest.mark.xfail
405 @pytest.mark.parametrize("boolean", BOOLEANS)
406 @pytest.mark.parametrize("channel", CHANNELS)
407 def test_burst_state_channel(generator, boolean, channel):
408 generator.channels[channel].burst_state = boolean
409 assert boolean == generator.channels[channel].burst_state
410
411
412 @pytest.mark.parametrize("burst_mode, expected", BURST_MODES)
413 @pytest.mark.parametrize("channel", CHANNELS)
414 def test_burst_mode_channel(generator, burst_mode, expected, channel):
415 generator.channels[channel].burst_mode = burst_mode
416 assert expected == generator.channels[channel].burst_mode
417
418
419 @pytest.mark.parametrize("burst_period", BURST_PERIOD)
420 @pytest.mark.parametrize("channel", CHANNELS)
421 def test_burst_period_channel(generator, burst_period, channel):
422 generator.channels[channel].burst_period = burst_period
423 assert burst_period == generator.channels[channel].burst_period
424
425
426 @pytest.mark.parametrize("burst_ncycles", BURST_NCYCLES)
427 @pytest.mark.parametrize("channel", CHANNELS)
428 def test_burst_ncycles_channel(generator, burst_ncycles, channel):
429 generator.channels[channel].burst_ncycles = burst_ncycles
430 assert burst_ncycles == generator.channels[channel].burst_ncycles
431
432
433 @pytest.mark.parametrize("channel", CHANNELS)
434 def test_arb_file_channel(generator, channel):
435 file = generator.channels[channel].arb_file
436 assert file != ""
437
438
439 @pytest.mark.parametrize("srate", SRATE)
440 @pytest.mark.parametrize("channel", CHANNELS)
441 def test_arb_srate_channel(generator, channel, srate):
442 generator.channels[channel].arb_srate = srate
443 assert srate == generator.channels[channel].arb_srate
444
445
446 @pytest.mark.parametrize("channel", CHANNELS)
447 def test_uploaded_arb_file_channel(generator, channel):
448 waveform = generate_simple_harmonic_waveform(3, 10)
449 generator.channels[channel].shape = "ARB"
450 generator.channels[channel].data_arb("test", waveform, data_format="float")
451 generator.channels[channel].arb_file = "test"
452 assert '"TEST"' == generator.channels[channel].arb_file
453
454
455 def test_phase_sync(generator):
456 generator.phase_sync()