Debian Codegraph @

pymeasure/instruments/eurotest/eurotestHPP120256.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 NON INFRINGEMENT. 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 import logging
25 import math
26
27 import re
28 import time
29
30 from pymeasure.instruments import Instrument
31 from pymeasure.instruments.validators import strict_range
32 from pymeasure.instruments.validators import strict_discrete_set
33
34 from enum import IntFlag
35
36 log = logging.getLogger(__name__)
37 log.addHandler(logging.NullHandler())
38
39
40 class EurotestHPP120256(Instrument):
41 """ Represents the Euro Test High Voltage DC Source model HPP-120-256
42 and provides a high-level interface for interacting with the instrument using the
43 Euro Test command set (Not SCPI command set).
44
45 .. code-block:: python
46
47 hpp120256 = EurotestHPP120256("GPIB0::20::INSTR")
48
49 print(hpp120256.id)
50 print(hpp120256.lam_status)
51 print(hpp120256.status)
52
53 hpp120256.ramp_to_zero(100.0)
54
55 hpp120256.voltage_ramp = 50.0 # V/s
56 hpp120256.current_limit = 2.0 # mA
57 inst.kill_enabled = True # Enable over-current protection
58 time.sleep(1.0) # Give time to enable kill
59 inst.output_enabled = True
60 time.sleep(1.0) # Give time to output on
61
62 abs_output_voltage_error = 0.02 # kV
63
64 hpp120256.wait_for_output_voltage_reached(abs_output_voltage_error, 1.0, 40.0)
65
66 # Here voltage HV output should be at 0.0 kV
67
68 print("Setting the output voltage to 1.0kV...")
69 hpp120256.voltage_setpoint = 1.0 # kV
70
71 # Now HV output should be rising to reach the 1.0kV at 50.0 V/s
72
73 hpp120256.wait_for_output_voltage_reached(abs_output_voltage_error, 1.0, 40.0)
74
75 # Here voltage HV output should be at 1.0 kV
76
77 hpp120256.shutdown()
78
79 hpp120256.wait_for_output_voltage_reached(abs_output_voltage_error, 1.0, 60.0)
80
81 # Here voltage HV output should be at 0.0 kV
82
83 inst.output_enabled = False
84
85 # Now the HV voltage source is in safe state
86 """
87
88 VOLTAGE_RANGE = [0.0, 12.0] # kVolts
89 CURRENT_RANGE = [0.0, 25.0] # mAmps
90 VOLTAGE_RAMP_RANGE = [10, 3000] # V/s
91 COMMAND_DELAY = 0.2 # s
92
93 response_encoding = "iso-8859-2"
94 regex = re.compile(r'([+-]?([\d]*\.)?[\d]+)')
95
96 def __init__(self,
97 adapter,
98 name="Euro Test High Voltage DC Source model HPP-120-256",
99 query_delay=0.1,
100 write_delay=0.4,
101 timeout=5000,
102 **kwargs):
103
104 super().__init__(
105 adapter,
106 name,
107 write_termination="\n",
108 read_termination="",
109 send_end=True,
110 includeSCPI=False,
111 timeout=timeout,
112 **kwargs
113 )
114
115 self.write_delay = write_delay
116 self.query_delay = query_delay
117 self.last_write_timestamp = 0.0
118
119 # ####################################
120 # # EuroTest-Command set. Non SCPI commands.
121 # ####################################
122
123 voltage_setpoint = Instrument.control(
124 "STATUS,U", "U,%.3fkV",
125 """Control the voltage set-point in kVolts (float strictly from 0 to 12).""",
126 # getter device response: "U, RANGE=3.000kV, VALUE=2.458kV"
127 validator=strict_range,
128 values=VOLTAGE_RANGE,
129 get_process=lambda r:
130 float(EurotestHPP120256.regex.search(r[2].strip()).groups()[0])
131 )
132
133 current_limit = Instrument.control(
134 "STATUS,I", "I,%.3fmA",
135 """Control the current limit in mAmps (float strictly from 0 to 25).""",
136 # When this property acts as get, the instrument will return a string like this:
137 # "I, RANGE=5000mA, VALUE=1739mA", then current_limit will return 1739.0,
138 # hence the convenience of the get_process.
139 validator=strict_range,
140 values=CURRENT_RANGE,
141 get_process=lambda r:
142 float(EurotestHPP120256.regex.search(r[2].strip()).groups()[0])
143 )
144
145 voltage_ramp = Instrument.control(
146 "STATUS,RAMP", "RAMP,%dV/s",
147 """Control the voltage ramp in Volts/second (int strictly from 10 to 3000).""",
148 # When this property acts as get, the instrument will return a string like this:
149 # "RAMP, RANGE=3000V/s, VALUE=1000V/s", then voltage_ramp will return 1000.0,
150 # hence the convenience of the get_process.
151 validator=strict_range,
152 values=VOLTAGE_RAMP_RANGE,
153 get_process=lambda r:
154 float(EurotestHPP120256.regex.search(r[2].strip()).groups()[0])
155 )
156
157 voltage = Instrument.measurement(
158 "STATUS,MU",
159 """Measure the actual output voltage in kVolts (float).""",
160 # This property is a get so, the instrument will return a string like this:
161 # "U, RANGE=3.000kV, VALUE=2.458kV", then voltage will return 2458.0,
162 # hence the convenience of the get_process.
163 get_process=lambda r:
164 float(EurotestHPP120256.regex.search(r[2].strip()).groups()[0])
165 )
166
167 voltage_range = Instrument.measurement(
168 "STATUS,MU",
169 """Measure the actual output voltage range in kVolts (float).""",
170 # This property is a get so, the instrument will return a string like this:
171 # "U, RANGE=3.000kV, VALUE=2.458kV", then voltage_range will return 3000.0,
172 # hence the convenience of the get_process.
173 get_process=lambda r:
174 float(EurotestHPP120256.regex.search(r[1]).groups()[0])
175 )
176
177 current = Instrument.measurement(
178 "STATUS,MI",
179 """Measure the actual output current in mAmps (float).""",
180 # This property is a get so, the instrument will return a string like this:
181 # "I, RANGE=5000mA, VALUE=1739mA", then current will return a 1739.0,
182 # hence the convenience of the get_process."""
183 get_process=lambda r:
184 float(EurotestHPP120256.regex.search(r[2].strip()).groups()[0])
185 )
186
187 current_range = Instrument.measurement(
188 "STATUS,MI",
189 """Measure the actual output current range in mAmps (float).""",
190 # This property is a get so, the instrument will return a string like this:
191 # "I, RANGE=5000mA, VALUE=1739mA, then current_range will return a 5000.0,
192 # hence the convenience of the get_process.
193 get_process=lambda r:
194 float(EurotestHPP120256.regex.search(r[1].strip()).groups()[0])
195 )
196
197 kill_enabled = Instrument.control(
198 "STATUS,DI", "KILL,%s",
199 """Control the instrument kill enable (boolean).""",
200 # When Kill is enabled yellow led is flashing and the output
201 # will be shut OFF permanently without ramp if Iout > IOUTmax.
202 validator=strict_discrete_set,
203 values={True: 'ENable', False: 'DISable'},
204 map_values=True,
205 get_process=lambda r:
206 EurotestHPP120256.EurotestHPP120256Status(
207 int(r[1].strip()[:-1].encode(EurotestHPP120256.response_encoding).
208 decode('utf-8', 'ignore'), 2)
209 ) == EurotestHPP120256.EurotestHPP120256Status.KILL_ENABLE
210 )
211
212 output_enabled = Instrument.control(
213 "STATUS,DI", "HV,%s",
214 """Control the instrument output enable (boolean).""",
215 # When output voltage is enabled green led is ON and the
216 # voltage_setting will be present on the output.
217 validator=strict_discrete_set,
218 values={True: 'ON', False: 'OFF'},
219 map_values=True,
220 get_process=lambda r:
221 EurotestHPP120256.EurotestHPP120256Status(
222 int(r[1].strip()[:-1].encode(EurotestHPP120256.response_encoding).
223 decode('utf-8', 'ignore'), 2)
224 ) == EurotestHPP120256.EurotestHPP120256Status.OUTPUT_ON
225 )
226
227 id = Instrument.measurement(
228 "ID",
229 """Get the identification of the instrument (string) """,
230 get_process=lambda r:
231 r[1].strip().encode(EurotestHPP120256.response_encoding).decode('utf-8', 'ignore')
232 )
233
234 status = Instrument.measurement(
235 "STATUS,DI",
236 """Get the instrument status (EurotestHPP120256Status).""",
237 # Every bit indicates the state of one subsystem of the HV Source.
238 # response DI, b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0,
239 # 0 1
240 # IpErr b15 no input error input error
241 # Ramp b14 no ramp ramp
242 # CutOut b13 - emergency off
243 # TpErr b12 no trip error trip error
244 # F3 b11 reserved
245 # F2 b10 reserved
246 # menu1 b9 submenu off submenu on
247 # menu0 b8 menu off menu on
248 # err b7 no error error
249 # Creg b6 no current control current control
250 # Vreg b5 no voltage control voltage control
251 # pol b4 negative positive
252 # inh b3 no ext. inhibit external inhibit
253 # local b2 remote local
254 # kilena b1 kill disable kill enable
255 # on b0 off high voltage is ON
256 get_process=lambda r:
257 EurotestHPP120256.EurotestHPP120256Status(
258 int(r[1].strip()[:-1].encode(EurotestHPP120256.response_encoding).
259 decode('utf-8', 'ignore'), 2)
260 )
261 )
262
263 lam_status = Instrument.measurement(
264 "STATUS,LAM",
265 """Get the instrument lam status (string).""",
266 # LAM status is the status of the unit from the point
267 # of view of the process. Fo example, as a response of asking STATUS,LAM, the HV
268 # voltage could response one of the messages from the next list:
269 # LAM,ERROR External Inhibit occurred during Kill enable
270 # LAM,INHIBIT External Inhibit occurred
271 # LAM,TRIP ERROR Software current trip occurred
272 # LAM,INPUT ERROR Wrong command received
273 # LAM,OK Status OK
274 get_process=lambda r:
275 r[1].strip().encode(EurotestHPP120256.response_encoding).decode('utf-8', 'ignore')
276 )
277
278 def emergency_off(self):
279 """ The output of the HV source will be switched OFF permanently and the values
280 of the voltage and current settings set to zero"""
281 log.info("Sending emergency off command to the instrument.")
282
283 self.write("EMCY OFF")
284
285 def shutdown(self, voltage_rate=200.0):
286 """
287 Change the output voltage setting (V) to zero and
288 the ramp speed - voltage_rate (V/s) of the output voltage.
289 After calling shutdown, if the HV voltage output > 0
290 it should drop to zero at a certain rate given by the voltage_rate parameter.
291 :param voltage_rate: indicates the changing rate (V/s) of the voltage output
292 """
293 log.info(f"Executing the shutdown function with voltage_rate: {voltage_rate} V/s.")
294
295 self.ramp_to_zero(voltage_rate)
296 super().shutdown()
297
298 def ramp_to_zero(self, voltage_rate=200.0):
299 """
300 Sets the voltage output setting to zero and the ramp setting
301 to a value determined by the voltage_rate parameter.
302 In summary, the method conducts (ramps) the voltage output to zero
303 at a determinated voltage changing rate (ramp in V/s).
304 :param voltage_rate: Is the changing rate (ramp in V/s) for the ramp setting
305 """
306 log.info(f"Executing the ramp_to_zero function with ramp: {voltage_rate} V/s.")
307
308 self.voltage_ramp = voltage_rate
309 self.voltage_setpoint = 0
310
311 def wait_for_output_voltage_reached(self, voltage_setpoint, abs_output_voltage_error=0.03,
312 check_period=1.0, timeout=60.0):
313 """
314 Wait until HV voltage output reaches the voltage setpoint.
315
316 Checks the voltage output every check_period seconds and raises an exception
317 if the voltage output doesn't reach the voltage setting until the timeout time.
318 :param voltage_setpoint: the voltage in kVolts setted in the HV power supply which
319 should be present at the output after some time (depends on the ramp setting).
320 :param abs_output_voltage_error: absolute error in kVolts for being considered
321 an output voltage reached.
322 :param check_period: voltage output will be measured every check_period (seconds) time.
323 :param timeout: time (seconds) give to the voltage output to reach the voltage setting.
324 :return: None
325 :raises: Exception if the voltage output can't reach the voltage setting
326 before the timeout completes (seconds).
327 """
328 log.info("Executing the wait_for_output_voltage_reached function.")
329
330 ref_time = time.time()
331 future_time = ref_time + timeout
332
333 log.debug(f"\tWaiting for voltage output set. "
334 f"Reading output voltage every {check_period} seconds.\n"
335 f"\tTimeout: {timeout} seconds.")
336
337 while True:
338 actual_time = time.time()
339 time.sleep(check_period) # wait for voltage output reaches the voltage output setting
340 voltage_output = self.voltage
341 # check if voltage_output is set. If so then no more wait
342 if math.isclose(voltage_output, voltage_setpoint,
343 rel_tol=0.0, abs_tol=abs_output_voltage_error):
344 break
345
346 log.debug("voltage_output_valid_range: "
347 "[" + str(voltage_setpoint - abs_output_voltage_error) +
348 ", " + str(voltage_setpoint + abs_output_voltage_error) + "]")
349 log.debug("voltage_output: " + str(voltage_output))
350 log.debug(f"Elapsed time: {round(actual_time - ref_time, ndigits=1)} seconds.")
351
352 if actual_time > future_time:
353 self.shutdown() # in case the voltage were applied at the output
354 raise TimeoutError("Timeout for wait_for_output_voltage_reached function")
355
356 log.info("Waiting for voltage output set done.")
357
358 # Wrapper functions for the Adapter object
359 def write(self, command, **kwargs):
360 """Overrides Instrument write method for including write_delay time after the parent call.
361
362 :param command: command string to be sent to the instrument
363 """
364 actual_write_delay = time.time() - self.last_write_timestamp
365 time.sleep(max(0, self.write_delay - actual_write_delay))
366 super().write(command, **kwargs)
367 self.last_write_timestamp = time.time()
368
369 def ask(self, command):
370 """ Overrides Instrument ask method for including query_delay time on parent call.
371 :param command: Command string to be sent to the instrument.
372 :returns: String returned by the device without read_termination.
373 """
374 return super().ask(command, self.query_delay)
375
376 class EurotestHPP120256Status(IntFlag):
377 """
378 Auxiliary class create for translating the instrument 16bits_status_string into
379 an Enum_IntFlag that will help to the user to understand such status.
380 """
381 # Status response from the instrument has to be interpreted as follows:
382 #
383 # response DI, b15 b14 b13 b12 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0,
384 # bit = 0,1
385 # IpErr b15 no input error input error
386 # Ramp b14 no ramp ramp
387 # CutOut b13 - emergency off
388 # TpErr b12 no trip error trip error
389 # F3 b11 reserved
390 # F2 b10 reserved
391 # menu1 b9 submenu off submenu on
392 # menu0 b8 menu off menu on
393 # err b7 no error error
394 # Creg b6 no current control current control
395 # Vreg b5 no voltage control voltage control
396 # pol b4 negative positive
397 # inh b3 no ext. inhibit external inhibit
398 # local b2 remote local
399 # kilena b1 kill disable kill enable
400 # on b0 off high voltage is ON
401 #
402 # For example, a status_string = "0100000000000111" will be translated to
403 # EurotestHPP120256_status.RAMP|LOCAL|KILL_ENABLE|OUTPUT_ON
404
405 INPUT_ERROR = 32768
406 RAMP = 16384
407 EMERGENCY_OFF = 8192
408 TRIP_ERROR = 4096
409 F3 = 2048
410 F2 = 1024
411 SUBMENU_ON = 512
412 MENU_ON = 256
413 ERROR = 128
414 CURRENT_CONTROL = 64
415 VOLTAGE_CONTROL = 32
416 POLARIZATION_POSITIVE = 16
417 EXTERNAL_INHIBIT = 8
418 LOCAL = 4
419 KILL_ENABLE = 2
420 OUTPUT_ON = 1