| 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 |
# ============================================================================= |
| 26 |
# Libraries / modules |
| 27 |
# ============================================================================= |
| 28 |
|
| 29 |
import logging |
| 30 |
from time import sleep, time |
| 31 |
import numpy as np |
| 32 |
from pymeasure.instruments import Instrument |
| 33 |
from pymeasure.instruments.validators import modular_range_bidirectional |
| 34 |
from pymeasure.instruments.validators import strict_discrete_set |
| 35 |
from pymeasure.instruments.validators import strict_range |
| 36 |
|
| 37 |
# ============================================================================= |
| 38 |
# Logging |
| 39 |
# ============================================================================= |
| 40 |
|
| 41 |
log = logging.getLogger(__name__) |
| 42 |
log.addHandler(logging.NullHandler()) |
| 43 |
|
| 44 |
|
| 45 |
# ============================================================================= |
| 46 |
# Instrument file |
| 47 |
# ============================================================================= |
| 48 |
|
| 49 |
|
| 50 |
class DSPBase(Instrument): |
| 51 |
"""This is the base class for the Signal Recovery DSP 72XX lock-in |
| 52 |
amplifiers. |
| 53 |
|
| 54 |
Do not directly instantiate an object with this class. Use one of the |
| 55 |
DSP 72XX series instrument classes that inherit from this parent |
| 56 |
class. Floating point command mode (i.e., the inclusion of the ``.`` |
| 57 |
character in commands) is included for usability. |
| 58 |
|
| 59 |
Untested commands are noted in docstrings. |
| 60 |
""" |
| 61 |
|
| 62 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 63 |
# Constants |
| 64 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 65 |
|
| 66 |
SENSITIVITIES = [ |
| 67 |
np.nan, 2.0e-9, 5.0e-9, 10.0e-9, 20.0e-9, 50.0e-9, 100.0e-9, |
| 68 |
200.0e-9, 500.0e-9, 1.0e-6, 2.0e-6, 5.0e-6, 10.0e-6, |
| 69 |
20.0e-6, 50.0e-6, 100.0e-6, 200.0e-6, 500.0e-6, 1.0e-3, |
| 70 |
2.0e-3, 5.0e-3, 10.0e-3, 20.0e-3, 50.0e-3, 100.0e-3, |
| 71 |
200.0e-3, 500.0e-3, 1.0 |
| 72 |
] |
| 73 |
SEN_MULTIPLIER = [1, 1e-6, 1e-8] |
| 74 |
|
| 75 |
TIME_CONSTANTS = [ |
| 76 |
10.0e-6, 20.0e-6, 40.0e-6, 80.0e-6, 160.0e-6, 320.0e-6, |
| 77 |
640.0e-6, 5.0e-3, 10.0e-3, 20.0e-3, 50.0e-3, 100.0e-3, |
| 78 |
200.0e-3, 500.0e-3, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, |
| 79 |
100.0, 200.0, 500.0, 1.0e3, 2.0e3, 5.0e3, 10.0e3, |
| 80 |
20.0e3, 50.0e3 |
| 81 |
] |
| 82 |
REFERENCES = ['internal', 'external rear', 'external front'] |
| 83 |
IMODES = ['voltage mode', 'current mode', 'low noise current mode'] |
| 84 |
|
| 85 |
CURVE_BITS = ['x', 'y', 'magnitude', 'phase', 'sensitivity', 'adc1', |
| 86 |
'adc2', 'dac1', 'dac2', 'noise', 'ratio', 'log ratio', |
| 87 |
'event', 'frequency part 1', 'frequency part 2', |
| 88 |
# Dual modes |
| 89 |
'x2', 'y2', 'magnitude2', 'phase2', 'sensitivity2'] |
| 90 |
|
| 91 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 92 |
# Initializer and important communication methods |
| 93 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 |
|
| 95 |
def __init__(self, adapter, name="Signal Recovery DSP 72XX Base", **kwargs): |
| 96 |
super().__init__( |
| 97 |
adapter, |
| 98 |
name, |
| 99 |
includeSCPI=False, |
| 100 |
**kwargs |
| 101 |
) |
| 102 |
|
| 103 |
def read(self, **kwargs): |
| 104 |
"""Read the response and remove extra unicode character from instrument readings.""" |
| 105 |
return super().read(**kwargs).replace('\x00', '') |
| 106 |
|
| 107 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 |
# Properties |
| 109 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 110 |
|
| 111 |
id = Instrument.measurement( |
| 112 |
"ID", |
| 113 |
"""Measure the model number of the instrument. |
| 114 |
|
| 115 |
Returned value is an integer.""", |
| 116 |
cast=int |
| 117 |
) |
| 118 |
|
| 119 |
imode = Instrument.control( |
| 120 |
"IMODE", "IMODE %d", |
| 121 |
"""Control the lock-in amplifier to detect a voltage or current |
| 122 |
signal. |
| 123 |
|
| 124 |
Valid values are ``voltage mode, ``current mode``, or ``low noise current mode``. |
| 125 |
""", |
| 126 |
validator=strict_discrete_set, |
| 127 |
values=IMODES, |
| 128 |
map_values=True |
| 129 |
) |
| 130 |
|
| 131 |
slope = Instrument.control( |
| 132 |
"SLOPE", "SLOPE %d", |
| 133 |
"""Control the low-pass filter roll-off. |
| 134 |
|
| 135 |
Valid values are the integers 6, 12, 18, or 24, which represents the |
| 136 |
slope of the low-pass filter in dB/octave. |
| 137 |
""", |
| 138 |
validator=strict_discrete_set, |
| 139 |
values=[6, 12, 18, 24], |
| 140 |
map_values=True |
| 141 |
) |
| 142 |
|
| 143 |
time_constant = Instrument.control( |
| 144 |
"TC", "TC %d", |
| 145 |
"""Control the filter time constant. |
| 146 |
|
| 147 |
Valid values are a strict set of time constants from 10 us to 50,000 s. |
| 148 |
Returned values are floating point numbers in seconds. |
| 149 |
""", |
| 150 |
validator=strict_discrete_set, |
| 151 |
values=TIME_CONSTANTS, |
| 152 |
map_values=True |
| 153 |
) |
| 154 |
|
| 155 |
shield = Instrument.control( |
| 156 |
"FLOAT", "FLOAT %d", |
| 157 |
"""Control the input connector shield state. |
| 158 |
|
| 159 |
Valid values are 0 to have shields grounded or 1 to have the shields |
| 160 |
floating (i.e., connected to ground via a 1 kOhm resistor). |
| 161 |
""", |
| 162 |
validator=strict_discrete_set, |
| 163 |
values=[0, 1] |
| 164 |
) |
| 165 |
|
| 166 |
fet = Instrument.control( |
| 167 |
"FET", "FET %d", |
| 168 |
"""Control the voltage preamplifier transistor type. |
| 169 |
|
| 170 |
Valid values are 0 for bipolar or 1 for FET. |
| 171 |
""", |
| 172 |
validator=strict_discrete_set, |
| 173 |
values=[0, 1] |
| 174 |
) |
| 175 |
|
| 176 |
coupling = Instrument.control( |
| 177 |
"CP", "CP %d", |
| 178 |
"""Control the input coupling mode. |
| 179 |
|
| 180 |
Valid values are 0 for AC coupling mode or 1 for DC coupling mode. |
| 181 |
""", |
| 182 |
validator=strict_discrete_set, |
| 183 |
values=[0, 1] |
| 184 |
) |
| 185 |
|
| 186 |
voltage = Instrument.control( |
| 187 |
"OA.", "OA. %g", |
| 188 |
"""Control the oscillator amplitude. |
| 189 |
|
| 190 |
Valid values are floating point numbers between 0 to 5 V. |
| 191 |
""", |
| 192 |
validator=strict_range, |
| 193 |
values=[0, 5] |
| 194 |
) |
| 195 |
|
| 196 |
frequency = Instrument.control( |
| 197 |
"OF.", "OF. %g", |
| 198 |
"""Control the oscillator frequency. |
| 199 |
|
| 200 |
Valid values are floating point numbers representing the frequency in Hz. |
| 201 |
""", |
| 202 |
validator=strict_range, |
| 203 |
values=[0, 2.5e5], |
| 204 |
dynamic=True |
| 205 |
) |
| 206 |
|
| 207 |
reference = Instrument.control( |
| 208 |
"IE", "IE %d", |
| 209 |
"""Control the oscillator reference input mode. |
| 210 |
|
| 211 |
Valid values are ``internal``, ``external rear`` or ``external front``. |
| 212 |
""", |
| 213 |
validator=strict_discrete_set, |
| 214 |
values=REFERENCES, |
| 215 |
map_values=True |
| 216 |
) |
| 217 |
|
| 218 |
harmonic = Instrument.control( |
| 219 |
"REFN", "REFN %d", |
| 220 |
"""Control the reference harmonic mode. |
| 221 |
|
| 222 |
Valid values are integers. |
| 223 |
""", |
| 224 |
validator=strict_range, |
| 225 |
values=[1, 65535], |
| 226 |
dynamic=True |
| 227 |
) |
| 228 |
|
| 229 |
reference_phase = Instrument.control( |
| 230 |
"REFP.", "REFP. %g", |
| 231 |
"""Control the reference absolute phase angle. |
| 232 |
|
| 233 |
Valid values are floating point numbers between 0 - 360 degrees. |
| 234 |
""", |
| 235 |
validator=modular_range_bidirectional, |
| 236 |
values=[0, 360] |
| 237 |
) |
| 238 |
|
| 239 |
dac1 = Instrument.control( |
| 240 |
"DAC. 1", "DAC. 1 %g", |
| 241 |
"""Control the voltage of the DAC1 output on the rear panel. |
| 242 |
|
| 243 |
Valid values are floating point numbers between -12 to 12 V. |
| 244 |
""", |
| 245 |
validator=strict_range, |
| 246 |
values=[-12, 12] |
| 247 |
) |
| 248 |
|
| 249 |
dac2 = Instrument.control( |
| 250 |
"DAC. 2", "DAC. 2 %g", |
| 251 |
"""Control the voltage of the DAC2 output on the rear panel. |
| 252 |
|
| 253 |
Valid values are floating point numbers between -12 to 12 V. |
| 254 |
""", |
| 255 |
validator=strict_range, |
| 256 |
values=[-12, 12] |
| 257 |
) |
| 258 |
|
| 259 |
@property |
| 260 |
def gain(self): |
| 261 |
"""Control the AC gain of signal channel amplifier.""" |
| 262 |
return self.values("ACGAIN") |
| 263 |
|
| 264 |
@gain.setter |
| 265 |
def gain(self, value): |
| 266 |
value = strict_discrete_set(int(value / 10), list(range(0, 10))) |
| 267 |
self.write("ACGAIN %d" % value) |
| 268 |
|
| 269 |
@property |
| 270 |
def sensitivity(self): |
| 271 |
"""Control the signal's measurement sensitivity range. |
| 272 |
|
| 273 |
When in voltage measurement mode, valid values are discrete values from |
| 274 |
2 nV to 1 V. When in current measurement mode, valid values are |
| 275 |
discrete values from 2 fA to 1 µA (for normal current mode) or up to |
| 276 |
10 nA (for low noise current mode). |
| 277 |
""" |
| 278 |
return self.values("SEN.")[0] |
| 279 |
|
| 280 |
@sensitivity.setter |
| 281 |
def sensitivity(self, value): |
| 282 |
# get the voltage/current mode: |
| 283 |
imode = self.IMODES.index(self.imode) |
| 284 |
|
| 285 |
# Scale the sensitivities to the correct range for voltage/current mode |
| 286 |
sensitivities = [s * self.SEN_MULTIPLIER[imode] |
| 287 |
for s in self.SENSITIVITIES] |
| 288 |
if imode == 2: |
| 289 |
sensitivities[0:7] = [np.nan] * 7 |
| 290 |
|
| 291 |
# Check and map the value |
| 292 |
value = strict_discrete_set(value, sensitivities) |
| 293 |
value = sensitivities.index(value) |
| 294 |
|
| 295 |
# Set sensitivity |
| 296 |
self.write("SEN %d" % value) |
| 297 |
|
| 298 |
@property |
| 299 |
def auto_gain(self): |
| 300 |
"""Control lock-in amplifier for automatic AC gain.""" |
| 301 |
return int(self.values("AUTOMATIC")) == 1 |
| 302 |
|
| 303 |
@auto_gain.setter |
| 304 |
def auto_gain(self, value): |
| 305 |
if value: |
| 306 |
self.write("AUTOMATIC 1") |
| 307 |
else: |
| 308 |
self.write("AUTOMATIC 0") |
| 309 |
|
| 310 |
x = Instrument.measurement( |
| 311 |
"X.", |
| 312 |
"""Measure the output signal's X channel. |
| 313 |
|
| 314 |
Returned value is a floating point number in volts. |
| 315 |
""" |
| 316 |
) |
| 317 |
|
| 318 |
y = Instrument.measurement( |
| 319 |
"Y.", |
| 320 |
"""Measure the output signal's Y channel. |
| 321 |
|
| 322 |
Returned value is a floating point number in volts. |
| 323 |
""" |
| 324 |
) |
| 325 |
|
| 326 |
xy = Instrument.measurement( |
| 327 |
"XY.", |
| 328 |
"""Measure both the X and Y channels. |
| 329 |
|
| 330 |
Returned values are floating point numbers in volts. |
| 331 |
""" |
| 332 |
) |
| 333 |
|
| 334 |
mag = Instrument.measurement( |
| 335 |
"MAG.", |
| 336 |
"""Measure the magnitude of the signal. |
| 337 |
|
| 338 |
Returned value is a floating point number in volts. |
| 339 |
""" |
| 340 |
) |
| 341 |
|
| 342 |
phase = Instrument.measurement( |
| 343 |
"PHA.", |
| 344 |
"""Measure the signal's absolute phase angle. |
| 345 |
|
| 346 |
Returned value is a floating point number in degrees. |
| 347 |
""" |
| 348 |
) |
| 349 |
|
| 350 |
adc1 = Instrument.measurement( |
| 351 |
"ADC. 1", |
| 352 |
"""Measure the voltage of the ADC1 input on the rear panel. |
| 353 |
|
| 354 |
Returned value is a floating point number in volts. |
| 355 |
""" |
| 356 |
) |
| 357 |
|
| 358 |
adc2 = Instrument.measurement( |
| 359 |
"ADC. 2", |
| 360 |
"""Measure the voltage of the ADC2 input on the rear panel. |
| 361 |
|
| 362 |
Returned value is a floating point number in volts. |
| 363 |
""" |
| 364 |
) |
| 365 |
|
| 366 |
ratio = Instrument.measurement( |
| 367 |
"RT.", |
| 368 |
"""Measure the ratio between the X channel and ADC1. |
| 369 |
|
| 370 |
Returned value is a unitless floating point number equivalent to the |
| 371 |
mathematical expression X/ADC1. |
| 372 |
""" |
| 373 |
) |
| 374 |
|
| 375 |
log_ratio = Instrument.measurement( |
| 376 |
"LR.", |
| 377 |
""" |
| 378 |
Measure the log (base 10) of the ratio between the X channel and ADC1. |
| 379 |
|
| 380 |
Returned value is a unitless floating point number equivalent to the |
| 381 |
mathematical expression log(X/ADC1). |
| 382 |
""" |
| 383 |
) |
| 384 |
|
| 385 |
curve_buffer_bits = Instrument.control( |
| 386 |
"CBD", "CBD %d", |
| 387 |
"""Control which data outputs are stored in the curve buffer. |
| 388 |
|
| 389 |
Valid values are values are integers between 1 and 65,535 (or 2,097,151 |
| 390 |
in dual reference mode). |
| 391 |
""", |
| 392 |
values=[1, 2097151], |
| 393 |
validator=strict_range, |
| 394 |
cast=int, |
| 395 |
dynamic=True |
| 396 |
) |
| 397 |
|
| 398 |
curve_buffer_length = Instrument.control( |
| 399 |
"LEN", "LEN %d", |
| 400 |
"""Control the length of the curve buffer. |
| 401 |
|
| 402 |
Valid values are integers between 1 and 32,768, but the actual maximum |
| 403 |
amount of points is determined by the amount of curves that are stored, |
| 404 |
as set via the curve_buffer_bits property (32,768 / n). |
| 405 |
""", |
| 406 |
values=[1, 32768], |
| 407 |
validator=strict_range, |
| 408 |
cast=int |
| 409 |
) |
| 410 |
|
| 411 |
curve_buffer_interval = Instrument.control( |
| 412 |
"STR", "STR %d", |
| 413 |
"""Control the time interval between the collection of successive |
| 414 |
points in the curve buffer. |
| 415 |
|
| 416 |
Valid values to the time interval are integers in ms with a |
| 417 |
resolution of 5 ms; input values are rounded up to a multiple of 5. |
| 418 |
Valid values are values between 0 and 1,000,000,000 (corresponding to |
| 419 |
12 days). The interval may be set to 0, which sets the rate of data |
| 420 |
storage to the curve buffer to 1.25 ms/point (800 Hz). However this |
| 421 |
only allows storage of the X and Y channel outputs. There is no need to |
| 422 |
issue a CBD 3 command to set this up since it happens automatically |
| 423 |
when acquisition starts. |
| 424 |
""", |
| 425 |
values=[1, 1000000000], |
| 426 |
validator=strict_range, |
| 427 |
cast=int |
| 428 |
) |
| 429 |
|
| 430 |
curve_buffer_status = Instrument.measurement( |
| 431 |
"M", |
| 432 |
"""Measure the status of the curve buffer acquisition. |
| 433 |
|
| 434 |
Command returns four values: |
| 435 |
**First value - Curve Acquisition Status:** Number with 5 possibilities: |
| 436 |
0: no activity |
| 437 |
1: acquisition via TD command running |
| 438 |
2: acquisition by a TDC command running |
| 439 |
5: acquisition via TD command halted |
| 440 |
6: acquisition bia TDC command halted |
| 441 |
**Second value - Number of Sweeps Acquired**: Number of sweeps already |
| 442 |
acquired. |
| 443 |
**Third value - Status Byte:** Decimal representation of the status byte |
| 444 |
(the same response as the ST command |
| 445 |
**Fourth value - Number of Points Acquired:** Number of points acquired |
| 446 |
in the curve buffer. |
| 447 |
""", |
| 448 |
cast=int, |
| 449 |
) |
| 450 |
|
| 451 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 452 |
# Methods |
| 453 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 454 |
|
| 455 |
def set_voltage_mode(self): |
| 456 |
"""Sets lock-in amplifier to measure a voltage signal.""" |
| 457 |
self.write("IMODE 0") |
| 458 |
|
| 459 |
def setDifferentialMode(self, lineFiltering=True): |
| 460 |
"""Sets lock-in amplifier to differential mode, measuring A-B.""" |
| 461 |
self.write("VMODE 3") |
| 462 |
self.write("LF %d 0" % (3 if lineFiltering else 0)) |
| 463 |
|
| 464 |
def setChannelAMode(self): |
| 465 |
"""Sets lock-in amplifier to measure a voltage signal only from the A |
| 466 |
input connector. |
| 467 |
""" |
| 468 |
self.write("VMODE 1") |
| 469 |
|
| 470 |
def auto_sensitivity(self): |
| 471 |
"""Adjusts the full-scale sensitivity so signal's magnitude lies |
| 472 |
between 30 - 90 % of full-scale. |
| 473 |
""" |
| 474 |
self.write("AS") |
| 475 |
|
| 476 |
def auto_phase(self): |
| 477 |
"""Adjusts the reference absolute phase to maximize the X channel |
| 478 |
output and minimize the Y channel output signals. |
| 479 |
""" |
| 480 |
self.write("AQN") |
| 481 |
|
| 482 |
def init_curve_buffer(self): |
| 483 |
"""Initializes the curve storage memory and status variables. All |
| 484 |
record of previously taken curves is removed. |
| 485 |
""" |
| 486 |
self.write("NC") |
| 487 |
|
| 488 |
def set_buffer(self, points, quantities=None, interval=10.0e-3): |
| 489 |
"""Prepares the curve buffer for a measurement. |
| 490 |
|
| 491 |
:param int points: |
| 492 |
Number of points to be recorded in the curve buffer |
| 493 |
|
| 494 |
:param list quantities: |
| 495 |
List containing the quantities (strings) that are to be |
| 496 |
recorded in the curve buffer, can be any of: |
| 497 |
'x', 'y', 'magnitude', 'phase', 'sensitivity', 'adc1', 'adc2', |
| 498 |
'adc3', 'dac1', 'dac2', |
| 499 |
'noise', 'ratio', 'log ratio', 'event', 'frequency' |
| 500 |
(or 'frequency part 1' and 'frequency part 2'); |
| 501 |
for both dual modes, additional options are: |
| 502 |
'x2', 'y2', 'magnitude2', 'phase2', 'sensitivity2'. |
| 503 |
Default is 'x' and 'y'. |
| 504 |
|
| 505 |
:param float interval: |
| 506 |
The interval between two subsequent points stored in the |
| 507 |
curve buffer in s. Default is 10 ms. |
| 508 |
""" |
| 509 |
|
| 510 |
if quantities is None: |
| 511 |
quantities = ["x", "y"] |
| 512 |
|
| 513 |
if "frequency" in quantities: |
| 514 |
quantities.remove("frequency") |
| 515 |
quantities.extend([ |
| 516 |
"frequency part 1", |
| 517 |
"frequency part 2" |
| 518 |
]) |
| 519 |
|
| 520 |
# remove all possible duplicates |
| 521 |
quantities = list({q.lower() for q in quantities}) |
| 522 |
|
| 523 |
bits = 0 |
| 524 |
for q in quantities: |
| 525 |
bits += 2 ** self.CURVE_BITS.index(q) |
| 526 |
|
| 527 |
self.curve_buffer_bits = bits |
| 528 |
self.curve_buffer_length = points |
| 529 |
|
| 530 |
self.curve_buffer_interval = int(interval * 1000) |
| 531 |
self.init_curve_buffer() |
| 532 |
|
| 533 |
def start_buffer(self): |
| 534 |
"""Initiates data acquisition. Acquisition starts at the current |
| 535 |
position in the curve buffer and continues at the rate set by the STR |
| 536 |
command until the buffer is full. |
| 537 |
""" |
| 538 |
self.write("TD") |
| 539 |
|
| 540 |
def wait_for_buffer(self, timeout=None, delay=0.1): |
| 541 |
""" Method that waits until the curve buffer is filled |
| 542 |
""" |
| 543 |
start = time() |
| 544 |
while self.curve_buffer_status[0] == 1: |
| 545 |
sleep(delay) |
| 546 |
if timeout is not None and time() < start + timeout: |
| 547 |
break |
| 548 |
|
| 549 |
def get_buffer(self, quantity=None, |
| 550 |
convert_to_float=True, wait_for_buffer=True): |
| 551 |
"""Retrieves the buffer after it has been filled. The data retrieved |
| 552 |
from the lock-in is in a fixed-point format, which requires translation |
| 553 |
before it can be interpreted as meaningful data. When |
| 554 |
`convert_to_float` is True the conversion is performed (if possible) |
| 555 |
before returning the data. |
| 556 |
|
| 557 |
:param str quantity: |
| 558 |
If provided, names the quantity that is to be retrieved from the |
| 559 |
curve buffer; can be any of: |
| 560 |
'x', 'y', 'magnitude', 'phase', 'sensitivity', 'adc1', 'adc2', |
| 561 |
'adc3', 'dac1', 'dac2', 'noise', 'ratio', 'log ratio', 'event', |
| 562 |
'frequency part 1' and 'frequency part 2'; |
| 563 |
for both dual modes, additional options are: |
| 564 |
'x2', 'y2', 'magnitude2', 'phase2', 'sensitivity2'. |
| 565 |
If no quantity is provided, all available data is retrieved. |
| 566 |
|
| 567 |
:param bool convert_to_float: |
| 568 |
Bool that determines whether to convert the fixed-point buffer-data |
| 569 |
to meaningful floating point values via the `buffer_to_float` |
| 570 |
method. If True, this method tries to convert all the available |
| 571 |
data to meaningful values; if this is not possible, an exception |
| 572 |
will be raised. If False, this conversion is not performed and the |
| 573 |
raw buffer-data is returned. |
| 574 |
|
| 575 |
:param bool wait_for_buffer: |
| 576 |
Bool that determines whether to wait for the data acquisition to |
| 577 |
finished if this method is called before the acquisition is |
| 578 |
finished. If True, the method waits until the buffer is filled |
| 579 |
before continuing; if False, the method raises an exception if the |
| 580 |
acquisition is not finished when the method is called. |
| 581 |
""" |
| 582 |
|
| 583 |
# Check if buffer is finished |
| 584 |
if self.curve_buffer_status[0] != 0: |
| 585 |
if wait_for_buffer: |
| 586 |
self.wait_for_buffer() |
| 587 |
else: |
| 588 |
raise RuntimeError("Buffer acquisition is not yet finished.") |
| 589 |
|
| 590 |
# Check which quantities are recorded in the buffer |
| 591 |
bits = format(self.curve_buffer_bits, '021b')[::-1] |
| 592 |
quantity_enums = [e for e, b in enumerate(bits) if b == "1"] |
| 593 |
|
| 594 |
# Check if the provided quantity (if any) is indeed recorded |
| 595 |
if quantity is not None: |
| 596 |
if self.CURVE_BITS.index(quantity) in quantity_enums: |
| 597 |
quantity_enums = [self.CURVE_BITS.index(quantity)] |
| 598 |
else: |
| 599 |
raise KeyError("The selected quantity '%s' is not recorded;" |
| 600 |
"quantity should be one of: %s" % ( |
| 601 |
quantity, ", ".join( |
| 602 |
[self.CURVE_BITS[q] for q in quantity_enums] |
| 603 |
))) |
| 604 |
|
| 605 |
# Retrieve the data |
| 606 |
data = {} |
| 607 |
for enum in quantity_enums: |
| 608 |
self.write("DC %d" % enum) |
| 609 |
q_data = [] |
| 610 |
|
| 611 |
while True: |
| 612 |
stb = format(self.adapter.connection.read_stb(), '08b')[::-1] |
| 613 |
|
| 614 |
if bool(int(stb[2])): |
| 615 |
raise ValueError("Status byte reports command parameter error.") |
| 616 |
|
| 617 |
if bool(int(stb[0])): |
| 618 |
break |
| 619 |
|
| 620 |
if bool(int(stb[7])): |
| 621 |
q_data.append(int(self.read().strip())) |
| 622 |
|
| 623 |
data[self.CURVE_BITS[enum]] = np.array(q_data) |
| 624 |
|
| 625 |
if convert_to_float: |
| 626 |
data = self.buffer_to_float(data) |
| 627 |
|
| 628 |
if quantity is not None: |
| 629 |
data = data[quantity] |
| 630 |
|
| 631 |
return data |
| 632 |
|
| 633 |
def buffer_to_float(self, buffer_data, sensitivity=None, |
| 634 |
sensitivity2=None, raise_error=True): |
| 635 |
"""Converts fixed-point buffer data to floating point data. |
| 636 |
|
| 637 |
The provided data is converted as much as possible, but there are some |
| 638 |
requirements to the data if all provided columns are to be converted; |
| 639 |
if a key in the provided data cannot be converted it will be omitted in |
| 640 |
the returned data or an exception will be raised, depending on the |
| 641 |
value of raise_error. |
| 642 |
|
| 643 |
The requirements for converting the data are as follows: |
| 644 |
|
| 645 |
- Converting X, Y, magnitude and noise requires sensitivity data, which |
| 646 |
can either be part of the provided data or can be provided via the |
| 647 |
sensitivity argument |
| 648 |
- The same holds for X2, Y2 and magnitude2 with sensitivity2. |
| 649 |
- Converting the frequency requires both 'frequency part 1' and |
| 650 |
'frequency part 2'. |
| 651 |
|
| 652 |
:param dict buffer_data: |
| 653 |
The data to be converted. Must be in the format as returned by the |
| 654 |
`get_buffer` method: a dict of numpy arrays. |
| 655 |
|
| 656 |
:param sensitivity: |
| 657 |
If provided, the sensitivity used to convert X, Y, magnitude and |
| 658 |
noise. Can be provided as a float or as an array that matches the |
| 659 |
length of elements in `buffer_data`. If both a sensitivity is |
| 660 |
provided and present in the buffer_data, the provided value is used |
| 661 |
for the conversion, but the sensitivity in the buffer_data is |
| 662 |
stored in the returned dict. |
| 663 |
|
| 664 |
:param sensitivity2: |
| 665 |
Same as the first sensitivity argument, but for X2, Y2, magnitude2 |
| 666 |
and noise2. |
| 667 |
|
| 668 |
:param bool raise_error: |
| 669 |
Determines whether an exception is raised in case not all keys |
| 670 |
provided in buffer_data can be converted. If False, the columns |
| 671 |
that cannot be converted are omitted in the returned dict. |
| 672 |
|
| 673 |
:return: Floating-point buffer data |
| 674 |
:rtype: dict |
| 675 |
""" |
| 676 |
|
| 677 |
data = {} |
| 678 |
|
| 679 |
def maybe_raise(message): |
| 680 |
if raise_error: |
| 681 |
raise ValueError(message) |
| 682 |
|
| 683 |
def convert_if_present(keys, multiply_by=1): |
| 684 |
"""Copy any available entries from buffer_data to data, scale with |
| 685 |
multiply_by. |
| 686 |
""" |
| 687 |
for key in keys: |
| 688 |
if key in buffer_data: |
| 689 |
data[key] = buffer_data[key] * multiply_by |
| 690 |
|
| 691 |
# Sensitivity (for both single and dual modes) |
| 692 |
for key in ["sensitivity", "sensitivity2"]: |
| 693 |
if key in buffer_data: |
| 694 |
data[key] = np.array([ |
| 695 |
self.SENSITIVITIES[v % 32] * self.SEN_MULTIPLIER[v // 32] |
| 696 |
for v in buffer_data[key] |
| 697 |
]) |
| 698 |
# Try to set sensitivity values from arg or data |
| 699 |
sensitivity = sensitivity or data.get('sensitivity', None) |
| 700 |
sensitivity2 = sensitivity2 or data.get('sensitivity2', None) |
| 701 |
|
| 702 |
if any(["x" in buffer_data, |
| 703 |
"y" in buffer_data, |
| 704 |
"magnitude" in buffer_data, |
| 705 |
"noise" in buffer_data, ]): |
| 706 |
if sensitivity is None: |
| 707 |
maybe_raise("X, Y, magnitude and noise cannot be converted as " |
| 708 |
"no sensitivity is provided, neither as argument " |
| 709 |
"nor as part of the buffer_data. ") |
| 710 |
else: |
| 711 |
convert_if_present(["x", "y", "magnitude", "noise"], sensitivity / 10000) |
| 712 |
|
| 713 |
# phase data (for both single and dual modes) |
| 714 |
convert_if_present(["phase", "phase2"], 1 / 100) |
| 715 |
|
| 716 |
# frequency data from frequency part 1 and 2 |
| 717 |
if "frequency part 1" in buffer_data or "frequency part 2" in buffer_data: |
| 718 |
if "frequency part 1" in buffer_data and "frequency part 2" in buffer_data: |
| 719 |
data["frequency"] = np.array([ |
| 720 |
int(format(v2, "016b") + format(v1, "016b"), 2) / 1000 for |
| 721 |
v1, v2 in zip(buffer_data["frequency part 1"], buffer_data["frequency part 2"]) |
| 722 |
]) |
| 723 |
else: |
| 724 |
maybe_raise("Can calculate the frequency only when both" |
| 725 |
"frequency part 1 and 2 are provided.") |
| 726 |
|
| 727 |
# conversion for, adc1, adc2, dac1, dac2, ratio, and log ratio |
| 728 |
convert_if_present(["adc1", "adc2", "dac1", |
| 729 |
"dac2", "ratio", "log ratio"], 1 / 1000) |
| 730 |
|
| 731 |
# adc3 (integrating converter); requires a call to adc3_time |
| 732 |
if "adc3" in buffer_data: |
| 733 |
data["adc3"] = buffer_data["adc3"] / (50000 * self.adc3_time) |
| 734 |
|
| 735 |
# event does not require a conversion |
| 736 |
convert_if_present(["event"]) |
| 737 |
|
| 738 |
# X, Y, and magnitude data for both dual modes |
| 739 |
if any(["x2" in buffer_data, |
| 740 |
"y2" in buffer_data, |
| 741 |
"magnitude2" in buffer_data, ]): |
| 742 |
if sensitivity2 is None: |
| 743 |
maybe_raise("X2, Y2 and magnitude2 cannot be converted as no " |
| 744 |
"sensitivity2 is provided, neither as argument nor " |
| 745 |
"as part of the buffer_data. ") |
| 746 |
else: |
| 747 |
convert_if_present(["x2", "y2", "magnitude2"], sensitivity2 / 10000) |
| 748 |
|
| 749 |
return data |
| 750 |
|
| 751 |
def shutdown(self): |
| 752 |
"""Safely shutdown the lock-in amplifier. |
| 753 |
|
| 754 |
Sets oscillator amplitude to 0 V and AC gain to 0 dB. |
| 755 |
""" |
| 756 |
log.info("Shutting down %s." % self.name) |
| 757 |
self.voltage = 0. |
| 758 |
self.gain = 0. |
| 759 |
super().shutdown() |