Debian Codegraph @

pymeasure/instruments/keithley/keithley6221.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 logging
26 import time
27 from warnings import warn
28
29 import numpy as np
30
31 from pymeasure.instruments import Instrument, SCPIMixin
32 from pymeasure.errors import RangeException
33 from pymeasure.instruments.validators import truncated_range, strict_discrete_set
34
35 from .buffer import KeithleyBuffer
36
37 log = logging.getLogger(__name__)
38 log.addHandler(logging.NullHandler())
39
40
41 class Keithley6221(KeithleyBuffer, SCPIMixin, Instrument):
42 """ Represents the Keithley 6221 AC and DC current source and provides a
43 high-level interface for interacting with the instrument.
44
45 .. code-block:: python
46
47 keithley = Keithley6221("GPIB::1")
48 keithley.clear()
49
50 # Use the keithley as an AC source
51 keithley.waveform_function = "square" # Set a square waveform
52 keithley.waveform_amplitude = 0.05 # Set the amplitude in Amps
53 keithley.waveform_offset = 0 # Set zero offset
54 keithley.source_compliance = 10 # Set compliance (limit) in V
55 keithley.waveform_dutycycle = 50 # Set duty cycle of wave in %
56 keithley.waveform_frequency = 347 # Set the frequency in Hz
57 keithley.waveform_ranging = "best" # Set optimal output ranging
58 keithley.waveform_duration_cycles = 100 # Set duration of the waveform
59
60 # Link end of waveform to Service Request status bit
61 keithley.operation_event_enabled = 128 # OSB listens to end of wave
62 keithley.srq_event_enabled = 128 # SRQ listens to OSB
63
64 keithley.waveform_arm() # Arm (load) the waveform
65
66 keithley.waveform_start() # Start the waveform
67
68 keithley.adapter.wait_for_srq() # Wait for the pulse to finish
69
70 keithley.waveform_abort() # Disarm (unload) the waveform
71
72 keithley.shutdown() # Disables output
73
74 """
75
76 def __init__(self, adapter, name="Keithley 6221 SourceMeter", **kwargs):
77 super().__init__(
78 adapter,
79 name,
80 **kwargs)
81
82 ##########
83 # OUTPUT #
84 ##########
85
86 source_enabled = Instrument.control(
87 "OUTPut?", "OUTPut %d",
88 """A boolean property that controls whether the source is enabled, takes
89 values True or False. The convenience methods :meth:`~.Keithley6221.enable_source` and
90 :meth:`~.Keithley6221.disable_source` can also be used.""",
91 validator=strict_discrete_set,
92 values={True: 1, False: 0},
93 map_values=True
94 )
95
96 source_delay = Instrument.control(
97 ":SOUR:DEL?", ":SOUR:DEL %g",
98 """ A floating point property that sets a manual delay for the source
99 after the output is turned on before a measurement is taken. When this
100 property is set, the auto delay is turned off. Valid values are
101 between 1e-3 [seconds] and 999999.999 [seconds].""",
102 validator=truncated_range,
103 values=[1e-3, 999999.999],
104 )
105
106 output_low_grounded = Instrument.control(
107 ":OUTP:LTE?", "OUTP:LTE %d",
108 """ A boolean property that controls whether the low output of the triax
109 connection is connected to earth ground (True) or is floating (False). """,
110 validator=strict_discrete_set,
111 values={True: 1, False: 0},
112 map_values=True
113 )
114
115 ##########
116 # SOURCE #
117 ##########
118
119 source_current = Instrument.control(
120 ":SOUR:CURR?", ":SOUR:CURR %g",
121 """ A floating point property that controls the source current
122 in Amps. """,
123 validator=truncated_range,
124 values=[-0.105, 0.105]
125 )
126 source_compliance = Instrument.control(
127 ":SOUR:CURR:COMP?", ":SOUR:CURR:COMP %g",
128 """A floating point property that controls the compliance of the current
129 source in Volts. valid values are in range 0.1 [V] to 105 [V].""",
130 validator=truncated_range,
131 values=[0.1, 105])
132 source_range = Instrument.control(
133 ":SOUR:CURR:RANG?", ":SOUR:CURR:RANG:AUTO 0;:SOUR:CURR:RANG %g",
134 """ A floating point property that controls the source current
135 range in Amps, which can take values between -0.105 A and +0.105 A.
136 Auto-range is disabled when this property is set. """,
137 validator=truncated_range,
138 values=[-0.105, 0.105]
139 )
140 source_auto_range = Instrument.control(
141 ":SOUR:CURR:RANG:AUTO?", ":SOUR:CURR:RANG:AUTO %d",
142 """ A boolean property that controls the auto range of the current source.
143 Valid values are True or False. """,
144 values={True: 1, False: 0},
145 map_values=True,
146 )
147
148 ##################
149 # WAVE FUNCTIONS #
150 ##################
151
152 waveform_function = Instrument.control(
153 ":SOUR:WAVE:FUNC?", ":SOUR:WAVE:FUNC %s",
154 """ A string property that controls the selected wave function. Valid
155 values are "sine", "ramp", "square", "arbitrary1", "arbitrary2",
156 "arbitrary3" and "arbitrary4". """,
157 values={
158 "sine": "SIN",
159 "ramp": "RAMP",
160 "square": "SQU",
161 "arbitrary1": "ARB1",
162 "arbitrary2": "ARB2",
163 "arbitrary3": "ARB3",
164 "arbitrary4": "ARB4",
165 },
166 map_values=True
167 )
168
169 waveform_frequency = Instrument.control(
170 ":SOUR:WAVE:FREQ?", ":SOUR:WAVE:FREQ %g",
171 """A floating point property that controls the frequency of the
172 waveform in Hertz. Valid values are in range 1e-3 to 1e5. """,
173 validator=truncated_range,
174 values=[1e-3, 1e5]
175 )
176 waveform_amplitude = Instrument.control(
177 ":SOUR:WAVE:AMPL?", ":SOUR:WAVE:AMPL %g",
178 """A floating point property that controls the (peak) amplitude of the
179 waveform in Amps. Valid values are in range 2e-12 to 0.105. """,
180 validator=truncated_range,
181 values=[2e-12, 0.105]
182 )
183 waveform_offset = Instrument.control(
184 ":SOUR:WAVE:OFFS?", ":SOUR:WAVE:OFFS %g",
185 """A floating point property that controls the offset of the waveform
186 in Amps. Valid values are in range -0.105 to 0.105. """,
187 validator=truncated_range,
188 values=[-0.105, 0.105]
189 )
190 waveform_dutycycle = Instrument.control(
191 ":SOUR:WAVE:DCYC?", ":SOUR:WAVE:DCYC %g",
192 """A floating point property that controls the duty-cycle of the
193 waveform in percent for the square and ramp waves. Valid values are in
194 range 0 to 100. """,
195 validator=truncated_range,
196 values=[0, 100]
197 )
198 waveform_duration_time = Instrument.control(
199 ":SOUR:WAVE:DUR:TIME?", ":SOUR:WAVE:DUR:TIME %g",
200 """A floating point property that controls the duration of the
201 waveform in seconds. Valid values are in range 100e-9 to 999999.999.
202 """,
203 validator=truncated_range,
204 values=[100e-9, 999999.999]
205 )
206 waveform_duration_cycles = Instrument.control(
207 ":SOUR:WAVE:DUR:CYCL?", ":SOUR:WAVE:DUR:CYCL %g",
208 """A floating point property that controls the duration of the
209 waveform in cycles. Valid values are in range 1e-3 to 99999999900.
210 """,
211 validator=truncated_range,
212 values=[1e-3, 99999999900]
213 )
214
215 def waveform_duration_set_infinity(self):
216 """ Set the waveform duration to infinity.
217 """
218 self.write(":SOUR:WAVE:DUR:TIME INF")
219
220 waveform_ranging = Instrument.control(
221 ":SOUR:WAVE:RANG?", ":SOUR:WAVE:RANG %s",
222 """ A string property that controls the source ranging of the
223 waveform. Valid values are "best" and "fixed". """,
224 values={"best": "BEST", "fixed": "FIX"},
225 map_values=True,
226 )
227 waveform_use_phasemarker = Instrument.control(
228 ":SOUR:WAVE:PMAR:STAT?", ":SOUR:WAVE:PMAR:STAT %s",
229 """ A boolean property that controls whether the phase marker option
230 is turned on or of. Valid values True (on) or False (off). Other
231 settings for the phase marker have not yet been implemented.""",
232 values={True: 1, False: 0},
233 map_values=True,
234 )
235 waveform_phasemarker_phase = Instrument.control(
236 ":SOUR:WAVE:PMAR?", ":SOUR:WAVE:PMAR %g",
237 """ A numerical property that controls the phase of the phase marker.""",
238 validator=truncated_range,
239 values=[-180, 180],
240 )
241 waveform_phasemarker_line = Instrument.control(
242 ":SOUR:WAVE:PMAR:OLIN?", ":SOUR:WAVE:PMAR:OLIN %d",
243 """ A numerical property that controls the line of the phase marker.""",
244 validator=truncated_range,
245 values=[1, 6],
246 )
247
248 def waveform_arm(self):
249 """ Arm the current waveform function. """
250 self.write(":SOUR:WAVE:ARM")
251
252 def waveform_start(self):
253 """ Start the waveform output. Must already be armed """
254 self.write(":SOUR:WAVE:INIT")
255
256 def waveform_abort(self):
257 """ Abort the waveform output and disarm the waveform function. """
258 self.write(":SOUR:WAVE:ABOR")
259
260 def define_arbitary_waveform(self, datapoints, location=1):
261 """ Define the data points for the arbitrary waveform and copy the
262 defined waveform into the given storage location.
263
264 :param datapoints: a list (or numpy array) of the data points; all
265 values have to be between -1 and 1; 100 points maximum.
266 :param location: integer storage location to store the waveform in.
267 Value must be in range 1 to 4.
268 """
269
270 # Check validity of parameters
271 if not isinstance(datapoints, (list, np.ndarray)):
272 raise ValueError("datapoints must be a list or numpy array")
273 elif len(datapoints) > 100:
274 raise ValueError("datapoints cannot be longer than 100 points")
275 elif not all([x >= -1 and x <= 1 for x in datapoints]):
276 raise ValueError("all data points must be between -1 and 1")
277
278 if location not in [1, 2, 3, 4]:
279 raise ValueError("location must be in [1, 2, 3, 4]")
280
281 # Make list of strings
282 datapoints = [str(x) for x in datapoints]
283 data = ", ".join(datapoints)
284
285 # Write the data points to the Keithley 6221
286 self.write(":SOUR:WAVE:ARB:DATA %s" % data)
287
288 # Copy the written data to the specified location
289 self.write(":SOUR:WAVE:ARB:COPY %d" % location)
290
291 # Select the newly made arbitrary waveform as waveform function
292 self.waveform_function = "arbitrary%d" % location
293
294 def enable_source(self):
295 """ Enables the source of current or voltage depending on the
296 configuration of the instrument. """
297 self.write("OUTPUT ON")
298
299 def disable_source(self):
300 """ Disables the source of current or voltage depending on the
301 configuration of the instrument. """
302 self.write("OUTPUT OFF")
303
304 def beep(self, frequency, duration):
305 """ Sounds a system beep.
306
307 :param frequency: A frequency in Hz between 65 Hz and 2 MHz
308 :param duration: A time in seconds between 0 and 7.9 seconds
309 """
310 self.write(f":SYST:BEEP {frequency:g}, {duration:g}")
311
312 def triad(self, base_frequency, duration):
313 """ Sounds a musical triad using the system beep.
314
315 :param base_frequency: A frequency in Hz between 65 Hz and 1.3 MHz
316 :param duration: A time in seconds between 0 and 7.9 seconds
317 """
318 self.beep(base_frequency, duration)
319 time.sleep(duration)
320 self.beep(base_frequency * 5.0 / 4.0, duration)
321 time.sleep(duration)
322 self.beep(base_frequency * 6.0 / 4.0, duration)
323
324 display_enabled = Instrument.control(
325 ":DISP:ENAB?", ":DISP:ENAB %d",
326 """ A boolean property that controls whether or not the display of the
327 sourcemeter is enabled. Valid values are True and False. """,
328 values={True: 1, False: 0},
329 map_values=True,
330 )
331
332 @property
333 def error(self):
334 warn("Deprecated to use `error`, use `next_error` instead.", FutureWarning)
335 return self.next_error
336
337 def reset(self):
338 """ Resets the instrument and clears the queue. """
339 self.write("status:queue:clear;*RST;:stat:pres;:*CLS;")
340
341 def trigger(self):
342 """ Executes a bus trigger, which can be used when
343 :meth:`~.trigger_on_bus` is configured.
344 """
345 return self.write("*TRG")
346
347 def trigger_immediately(self):
348 """ Configures measurements to be taken with the internal
349 trigger at the maximum sampling rate.
350 """
351 self.write(":ARM:SOUR IMM;:TRIG:SOUR IMM;")
352
353 def trigger_on_bus(self):
354 """ Configures the trigger to detect events based on the bus
355 trigger, which can be activated by :meth:`~.trigger`.
356 """
357 self.write(":ARM:SOUR BUS;:TRIG:SOUR BUS;")
358
359 def set_timed_arm(self, interval):
360 """ Sets up the measurement to be taken with the internal
361 trigger at a variable sampling rate defined by the interval
362 in seconds between sampling points
363 """
364 if interval > 99999.99 or interval < 0.001:
365 raise RangeException("Keithley 6221 can only be time"
366 " triggered between 1 mS and 1 Ms")
367 self.write(":ARM:SOUR TIM;:ARM:TIM %.3f" % interval)
368
369 def trigger_on_external(self, line=1):
370 """ Configures the measurement trigger to be taken from a
371 specific line of an external trigger
372
373 :param line: A trigger line from 1 to 4
374 """
375 cmd = ":ARM:SOUR TLIN;:TRIG:SOUR TLIN;"
376 cmd += ":ARM:ILIN %d;:TRIG:ILIN %d;" % (line, line)
377 self.write(cmd)
378
379 def output_trigger_on_external(self, line=1, after='DEL'):
380 """ Configures the output trigger on the specified trigger link
381 line number, with the option of supplying the part of the
382 measurement after which the trigger should be generated
383 (default to delay, which is right before the measurement)
384
385 :param line: A trigger line from 1 to 4
386 :param after: An event string that determines when to trigger
387 """
388 self.write(":TRIG:OUTP %s;:TRIG:OLIN %d;" % (after, line))
389
390 def disable_output_trigger(self):
391 """ Disables the output trigger for the Trigger layer
392 """
393 self.write(":TRIG:OUTP NONE")
394
395 def shutdown(self):
396 """ Disables the output. """
397 log.info("Shutting down %s." % self.name)
398 self.disable_source()
399 super().shutdown()
400
401 ###############
402 # Status bits #
403 ###############
404
405 measurement_event_enabled = Instrument.control(
406 ":STAT:MEAS:ENAB?", ":STAT:MEAS:ENAB %d",
407 """ An integer value that controls which measurement events are
408 registered in the Measurement Summary Bit (MSB) status bit. Refer to
409 the Model 6220/6221 Reference Manual for more information about
410 programming the status bits.
411 """,
412 cast=int,
413 validator=truncated_range,
414 values=[0, 65535],
415 )
416
417 operation_event_enabled = Instrument.control(
418 ":STAT:OPER:ENAB?", ":STAT:OPER:ENAB %d",
419 """ An integer value that controls which operation events are
420 registered in the Operation Summary Bit (OSB) status bit. Refer to
421 the Model 6220/6221 Reference Manual for more information about
422 programming the status bits.
423 """,
424 cast=int,
425 validator=truncated_range,
426 values=[0, 65535],
427 )
428
429 questionable_event_enabled = Instrument.control(
430 ":STAT:QUES:ENAB?", ":STAT:QUES:ENAB %d",
431 """ An integer value that controls which questionable events are
432 registered in the Questionable Summary Bit (QSB) status bit. Refer to
433 the Model 6220/6221 Reference Manual for more information about
434 programming the status bits.
435 """,
436 cast=int,
437 validator=truncated_range,
438 values=[0, 65535],
439 )
440
441 standard_event_enabled = Instrument.control(
442 "ESE?", "ESE %d",
443 """ An integer value that controls which standard events are
444 registered in the Event Summary Bit (ESB) status bit. Refer to
445 the Model 6220/6221 Reference Manual for more information about
446 programming the status bits.
447 """,
448 cast=int,
449 validator=truncated_range,
450 values=[0, 65535],
451 )
452
453 srq_event_enabled = Instrument.control(
454 "*SRE?", "*SRE %d",
455 """ An integer value that controls which event registers trigger the
456 Service Request (SRQ) status bit. Refer to the Model 6220/6221
457 Reference Manual for more information about programming the status
458 bits.
459 """,
460 cast=int,
461 validator=truncated_range,
462 values=[0, 255],
463 )
464
465 measurement_events = Instrument.measurement(
466 ":STAT:MEAS?",
467 """ An integer value that reads which measurement events have been
468 registered in the Measurement event registers. Refer to the Model
469 6220/6221 Reference Manual for more information about programming
470 the status bits. Reading this value clears the register.
471 """,
472 cast=int,
473 )
474
475 operation_events = Instrument.measurement(
476 ":STAT:OPER?",
477 """ An integer value that reads which operation events have been
478 registered in the Operation event registers. Refer to the Model
479 6220/6221 Reference Manual for more information about programming
480 the status bits. Reading this value clears the register.
481 """,
482 cast=int,
483 )
484
485 questionable_events = Instrument.measurement(
486 ":STAT:QUES?",
487 """ An integer value that reads which questionable events have been
488 registered in the Questionable event registers. Refer to the Model
489 6220/6221 Reference Manual for more information about programming
490 the status bits. Reading this value clears the register.
491 """,
492 cast=int,
493 )
494
495 standard_events = Instrument.measurement(
496 "*ESR?",
497 """ An integer value that reads which standard events have been
498 registered in the Standard event registers. Refer to the Model
499 6220/6221 Reference Manual for more information about programming
500 the status bits. Reading this value clears the register.
501 """,
502 cast=int,
503 )