| 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 numpy as np |
| 26 |
from time import sleep |
| 27 |
from importlib.util import find_spec |
| 28 |
|
| 29 |
if find_spec('pycomedi'): # Guard against pycomedi not being installed |
| 30 |
from pycomedi.subdevice import StreamingSubdevice |
| 31 |
from pycomedi.constant import AREF, CMDF, SUBDEVICE_TYPE, TRIG_SRC, UNIT |
| 32 |
from pycomedi.constant import _NamedInt |
| 33 |
from pycomedi.channel import AnalogChannel |
| 34 |
from pycomedi.utility import inttrig_insn |
| 35 |
|
| 36 |
|
| 37 |
def getAI(device, channel, range=None): |
| 38 |
""" Returns the analog input channel as specified for a given device |
| 39 |
""" |
| 40 |
ai = device.find_subdevice_by_type( |
| 41 |
SUBDEVICE_TYPE.ai, factory=StreamingSubdevice |
| 42 |
).channel(channel, factory=AnalogChannel, aref=AREF.diff) |
| 43 |
if range is not None: |
| 44 |
ai.range = ai.find_range(unit=UNIT.volt, min=range[0], max=range[1]) |
| 45 |
return ai |
| 46 |
|
| 47 |
|
| 48 |
def getAO(device, channel, range=None): |
| 49 |
""" Returns the analog output channel as specified for a given device |
| 50 |
""" |
| 51 |
ao = device.find_subdevice_by_type( |
| 52 |
SUBDEVICE_TYPE.ao, factory=StreamingSubdevice |
| 53 |
).channel(channel, factory=AnalogChannel, aref=AREF.diff) |
| 54 |
if range is not None: |
| 55 |
ao.range = ao.find_range(unit=UNIT.volt, min=range[0], max=range[1]) |
| 56 |
return ao |
| 57 |
|
| 58 |
|
| 59 |
def readAI(device, channel, range=None, count=1): |
| 60 |
""" Reads a single measurement (count==1) from the analog input channel |
| 61 |
of the device specified. Multiple readings can be preformed with count |
| 62 |
not equal to one, which are seperated by an arbitrary time |
| 63 |
""" |
| 64 |
ai = getAI(device, channel, range) |
| 65 |
converter = ai.get_converter() |
| 66 |
if count == 1: |
| 67 |
return converter.to_physical(ai.data_read()) |
| 68 |
else: |
| 69 |
return converter.to_physical(ai.data_read_n(count)) |
| 70 |
|
| 71 |
|
| 72 |
def writeAO(device, channel, voltage, range=None): |
| 73 |
""" Writes a single voltage to the analog output channel of the |
| 74 |
device specified |
| 75 |
""" |
| 76 |
ao = getAO(device, channel, range) |
| 77 |
converter = ao.get_converter() |
| 78 |
ao.data_write(converter.from_physical(voltage)) |
| 79 |
|
| 80 |
|
| 81 |
class SynchronousAI: |
| 82 |
|
| 83 |
def __init__(self, channels, period, samples): |
| 84 |
self.channels = channels |
| 85 |
self.samples = samples |
| 86 |
self.period = period |
| 87 |
self.scanPeriod = int(1e9 * float(period) / float(samples)) # nano-seconds |
| 88 |
|
| 89 |
self.subdevice = self.channels[0].subdevice |
| 90 |
self.subdevice.cmd = self._command() |
| 91 |
|
| 92 |
def _command(self): |
| 93 |
""" Returns the command used to initiate and end the sampling |
| 94 |
""" |
| 95 |
command = self.subdevice.get_cmd_generic_timed(len(self.channels), |
| 96 |
self.scanPeriod) |
| 97 |
command.start_src = TRIG_SRC.int |
| 98 |
command.start_arg = 0 |
| 99 |
command.stop_src = TRIG_SRC.count |
| 100 |
command.stop_arg = self.samples |
| 101 |
command.chanlist = self.channels |
| 102 |
# Adding to remove chunk transfers (TRIG_WAKE_EOS) |
| 103 |
wake_eos = _NamedInt('wake_eos', 32) |
| 104 |
if wake_eos not in CMDF: |
| 105 |
CMDF.append(wake_eos) |
| 106 |
command.flags = CMDF.wake_eos |
| 107 |
return command |
| 108 |
|
| 109 |
def _verifyCommand(self): |
| 110 |
""" Checks the command over three times and allows comedi to correct |
| 111 |
the command given any device specific conflicts |
| 112 |
""" |
| 113 |
for i in range(3): |
| 114 |
rc = self.subdevice.command_test() # Verify command is correct |
| 115 |
if rc is None: |
| 116 |
break |
| 117 |
|
| 118 |
def measure(self, hasAborted=lambda: False): |
| 119 |
""" Initiates the scan after first checking the command |
| 120 |
and does not block, returns the starting timestamp |
| 121 |
""" |
| 122 |
self._verifyCommand() |
| 123 |
sleep(0.01) |
| 124 |
self.subdevice.command() |
| 125 |
|
| 126 |
length = len(self.channels) |
| 127 |
dtype = self.subdevice.get_dtype() |
| 128 |
converters = [c.get_converter() for c in self.channels] |
| 129 |
|
| 130 |
self.data = np.zeros((self.samples, length), dtype=np.float32) |
| 131 |
|
| 132 |
# Trigger AI |
| 133 |
self.subdevice.device.do_insn(inttrig_insn(self.subdevice)) |
| 134 |
|
| 135 |
# Measurement loop |
| 136 |
count = 0 |
| 137 |
size = int(self.data.itemsize / 2) * length |
| 138 |
previous_bin_slice = b'' |
| 139 |
|
| 140 |
while not hasAborted() and self.samples > count: |
| 141 |
bin_slice = previous_bin_slice |
| 142 |
while len(bin_slice) < size: |
| 143 |
bin_slice += self.subdevice.device.file.read(size) |
| 144 |
previous_bin_slice = bin_slice[size:] |
| 145 |
bin_slice = bin_slice[:size] |
| 146 |
slice = np.fromstring( |
| 147 |
bin_slice, |
| 148 |
dtype=dtype, |
| 149 |
count=length |
| 150 |
) |
| 151 |
|
| 152 |
if len(slice) != length: # Reading finished |
| 153 |
break |
| 154 |
|
| 155 |
# Convert to physical values |
| 156 |
for i, c in enumerate(converters): |
| 157 |
self.data[count, i] = c.to_physical(slice[i]) |
| 158 |
|
| 159 |
self.emit_progress(100. * count / self.samples) |
| 160 |
self.emit_data(self.data[count]) |
| 161 |
count += 1 |
| 162 |
|
| 163 |
# Cancel measurement if it is still running (abort event) |
| 164 |
if self.subdevice.get_flags().running: |
| 165 |
self.subdevice.cancel() |
| 166 |
|
| 167 |
|
| 168 |
""" Command for limited samples |
| 169 |
|
| 170 |
command = self.subdevice.get_cmd_generic_timed(len(self.channels), |
| 171 |
self.scanPeriod) |
| 172 |
command.start_src = TRIG_SRC.int |
| 173 |
command.start_arg = 0 |
| 174 |
command.stop_src = TRIG_SRC.count |
| 175 |
command.stop_arg = self.samples |
| 176 |
command.chanlist = self.channels |
| 177 |
|
| 178 |
Command for continuous AI |
| 179 |
|
| 180 |
command = self.subdevice.get_cmd_generic_timed(len(self.channels), self.scanPeriod) |
| 181 |
command.start_src = TRIG_SRC.int |
| 182 |
command.start_arg = 0 |
| 183 |
command.stop_src = TRIG_SRC.none |
| 184 |
command.stop_arg = 0 |
| 185 |
command.chanlist = self.channels |
| 186 |
""" |