Debian Codegraph @

tests/instruments/hp/test_hp8116a_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 import math
27 import time
28 from pymeasure.instruments.hp import HP8116A, hp8116a
29
30 pytest.skip('Only work with connected hardware', allow_module_level=True)
31
32
33 class TestHP8116A:
34 """
35 Unit tests for HP8116A class.
36
37 This test suite, needs the following setup to work properly:
38 - A HP8116A device should be connected to the computer;
39 - The device's address must be set in the RESOURCE constant.
40 - You must set HAS_OPTION_001 according to the device's sweep/burst capability.
41 """
42
43 RESOURCE = 'GPIB0::12'
44 HAS_OPTION_001 = True
45
46 BOOLEANS = [False, True]
47 OPERATING_MODES_WO_OPT001 = ['normal', 'triggered', 'gate', 'external_width']
48 OPERATING_MODES_OPT001 = ['internal_sweep', 'external_sweep', 'internal_burst',
49 'external_burst']
50 CONTROL_MODES = ['off', 'FM', 'AM', 'PWM', 'VCO']
51 TRIGGER_SLOPES = ['off', 'positive', 'negative']
52 SHAPES = ['dc', 'sine', 'triangle', 'square', 'pulse']
53
54 UNITS = {
55 'nano': 'NZ',
56 'micro': 'UZ',
57 'milli': 'MZ',
58 'no_prefix': 'HZ',
59 'kilo': 'KHZ',
60 'mega': 'MHZ'
61 }
62 VALUES_WITH_UNITS = [[1e-9, '1 NZ'], [1e-6, '1 UZ'], [1e-3, '1 MZ'], [1, '1 HZ'],
63 [1e3, '1 KHZ'], [1e6, '1 MHZ'], [1.23456e-9, '1.23 NZ'], [1/3, '333 MZ'],
64 [1.23456e-6, '1.23 UZ'], [1e-6, '1 UZ']]
65
66 VALUES_WITH_UNITS_TO_PARSE = [['FRQ 2.34 MZ', 2.34e-3], ['FRQ 23.4 MZ', 23.4e-3],
67 ['FRQ 234 MZ', 234e-3], ['FRQ 2.34 HZ', 2.34],
68 ['FRQ 23.4 HZ', 23.4], ['FRQ 234 HZ', 234.0],
69 ['FRQ 2.34KHZ', 2.34e3], ['FRQ 23.4KHZ', 23.4e3],
70 ['FRQ 234 KHZ', 234e3], ['FRQ 2.34MHZ', 2.34e6],
71 ['FRQ 23.4MHZ', 23.4e6]]
72
73 FREQUENCIES = [[1, 1], [1.23, 1.23], [1e3, 1e3], [1.23e3, 1.23e3], [1e6, 1e6], [1.23e6, 1.23e6],
74 [1.234, 1.23], [1.234e3, 1.23e3], [10.234e3, 10.2e3], [1.234e6, 1.23e6]]
75
76 instr = HP8116A(RESOURCE)
77
78 @pytest.fixture
79 def make_resetted_instr(self):
80 self.instr.reset()
81 return self.instr
82
83 def test_given_instrument_resetted_when_triggered_then_normal(self, make_resetted_instr):
84 instr = make_resetted_instr
85 instr.write('M2') # Triggered mode
86 instr.reset()
87 assert 'M1' in instr.ask('CST', 10)
88
89 @pytest.mark.parametrize('case', OPERATING_MODES_WO_OPT001)
90 def test_operating_modes_no_option001(self, make_resetted_instr, case):
91 instr = make_resetted_instr
92 if case == 'external_width':
93 instr.write('W4') # External width mode only valid in pulse mode
94 instr.operating_mode = case
95 assert instr.operating_mode == case
96
97 @pytest.mark.parametrize('case', OPERATING_MODES_OPT001)
98 def test_operating_modes_option001(self, make_resetted_instr, case):
99 instr = make_resetted_instr
100
101 instr.operating_mode = case
102 if self.HAS_OPTION_001:
103 assert instr.operating_mode == case
104 else:
105 assert instr.operating_mode == 'normal'
106
107 @pytest.mark.parametrize('case', CONTROL_MODES)
108 def test_control_modes(self, make_resetted_instr, case):
109 instr = make_resetted_instr
110 if case == 'PWM':
111 instr.write('W4') # PWM mode only valid in pulse mode
112 instr.control_mode = case
113 assert instr.control_mode == case
114
115 @pytest.mark.parametrize('case', TRIGGER_SLOPES)
116 def test_trigger_slopes(self, make_resetted_instr, case):
117 instr = make_resetted_instr
118 instr.trigger_slope = case
119 assert instr.trigger_slope == case
120
121 @pytest.mark.parametrize('case', SHAPES)
122 def test_shapes(self, make_resetted_instr, case):
123 instr = make_resetted_instr
124 instr.shape = case
125 assert instr.shape == case
126
127 def test_given_long_operation_then_buffer_not_empty_bit_set(self, make_resetted_instr):
128 instr = make_resetted_instr
129 instr.adapter.write('W4') # Takes about 330 ms to process according to the service manual
130 assert (instr.status & hp8116a.Status.buffer_not_empty)
131 instr._wait_for_commands_processed() # Now wait so that the instrument doesn't lock up
132
133 def test_write(self, make_resetted_instr):
134 instr = make_resetted_instr
135 instr.write('W4')
136 assert not (instr.status & hp8116a.Status.buffer_not_empty)
137
138 def test_given_read_called_then_exception(self, make_resetted_instr):
139 instr = make_resetted_instr
140 with pytest.raises(NotImplementedError):
141 instr.read()
142
143 def test_given_ask_called_then_return_value_valid(self, make_resetted_instr):
144 instr = make_resetted_instr
145 ret = instr.ask('IFRQ', 14)
146 assert type(ret) is str
147 assert '\n' not in ret
148 assert '\r' not in ret
149 assert 'FRQ' in ret
150
151 @pytest.mark.parametrize('case, expected', VALUES_WITH_UNITS)
152 def test_get_value_with_unit(self, case, expected):
153 ret = HP8116A._get_value_with_unit(case, self.UNITS)
154 assert ret == expected
155
156 @pytest.mark.parametrize('case, expected', VALUES_WITH_UNITS_TO_PARSE)
157 def test_parse_value_with_unit(self, case, expected):
158 ret = HP8116A._parse_value_with_unit(case, self.UNITS)
159 assert math.isclose(ret, expected)
160
161 def test_generate_1_2_5_sequence(self):
162 min = 200e-6
163 max = 1
164 expected = [200e-6, 500e-6, 1e-3, 2e-3, 5e-3, 10e-3, 20e-3, 50e-3,
165 100e-3, 200e-3, 500e-3, 1]
166 ret = HP8116A._generate_1_2_5_sequence(min, max)
167 assert ret == expected
168
169 @pytest.mark.parametrize('case', BOOLEANS)
170 def test_haversine_enabled(self, case, make_resetted_instr):
171 instr = make_resetted_instr
172 instr.haversine_enabled = case
173 assert instr.haversine_enabled == case
174
175 @pytest.mark.parametrize('case', BOOLEANS)
176 def test_autovernier_enabled(self, case, make_resetted_instr):
177 instr = make_resetted_instr
178 instr.autovernier_enabled = case
179 assert instr.autovernier_enabled == case
180
181 @pytest.mark.parametrize('case', BOOLEANS)
182 def test_limit_enabled(self, case, make_resetted_instr):
183 instr = make_resetted_instr
184 instr.limit_enabled = case
185 assert instr.limit_enabled == case
186
187 @pytest.mark.parametrize('case', BOOLEANS)
188 def test_complement_enabled(self, case, make_resetted_instr):
189 instr = make_resetted_instr
190 instr.complement_enabled = case
191 assert instr.complement_enabled == case
192
193 @pytest.mark.parametrize('case', BOOLEANS)
194 def test_output_enabled(self, case, make_resetted_instr):
195 instr = make_resetted_instr
196 instr.output_enabled = case
197 assert instr.output_enabled == case
198
199 @pytest.mark.parametrize('case, expected', FREQUENCIES)
200 def test_frequency(self, case, expected, make_resetted_instr):
201 instr = make_resetted_instr
202 instr.frequency = case
203 assert math.isclose(instr.frequency, expected)
204
205 def test_given_invalid_frequency_when_set_frequency_then_(self, make_resetted_instr):
206 instr = make_resetted_instr
207 with pytest.raises(ValueError):
208 instr.frequency = 1e9 # Sadly 1 GHz is too high :(
209
210 def test_check_has_option_001(self, make_resetted_instr):
211 instr = make_resetted_instr
212 assert instr._check_has_option_001() == self.HAS_OPTION_001
213
214 def test_given_resetted_when_check_errors_then_no_error(self, make_resetted_instr):
215 instr = make_resetted_instr
216 errors = instr.check_errors()
217 assert type(errors) is list
218 assert len(errors) == 0
219
220 def test_given_invalid_duty_cycle_when_check_errors_then_error(self, make_resetted_instr):
221 instr = make_resetted_instr
222 instr.frequency = 50e6
223 instr.duty_cycle = 90
224 errors = instr.check_errors()
225 assert len(errors) == 1
226 assert errors[0] == 'DUTY C. ERROR'
227
228 def test_given_two_error_conditions_when_check_errors_then_two_errors(self,
229 make_resetted_instr):
230 instr = make_resetted_instr
231
232 # Triggering duty cycle error since 90 % is too high at 50 MHz
233 instr.frequency = 50e6
234 instr.duty_cycle = 90
235
236 # Triggering handling error since autovernier is not possible in triggered mode
237 instr.operating_mode = 'triggered'
238 instr.autovernier_enabled = True
239
240 errors = instr.check_errors()
241 assert len(errors) == 2
242 assert 'DUTY C. ERROR' in errors
243 assert 'HANDLING ERROR' in errors
244
245 def test_given_autovernier_disabled_when_start_autovernier_then_error(self,
246 make_resetted_instr):
247 instr = make_resetted_instr
248 with pytest.raises(RuntimeError):
249 instr.start_autovernier(HP8116A.frequency, HP8116A.Digit.LEAST_SIGNIFICANT,
250 HP8116A.Direction.UP)
251
252 def test_given_invalid_control_when_start_autovernier_then_error(self, make_resetted_instr):
253 instr = make_resetted_instr
254 instr.autovernier_enabled = True
255 with pytest.raises(ValueError):
256 instr.start_autovernier(HP8116A.sweep_start, HP8116A.Digit.LEAST_SIGNIFICANT,
257 HP8116A.Direction.UP)
258
259 def test_autovernier(self, make_resetted_instr):
260 instr = make_resetted_instr
261 instr.autovernier_enabled = True
262 instr.start_autovernier(HP8116A.amplitude, HP8116A.Digit.SECOND_SIGNIFICANT,
263 HP8116A.Direction.UP, 100e-3)
264 time.sleep(0.5)
265 instr.autovernier_enabled = False
266 assert instr.amplitude > 120e-3