| 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 |
import re |
| 28 |
from warnings import warn |
| 29 |
|
| 30 |
import numpy as np |
| 31 |
|
| 32 |
from pymeasure.instruments import Instrument, SCPIMixin |
| 33 |
from pymeasure.instruments.validators import truncated_range |
| 34 |
from .buffer import KeithleyBuffer |
| 35 |
|
| 36 |
log = logging.getLogger(__name__) |
| 37 |
log.addHandler(logging.NullHandler()) |
| 38 |
|
| 39 |
|
| 40 |
class Keithley6517B(KeithleyBuffer, SCPIMixin, Instrument): |
| 41 |
""" Represents the Keithley 6517B ElectroMeter and provides a |
| 42 |
high-level interface for interacting with the instrument. |
| 43 |
|
| 44 |
.. code-block:: python |
| 45 |
|
| 46 |
keithley = Keithley6517B("GPIB::1") |
| 47 |
|
| 48 |
keithley.apply_voltage() # Sets up to source current |
| 49 |
keithley.source_voltage_range = 200 # Sets the source voltage |
| 50 |
# range to 200 V |
| 51 |
keithley.source_voltage = 20 # Sets the source voltage to 20 V |
| 52 |
keithley.enable_source() # Enables the source output |
| 53 |
|
| 54 |
keithley.measure_resistance() # Sets up to measure resistance |
| 55 |
|
| 56 |
keithley.ramp_to_voltage(50) # Ramps the voltage to 50 V |
| 57 |
print(keithley.resistance) # Prints the resistance in Ohms |
| 58 |
|
| 59 |
keithley.shutdown() # Ramps the voltage to 0 V |
| 60 |
# and disables output |
| 61 |
|
| 62 |
""" |
| 63 |
|
| 64 |
def __init__(self, adapter, name="Keithley 6517B Electrometer/High Resistance Meter", **kwargs): |
| 65 |
super().__init__( |
| 66 |
adapter, name, |
| 67 |
**kwargs |
| 68 |
) |
| 69 |
|
| 70 |
source_enabled = Instrument.measurement( |
| 71 |
"OUTPUT?", |
| 72 |
""" Reads a boolean value that is True if the source is enabled. """, |
| 73 |
cast=bool |
| 74 |
) |
| 75 |
|
| 76 |
@staticmethod |
| 77 |
def extract_value(result): |
| 78 |
""" extracts the physical value from a result object returned |
| 79 |
by the instrument """ |
| 80 |
m = re.fullmatch(r'([+\-0-9E.]+)[A-Z]{4}', result[0]) |
| 81 |
if m: |
| 82 |
return float(m.group(1)) |
| 83 |
return None |
| 84 |
|
| 85 |
############### |
| 86 |
# Current (A) # |
| 87 |
############### |
| 88 |
|
| 89 |
current = Instrument.measurement( |
| 90 |
":MEAS?", |
| 91 |
""" Reads the current in Amps, if configured for this reading. |
| 92 |
""", get_process=extract_value |
| 93 |
) |
| 94 |
|
| 95 |
current_range = Instrument.control( |
| 96 |
":SENS:CURR:RANG?", ":SENS:CURR:RANG:AUTO 0;:SENS:CURR:RANG %g", |
| 97 |
""" A floating point property that controls the measurement current |
| 98 |
range in Amps, which can take values between -20 and +20 mA. |
| 99 |
Auto-range is disabled when this property is set. """, |
| 100 |
validator=truncated_range, |
| 101 |
values=[-20e-3, 20e-3] |
| 102 |
) |
| 103 |
|
| 104 |
current_nplc = Instrument.control( |
| 105 |
":SENS:CURR:NPLC?", ":SENS:CURR:NPLC %g", |
| 106 |
""" A floating point property that controls the number of power |
| 107 |
line cycles (NPLC) for the DC current measurements, which sets the |
| 108 |
integration period and measurement speed. Takes values from 0.01 to |
| 109 |
10, where 0.1, 1, and 10 are Fast, Medium, and Slow respectively. """, |
| 110 |
values=[0.01, 10] |
| 111 |
) |
| 112 |
|
| 113 |
source_current_resistance_limit = Instrument.control( |
| 114 |
":SOUR:CURR:RLIM?", ":SOUR:CURR:RLIM %g", |
| 115 |
""" Boolean property which enables or disables resistance |
| 116 |
current limit """, |
| 117 |
cast=bool |
| 118 |
) |
| 119 |
|
| 120 |
############### |
| 121 |
# Voltage (V) # |
| 122 |
############### |
| 123 |
|
| 124 |
voltage = Instrument.measurement( |
| 125 |
":MEAS:VOLT?", |
| 126 |
""" Reads the voltage in Volts, if configured for this reading. |
| 127 |
""", get_process=extract_value |
| 128 |
) |
| 129 |
|
| 130 |
voltage_range = Instrument.control( |
| 131 |
":SENS:VOLT:RANG?", ":SENS:VOLT:RANG:AUTO 0;:SENS:VOLT:RANG %g", |
| 132 |
""" A floating point property that controls the measurement voltage |
| 133 |
range in Volts, which can take values from -1000 to 1000 V. |
| 134 |
Auto-range is disabled when this property is set. """, |
| 135 |
validator=truncated_range, |
| 136 |
values=[-1000, 1000] |
| 137 |
) |
| 138 |
|
| 139 |
voltage_nplc = Instrument.control( |
| 140 |
":SENS:VOLT:NPLC?", ":SENS:VOLT:NPLC %g", |
| 141 |
""" A floating point property that controls the number of power |
| 142 |
line cycles (NPLC) for the DC voltage measurements, which sets the |
| 143 |
integration period and measurement speed. Takes values from 0.01 to |
| 144 |
10, where 0.1, 1, and 10 are Fast, Medium, and Slow respectively. """ |
| 145 |
) |
| 146 |
|
| 147 |
source_voltage = Instrument.control( |
| 148 |
":SOUR:VOLT?", ":SOUR:VOLT:LEV %g", |
| 149 |
""" A floating point property that controls the source voltage |
| 150 |
in Volts. """ |
| 151 |
) |
| 152 |
|
| 153 |
source_voltage_range = Instrument.control( |
| 154 |
":SOUR:VOLT:RANG?", ":SOUR:VOLT:RANG:AUTO 0;:SOUR:VOLT:RANG %g", |
| 155 |
""" A floating point property that controls the source voltage |
| 156 |
range in Volts, which can take values from -1000 to 1000 V. |
| 157 |
Auto-range is disabled when this property is set. """, |
| 158 |
validator=truncated_range, |
| 159 |
values=[-1000, 1000] |
| 160 |
) |
| 161 |
|
| 162 |
#################### |
| 163 |
# Resistance (Ohm) # |
| 164 |
#################### |
| 165 |
|
| 166 |
resistance = Instrument.measurement( |
| 167 |
":READ?", |
| 168 |
""" Reads the resistance in Ohms, if configured for this reading. |
| 169 |
""", get_process=extract_value |
| 170 |
) |
| 171 |
resistance_range = Instrument.control( |
| 172 |
":SENS:RES:RANG?", ":SENS:RES:RANG:AUTO 0;:SENS:RES:RANG %g", |
| 173 |
""" A floating point property that controls the resistance range |
| 174 |
in Ohms, which can take values from 0 to 100e18 Ohms. |
| 175 |
Auto-range is disabled when this property is set. """, |
| 176 |
validator=truncated_range, |
| 177 |
values=[0, 100e18] |
| 178 |
) |
| 179 |
resistance_nplc = Instrument.control( |
| 180 |
":SENS:RES:NPLC?", ":SENS:RES:NPLC %g", |
| 181 |
""" A floating point property that controls the number of power line cycles |
| 182 |
(NPLC) for the 2-wire resistance measurements, which sets the |
| 183 |
integration period and measurement speed. Takes values from 0.01 |
| 184 |
to 10, where 0.1, 1, and 10 are Fast, Medium, and Slow respectively. |
| 185 |
""" |
| 186 |
) |
| 187 |
|
| 188 |
buffer_points = Instrument.control( |
| 189 |
":TRAC:POIN?", ":TRAC:POIN %d", |
| 190 |
""" An integer property that controls the number of buffer points. This |
| 191 |
does not represent actual points in the buffer, but the configuration |
| 192 |
value instead. """, |
| 193 |
validator=truncated_range, |
| 194 |
values=[1, 6875000], |
| 195 |
cast=int |
| 196 |
) |
| 197 |
|
| 198 |
#################### |
| 199 |
# Methods # |
| 200 |
#################### |
| 201 |
|
| 202 |
def enable_source(self): |
| 203 |
""" Enables the source of current or voltage depending on the |
| 204 |
configuration of the instrument. """ |
| 205 |
self.write("OUTPUT ON") |
| 206 |
|
| 207 |
def disable_source(self): |
| 208 |
""" Disables the source of current or voltage depending on the |
| 209 |
configuration of the instrument. """ |
| 210 |
self.write("OUTPUT OFF") |
| 211 |
|
| 212 |
def measure_resistance(self, nplc=1, resistance=2.1e5, auto_range=True): |
| 213 |
""" Configures the measurement of resistance. |
| 214 |
|
| 215 |
:param nplc: Number of power line cycles (NPLC) from 0.01 to 10 |
| 216 |
:param resistance: Upper limit of resistance in Ohms, |
| 217 |
from -210 POhms to 210 POhms |
| 218 |
:param auto_range: Enables auto_range if True, else uses the |
| 219 |
resistance_range attribute |
| 220 |
""" |
| 221 |
log.info("%s is measuring resistance.", self.name) |
| 222 |
self.write(":SENS:FUNC 'RES';" |
| 223 |
":SENS:RES:NPLC %f;" % nplc) |
| 224 |
if auto_range: |
| 225 |
self.write(":SENS:RES:RANG:AUTO 1;") |
| 226 |
else: |
| 227 |
self.resistance_range = resistance |
| 228 |
self.check_errors() |
| 229 |
|
| 230 |
def measure_voltage(self, nplc=1, voltage=21.0, auto_range=True): |
| 231 |
""" Configures the measurement of voltage. |
| 232 |
|
| 233 |
:param nplc: Number of power line cycles (NPLC) from 0.01 to 10 |
| 234 |
:param voltage: Upper limit of voltage in Volts, from -1000 V to 1000 V |
| 235 |
:param auto_range: Enables auto_range if True, else uses the |
| 236 |
voltage_range attribute |
| 237 |
""" |
| 238 |
log.info("%s is measuring voltage.", self.name) |
| 239 |
self.write(":SENS:FUNC 'VOLT';" |
| 240 |
":SENS:VOLT:NPLC %f;" % nplc) |
| 241 |
if auto_range: |
| 242 |
self.write(":SENS:VOLT:RANG:AUTO 1;") |
| 243 |
else: |
| 244 |
self.voltage_range = voltage |
| 245 |
self.check_errors() |
| 246 |
|
| 247 |
def measure_current(self, nplc=1, current=1.05e-4, auto_range=True): |
| 248 |
""" Configures the measurement of current. |
| 249 |
|
| 250 |
:param nplc: Number of power line cycles (NPLC) from 0.01 to 10 |
| 251 |
:param current: Upper limit of current in Amps, from -21 mA to 21 mA |
| 252 |
:param auto_range: Enables auto_range if True, else uses the |
| 253 |
current_range attribute |
| 254 |
""" |
| 255 |
log.info("%s is measuring current.", self.name) |
| 256 |
self.write(":SENS:FUNC 'CURR';" |
| 257 |
":SENS:CURR:NPLC %f;" % nplc) |
| 258 |
if auto_range: |
| 259 |
self.write(":SENS:CURR:RANG:AUTO 1;") |
| 260 |
else: |
| 261 |
self.current_range = current |
| 262 |
self.check_errors() |
| 263 |
|
| 264 |
def auto_range_source(self): |
| 265 |
""" Configures the source to use an automatic range. |
| 266 |
""" |
| 267 |
self.write(":SOUR:VOLT:RANG:AUTO 1") |
| 268 |
|
| 269 |
def apply_voltage(self, voltage_range=None): |
| 270 |
""" Configures the instrument to apply a source voltage, and |
| 271 |
uses an auto range unless a voltage range is specified. |
| 272 |
|
| 273 |
:param voltage_range: A :attr:`~.Keithley6517B.voltage_range` value |
| 274 |
or None (activates auto range) |
| 275 |
""" |
| 276 |
log.info("%s is sourcing voltage.", self.name) |
| 277 |
if voltage_range is None: |
| 278 |
self.auto_range_source() |
| 279 |
else: |
| 280 |
self.source_voltage_range = voltage_range |
| 281 |
self.check_errors() |
| 282 |
|
| 283 |
@property |
| 284 |
def error(self): |
| 285 |
warn("Deprecated to use `error`, use `next_error` instead.", FutureWarning) |
| 286 |
return self.next_error |
| 287 |
|
| 288 |
def reset(self): |
| 289 |
""" Resets the instrument and clears the queue. """ |
| 290 |
self.write("*RST;:stat:pres;:*CLS;") |
| 291 |
|
| 292 |
def ramp_to_voltage(self, target_voltage, steps=30, pause=20e-3): |
| 293 |
""" Ramps to a target voltage from the set voltage value over |
| 294 |
a certain number of linear steps, each separated by a pause duration. |
| 295 |
|
| 296 |
:param target_voltage: A voltage in Volts |
| 297 |
:param steps: An integer number of steps |
| 298 |
:param pause: A pause duration in seconds to wait between steps |
| 299 |
""" |
| 300 |
voltages = np.linspace( |
| 301 |
self.source_voltage, |
| 302 |
target_voltage, |
| 303 |
steps |
| 304 |
) |
| 305 |
for voltage in voltages: |
| 306 |
self.source_voltage = voltage |
| 307 |
time.sleep(pause) |
| 308 |
|
| 309 |
def trigger(self): |
| 310 |
""" Executes a bus trigger, which can be used when |
| 311 |
:meth:`~.trigger_on_bus` is configured. |
| 312 |
""" |
| 313 |
return self.write("*TRG") |
| 314 |
|
| 315 |
def trigger_immediately(self): |
| 316 |
""" Configures measurements to be taken with the internal |
| 317 |
trigger at the maximum sampling rate. |
| 318 |
""" |
| 319 |
self.write(":TRIG:SOUR IMM;") |
| 320 |
|
| 321 |
def trigger_on_bus(self): |
| 322 |
""" Configures the trigger to detect events based on the bus |
| 323 |
trigger, which can be activated by :meth:`~.trigger`. |
| 324 |
""" |
| 325 |
self.write(":TRIG:SOUR BUS;") |
| 326 |
|
| 327 |
def shutdown(self): |
| 328 |
""" Ensures that the current or voltage is turned to zero |
| 329 |
and disables the output. """ |
| 330 |
log.info("Shutting down %s.", self.name) |
| 331 |
self.ramp_to_voltage(0.0) |
| 332 |
self.stop_buffer() |
| 333 |
self.disable_source() |
| 334 |
super().shutdown() |