| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# pyvirtualbench library: Copyright (c) 2015 Charles Armstrap <[email protected]> |
| 6 |
# |
| 7 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
# of this software and associated documentation files (the "Software"), to deal |
| 9 |
# in the Software without restriction, including without limitation the rights |
| 10 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
# copies of the Software, and to permit persons to whom the Software is |
| 12 |
# furnished to do so, subject to the following conditions: |
| 13 |
# |
| 14 |
# The above copyright notice and this permission notice shall be included in |
| 15 |
# all copies or substantial portions of the Software. |
| 16 |
# |
| 17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
# THE SOFTWARE. |
| 24 |
# |
| 25 |
# Requires 'pyvirtualbench' package: |
| 26 |
# https://github.com/armstrap/armstrap-pyvirtualbench |
| 27 |
|
| 28 |
import logging |
| 29 |
import re |
| 30 |
# ctypes only required for VirtualBench_Direct class |
| 31 |
from ctypes import (c_int, cdll, byref) |
| 32 |
from datetime import datetime, timezone, timedelta |
| 33 |
import numpy as np |
| 34 |
import pandas as pd |
| 35 |
|
| 36 |
from pymeasure.instruments.validators import ( |
| 37 |
strict_discrete_set, strict_discrete_range, |
| 38 |
truncated_discrete_set, strict_range |
| 39 |
) |
| 40 |
|
| 41 |
log = logging.getLogger(__name__) |
| 42 |
log.addHandler(logging.NullHandler()) |
| 43 |
|
| 44 |
try: |
| 45 |
# Requires 'pyvirtualbench' package: |
| 46 |
# https://github.com/armstrap/armstrap-pyvirtualbench |
| 47 |
import pyvirtualbench as pyvb |
| 48 |
except ModuleNotFoundError as err: |
| 49 |
log.info('Failed loading the pyvirtualbench package. ' |
| 50 |
+ 'Check the NI VirtualBench documentation on how to ' |
| 51 |
+ 'install this external dependency. ' |
| 52 |
+ f'ImportError: {err}') |
| 53 |
raise |
| 54 |
|
| 55 |
|
| 56 |
class VirtualBench_Direct(pyvb.PyVirtualBench): |
| 57 |
""" Represents National Instruments Virtual Bench main frame. |
| 58 |
This class provides direct access to the armstrap/pyvirtualbench |
| 59 |
Python wrapper. |
| 60 |
""" |
| 61 |
|
| 62 |
def __init__(self, device_name='', name='VirtualBench'): |
| 63 |
''' Initialize the VirtualBench library. This must be called at least |
| 64 |
once for the application. The 'version' parameter must be set to |
| 65 |
the NIVB_LIBRARY_VERSION constant. |
| 66 |
''' |
| 67 |
self.device_name = device_name |
| 68 |
self.name = name |
| 69 |
self.nilcicapi = cdll.LoadLibrary("nilcicapi") |
| 70 |
self.library_handle = c_int(0) |
| 71 |
status = self.nilcicapi.niVB_Initialize(pyvb.NIVB_LIBRARY_VERSION, |
| 72 |
byref(self.library_handle)) |
| 73 |
if (status != pyvb.Status.SUCCESS): |
| 74 |
raise pyvb.PyVirtualBenchException(status, self.nilcicapi, |
| 75 |
self.library_handle) |
| 76 |
log.info("Initializing %s." % self.name) |
| 77 |
|
| 78 |
def __del__(self): |
| 79 |
""" Ensures the connection is closed upon deletion |
| 80 |
""" |
| 81 |
self.release() |
| 82 |
|
| 83 |
|
| 84 |
class VirtualBench(): |
| 85 |
""" Represents National Instruments Virtual Bench main frame. |
| 86 |
|
| 87 |
Subclasses implement the functionalities of the different modules: |
| 88 |
|
| 89 |
- Mixed-Signal-Oscilloscope (MSO) |
| 90 |
- Digital Input Output (DIO) |
| 91 |
- Function Generator (FGEN) |
| 92 |
- Power Supply (PS) |
| 93 |
- Serial Peripheral Interface (SPI) -> not implemented |
| 94 |
for pymeasure yet |
| 95 |
- Inter Integrated Circuit (I2C) -> not implemented for pymeasure yet |
| 96 |
|
| 97 |
For every module exist methods to save/load the configuration to file. |
| 98 |
These methods are not wrapped so far, checkout the pyvirtualbench file. |
| 99 |
|
| 100 |
All calibration methods and classes are not wrapped so far, since these |
| 101 |
are not required on a very regular basis. Also the connections via network |
| 102 |
are not yet implemented. |
| 103 |
Check the pyvirtualbench file, if you need the functionality. |
| 104 |
|
| 105 |
:param str device_name: Full unique device name |
| 106 |
:param str name: Name for display in pymeasure |
| 107 |
""" |
| 108 |
|
| 109 |
def __init__(self, device_name='', name='VirtualBench'): |
| 110 |
''' Initialize the VirtualBench library. This must be called at least |
| 111 |
once for the application. The 'version' parameter must be set to the |
| 112 |
NIVB_LIBRARY_VERSION constant. |
| 113 |
''' |
| 114 |
self.device_name = device_name |
| 115 |
self.name = name |
| 116 |
self.vb = pyvb.PyVirtualBench(self.device_name) |
| 117 |
log.info("Initializing %s." % self.name) |
| 118 |
|
| 119 |
def __del__(self): |
| 120 |
""" Ensures the connection is closed upon deletion |
| 121 |
""" |
| 122 |
if self.vb.library_handle is not None: |
| 123 |
self.vb.release() |
| 124 |
|
| 125 |
def shutdown(self): |
| 126 |
''' Finalize the VirtualBench library. |
| 127 |
''' |
| 128 |
log.info("Shutting down %s" % self.name) |
| 129 |
self.vb.release() |
| 130 |
self.isShutdown = True |
| 131 |
|
| 132 |
def get_library_version(self): |
| 133 |
''' Return the version of the VirtualBench runtime library |
| 134 |
''' |
| 135 |
return self.vb.get_library_version() |
| 136 |
|
| 137 |
def convert_timestamp_to_values(self, timestamp): |
| 138 |
""" Converts a timestamp to seconds and fractional seconds |
| 139 |
|
| 140 |
:param timestamp: VirtualBench timestamp |
| 141 |
:type timestamp: pyvb.Timestamp |
| 142 |
:return: (seconds_since_1970, fractional seconds) |
| 143 |
:rtype: (int, float) |
| 144 |
""" |
| 145 |
if not isinstance(timestamp, pyvb.Timestamp): |
| 146 |
raise ValueError("{} is not a VirtualBench Timestamp object" |
| 147 |
.format(timestamp)) |
| 148 |
return self.vb.convert_timestamp_to_values(timestamp) |
| 149 |
|
| 150 |
def convert_values_to_timestamp(self, seconds_since_1970, |
| 151 |
fractional_seconds): |
| 152 |
""" Converts seconds and fractional seconds to a timestamp |
| 153 |
|
| 154 |
:param seconds_since_1970: Date/Time in seconds since 1970 |
| 155 |
:type seconds_since_1970: int |
| 156 |
:param fractional_seconds: Fractional seconds |
| 157 |
:type fractional_seconds: float |
| 158 |
:return: VirtualBench timestamp |
| 159 |
:rtype: pyvb.Timestamp |
| 160 |
""" |
| 161 |
return self.vb.convert_values_to_timestamp(seconds_since_1970, |
| 162 |
fractional_seconds) |
| 163 |
|
| 164 |
def convert_values_to_datetime(self, timestamp): |
| 165 |
""" Converts timestamp to datetime object |
| 166 |
|
| 167 |
:param timestamp: VirtualBench timestamp |
| 168 |
:type timestamp: pyvb.Timestamp |
| 169 |
:return: Timestamp as DateTime object |
| 170 |
:rtype: DateTime |
| 171 |
""" |
| 172 |
(seconds_since_1970, |
| 173 |
fractional_seconds) = self.convert_timestamp_to_values(timestamp) |
| 174 |
fractional_seconds = timedelta(seconds=fractional_seconds) |
| 175 |
return (datetime.fromtimestamp(seconds_since_1970, timezone.utc) + |
| 176 |
fractional_seconds) |
| 177 |
|
| 178 |
def collapse_channel_string(self, names_in): |
| 179 |
""" Collapses a channel string into a comma and colon-delimited |
| 180 |
equivalent. Last element is the number of channels. |
| 181 |
|
| 182 |
:param names_in: Channel string |
| 183 |
:type names_in: str |
| 184 |
:return: Channel string with colon notation where possible, |
| 185 |
number of channels |
| 186 |
:rtype: (str, int) |
| 187 |
""" |
| 188 |
if not isinstance(names_in, str): |
| 189 |
raise ValueError(f"{names_in} is not a string") |
| 190 |
return self.vb.collapse_channel_string(names_in) |
| 191 |
|
| 192 |
def expand_channel_string(self, names_in): |
| 193 |
""" Expands a channel string into a comma-delimited (no colon) |
| 194 |
equivalent. Last element is the number of channels. |
| 195 |
``'dig/0:2'`` -> ``('dig/0, dig/1, dig/2',3)`` |
| 196 |
|
| 197 |
:param names_in: Channel string |
| 198 |
:type names_in: str |
| 199 |
:return: Channel string with all channels separated by comma, |
| 200 |
number of channels |
| 201 |
:rtype: (str, int) |
| 202 |
""" |
| 203 |
return self.vb.expand_channel_string(names_in) |
| 204 |
|
| 205 |
def get_calibration_information(self): |
| 206 |
""" Returns calibration information for the specified device, |
| 207 |
including the last calibration date and calibration interval. |
| 208 |
|
| 209 |
:return: Calibration date, recommended calibration interval in months, |
| 210 |
calibration interval in months |
| 211 |
:rtype: (pyvb.Timestamp, int, int) |
| 212 |
""" |
| 213 |
return self.vb.get_calibration_information(self.device_name) |
| 214 |
|
| 215 |
def acquire_digital_input_output(self, lines, reset=False): |
| 216 |
""" Establishes communication with the DIO module. This method should |
| 217 |
be called once per session. |
| 218 |
|
| 219 |
:param lines: Lines to acquire, reading is possible on all lines |
| 220 |
:type lines: str |
| 221 |
:param reset: Reset DIO module, defaults to False |
| 222 |
:type reset: bool, optional |
| 223 |
""" |
| 224 |
reset = strict_discrete_set(reset, [True, False]) |
| 225 |
self.dio = self.DigitalInputOutput(self.vb, lines, reset, |
| 226 |
vb_name=self.name) |
| 227 |
|
| 228 |
def acquire_power_supply(self, reset=False): |
| 229 |
""" Establishes communication with the PS module. This method should be |
| 230 |
called once per session. |
| 231 |
|
| 232 |
:param reset: Reset the PS module, defaults to False |
| 233 |
:type reset: bool, optional |
| 234 |
""" |
| 235 |
reset = strict_discrete_set(reset, [True, False]) |
| 236 |
self.ps = self.PowerSupply(self.vb, reset, vb_name=self.name) |
| 237 |
|
| 238 |
def acquire_function_generator(self, reset=False): |
| 239 |
""" Establishes communication with the FGEN module. This method should |
| 240 |
be called once per session. |
| 241 |
|
| 242 |
:param reset: Reset the FGEN module, defaults to False |
| 243 |
:type reset: bool, optional |
| 244 |
""" |
| 245 |
reset = strict_discrete_set(reset, [True, False]) |
| 246 |
self.fgen = self.FunctionGenerator(self.vb, reset, vb_name=self.name) |
| 247 |
|
| 248 |
def acquire_mixed_signal_oscilloscope(self, reset=False): |
| 249 |
""" Establishes communication with the MSO module. This method should |
| 250 |
be called once per session. |
| 251 |
|
| 252 |
:param reset: Reset the MSO module, defaults to False |
| 253 |
:type reset: bool, optional |
| 254 |
""" |
| 255 |
reset = strict_discrete_set(reset, [True, False]) |
| 256 |
self.mso = self.MixedSignalOscilloscope(self.vb, reset, |
| 257 |
vb_name=self.name) |
| 258 |
|
| 259 |
def acquire_digital_multimeter(self, reset=False): |
| 260 |
""" Establishes communication with the DMM module. This method should |
| 261 |
be called once per session. |
| 262 |
|
| 263 |
:param reset: Reset the DMM module, defaults to False |
| 264 |
:type reset: bool, optional |
| 265 |
""" |
| 266 |
reset = strict_discrete_set(reset, [True, False]) |
| 267 |
self.dmm = self.DigitalMultimeter(self.vb, reset=reset, |
| 268 |
vb_name=self.name) |
| 269 |
|
| 270 |
class VirtualBenchInstrument(): |
| 271 |
def __init__(self, acquire_instr, reset, |
| 272 |
instr_identifier, vb_name=''): |
| 273 |
"""Initialize instrument of VirtualBench device. |
| 274 |
Sets class variables and provides basic methods |
| 275 |
common to all VB instruments. |
| 276 |
|
| 277 |
:param acquire_instr: Method to acquire the instrument |
| 278 |
:type acquire_instr: method |
| 279 |
:param reset: Resets the instrument |
| 280 |
:type reset: bool |
| 281 |
:param instr_identifier: Shorthand identifier, e.g. mso or fgen |
| 282 |
:type instr_identifier: str |
| 283 |
:param vb_name: Name of VB device for logging, defaults to '' |
| 284 |
:type vb_name: str, optional |
| 285 |
""" |
| 286 |
# Parameters & Handle of VirtualBench Instance |
| 287 |
self._vb_handle = acquire_instr.__self__ |
| 288 |
self._device_name = self._vb_handle.device_name |
| 289 |
self.name = (vb_name + " " + instr_identifier.upper()).strip() |
| 290 |
log.info("Initializing %s." % self.name) |
| 291 |
self._instrument_handle = acquire_instr(self._device_name, reset) |
| 292 |
self.isShutdown = False |
| 293 |
|
| 294 |
def __del__(self): |
| 295 |
""" Ensures the connection is closed upon deletion |
| 296 |
""" |
| 297 |
if self.isShutdown is not True: |
| 298 |
self._instrument_handle.release() |
| 299 |
|
| 300 |
def shutdown(self): |
| 301 |
''' Removes the session and deallocates any resources acquired |
| 302 |
during the session. If output is enabled on any channels, they |
| 303 |
remain in their current state. |
| 304 |
''' |
| 305 |
log.info("Shutting down %s" % self.name) |
| 306 |
self._instrument_handle.release() |
| 307 |
self.isShutdown = True |
| 308 |
|
| 309 |
class DigitalInputOutput(VirtualBenchInstrument): |
| 310 |
""" Represents Digital Input Output (DIO) Module of Virtual Bench |
| 311 |
device. Allows to read/write digital channels and/or set channels |
| 312 |
to export the start signal of FGEN module or trigger of MSO module. |
| 313 |
""" |
| 314 |
|
| 315 |
def __init__(self, virtualbench, lines, reset, vb_name=''): |
| 316 |
""" Acquire DIO module |
| 317 |
|
| 318 |
:param virtualbench: VirtualBench Instance |
| 319 |
:type virtualbench: VirtualBench |
| 320 |
:param lines: Lines to acquire |
| 321 |
:type lines: str |
| 322 |
:param reset: Rest DIO module |
| 323 |
:type reset: bool |
| 324 |
""" |
| 325 |
# Parameters & Handle of VirtualBench Instance |
| 326 |
self._device_name = virtualbench.device_name |
| 327 |
self._vb_handle = virtualbench |
| 328 |
self.name = vb_name + " DIO" |
| 329 |
# Validate lines argument |
| 330 |
# store line names & numbers for future reference |
| 331 |
(self._line_names, self._line_numbers) = self.validate_lines( |
| 332 |
lines, return_single_lines=True, validate_init=False) |
| 333 |
# Create DIO Instance |
| 334 |
log.info("Initializing %s." % self.name) |
| 335 |
self.dio = self._vb_handle.acquire_digital_input_output( |
| 336 |
self._line_names, reset) |
| 337 |
# for methods provided by super class |
| 338 |
self._instrument_handle = self.dio |
| 339 |
self.isShutdown = False |
| 340 |
|
| 341 |
def validate_lines(self, lines, return_single_lines=False, |
| 342 |
validate_init=False): |
| 343 |
"""Validate lines string |
| 344 |
Allowed patterns (case sensitive): |
| 345 |
|
| 346 |
- ``'VBxxxx-xxxxxxx/dig/0:7'`` |
| 347 |
- ``'VBxxxx-xxxxxxx/dig/0'`` |
| 348 |
- ``'dig/0'`` |
| 349 |
- ``'VBxxxx-xxxxxxx/trig'`` |
| 350 |
- ``'trig'`` |
| 351 |
|
| 352 |
Allowed Line Numbers: 0-7 or trig |
| 353 |
|
| 354 |
:param lines: Line string to test |
| 355 |
:type lines: str |
| 356 |
:param return_single_lines: Return list of line numbers as well, |
| 357 |
defaults to False |
| 358 |
:type return_single_lines: bool, optional |
| 359 |
:param validate_init: Check if lines are initialized (in |
| 360 |
:code:`self._line_numbers`), |
| 361 |
defaults to False |
| 362 |
:type validate_init: bool, optional |
| 363 |
:return: Line string, optional list of single line numbers |
| 364 |
:rtype: str, optional (str, list) |
| 365 |
""" |
| 366 |
|
| 367 |
def error(lines=lines): |
| 368 |
raise ValueError( |
| 369 |
f"Line specification {lines} is not valid!") |
| 370 |
|
| 371 |
lines = self._vb_handle.expand_channel_string(lines)[0] |
| 372 |
lines = lines.split(', ') |
| 373 |
return_lines = [] |
| 374 |
single_lines = [] |
| 375 |
for line in lines: |
| 376 |
if line == 'trig': |
| 377 |
device = self._device_name |
| 378 |
# otherwise (device_name/)dig/line or device_name/trig |
| 379 |
else: |
| 380 |
# split off line number by last '/' |
| 381 |
try: |
| 382 |
(device, line) = re.match( |
| 383 |
r'(.*)(?:/)(.+)', |
| 384 |
line).groups() |
| 385 |
except IndexError: |
| 386 |
error() |
| 387 |
if (line == 'trig') and (device == self._device_name): |
| 388 |
single_lines.append('trig') |
| 389 |
return_lines.append(self._device_name + '/' + line) |
| 390 |
elif int(line) in range(0, 8): |
| 391 |
line = int(line) |
| 392 |
single_lines.append(line) |
| 393 |
# validate device name: either 'dig' or 'device_name/dig' |
| 394 |
if device == 'dig': |
| 395 |
pass |
| 396 |
else: |
| 397 |
try: |
| 398 |
device = re.match( |
| 399 |
r'(VB[0-9]{4}-[0-9a-zA-Z]{7})(?:/dig)', |
| 400 |
device).groups()[0] |
| 401 |
except (IndexError, KeyError): |
| 402 |
error() |
| 403 |
# device_name has to match |
| 404 |
if not device == self._device_name: |
| 405 |
error() |
| 406 |
# constructing line references for output |
| 407 |
return_lines.append((self._device_name + '/dig/%d') % line) |
| 408 |
else: |
| 409 |
error() |
| 410 |
# check if lines are initialized |
| 411 |
if validate_init is True: |
| 412 |
if line not in self._line_numbers: |
| 413 |
raise ValueError( |
| 414 |
f"Digital Line {line} is not initialized") |
| 415 |
|
| 416 |
# create comma separated channel string |
| 417 |
return_lines = ', '.join(return_lines) |
| 418 |
# collapse string if possible |
| 419 |
return_lines = self._vb_handle.collapse_channel_string( |
| 420 |
return_lines)[0] # drop number of lines |
| 421 |
if return_single_lines is True: |
| 422 |
return return_lines, single_lines |
| 423 |
else: |
| 424 |
return return_lines |
| 425 |
|
| 426 |
def tristate_lines(self, lines): |
| 427 |
''' Sets all specified lines to a high-impedance state. (Default) |
| 428 |
''' |
| 429 |
lines = self.validate_lines(lines, validate_init=True) |
| 430 |
self.dio.tristate_lines(lines) |
| 431 |
|
| 432 |
def export_signal(self, line, digitalSignalSource): |
| 433 |
""" Exports a signal to the specified line. |
| 434 |
|
| 435 |
:param line: Line string |
| 436 |
:type line: str |
| 437 |
:param digitalSignalSource: ``0`` for FGEN start or ``1`` |
| 438 |
for MSO trigger |
| 439 |
:type digitalSignalSource: int |
| 440 |
""" |
| 441 |
line = self.validate_lines(line, validate_init=True) |
| 442 |
digitalSignalSource_values = {"FGEN START": 0, "MSO TRIGGER": 1} |
| 443 |
digitalSignalSource = strict_discrete_set( |
| 444 |
digitalSignalSource.upper(), digitalSignalSource_values) |
| 445 |
digitalSignalSource = digitalSignalSource_values[ |
| 446 |
digitalSignalSource.upper()] |
| 447 |
self.dio.export_signal(line, digitalSignalSource) |
| 448 |
|
| 449 |
def query_line_configuration(self): |
| 450 |
''' Indicates the current line configurations. Tristate Lines, |
| 451 |
Static Lines, and Export Lines contain comma-separated |
| 452 |
range_data and/or colon-delimited lists of all acquired lines |
| 453 |
''' |
| 454 |
return self.dio.query_line_configuration() |
| 455 |
|
| 456 |
def query_export_signal(self, line): |
| 457 |
""" Indicates the signal being exported on the specified line. |
| 458 |
|
| 459 |
:param line: Line string |
| 460 |
:type line: str |
| 461 |
:return: Exported signal (FGEN start or MSO trigger) |
| 462 |
:rtype: enum |
| 463 |
""" |
| 464 |
line = self.validate_lines(line, validate_init=True) |
| 465 |
return self.dio.query_export_signal(line) |
| 466 |
|
| 467 |
def write(self, lines, data): |
| 468 |
""" Writes data to the specified lines. |
| 469 |
|
| 470 |
:param lines: Line string |
| 471 |
:type lines: str |
| 472 |
:param data: List of data, (``True`` = High, ``False`` = Low) |
| 473 |
:type data: list or tuple |
| 474 |
""" |
| 475 |
lines = self.validate_lines(lines, validate_init=True) |
| 476 |
try: |
| 477 |
for value in data: |
| 478 |
strict_discrete_set(value, [True, False]) |
| 479 |
except Exception: |
| 480 |
raise ValueError( |
| 481 |
f"Data {data} is not iterable (list or tuple).") |
| 482 |
log.debug(f"{self.name}: {lines} output {data}.") |
| 483 |
self.dio.write(lines, data) |
| 484 |
|
| 485 |
def read(self, lines): |
| 486 |
""" Reads the current state of the specified lines. |
| 487 |
|
| 488 |
:param lines: Line string, requires full name specification e.g. |
| 489 |
``'VB8012-xxxxxxx/dig/0:7'`` since instrument_handle |
| 490 |
is not required (only library_handle) |
| 491 |
:type lines: str |
| 492 |
:return: List of line states (HIGH/LOW) |
| 493 |
:rtype: list |
| 494 |
""" |
| 495 |
lines = self.validate_lines(lines, validate_init=False) |
| 496 |
# init not necessary for readout |
| 497 |
return self.dio.read(lines) |
| 498 |
|
| 499 |
def reset_instrument(self): |
| 500 |
''' Resets the session configuration to default values, and |
| 501 |
resets the device and driver software to a known state. |
| 502 |
''' |
| 503 |
self.dio.reset_instrument() |
| 504 |
|
| 505 |
class DigitalMultimeter(VirtualBenchInstrument): |
| 506 |
""" Represents Digital Multimeter (DMM) Module of Virtual Bench |
| 507 |
device. Allows to measure either DC/AC voltage or current, |
| 508 |
Resistance or Diodes. |
| 509 |
""" |
| 510 |
|
| 511 |
def __init__(self, virtualbench, reset, vb_name=''): |
| 512 |
""" Acquire DMM module |
| 513 |
|
| 514 |
:param virtualbench: Instance of the VirtualBench class |
| 515 |
:type virtualbench: VirtualBench |
| 516 |
:param reset: Resets the instrument |
| 517 |
:type reset: bool |
| 518 |
""" |
| 519 |
super().__init__( |
| 520 |
virtualbench.acquire_digital_multimeter, |
| 521 |
reset, 'dmm', vb_name) |
| 522 |
self.dmm = self._instrument_handle |
| 523 |
|
| 524 |
@staticmethod |
| 525 |
def validate_range(dmm_function, range): |
| 526 |
""" Checks if ``range`` is valid for the chosen ``dmm_function`` |
| 527 |
|
| 528 |
:param int dmm_function: DMM Function |
| 529 |
:param range: Range value, e.g. maximum value to measure |
| 530 |
:type range: int or float |
| 531 |
:return: Range value to pass to instrument |
| 532 |
:rtype: int |
| 533 |
""" |
| 534 |
ref_ranges = { |
| 535 |
0: [0.1, 1, 10, 100, 300], |
| 536 |
1: [0.1, 1, 10, 100, 265], |
| 537 |
2: [0.01, 0.1, 1, 10], |
| 538 |
3: [0.005, 0.05, 0.5, 5], |
| 539 |
4: [100, 1000, 10000, 100000, 1000000, |
| 540 |
10000000, 100000000], |
| 541 |
} |
| 542 |
range = truncated_discrete_set(range, ref_ranges[dmm_function]) |
| 543 |
return range |
| 544 |
|
| 545 |
def validate_dmm_function(self, dmm_function): |
| 546 |
""" Check if DMM function *dmm_function* exists |
| 547 |
|
| 548 |
:param dmm_function: DMM function index or name: |
| 549 |
|
| 550 |
- ``'DC_VOLTS'``, ``'AC_VOLTS'`` |
| 551 |
- ``'DC_CURRENT'``, ``'AC_CURRENT'`` |
| 552 |
- ``'RESISTANCE'`` |
| 553 |
- ``'DIODE'`` |
| 554 |
|
| 555 |
:type dmm_function: int or str |
| 556 |
:return: DMM function index to pass to the instrument |
| 557 |
:rtype: int |
| 558 |
""" |
| 559 |
try: |
| 560 |
pyvb.DmmFunction(dmm_function) |
| 561 |
except Exception: |
| 562 |
try: |
| 563 |
dmm_function = pyvb.DmmFunction[dmm_function.upper()] |
| 564 |
except Exception: |
| 565 |
raise ValueError( |
| 566 |
"DMM Function may be 0-5, 'DC_VOLTS'," + |
| 567 |
" 'AC_VOLTS', 'DC_CURRENT', 'AC_CURRENT'," + |
| 568 |
" 'RESISTANCE' or 'DIODE'") |
| 569 |
return dmm_function |
| 570 |
|
| 571 |
def validate_auto_range_terminal(self, auto_range_terminal): |
| 572 |
""" Check value for choosing the auto range terminal for |
| 573 |
DC current measurement |
| 574 |
|
| 575 |
:param auto_range_terminal: Terminal to perform |
| 576 |
auto ranging (``'LOW'`` |
| 577 |
or ``'HIGH'``) |
| 578 |
:type auto_range_terminal: int or str |
| 579 |
:return: Auto range terminal to pass to the instrument |
| 580 |
:rtype: int |
| 581 |
""" |
| 582 |
try: |
| 583 |
pyvb.DmmCurrentTerminal(auto_range_terminal) |
| 584 |
except Exception: |
| 585 |
try: |
| 586 |
auto_range_terminal = pyvb.DmmCurrentTerminal[ |
| 587 |
auto_range_terminal.upper()] |
| 588 |
except Exception: |
| 589 |
raise ValueError( |
| 590 |
"Current Auto Range Terminal may be 0, 1," + |
| 591 |
" 'LOW' or 'HIGH'") |
| 592 |
return auto_range_terminal |
| 593 |
|
| 594 |
def configure_measurement(self, dmm_function, auto_range=True, |
| 595 |
manual_range=1.0): |
| 596 |
""" Configure Instrument to take a DMM measurement |
| 597 |
|
| 598 |
:param dmm_function:DMM function index or name: |
| 599 |
|
| 600 |
- ``'DC_VOLTS'``, ``'AC_VOLTS'`` |
| 601 |
- ``'DC_CURRENT'``, ``'AC_CURRENT'`` |
| 602 |
- ``'RESISTANCE'`` |
| 603 |
- ``'DIODE'`` |
| 604 |
|
| 605 |
:type dmm_function: int or str |
| 606 |
:param bool auto_range: Enable/Disable auto ranging |
| 607 |
:param float manual_range: Manually set measurement range |
| 608 |
""" |
| 609 |
dmm_function = self.validate_dmm_function(dmm_function) |
| 610 |
auto_range = strict_discrete_set(auto_range, [True, False]) |
| 611 |
if auto_range is False: |
| 612 |
manual_range = self.validate_range(dmm_function, range) |
| 613 |
self.dmm.configure_measurement( |
| 614 |
dmm_function, auto_range=auto_range, manual_range=manual_range) |
| 615 |
|
| 616 |
def configure_dc_voltage(self, dmm_input_resistance): |
| 617 |
""" Configure DC voltage input resistance |
| 618 |
|
| 619 |
:param dmm_input_resistance: Input resistance (``'TEN_MEGA_OHM'`` |
| 620 |
or ``'TEN_GIGA_OHM'``) |
| 621 |
:type dmm_input_resistance: int or str |
| 622 |
""" |
| 623 |
try: |
| 624 |
pyvb.DmmInputResistance(dmm_input_resistance) |
| 625 |
except Exception: |
| 626 |
try: |
| 627 |
dmm_input_resistance = pyvb.DmmInputResistance[ |
| 628 |
dmm_input_resistance.upper()] |
| 629 |
except Exception: |
| 630 |
raise ValueError( |
| 631 |
"Input Resistance may be 0, 1," + |
| 632 |
" 'TEN_MEGA_OHM' or 'TEN_GIGA_OHM'") |
| 633 |
self.dmm.configure_dc_voltage(dmm_input_resistance) |
| 634 |
|
| 635 |
def configure_dc_current(self, auto_range_terminal): |
| 636 |
""" Configure auto rage terminal for DC current measurement |
| 637 |
|
| 638 |
:param auto_range_terminal: Terminal to perform auto ranging |
| 639 |
(``'LOW'`` or ``'HIGH'``) |
| 640 |
""" |
| 641 |
auto_range_terminal = self.validate_auto_range_terminal( |
| 642 |
auto_range_terminal) |
| 643 |
self.dmm.configure_dc_current(auto_range_terminal) |
| 644 |
|
| 645 |
def configure_ac_current(self, auto_range_terminal): |
| 646 |
""" Configure auto rage terminal for AC current measurement |
| 647 |
|
| 648 |
:param auto_range_terminal: Terminal to perform auto ranging |
| 649 |
(``'LOW'`` or ``'HIGH'``) |
| 650 |
""" |
| 651 |
auto_range_terminal = self.validate_auto_range_terminal( |
| 652 |
auto_range_terminal) |
| 653 |
self.dmm.configure_ac_current(auto_range_terminal) |
| 654 |
|
| 655 |
def query_measurement(self): |
| 656 |
""" Query DMM measurement settings from the instrument |
| 657 |
|
| 658 |
:return: Auto range, range data |
| 659 |
:rtype: (bool, float) |
| 660 |
""" |
| 661 |
return self.dmm.query_measurement(0) |
| 662 |
|
| 663 |
def query_dc_voltage(self): |
| 664 |
""" Indicates input resistance setting for DC voltage measurement |
| 665 |
""" |
| 666 |
self.dmm.query_dc_voltage() |
| 667 |
|
| 668 |
def query_dc_current(self): |
| 669 |
""" Indicates auto range terminal for DC current measurement |
| 670 |
""" |
| 671 |
self.dmm.query_dc_current() |
| 672 |
|
| 673 |
def query_ac_current(self): |
| 674 |
""" Indicates auto range terminal for AC current measurement |
| 675 |
""" |
| 676 |
self.dmm.query_ac_current() |
| 677 |
|
| 678 |
def read(self): |
| 679 |
""" Read measurement value from the instrument |
| 680 |
|
| 681 |
:return: Measurement value |
| 682 |
:rtype: float |
| 683 |
""" |
| 684 |
self.dmm.read() |
| 685 |
|
| 686 |
def reset_instrument(self): |
| 687 |
""" Reset the DMM module to defaults |
| 688 |
""" |
| 689 |
self.dmm.reset_instrument() |
| 690 |
|
| 691 |
class FunctionGenerator(VirtualBenchInstrument): |
| 692 |
""" Represents Function Generator (FGEN) Module of Virtual |
| 693 |
Bench device. |
| 694 |
""" |
| 695 |
|
| 696 |
def __init__(self, virtualbench, reset, vb_name=''): |
| 697 |
""" Acquire FGEN module |
| 698 |
|
| 699 |
:param virtualbench: Instance of the VirtualBench class |
| 700 |
:type virtualbench: VirtualBench |
| 701 |
:param reset: Resets the instrument |
| 702 |
:type reset: bool |
| 703 |
""" |
| 704 |
super().__init__( |
| 705 |
virtualbench.acquire_function_generator, |
| 706 |
reset, 'fgen', vb_name) |
| 707 |
self.fgen = self._instrument_handle |
| 708 |
|
| 709 |
self._waveform_functions = {"SINE": 0, "SQUARE": 1, |
| 710 |
"TRIANGLE/RAMP": 2, "DC": 3} |
| 711 |
# self._waveform_functions_index = { |
| 712 |
# v: k for k, v in self._waveform_functions.items()} |
| 713 |
self._max_frequency = {"SINE": 20000000, "SQUARE": 5000000, |
| 714 |
"TRIANGLE/RAMP": 1000000, "DC": 20000000} |
| 715 |
|
| 716 |
def configure_standard_waveform(self, waveform_function, amplitude, |
| 717 |
dc_offset, frequency, duty_cycle): |
| 718 |
""" Configures the instrument to output a standard waveform. |
| 719 |
Check instrument manual for maximum ratings which depend on load. |
| 720 |
|
| 721 |
:param waveform_function: Waveform function (``"SINE", "SQUARE", |
| 722 |
"TRIANGLE/RAMP", "DC"``) |
| 723 |
:type waveform_function: int or str |
| 724 |
:param amplitude: Amplitude in volts |
| 725 |
:type amplitude: float |
| 726 |
:param dc_offset: DC offset in volts |
| 727 |
:type dc_offset: float |
| 728 |
:param frequency: Frequency in Hz |
| 729 |
:type frequency: float |
| 730 |
:param duty_cycle: Duty cycle in % |
| 731 |
:type duty_cycle: int |
| 732 |
""" |
| 733 |
waveform_function = strict_discrete_set( |
| 734 |
waveform_function.upper(), self._waveform_functions) |
| 735 |
max_frequency = self._max_frequency[waveform_function.upper()] |
| 736 |
waveform_function = self._waveform_functions[ |
| 737 |
waveform_function.upper()] |
| 738 |
amplitude = strict_range(amplitude, (0, 24)) |
| 739 |
dc_offset = strict_range(dc_offset, (-12, 12)) |
| 740 |
if (amplitude / 2 + abs(dc_offset)) > 12: |
| 741 |
raise ValueError( |
| 742 |
"Amplitude and DC Offset may not exceed +/-12V") |
| 743 |
duty_cycle = strict_range(duty_cycle, (0, 100)) |
| 744 |
frequency = strict_range(frequency, (0, max_frequency)) |
| 745 |
self.fgen.configure_standard_waveform( |
| 746 |
waveform_function, amplitude, dc_offset, frequency, duty_cycle) |
| 747 |
|
| 748 |
def configure_arbitrary_waveform(self, waveform, sample_period): |
| 749 |
""" Configures the instrument to output a waveform. The waveform is |
| 750 |
output either after the end of the current waveform if output |
| 751 |
is enabled, or immediately after output is enabled. |
| 752 |
|
| 753 |
:param waveform: Waveform as list of values |
| 754 |
:type waveform: list |
| 755 |
:param sample_period: Time between two waveform points |
| 756 |
(maximum of 125MS/s, which equals 80ns) |
| 757 |
:type sample_period: float |
| 758 |
""" |
| 759 |
strict_range(len(waveform), (1, 1e6)) # 1MS |
| 760 |
sample_period = strict_range(sample_period, (8e-8, 1)) |
| 761 |
self.fgen.configure_arbitrary_waveform(waveform, sample_period) |
| 762 |
|
| 763 |
def configure_arbitrary_waveform_gain_and_offset(self, gain, |
| 764 |
dc_offset): |
| 765 |
""" Configures the instrument to output an arbitrary waveform with |
| 766 |
a specified gain and offset value. The waveform is output either |
| 767 |
after the end of the current waveform if output is enabled, or |
| 768 |
immediately after output is enabled. |
| 769 |
|
| 770 |
:param gain: Gain, multiplier of waveform values |
| 771 |
:type gain: float |
| 772 |
:param dc_offset: DC offset in volts |
| 773 |
:type dc_offset: float |
| 774 |
""" |
| 775 |
dc_offset = strict_range(dc_offset, (-12, 12)) |
| 776 |
self.fgen.configure_arbitrary_waveform_gain_and_offset( |
| 777 |
gain, dc_offset) |
| 778 |
|
| 779 |
@property |
| 780 |
def filter(self): |
| 781 |
''' Enables or disables the filter on the instrument. |
| 782 |
|
| 783 |
:param bool enable_filter: Enable/Disable filter |
| 784 |
''' |
| 785 |
return self.fgen.query_filter |
| 786 |
|
| 787 |
@filter.setter |
| 788 |
def filter(self, enable_filter): |
| 789 |
enable_filter = strict_discrete_set(enable_filter, [True, False]) |
| 790 |
self.fgen.enable_filter(enable_filter) |
| 791 |
|
| 792 |
def query_waveform_mode(self): |
| 793 |
""" Indicates whether the waveform output by the instrument is a |
| 794 |
standard or arbitrary waveform. |
| 795 |
|
| 796 |
:return: Waveform mode |
| 797 |
:rtype: enum |
| 798 |
""" |
| 799 |
return self.fgen.query_waveform_mode() |
| 800 |
|
| 801 |
def query_standard_waveform(self): |
| 802 |
""" Returns the settings for a standard waveform generation. |
| 803 |
|
| 804 |
:return: Waveform function, amplitude, dc_offset, frequency, |
| 805 |
duty_cycle |
| 806 |
:rtype: (enum, float, float, float, int) |
| 807 |
""" |
| 808 |
return self.fgen.query_standard_waveform() |
| 809 |
|
| 810 |
def query_arbitrary_waveform(self): |
| 811 |
""" Returns the samples per second for arbitrary waveform |
| 812 |
generation. |
| 813 |
|
| 814 |
:return: Samples per second |
| 815 |
:rtype: int |
| 816 |
""" |
| 817 |
return self.fgen.query_arbitrary_waveform() |
| 818 |
|
| 819 |
def query_arbitrary_waveform_gain_and_offset(self): |
| 820 |
""" Returns the settings for arbitrary waveform generation that |
| 821 |
includes gain and offset settings. |
| 822 |
|
| 823 |
:return: Gain, DC offset |
| 824 |
:rtype: (float, float) |
| 825 |
""" |
| 826 |
return self.fgen.query_arbitrary_waveform_gain_and_offset() |
| 827 |
|
| 828 |
def query_generation_status(self): |
| 829 |
""" Returns the status of waveform generation on the instrument. |
| 830 |
|
| 831 |
:return: Status |
| 832 |
:rtype: enum |
| 833 |
""" |
| 834 |
return self.fgen.query_generation_status() |
| 835 |
|
| 836 |
def run(self): |
| 837 |
''' Transitions the session from the Stopped state to the Running |
| 838 |
state. |
| 839 |
''' |
| 840 |
log.info("%s START" % self.name) |
| 841 |
self.fgen.run() |
| 842 |
|
| 843 |
def self_calibrate(self): |
| 844 |
'''Performs offset nulling calibration on the device. You must run |
| 845 |
FGEN Initialize prior to running this method. |
| 846 |
''' |
| 847 |
self.fgen.self_calibrate() |
| 848 |
|
| 849 |
def stop(self): |
| 850 |
''' Transitions the acquisition from either the Triggered or |
| 851 |
Running state to the Stopped state. |
| 852 |
''' |
| 853 |
log.info("%s STOP" % self.name) |
| 854 |
self.fgen.stop() |
| 855 |
|
| 856 |
def reset_instrument(self): |
| 857 |
''' Resets the session configuration to default values, and resets |
| 858 |
the device and driver software to a known state. |
| 859 |
''' |
| 860 |
self.fgen.reset_instrument() |
| 861 |
|
| 862 |
class MixedSignalOscilloscope(VirtualBenchInstrument): |
| 863 |
""" Represents Mixed Signal Oscilloscope (MSO) Module of Virtual Bench |
| 864 |
device. Allows to measure oscilloscope data from analog and digital |
| 865 |
channels. |
| 866 |
|
| 867 |
Methods from pyvirtualbench not implemented in pymeasure yet: |
| 868 |
|
| 869 |
- ``enable_digital_channels`` |
| 870 |
- ``configure_digital_threshold`` |
| 871 |
- ``configure_advanced_digital_timing`` |
| 872 |
- ``configure_state_mode`` |
| 873 |
- ``configure_digital_edge_trigger`` |
| 874 |
- ``configure_digital_pattern_trigger`` |
| 875 |
- ``configure_digital_glitch_trigger`` |
| 876 |
- ``configure_digital_pulse_width_trigger`` |
| 877 |
- ``query_digital_channel`` |
| 878 |
- ``query_enabled_digital_channels`` |
| 879 |
- ``query_digital_threshold`` |
| 880 |
- ``query_advanced_digital_timing`` |
| 881 |
- ``query_state_mode`` |
| 882 |
- ``query_digital_edge_trigger`` |
| 883 |
- ``query_digital_pattern_trigger`` |
| 884 |
- ``query_digital_glitch_trigger`` |
| 885 |
- ``query_digital_pulse_width_trigger`` |
| 886 |
- ``read_digital_u64`` |
| 887 |
""" |
| 888 |
|
| 889 |
def __init__(self, virtualbench, reset, vb_name=''): |
| 890 |
""" Acquire MSO module |
| 891 |
|
| 892 |
:param virtualbench: Instance of the VirtualBench class |
| 893 |
:type virtualbench: VirtualBench |
| 894 |
:param reset: Resets the instrument |
| 895 |
:type reset: bool |
| 896 |
""" |
| 897 |
super().__init__( |
| 898 |
virtualbench.acquire_mixed_signal_oscilloscope, |
| 899 |
reset, 'mso', vb_name) |
| 900 |
self.mso = self._instrument_handle |
| 901 |
|
| 902 |
@staticmethod |
| 903 |
def validate_trigger_instance(trigger_instance): |
| 904 |
""" Check if ``trigger_instance`` is a valid choice |
| 905 |
|
| 906 |
:param trigger_instance: Trigger instance (``'A'`` or ``'B'``) |
| 907 |
:type trigger_instance: int or str |
| 908 |
:return: Trigger instance |
| 909 |
:rtype: int |
| 910 |
""" |
| 911 |
try: |
| 912 |
pyvb.MsoTriggerInstance(trigger_instance) |
| 913 |
except Exception: |
| 914 |
try: |
| 915 |
trigger_instance = pyvb.MsoTriggerInstance[ |
| 916 |
trigger_instance.upper()] |
| 917 |
except Exception: |
| 918 |
raise ValueError( |
| 919 |
"Trigger Instance may be 0, 1, 'A' or 'B'") |
| 920 |
return trigger_instance |
| 921 |
|
| 922 |
def validate_channel(self, channel): |
| 923 |
""" Check if ``channel`` is a correct specification |
| 924 |
|
| 925 |
:param str channel: Channel string |
| 926 |
:return: Channel string |
| 927 |
:rtype: str |
| 928 |
""" |
| 929 |
def error(channel=channel): |
| 930 |
raise ValueError( |
| 931 |
f"Channel specification {channel} is not valid!") |
| 932 |
channels = self._vb_handle.expand_channel_string(channel)[0] |
| 933 |
channels = channels.split(', ') |
| 934 |
return_value = [] |
| 935 |
for channel in channels: |
| 936 |
# split off lines by last '/' |
| 937 |
try: |
| 938 |
(device, channel) = re.match( |
| 939 |
r'(.*)(?:/)(.+)', channel).groups() |
| 940 |
except Exception: |
| 941 |
error() |
| 942 |
# validate numbers in range 1-2 |
| 943 |
if not int(channel) in range(1, 3): |
| 944 |
error() |
| 945 |
# validate device name: either 'mso' or 'device_name/mso' |
| 946 |
if device == 'mso': |
| 947 |
pass |
| 948 |
else: |
| 949 |
try: |
| 950 |
device = re.match( |
| 951 |
r'(VB[0-9]{4}-[0-9a-zA-Z]{7})(?:/)(.+)', |
| 952 |
device).groups()[0] |
| 953 |
except Exception: |
| 954 |
error() |
| 955 |
# device_name has to match |
| 956 |
if not device == self._device_name: |
| 957 |
error() |
| 958 |
# constructing line references for output |
| 959 |
return_value.append('mso/' + channel) |
| 960 |
|
| 961 |
return_value = ', '.join(return_value) |
| 962 |
return_value = self._vb_handle.collapse_channel_string( |
| 963 |
return_value)[0] # drop number of channels |
| 964 |
return return_value |
| 965 |
|
| 966 |
# -------------------------- |
| 967 |
# Configure Instrument |
| 968 |
# -------------------------- |
| 969 |
|
| 970 |
def auto_setup(self): |
| 971 |
""" Automatically configure the instrument |
| 972 |
""" |
| 973 |
self.mso.auto_setup() |
| 974 |
|
| 975 |
def configure_analog_channel(self, channel, enable_channel, |
| 976 |
vertical_range, vertical_offset, |
| 977 |
probe_attenuation, vertical_coupling): |
| 978 |
""" Configure analog measurement channel |
| 979 |
|
| 980 |
:param str channel: Channel string |
| 981 |
:param bool enable_channel: Enable/Disable channel |
| 982 |
:param float vertical_range: Vertical measurement range (0V - 20V), |
| 983 |
the instrument discretizes to these ranges: |
| 984 |
``[20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05]`` |
| 985 |
which are 5x the values shown in the native UI. |
| 986 |
:param float vertical_offset: Vertical offset to correct for |
| 987 |
(inverted compared to VB native UI, |
| 988 |
-20V - +20V, resolution 0.1mV) |
| 989 |
:param probe_attenuation: Probe attenuation (``'ATTENUATION_10X'`` |
| 990 |
or ``'ATTENUATION_1X'``) |
| 991 |
:type probe_attenuation: int or str |
| 992 |
:param vertical_coupling: Vertical coupling (``'AC'`` or ``'DC'``) |
| 993 |
:type vertical_coupling: int or str |
| 994 |
""" |
| 995 |
channel = self.validate_channel(channel) |
| 996 |
enable_channel = strict_discrete_set( |
| 997 |
enable_channel, [True, False]) |
| 998 |
vertical_range = strict_range(vertical_range, (0, 20)) |
| 999 |
vertical_offset = strict_discrete_range( |
| 1000 |
vertical_offset, [-20, 20], 1e-4 |
| 1001 |
) |
| 1002 |
try: |
| 1003 |
pyvb.MsoProbeAttenuation(probe_attenuation) |
| 1004 |
except Exception: |
| 1005 |
try: |
| 1006 |
probe_attenuation = pyvb.MsoProbeAttenuation[ |
| 1007 |
probe_attenuation.upper()] |
| 1008 |
except Exception: |
| 1009 |
raise ValueError( |
| 1010 |
"Probe Attenuation may be 1, 10," + |
| 1011 |
" 'ATTENUATION_10X' or 'ATTENUATION_1X'") |
| 1012 |
try: |
| 1013 |
pyvb.MsoCoupling(vertical_coupling) |
| 1014 |
except Exception: |
| 1015 |
try: |
| 1016 |
vertical_coupling = pyvb.MsoCoupling[ |
| 1017 |
vertical_coupling.upper()] |
| 1018 |
except Exception: |
| 1019 |
raise ValueError( |
| 1020 |
"Probe Attenuation may be 0, 1, 'AC' or 'DC'") |
| 1021 |
|
| 1022 |
self.mso.configure_analog_channel( |
| 1023 |
channel, enable_channel, vertical_range, vertical_offset, |
| 1024 |
probe_attenuation, vertical_coupling) |
| 1025 |
|
| 1026 |
def configure_analog_channel_characteristics(self, channel, |
| 1027 |
input_impedance, |
| 1028 |
bandwidth_limit): |
| 1029 |
""" Configure electrical characteristics of the specified channel |
| 1030 |
|
| 1031 |
:param str channel: Channel string |
| 1032 |
:param input_impedance: Input Impedance (``'ONE_MEGA_OHM'`` or |
| 1033 |
``'FIFTY_OHMS'``) |
| 1034 |
:type input_impedance: int or str |
| 1035 |
:param int bandwidth_limit: Bandwidth limit (100MHz or 20MHz) |
| 1036 |
""" |
| 1037 |
channel = self.validate_channel(channel) |
| 1038 |
try: |
| 1039 |
pyvb.MsoInputImpedance(input_impedance) |
| 1040 |
except Exception: |
| 1041 |
try: |
| 1042 |
input_impedance = pyvb.MsoInputImpedance[ |
| 1043 |
input_impedance.upper()] |
| 1044 |
except Exception: |
| 1045 |
raise ValueError( |
| 1046 |
"Probe Attenuation may be 0, 1," + |
| 1047 |
" 'ONE_MEGA_OHM' or 'FIFTY_OHMS'") |
| 1048 |
bandwidth_limit = strict_discrete_set( |
| 1049 |
bandwidth_limit, [100000000, 20000000]) # 100 Mhz or 20Mhz |
| 1050 |
self.mso.configure_analog_channel_characteristics( |
| 1051 |
channel, input_impedance, bandwidth_limit) |
| 1052 |
|
| 1053 |
def configure_timing(self, sample_rate, acquisition_time, |
| 1054 |
pretrigger_time, sampling_mode): |
| 1055 |
""" Configure timing settings of the MSO |
| 1056 |
|
| 1057 |
:param int sample_rate: Sample rate (15.26kS - 1GS) |
| 1058 |
:param float acquisition_time: Acquisition time (1ns - 68.711s) |
| 1059 |
:param float pretrigger_time: Pretrigger time (0s - 10s) |
| 1060 |
:param sampling_mode: Sampling mode (``'SAMPLE'`` or |
| 1061 |
``'PEAK_DETECT'``) |
| 1062 |
""" |
| 1063 |
sample_rate = strict_range(sample_rate, (15260, 1e9)) |
| 1064 |
acquisition_time = strict_discrete_range( |
| 1065 |
acquisition_time, (1e-09, 68.711), 1e-09) |
| 1066 |
# acquisition is also limited by buffer size, |
| 1067 |
# which depends on sample rate as well as acquisition time |
| 1068 |
pretrigger_time = strict_range(pretrigger_time, (0, 10)) |
| 1069 |
try: |
| 1070 |
pyvb.MsoSamplingMode(sampling_mode) |
| 1071 |
except Exception: |
| 1072 |
try: |
| 1073 |
sampling_mode = pyvb.MsoSamplingMode[sampling_mode.upper()] |
| 1074 |
except Exception: |
| 1075 |
raise ValueError( |
| 1076 |
"Sampling Mode may be 0, 1, 'SAMPLE' or 'PEAK_DETECT'") |
| 1077 |
|
| 1078 |
self.mso.configure_timing( |
| 1079 |
sample_rate, acquisition_time, pretrigger_time, sampling_mode) |
| 1080 |
|
| 1081 |
def configure_immediate_trigger(self): |
| 1082 |
""" Configures a trigger to immediately activate on the specified |
| 1083 |
channels after the pretrigger time has expired. |
| 1084 |
""" |
| 1085 |
self.mso.configure_immediate_trigger() |
| 1086 |
|
| 1087 |
def configure_analog_edge_trigger(self, trigger_source, trigger_slope, |
| 1088 |
trigger_level, trigger_hysteresis, |
| 1089 |
trigger_instance): |
| 1090 |
""" Configures a trigger to activate on the specified source when |
| 1091 |
the analog edge reaches the specified levels. |
| 1092 |
|
| 1093 |
:param str trigger_source: Channel string |
| 1094 |
:param trigger_slope: Trigger slope (``'RISING'``, ``'FALLING'`` |
| 1095 |
or ``'EITHER'``) |
| 1096 |
:type trigger_slope: int or str |
| 1097 |
:param float trigger_level: Trigger level |
| 1098 |
:param float trigger_hysteresis: Trigger hysteresis |
| 1099 |
:param trigger_instance: Trigger instance |
| 1100 |
:type trigger_instance: int or str |
| 1101 |
""" |
| 1102 |
trigger_source = self.validate_channel(trigger_source) |
| 1103 |
try: |
| 1104 |
pyvb.EdgeWithEither(trigger_slope) |
| 1105 |
except Exception: |
| 1106 |
try: |
| 1107 |
trigger_slope = pyvb.EdgeWithEither[trigger_slope.upper()] |
| 1108 |
except Exception: |
| 1109 |
raise ValueError( |
| 1110 |
"Trigger Slope may be 0, 1, 2, 'RISING'," + |
| 1111 |
" 'FALLING' or 'EITHER'") |
| 1112 |
trigger_instance = self.validate_trigger_instance(trigger_instance) |
| 1113 |
self.mso.configure_analog_edge_trigger( |
| 1114 |
trigger_source, trigger_slope, trigger_level, |
| 1115 |
trigger_hysteresis, trigger_instance) |
| 1116 |
|
| 1117 |
def configure_analog_pulse_width_trigger(self, trigger_source, |
| 1118 |
trigger_polarity, |
| 1119 |
trigger_level, |
| 1120 |
comparison_mode, lower_limit, |
| 1121 |
upper_limit, |
| 1122 |
trigger_instance): |
| 1123 |
""" Configures a trigger to activate on the specified source when |
| 1124 |
the analog edge reaches the specified levels within a specified |
| 1125 |
window of time. |
| 1126 |
|
| 1127 |
:param str trigger_source: Channel string |
| 1128 |
:param trigger_polarity: Trigger slope (``'POSITIVE'`` or |
| 1129 |
``'NEGATIVE'``) |
| 1130 |
:type trigger_polarity: int or str |
| 1131 |
:param float trigger_level: Trigger level |
| 1132 |
:param comparison_mode: Mode of compariosn ( |
| 1133 |
``'GREATER_THAN_UPPER_LIMIT'``, |
| 1134 |
``'LESS_THAN_LOWER_LIMIT'``, |
| 1135 |
``'INSIDE_LIMITS'`` or |
| 1136 |
``'OUTSIDE_LIMITS'``) |
| 1137 |
:type comparison_mode: int or str |
| 1138 |
:param float lower_limit: Lower limit |
| 1139 |
:param float upper_limit: Upper limit |
| 1140 |
:param trigger_instance: Trigger instance |
| 1141 |
:type trigger_instance: int or str |
| 1142 |
""" |
| 1143 |
trigger_source = self.validate_channel(trigger_source) |
| 1144 |
try: |
| 1145 |
pyvb.MsoTriggerPolarity(trigger_polarity) |
| 1146 |
except Exception: |
| 1147 |
try: |
| 1148 |
trigger_polarity = pyvb.MsoTriggerPolarity[ |
| 1149 |
trigger_polarity.upper()] |
| 1150 |
except Exception: |
| 1151 |
raise ValueError( |
| 1152 |
"Comparison Mode may be 0, 1, 2, 3," + |
| 1153 |
" 'GREATER_THAN_UPPER_LIMIT'," + |
| 1154 |
" 'LESS_THAN_LOWER_LIMIT'," + |
| 1155 |
" 'INSIDE_LIMITS' or 'OUTSIDE_LIMITS'") |
| 1156 |
try: |
| 1157 |
pyvb.MsoComparisonMode(comparison_mode) |
| 1158 |
except Exception: |
| 1159 |
try: |
| 1160 |
comparison_mode = pyvb.MsoComparisonMode[ |
| 1161 |
comparison_mode.upper()] |
| 1162 |
except Exception: |
| 1163 |
raise ValueError( |
| 1164 |
"Trigger Polarity may be 0, 1," + |
| 1165 |
" 'POSITIVE' or 'NEGATIVE'") |
| 1166 |
trigger_instance = self.validate_trigger_instance(trigger_instance) |
| 1167 |
self.mso.configure_analog_pulse_width_trigger( |
| 1168 |
trigger_source, trigger_polarity, trigger_level, |
| 1169 |
comparison_mode, lower_limit, upper_limit, trigger_instance) |
| 1170 |
|
| 1171 |
def configure_trigger_delay(self, trigger_delay): |
| 1172 |
""" Configures the amount of time to wait after a trigger condition |
| 1173 |
is met before triggering. |
| 1174 |
|
| 1175 |
:param float trigger_delay: Trigger delay (0s - 17.1799s) |
| 1176 |
""" |
| 1177 |
self.mso.configure_trigger_delay(trigger_delay) |
| 1178 |
|
| 1179 |
def query_analog_channel(self, channel): |
| 1180 |
""" Indicates the vertical configuration of the specified channel. |
| 1181 |
|
| 1182 |
:return: Channel enabled, vertical range, vertical offset, |
| 1183 |
probe attenuation, vertical coupling |
| 1184 |
:rtype: (bool, float, float, enum, enum) |
| 1185 |
""" |
| 1186 |
channel = self.validate_channel(channel) |
| 1187 |
return self.mso.query_analog_channel(channel) |
| 1188 |
|
| 1189 |
def query_enabled_analog_channels(self): |
| 1190 |
""" Returns String of enabled analog channels. |
| 1191 |
|
| 1192 |
:return: Enabled analog channels |
| 1193 |
:rtype: str |
| 1194 |
""" |
| 1195 |
return self.mso.query_enabled_analog_channels() |
| 1196 |
|
| 1197 |
def query_analog_channel_characteristics(self, channel): |
| 1198 |
""" Indicates the properties that control the electrical |
| 1199 |
characteristics of the specified channel. |
| 1200 |
This method returns an error if too much power is |
| 1201 |
applied to the channel. |
| 1202 |
|
| 1203 |
:return: Input impedance, bandwidth limit |
| 1204 |
:rtype: (enum, float) |
| 1205 |
""" |
| 1206 |
return self.mso.query_analog_channel_characteristics(channel) |
| 1207 |
|
| 1208 |
def query_timing(self): |
| 1209 |
""" Indicates the timing configuration of the MSO. |
| 1210 |
Call directly before measurement to read the actual timing |
| 1211 |
configuration and write it to the corresponding class variables. |
| 1212 |
Necessary to interpret the measurement data, since it contains no |
| 1213 |
time information. |
| 1214 |
|
| 1215 |
:return: Sample rate, acquisition time, pretrigger time, |
| 1216 |
sampling mode |
| 1217 |
:rtype: (float, float, float, enum) |
| 1218 |
""" |
| 1219 |
(self.sample_rate, self.acquisition_time, |
| 1220 |
self.pretrigger_time, |
| 1221 |
self.sampling_mode) = self.mso.query_timing() |
| 1222 |
return (self.sample_rate, self.acquisition_time, |
| 1223 |
self.pretrigger_time, self.sampling_mode) |
| 1224 |
|
| 1225 |
def query_trigger_type(self, trigger_instance): |
| 1226 |
""" Indicates the trigger type of the specified instance. |
| 1227 |
|
| 1228 |
:param trigger_instance: Trigger instance (``'A'`` or ``'B'``) |
| 1229 |
:return: Trigger type |
| 1230 |
:rtype: str |
| 1231 |
""" |
| 1232 |
return self.mso.query_trigger_type() |
| 1233 |
|
| 1234 |
def query_analog_edge_trigger(self, trigger_instance): |
| 1235 |
""" Indicates the analog edge trigger configuration of the |
| 1236 |
specified instance. |
| 1237 |
|
| 1238 |
:return: Trigger source, trigger slope, trigger level, trigger |
| 1239 |
hysteresis |
| 1240 |
:rtype: (str, enum, float, float) |
| 1241 |
""" |
| 1242 |
trigger_instance = self.validate_trigger_instance(trigger_instance) |
| 1243 |
return self.mso.query_analog_edge_trigger(trigger_instance) |
| 1244 |
|
| 1245 |
def query_trigger_delay(self): |
| 1246 |
""" Indicates the trigger delay setting of the MSO. |
| 1247 |
|
| 1248 |
:return: Trigger delay |
| 1249 |
:rtype: float |
| 1250 |
""" |
| 1251 |
return self.mso.query_trigger_delay() |
| 1252 |
|
| 1253 |
def query_analog_pulse_width_trigger(self, trigger_instance): |
| 1254 |
""" Indicates the analog pulse width trigger configuration of the |
| 1255 |
specified instance. |
| 1256 |
|
| 1257 |
:return: Trigger source, trigger polarity, trigger level, |
| 1258 |
comparison mode, lower limit, upper limit |
| 1259 |
:rtype: (str, enum, float, enum, float, float) |
| 1260 |
""" |
| 1261 |
trigger_instance = self.validate_trigger_instance(trigger_instance) |
| 1262 |
return self.mso.query_analog_pulse_width_trigger(trigger_instance) |
| 1263 |
|
| 1264 |
def query_acquisition_status(self): |
| 1265 |
""" Returns the status of a completed or ongoing acquisition. |
| 1266 |
""" |
| 1267 |
return self.mso.query_acquisition_status() |
| 1268 |
|
| 1269 |
# -------------------------- |
| 1270 |
# Measurement Control |
| 1271 |
# -------------------------- |
| 1272 |
|
| 1273 |
def run(self, autoTrigger=True): |
| 1274 |
""" Transitions the acquisition from the Stopped state to the |
| 1275 |
Running state. If the current state is Triggered, the |
| 1276 |
acquisition is first transitioned to the Stopped state before |
| 1277 |
transitioning to the Running state. This method returns an |
| 1278 |
error if too much power is applied to any enabled channel. |
| 1279 |
|
| 1280 |
:param bool autoTrigger: Enable/Disable auto triggering |
| 1281 |
""" |
| 1282 |
self.mso.run(autoTrigger) |
| 1283 |
|
| 1284 |
def force_trigger(self): |
| 1285 |
""" Causes a software-timed trigger to occur after the pretrigger |
| 1286 |
time has expired. |
| 1287 |
""" |
| 1288 |
self.mso.force_trigger() |
| 1289 |
|
| 1290 |
def stop(self): |
| 1291 |
""" Transitions the acquisition from either the Triggered or |
| 1292 |
Running state to the Stopped state. |
| 1293 |
""" |
| 1294 |
self.mso.stop() |
| 1295 |
|
| 1296 |
def read_analog_digital_u64(self): |
| 1297 |
""" Transfers data from the instrument as long as the acquisition |
| 1298 |
state is Acquisition Complete. If the state is either Running or |
| 1299 |
Triggered, this method will wait until the state transitions to |
| 1300 |
Acquisition Complete. If the state is Stopped, this method |
| 1301 |
returns an error. |
| 1302 |
|
| 1303 |
:return: Analog data out, analog data stride, analog t0, |
| 1304 |
digital data out, digital timestamps out, digital t0, |
| 1305 |
trigger timestamp, trigger reason |
| 1306 |
:rtype: (list, int, pyvb.Timestamp, list, list, pyvb.Timestamp, |
| 1307 |
pyvb.Timestamp, enum) |
| 1308 |
""" |
| 1309 |
return self.mso.read_analog_digital_u64() |
| 1310 |
|
| 1311 |
def read_analog_digital_dataframe(self): |
| 1312 |
""" Transfers data from the instrument and returns a pandas |
| 1313 |
dataframe of the analog measurement data, including time |
| 1314 |
coordinates |
| 1315 |
|
| 1316 |
:return: Dataframe with time and measurement data |
| 1317 |
:rtype: pd.DataFrame |
| 1318 |
""" |
| 1319 |
(analog_data_out, analog_data_stride |
| 1320 |
# , analog_t0, digital_data_out, digital_timestamps_out, |
| 1321 |
# digital_t0, trigger_timestamp, trigger_reason |
| 1322 |
) = self.read_analog_digital_u64()[0:2] |
| 1323 |
|
| 1324 |
number_of_samples = int(self.sample_rate * |
| 1325 |
self.acquisition_time) + 1 |
| 1326 |
if not number_of_samples == (len(analog_data_out) / |
| 1327 |
analog_data_stride): |
| 1328 |
# try updating timing parameters |
| 1329 |
self.query_timing() |
| 1330 |
number_of_samples = int(self.sample_rate * |
| 1331 |
self.acquisition_time) + 1 |
| 1332 |
if not number_of_samples == (len(analog_data_out) / |
| 1333 |
analog_data_stride): |
| 1334 |
raise ValueError( |
| 1335 |
"Length of Analog Data does not match" + |
| 1336 |
" Timing Parameters") |
| 1337 |
|
| 1338 |
pretrigger_samples = int(self.sample_rate * self.pretrigger_time) |
| 1339 |
times = ( |
| 1340 |
list(range(-pretrigger_samples, 0)) |
| 1341 |
+ list(range(0, number_of_samples - pretrigger_samples))) |
| 1342 |
times = [list(map(lambda x: x * 1 / self.sample_rate, times))] |
| 1343 |
|
| 1344 |
np_array = np.array(analog_data_out) |
| 1345 |
np_array = np.split(np_array, analog_data_stride) |
| 1346 |
np_array = np.append(np.array(times), np_array, axis=0) |
| 1347 |
np_array = np.transpose(np_array) |
| 1348 |
return pd.DataFrame(data=np_array) |
| 1349 |
|
| 1350 |
def reset_instrument(self): |
| 1351 |
""" Resets the session configuration to default values, and resets |
| 1352 |
the device and driver software to a known state. |
| 1353 |
""" |
| 1354 |
self.mso.reset() |
| 1355 |
|
| 1356 |
class PowerSupply(VirtualBenchInstrument): |
| 1357 |
""" Represents Power Supply (PS) Module of Virtual Bench device |
| 1358 |
""" |
| 1359 |
|
| 1360 |
def __init__(self, virtualbench, reset, vb_name=''): |
| 1361 |
""" Acquire PS module |
| 1362 |
|
| 1363 |
:param virtualbench: Instance of the VirtualBench class |
| 1364 |
:type virtualbench: VirtualBench |
| 1365 |
:param reset: Resets the instrument |
| 1366 |
:type reset: bool |
| 1367 |
""" |
| 1368 |
super().__init__( |
| 1369 |
virtualbench.acquire_power_supply, |
| 1370 |
reset, 'ps', vb_name) |
| 1371 |
self.ps = self._instrument_handle |
| 1372 |
|
| 1373 |
def validate_channel(self, channel, current=False, voltage=False): |
| 1374 |
""" Check if channel string is valid and if output current/voltage |
| 1375 |
are within the output ranges of the channel |
| 1376 |
|
| 1377 |
:param channel: Channel string (``"ps/+6V","ps/+25V","ps/-25V"``) |
| 1378 |
:type channel: str |
| 1379 |
:param current: Current output, defaults to False |
| 1380 |
:type current: bool, optional |
| 1381 |
:param voltage: Voltage output, defaults to False |
| 1382 |
:type voltage: bool, optional |
| 1383 |
:return: channel or channel, current & voltage |
| 1384 |
:rtype: str or (str, float, float) |
| 1385 |
""" |
| 1386 |
if current is False and voltage is False: |
| 1387 |
return strict_discrete_set( |
| 1388 |
channel, ["ps/+6V", "ps/+25V", "ps/-25V"]) |
| 1389 |
else: |
| 1390 |
channel = strict_discrete_set( |
| 1391 |
channel, ["ps/+6V", "ps/+25V", "ps/-25V"]) |
| 1392 |
if channel == "ps/+6V": |
| 1393 |
current_range = (0, 1) |
| 1394 |
voltage_range = (0, 6) |
| 1395 |
else: |
| 1396 |
current_range = (0, 5) |
| 1397 |
voltage_range = (0, 25) |
| 1398 |
if channel == "ps/-25V": |
| 1399 |
voltage_range = (0, -25) |
| 1400 |
current = strict_discrete_range(current, current_range, 1e-3) |
| 1401 |
voltage = strict_discrete_range(voltage, voltage_range, 1e-3) |
| 1402 |
return (channel, current, voltage) |
| 1403 |
|
| 1404 |
def configure_voltage_output(self, channel, voltage_level, |
| 1405 |
current_limit): |
| 1406 |
''' Configures a voltage output on the specified channel. This |
| 1407 |
method should be called once for every channel you want to |
| 1408 |
configure to output voltage. |
| 1409 |
''' |
| 1410 |
(channel, current_limit, voltage_level) = self.validate_channel( |
| 1411 |
channel, current_limit, voltage_level) |
| 1412 |
self.ps.configure_voltage_output( |
| 1413 |
channel, voltage_level, current_limit) |
| 1414 |
|
| 1415 |
def configure_current_output(self, channel, current_level, |
| 1416 |
voltage_limit): |
| 1417 |
''' Configures a current output on the specified channel. This |
| 1418 |
method should be called once for every channel you want to |
| 1419 |
configure to output current. |
| 1420 |
''' |
| 1421 |
(channel, current_level, voltage_limit) = self.validate_channel( |
| 1422 |
channel, current_level, voltage_limit) |
| 1423 |
self.ps.configure_current_output( |
| 1424 |
channel, current_level, voltage_limit) |
| 1425 |
|
| 1426 |
def query_voltage_output(self, channel): |
| 1427 |
''' Indicates the voltage output settings on the specified channel. |
| 1428 |
''' |
| 1429 |
channel = self.validate_channel(channel) |
| 1430 |
return self.ps.query_voltage_output(channel) |
| 1431 |
|
| 1432 |
def query_current_output(self, channel): |
| 1433 |
''' Indicates the current output settings on the specified channel. |
| 1434 |
''' |
| 1435 |
channel = self.validate_channel(channel) |
| 1436 |
return self.ps.query_current_output(channel) |
| 1437 |
|
| 1438 |
@property |
| 1439 |
def outputs_enabled(self): |
| 1440 |
''' Enables or disables all outputs on all channels of the |
| 1441 |
instrument. |
| 1442 |
|
| 1443 |
:param bool enable_outputs: Enable/Disable outputs |
| 1444 |
''' |
| 1445 |
return self.ps.query_outputs_enabled() |
| 1446 |
|
| 1447 |
@outputs_enabled.setter |
| 1448 |
def outputs_enabled(self, enable_outputs): |
| 1449 |
enable_outputs = strict_discrete_set( |
| 1450 |
enable_outputs, [True, False]) |
| 1451 |
log.info(f"{self.name} Output {enable_outputs}.") |
| 1452 |
self.ps.enable_all_outputs(enable_outputs) |
| 1453 |
|
| 1454 |
@property |
| 1455 |
def tracking(self): |
| 1456 |
''' Enables or disables tracking between the positive and negative |
| 1457 |
25V channels. If enabled, any configuration change on the |
| 1458 |
positive 25V channel is mirrored to the negative 25V channel, |
| 1459 |
and any writes to the negative 25V channel are ignored. |
| 1460 |
|
| 1461 |
:param bool enable_tracking: Enable/Disable tracking |
| 1462 |
''' |
| 1463 |
return self.ps.query_tracking() |
| 1464 |
|
| 1465 |
@tracking.setter |
| 1466 |
def tracking(self, enable_tracking): |
| 1467 |
enable_tracking = strict_discrete_set( |
| 1468 |
enable_tracking, [True, False]) |
| 1469 |
self.ps.enable_tracking(enable_tracking) |
| 1470 |
|
| 1471 |
def read_output(self, channel): |
| 1472 |
''' Reads the voltage and current levels and outout mode of the |
| 1473 |
specified channel. |
| 1474 |
''' |
| 1475 |
channel = self.validate_channel(channel) |
| 1476 |
return self.ps.read_output() |
| 1477 |
|
| 1478 |
def reset_instrument(self): |
| 1479 |
''' Resets the session configuration to default values, and resets |
| 1480 |
the device and driver software to a known state. |
| 1481 |
''' |
| 1482 |
self.ps.reset_instrument() |