Debian Codegraph @

pymeasure/instruments/pendulum/cnt91.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import logging
26 from time import sleep
27 from warnings import warn
28
29 from pymeasure.instruments import Instrument, SCPIUnknownMixin
30 from pymeasure.instruments.validators import (
31 strict_discrete_set,
32 strict_range,
33 truncated_range,
34 )
35
36 log = logging.getLogger(__name__)
37 log.addHandler(logging.NullHandler())
38
39 # Defined outside of the class, since it is used by `Instrument.control` without access to `self`.
40 MIN_GATE_TIME = 2e-8 # Programmer's guide 8-92
41 MAX_GATE_TIME = 1000 # Programmer's guide 8-92
42 MIN_BUFFER_SIZE = 4 # Programmer's guide 8-39
43 MAX_BUFFER_SIZE = 10000 # Programmer's guide 8-39
44
45
46 class CNT91(SCPIUnknownMixin, Instrument):
47 """Represents a Pendulum CNT-91 frequency counter."""
48
49 CHANNELS = {"A": 1, "B": 2, "C": 3, "E": 4, "INTREF": 6}
50
51 def __init__(self, adapter, name="Pendulum CNT-91", **kwargs):
52 # allow long-term measurements, add 30 s for data transfer
53 kwargs.setdefault("timeout", 24 * 60 * 60 * 1000 + 30)
54 kwargs.setdefault("read_termination", "\n")
55
56 super().__init__(
57 adapter,
58 name,
59 asrl={"baud_rate": 256000},
60 **kwargs,
61 )
62
63 @property
64 def batch_size(self):
65 """Get maximum number of buffer entries that can be transmitted at once."""
66 if not hasattr(self, "_batch_size"):
67 self._batch_size = int(self.ask("FORM:SMAX?"))
68 return self._batch_size
69
70 external_start_arming_source = Instrument.control(
71 "ARM:SOUR?",
72 "ARM:SOUR %s",
73 """Control external arming source ('A', 'B', 'E' (rear) or 'IMM' for immediately arming).""", # noqa: E501
74 validator=strict_discrete_set,
75 values={"A": "EXT1", "B": "EXT2", "E": "EXT4", "IMM": "IMM"},
76 map_values=True,
77 )
78
79 external_arming_start_slope = Instrument.control(
80 "ARM:SLOP?",
81 "ARM:SLOP %s",
82 """Control slope for the start arming condition (str 'POS' or 'NEG').""",
83 validator=strict_discrete_set,
84 values=["POS", "NEG"],
85 )
86
87 continuous = Instrument.control(
88 "INIT:CONT?",
89 "INIT:CONT %s",
90 """Control whether to perform continuous measurements.""",
91 strict_discrete_set,
92 values={True: 1.0, False: 0.0},
93 map_values=True,
94 )
95
96 @property
97 def measurement_time(self):
98 """
99 Control gate time of one measurement in s (float strictly from 2e-8 to 1000).
100
101 .. deprecated:: 0.14
102 Use `gate_time` instead.
103 """
104 warn("`measurement_time` is deprecated, use `gate_time` instead.", FutureWarning)
105 return self.gate_time
106
107 @measurement_time.setter
108 def measurement_time(self, value):
109 warn("`measurement_time` is deprecated, use `gate_time` instead.", FutureWarning)
110 self.gate_time = value
111
112 gate_time = Instrument.control(
113 ":ACQ:APER?",
114 ":ACQ:APER %s",
115 """Control gate time of one measurement in s (float strictly from 2e-8 to 1000).""",
116 validator=strict_range,
117 values=[MIN_GATE_TIME, MAX_GATE_TIME], # Programmer's guide 8-92
118 )
119
120 format = Instrument.control(
121 "FORM?",
122 "FORM %s",
123 "Control response format ('ASCII' or 'REAL').",
124 validator=strict_discrete_set,
125 values={"ASCII": "ASC", "REAL": "REAL"},
126 map_values=True,
127 )
128
129 interpolator_autocalibrated = Instrument.control(
130 ":CAL:INT:AUTO?",
131 "CAL:INT:AUTO %s",
132 """Control if interpolators should be calibrated automatically (bool).""",
133 strict_discrete_set,
134 values={True: 1.0, False: 0.0},
135 map_values=True,
136 )
137
138 def read_buffer(self, n=MAX_BUFFER_SIZE):
139 """
140 Read out `n` samples from the buffer.
141
142 :param n: Number of samples that should be read from the buffer. The maximum number of
143 10000 samples is read out by default.
144 :return: Frequency values from the buffer.
145 """
146 n = truncated_range(n, [MIN_BUFFER_SIZE, MAX_BUFFER_SIZE]) # Programmer's guide 8-39
147 while not self.complete:
148 # Wait until the buffer is filled.
149 sleep(0.01)
150 return self.values(f":FETC:ARR? {'MAX' if n == MAX_BUFFER_SIZE else n}")
151
152 def configure_frequency_array_measurement(self, n_samples, channel, back_to_back=True):
153 """
154 Configure the counter for an array of measurements.
155
156 :param n_samples: The number of samples
157 :param channel: Measurement channel (A, B, C, E, INTREF)
158 :param back_to_back: If True, the buffer measurement is performed back-to-back.
159 """
160 n_samples = truncated_range(n_samples, [MIN_BUFFER_SIZE, MAX_BUFFER_SIZE])
161 channel = strict_discrete_set(channel, self.CHANNELS)
162 channel = self.CHANNELS[channel]
163 self.write(f":CONF:ARR:FREQ{':BTB' if back_to_back else ''} {n_samples},(@{channel})")
164
165 def buffer_frequency_time_series(
166 self,
167 channel,
168 n_samples,
169 sample_rate=None, # deprecated, only kept for backwards compatibility
170 gate_time=None,
171 trigger_source=None,
172 back_to_back=True,
173 ):
174 """
175 Record a time series to the buffer and read it out after completion.
176
177 :param channel: Channel that should be used
178 :param n_samples: The number of samples
179 :param gate_time: Gate time in s
180 :param trigger_source: Optionally specify a trigger source to start the measurement
181 :param back_to_back: If True, the buffer measurement is performed back-to-back.
182 :param sample_rate: Sample rate in Hz
183
184 .. deprecated:: 0.14
185 Use parameter `gate_time` instead.
186 """
187 if (gate_time is None) and (sample_rate is None):
188 raise ValueError("`gate_time` must be specified.")
189 if sample_rate is not None:
190 warn("`sample_rate` is deprecated, use `gate_time` instead.", FutureWarning)
191 if gate_time is not None:
192 raise ValueError("Only one of `gate_time` and `sample_rate` can be specified.")
193 gate_time = 1 / sample_rate
194 self.clear()
195 self.format = "ASCII"
196 self.configure_frequency_array_measurement(n_samples, channel, back_to_back=back_to_back)
197 self.continuous = False
198 self.gate_time = gate_time
199
200 if trigger_source:
201 self.external_start_arming_source = trigger_source
202
203 # start the measurement (or wait for trigger)
204 self.write(":INIT")