Debian Codegraph @

pymeasure/instruments/keithley/keithley2600.py

raw
1 # This file is part of the PyMeasure package.
2 #
3 # Copyright (c) 2013-2024 PyMeasure Developers
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22 #
23
24 import logging
25 import time
26 from warnings import warn
27
28 import numpy as np
29 from pymeasure.instruments import Instrument, SCPIUnknownMixin
30 from pymeasure.instruments.validators import truncated_range, strict_discrete_set
31
32 # Setup logging
33 log = logging.getLogger(__name__)
34 log.addHandler(logging.NullHandler())
35
36
37 class Keithley2600(SCPIUnknownMixin, Instrument):
38 """Represents the Keithley 2600 series (channel A and B) SourceMeter"""
39
40 def __init__(self, adapter, name="Keithley 2600 SourceMeter", **kwargs):
41 super().__init__(
42 adapter,
43 name,
44 **kwargs
45 )
46 self.ChA = Channel(self, 'a')
47 self.ChB = Channel(self, 'b')
48
49 @property
50 def next_error(self):
51 """ Returns a tuple of an error code and message from a
52 single error. """
53 err = self.ask('print(errorqueue.next())')
54 err = err.split('\t')
55 # Keithley Instruments Inc. sometimes on startup
56 # if tab delimitated message is greater than one, grab first two as code, message
57 # otherwise, assign code & message to returned error
58 if len(err) > 1:
59 err = (int(float(err[0])), err[1])
60 code = err[0]
61 message = err[1].replace('"', '')
62 else:
63 code = message = err[0]
64 log.info(f"ERROR {str(code)},{str(message)} - len {str(len(err))}")
65 return (code, message)
66
67 @property
68 def error(self):
69 warn("Deprecated to use `error`, use `next_error` instead.", FutureWarning)
70 return self.next_error
71
72
73 class Channel:
74
75 def __init__(self, instrument, channel):
76 self.instrument = instrument
77 self.channel = channel
78
79 def ask(self, cmd):
80 return float(self.instrument.ask(f'print(smu{self.channel}.{cmd})'))
81
82 def write(self, cmd):
83 self.instrument.write(f'smu{self.channel}.{cmd}')
84
85 def values(self, cmd, **kwargs):
86 """ Reads a set of values from the instrument through the adapter,
87 passing on any key-word arguments.
88 """
89 return self.instrument.values(f'print(smu{self.channel}.{cmd})')
90
91 def binary_values(self, cmd, header_bytes=0, dtype=np.float32):
92 return self.instrument.binary_values('print(smu%s.%s)' %
93 (self.channel, cmd,), header_bytes, dtype)
94
95 def check_errors(self):
96 return self.instrument.check_errors()
97
98 source_output = Instrument.control(
99 'source.output', 'source.output=%d',
100 """Property controlling the channel output state (ON of OFF)
101 """,
102 validator=strict_discrete_set,
103 values={'OFF': 0, 'ON': 1},
104 map_values=True
105 )
106
107 source_mode = Instrument.control(
108 'source.func', 'source.func=%d',
109 """Property controlling the channel source function (Voltage or Current)
110 """,
111 validator=strict_discrete_set,
112 values={'voltage': 1, 'current': 0},
113 map_values=True
114 )
115
116 measure_nplc = Instrument.control(
117 'measure.nplc', 'measure.nplc=%f',
118 """ Property controlling the nplc value """,
119 validator=truncated_range,
120 values=[0.001, 25],
121 map_values=True
122 )
123
124 ###############
125 # Current (A) #
126 ###############
127 current = Instrument.measurement(
128 'measure.i()',
129 """ Reads the current in Amps """
130 )
131
132 source_current = Instrument.control(
133 'source.leveli', 'source.leveli=%f',
134 """ Property controlling the applied source current """,
135 validator=truncated_range,
136 values=[-1.5, 1.5]
137 )
138
139 compliance_current = Instrument.control(
140 'source.limiti', 'source.limiti=%f',
141 """ Property controlling the source compliance current """,
142 validator=truncated_range,
143 values=[-1.5, 1.5]
144 )
145
146 source_current_range = Instrument.control(
147 'source.rangei', 'source.rangei=%f',
148 """Property controlling the source current range """,
149 validator=truncated_range,
150 values=[-1.5, 1.5]
151 )
152
153 current_range = Instrument.control(
154 'measure.rangei', 'measure.rangei=%f',
155 """Property controlling the measurement current range """,
156 validator=truncated_range,
157 values=[-1.5, 1.5]
158 )
159
160 ###############
161 # Voltage (V) #
162 ###############
163 voltage = Instrument.measurement(
164 'measure.v()',
165 """ Reads the voltage in Volts """
166 )
167
168 source_voltage = Instrument.control(
169 'source.levelv', 'source.levelv=%f',
170 """ Property controlling the applied source voltage """,
171 validator=truncated_range,
172 values=[-200, 200]
173 )
174
175 compliance_voltage = Instrument.control(
176 'source.limitv', 'source.limitv=%f',
177 """ Property controlling the source compliance voltage """,
178 validator=truncated_range,
179 values=[-200, 200]
180 )
181
182 source_voltage_range = Instrument.control(
183 'source.rangev', 'source.rangev=%f',
184 """Property controlling the source current range """,
185 validator=truncated_range,
186 values=[-200, 200]
187 )
188
189 voltage_range = Instrument.control(
190 'measure.rangev', 'measure.rangev=%f',
191 """Property controlling the measurement voltage range """,
192 validator=truncated_range,
193 values=[-200, 200]
194 )
195
196 ####################
197 # Resistance (Ohm) #
198 ####################
199 resistance = Instrument.measurement(
200 'measure.r()',
201 """ Reads the resistance in Ohms """
202 )
203
204 wires_mode = Instrument.control(
205 'sense', 'sense=%d',
206 """Property controlling the resistance measurement mode: 4 wires or 2 wires""",
207 validator=strict_discrete_set,
208 values={'4': 1, '2': 0},
209 map_values=True
210 )
211
212 #######################
213 # Measurement Methods #
214 #######################
215
216 def measure_voltage(self, nplc=1, voltage=21.0, auto_range=True):
217 """ Configures the measurement of voltage.
218 :param nplc: Number of power line cycles (NPLC) from 0.001 to 25
219 :param voltage: Upper limit of voltage in Volts, from -200 V to 200 V
220 :param auto_range: Enables auto_range if True, else uses the set voltage
221 """
222 log.info("%s is measuring voltage." % self.channel)
223 self.write('measure.v()')
224 self.write('measure.nplc=%f' % nplc)
225 if auto_range:
226 self.write('measure.autorangev=1')
227 else:
228 self.voltage_range = voltage
229 self.check_errors()
230
231 def measure_current(self, nplc=1, current=1.05e-4, auto_range=True):
232 """ Configures the measurement of current.
233 :param nplc: Number of power line cycles (NPLC) from 0.001 to 25
234 :param current: Upper limit of current in Amps, from -1.5 A to 1.5 A
235 :param auto_range: Enables auto_range if True, else uses the set current
236 """
237 log.info("%s is measuring current." % self.channel)
238 self.write('measure.i()')
239 self.write('measure.nplc=%f' % nplc)
240 if auto_range:
241 self.write('measure.autorangei=1')
242 else:
243 self.current_range = current
244 self.check_errors()
245
246 def auto_range_source(self):
247 """ Configures the source to use an automatic range.
248 """
249 if self.source_mode == 'current':
250 self.write('source.autorangei=1')
251 else:
252 self.write('source.autorangev=1')
253
254 def apply_current(self, current_range=None, compliance_voltage=0.1):
255 """ Configures the instrument to apply a source current, and
256 uses an auto range unless a current range is specified.
257 The compliance voltage is also set.
258 :param compliance_voltage: A float in the correct range for a
259 :attr:`~.Keithley2600.compliance_voltage`
260 :param current_range: A :attr:`~.Keithley2600.current_range` value or None
261 """
262 log.info("%s is sourcing current." % self.channel)
263 self.source_mode = 'current'
264 if current_range is None:
265 self.auto_range_source()
266 else:
267 self.source_current_range = current_range
268 self.compliance_voltage = compliance_voltage
269 self.check_errors()
270
271 def apply_voltage(self, voltage_range=None,
272 compliance_current=0.1):
273 """ Configures the instrument to apply a source voltage, and
274 uses an auto range unless a voltage range is specified.
275 The compliance current is also set.
276 :param compliance_current: A float in the correct range for a
277 :attr:`~.Keithley2600.compliance_current`
278 :param voltage_range: A :attr:`~.Keithley2600.voltage_range` value or None
279 """
280 log.info("%s is sourcing voltage." % self.channel)
281 self.source_mode = 'voltage'
282 if voltage_range is None:
283 self.auto_range_source()
284 else:
285 self.source_voltage_range = voltage_range
286 self.compliance_current = compliance_current
287 self.check_errors()
288
289 def ramp_to_voltage(self, target_voltage, steps=30, pause=0.1):
290 """ Ramps to a target voltage from the set voltage value over
291 a certain number of linear steps, each separated by a pause duration.
292 :param target_voltage: A voltage in Amps
293 :param steps: An integer number of steps
294 :param pause: A pause duration in seconds to wait between steps """
295 voltages = np.linspace(self.source_voltage, target_voltage, steps)
296 for voltage in voltages:
297 self.source_voltage = voltage
298 time.sleep(pause)
299
300 def ramp_to_current(self, target_current, steps=30, pause=0.1):
301 """ Ramps to a target current from the set current value over
302 a certain number of linear steps, each separated by a pause duration.
303 :param target_current: A current in Amps
304 :param steps: An integer number of steps
305 :param pause: A pause duration in seconds to wait between steps """
306 currents = np.linspace(self.source_current, target_current, steps)
307 for current in currents:
308 self.source_current = current
309 time.sleep(pause)
310
311 def shutdown(self):
312 """ Ensures that the current or voltage is turned to zero
313 and disables the output. """
314 log.info("Shutting down channel %s." % self.channel)
315 if self.source_mode == 'current':
316 self.ramp_to_current(0.0)
317 else:
318 self.ramp_to_voltage(0.0)
319 self.source_output = 'OFF'