scietex.hal.voltage-amplifier 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scietex/hal/voltage_amplifier/__init__.py +10 -0
- scietex/hal/voltage_amplifier/base/__init__.py +56 -0
- scietex/hal/voltage_amplifier/base/capabilities.py +322 -0
- scietex/hal/voltage_amplifier/base/data.py +68 -0
- scietex/hal/voltage_amplifier/base/protocol.py +203 -0
- scietex/hal/voltage_amplifier/generic/__init__.py +6 -0
- scietex/hal/voltage_amplifier/generic/modbus/__init__.py +37 -0
- scietex/hal/voltage_amplifier/generic/modbus/client.py +384 -0
- scietex/hal/voltage_amplifier/generic/modbus/errors.py +49 -0
- scietex/hal/voltage_amplifier/generic/modbus/registers.py +94 -0
- scietex/hal/voltage_amplifier/py.typed +0 -0
- scietex/hal/voltage_amplifier/registry.py +66 -0
- scietex/hal/voltage_amplifier/version.py +1 -0
- scietex_hal_voltage_amplifier-0.1.0.dist-info/METADATA +318 -0
- scietex_hal_voltage_amplifier-0.1.0.dist-info/RECORD +18 -0
- scietex_hal_voltage_amplifier-0.1.0.dist-info/WHEEL +5 -0
- scietex_hal_voltage_amplifier-0.1.0.dist-info/licenses/LICENSE +21 -0
- scietex_hal_voltage_amplifier-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Scietex voltage amplifier HAL package.
|
|
2
|
+
|
|
3
|
+
A ``VoltageAmplifier`` scales an input voltage: the driver layer exposes its
|
|
4
|
+
capability set, its state snapshot, and the transport-agnostic control contract
|
|
5
|
+
that concrete protocols implement.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .version import __version__
|
|
9
|
+
|
|
10
|
+
__all__ = ["__version__"]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base voltage amplifier subpackage.
|
|
3
|
+
|
|
4
|
+
Holds the transport-agnostic pieces every driver shares: the capability model
|
|
5
|
+
that advertises what an amplifier can do, the state snapshot it reports, and the
|
|
6
|
+
driver contract concrete protocols implement.
|
|
7
|
+
|
|
8
|
+
Classes:
|
|
9
|
+
Access: Whether a setting can be read and whether it can be written.
|
|
10
|
+
AmplifierState: A snapshot of the values an amplifier reports.
|
|
11
|
+
FilterCapabilities: Signal-filter surface.
|
|
12
|
+
FilterSetting: One filter in the amplifier's signal path.
|
|
13
|
+
FilterType: The filter responses an amplifier can apply.
|
|
14
|
+
IndicationCapabilities: A read-only status flag.
|
|
15
|
+
InputVoltageCapabilities: Static input voltage range limits.
|
|
16
|
+
OutputVoltageCapabilities: Static output voltage swing limits.
|
|
17
|
+
SelectableCapabilities: An access plus the values it accepts.
|
|
18
|
+
VoltageAmplifierCapabilities: The complete capability set for one amplifier.
|
|
19
|
+
VoltageAmplifierProtocol: The contract every voltage amplifier driver
|
|
20
|
+
implements.
|
|
21
|
+
VoltageGainCapabilities: Gain selection with the bounds it accepts.
|
|
22
|
+
|
|
23
|
+
Modules:
|
|
24
|
+
capabilities: Defines the capability model.
|
|
25
|
+
data: Defines the state snapshot and the filter setting.
|
|
26
|
+
protocol: Defines the `VoltageAmplifierProtocol` driver contract.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from .capabilities import (
|
|
30
|
+
Access,
|
|
31
|
+
FilterCapabilities,
|
|
32
|
+
FilterType,
|
|
33
|
+
IndicationCapabilities,
|
|
34
|
+
InputVoltageCapabilities,
|
|
35
|
+
OutputVoltageCapabilities,
|
|
36
|
+
SelectableCapabilities,
|
|
37
|
+
VoltageAmplifierCapabilities,
|
|
38
|
+
VoltageGainCapabilities,
|
|
39
|
+
)
|
|
40
|
+
from .data import AmplifierState, FilterSetting
|
|
41
|
+
from .protocol import VoltageAmplifierProtocol
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"Access",
|
|
45
|
+
"AmplifierState",
|
|
46
|
+
"FilterCapabilities",
|
|
47
|
+
"FilterSetting",
|
|
48
|
+
"FilterType",
|
|
49
|
+
"IndicationCapabilities",
|
|
50
|
+
"InputVoltageCapabilities",
|
|
51
|
+
"OutputVoltageCapabilities",
|
|
52
|
+
"SelectableCapabilities",
|
|
53
|
+
"VoltageAmplifierCapabilities",
|
|
54
|
+
"VoltageAmplifierProtocol",
|
|
55
|
+
"VoltageGainCapabilities",
|
|
56
|
+
]
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Voltage amplifier capability model.
|
|
3
|
+
|
|
4
|
+
An amplifier advertises what it can do through a single
|
|
5
|
+
:class:`VoltageAmplifierCapabilities` value, grouped by function rather than by
|
|
6
|
+
method name. The grouping is semantic: a driver maps its own vocabulary onto
|
|
7
|
+
these groups, so a caller can ask "can this amplifier change its gain?" without
|
|
8
|
+
knowing whether the underlying protocol calls the setting a factor, a code, or a
|
|
9
|
+
register.
|
|
10
|
+
|
|
11
|
+
Two kinds of surface are modelled separately. A switch is an :class:`Access`,
|
|
12
|
+
which distinguishes "readable" from "writeable" because many devices expose a
|
|
13
|
+
setting read-only. A switch that also takes a value is a
|
|
14
|
+
:class:`SelectableCapabilities`, which adds the domain of accepted values.
|
|
15
|
+
|
|
16
|
+
A third kind is static geometry: a limit that never changes while the device
|
|
17
|
+
runs, such as the input voltage range or the output voltage swing, is capability
|
|
18
|
+
data rather than state. It is read from the capability set directly instead of
|
|
19
|
+
through a protocol method or a ``supports_*`` property.
|
|
20
|
+
|
|
21
|
+
The groups are frozen dataclasses so a driver declares them once as a class
|
|
22
|
+
attribute and callers cannot mutate them.
|
|
23
|
+
|
|
24
|
+
Classes:
|
|
25
|
+
FilterType: The filter responses an amplifier can apply.
|
|
26
|
+
Access: Whether a setting can be read and whether it can be written.
|
|
27
|
+
SelectableCapabilities: An access plus the values it accepts.
|
|
28
|
+
OutputVoltageCapabilities: Static output voltage swing limits.
|
|
29
|
+
InputVoltageCapabilities: Static input voltage range limits.
|
|
30
|
+
VoltageGainCapabilities: Gain selection with the bounds it accepts.
|
|
31
|
+
FilterCapabilities: Signal-filter surface.
|
|
32
|
+
IndicationCapabilities: A read-only status flag.
|
|
33
|
+
VoltageAmplifierCapabilities: The complete capability set for one amplifier.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
from dataclasses import dataclass, field
|
|
37
|
+
from enum import Enum
|
|
38
|
+
from typing import Generic, TypeVar
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class FilterType(str, Enum):
|
|
42
|
+
"""The filter responses an amplifier can apply.
|
|
43
|
+
|
|
44
|
+
The member value is the wire spelling a driver sends; it doubles as the
|
|
45
|
+
serialised form because the class inherits from ``str``.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
LOW_PASS = "low_pass"
|
|
49
|
+
HIGH_PASS = "high_pass"
|
|
50
|
+
BAND_PASS = "band_pass"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class Access:
|
|
55
|
+
"""
|
|
56
|
+
Whether a setting can be read and whether it can be written.
|
|
57
|
+
|
|
58
|
+
Attributes:
|
|
59
|
+
read (bool): Whether the current value can be read back.
|
|
60
|
+
write (bool): Whether the value can be set.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
read: bool = False
|
|
64
|
+
write: bool = False
|
|
65
|
+
|
|
66
|
+
def __bool__(self) -> bool:
|
|
67
|
+
"""Return whether the setting is supported in either direction."""
|
|
68
|
+
return self.read or self.write
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
T = TypeVar("T")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True, slots=True, eq=False)
|
|
75
|
+
class SelectableCapabilities(Generic[T]):
|
|
76
|
+
"""
|
|
77
|
+
An access plus the values it accepts.
|
|
78
|
+
|
|
79
|
+
Attributes:
|
|
80
|
+
access (Access): Whether the setting is readable and writeable.
|
|
81
|
+
options (tuple[T, ...]): The values the device accepts, in the device's
|
|
82
|
+
own order and native units. An empty tuple means the device does
|
|
83
|
+
not enumerate its domain, which is typical of a continuous setting
|
|
84
|
+
such as a gain or a cutoff frequency; it does **not** mean the
|
|
85
|
+
device accepts no value. A caller that needs the legal domain of an
|
|
86
|
+
enumerating device reads the options, and against a device with an
|
|
87
|
+
empty tuple it must rely on the state snapshot instead.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
access: Access = field(default_factory=Access)
|
|
91
|
+
options: tuple[T, ...] = ()
|
|
92
|
+
|
|
93
|
+
def __bool__(self) -> bool:
|
|
94
|
+
"""Return whether the setting is supported in either direction."""
|
|
95
|
+
return bool(self.access)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass(frozen=True, slots=True)
|
|
99
|
+
class OutputVoltageCapabilities:
|
|
100
|
+
"""
|
|
101
|
+
Static output voltage swing limits.
|
|
102
|
+
|
|
103
|
+
The limits are geometry rather than runtime state, which is why they live in
|
|
104
|
+
the capability set: they do not change while the device runs. The output
|
|
105
|
+
voltage the device currently reports is a field of the state snapshot.
|
|
106
|
+
|
|
107
|
+
Attributes:
|
|
108
|
+
min_v (float | None): Most negative output voltage in V, or ``None`` when
|
|
109
|
+
the driver does not know the limit.
|
|
110
|
+
max_v (float | None): Most positive output voltage in V, or ``None`` when
|
|
111
|
+
the driver does not know the limit.
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
min_v: float | None = None
|
|
115
|
+
max_v: float | None = None
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True, slots=True)
|
|
119
|
+
class InputVoltageCapabilities:
|
|
120
|
+
"""
|
|
121
|
+
Static input voltage range limits.
|
|
122
|
+
|
|
123
|
+
The input range is the span the front end accepts before it clips or
|
|
124
|
+
saturates. Like the output swing it is geometry rather than runtime state,
|
|
125
|
+
so it belongs to the capability set, while the input voltage the device
|
|
126
|
+
currently reports is a field of the state snapshot.
|
|
127
|
+
|
|
128
|
+
Attributes:
|
|
129
|
+
min_v (float | None): Most negative input voltage in V, or ``None`` when
|
|
130
|
+
the driver does not know the limit.
|
|
131
|
+
max_v (float | None): Most positive input voltage in V, or ``None`` when
|
|
132
|
+
the driver does not know the limit.
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
min_v: float | None = None
|
|
136
|
+
max_v: float | None = None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass(frozen=True, slots=True)
|
|
140
|
+
class VoltageGainCapabilities:
|
|
141
|
+
"""
|
|
142
|
+
Gain selection with the bounds it accepts.
|
|
143
|
+
|
|
144
|
+
Gain is dimensionless and expressed in V/V: the output voltage divided by
|
|
145
|
+
the input voltage. A device may enumerate its gains, describe them as a
|
|
146
|
+
bounded continuous range, or do neither, and the three cases are
|
|
147
|
+
distinguishable: enumerated gains appear in ``gain.options``, a bounded
|
|
148
|
+
range appears in ``min_gain`` and ``max_gain``, and an empty ``options``
|
|
149
|
+
with both bounds unknown means the domain is unknown rather than empty.
|
|
150
|
+
|
|
151
|
+
``step`` documents the smallest increment the device resolves; it never
|
|
152
|
+
quantises anything at this layer, so :meth:`accepts` does not round.
|
|
153
|
+
|
|
154
|
+
Attributes:
|
|
155
|
+
gain (SelectableCapabilities[float]): Selectable gains as dimensionless
|
|
156
|
+
factors. An empty tuple means the domain is continuous or not
|
|
157
|
+
enumerated.
|
|
158
|
+
min_gain (float | None): Lowest settable gain in V/V, or ``None`` when
|
|
159
|
+
the driver does not know the bound.
|
|
160
|
+
max_gain (float | None): Highest settable gain in V/V, or ``None`` when
|
|
161
|
+
the driver does not know the bound.
|
|
162
|
+
step (float | None): Smallest gain increment the device resolves, or
|
|
163
|
+
``None`` when unknown.
|
|
164
|
+
default_gain (float | None): The gain the device applies at power-on or
|
|
165
|
+
reset, or ``None`` when the driver does not know it. It is never
|
|
166
|
+
assumed to be unity.
|
|
167
|
+
"""
|
|
168
|
+
|
|
169
|
+
gain: SelectableCapabilities[float] = field(default_factory=SelectableCapabilities)
|
|
170
|
+
min_gain: float | None = None
|
|
171
|
+
max_gain: float | None = None
|
|
172
|
+
step: float | None = None
|
|
173
|
+
default_gain: float | None = None
|
|
174
|
+
|
|
175
|
+
def __bool__(self) -> bool:
|
|
176
|
+
"""Return whether the gain is selectable or bounded."""
|
|
177
|
+
return bool(self.gain) or self.min_gain is not None or self.max_gain is not None
|
|
178
|
+
|
|
179
|
+
def accepts(self, gain: float) -> bool | None:
|
|
180
|
+
"""
|
|
181
|
+
Tell whether the device accepts a gain.
|
|
182
|
+
|
|
183
|
+
Enumerated options take precedence over the bounds, because a device
|
|
184
|
+
that lists its gains accepts exactly those values. When the options are
|
|
185
|
+
empty the bounds are used, and a bound that is ``None`` leaves that side
|
|
186
|
+
unbounded.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
gain: The dimensionless gain in V/V to test.
|
|
190
|
+
|
|
191
|
+
Returns:
|
|
192
|
+
bool | None: Whether the gain lies in the advertised domain, or
|
|
193
|
+
``None`` when the driver enumerates no option and knows no bound, so
|
|
194
|
+
the domain is unknown rather than empty.
|
|
195
|
+
"""
|
|
196
|
+
if self.gain.options:
|
|
197
|
+
return gain in self.gain.options
|
|
198
|
+
if self.min_gain is None and self.max_gain is None:
|
|
199
|
+
return None
|
|
200
|
+
below_min = self.min_gain is not None and gain < self.min_gain
|
|
201
|
+
above_max = self.max_gain is not None and gain > self.max_gain
|
|
202
|
+
return not (below_min or above_max)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@dataclass(frozen=True, slots=True)
|
|
206
|
+
class FilterCapabilities:
|
|
207
|
+
"""
|
|
208
|
+
Signal-filter surface.
|
|
209
|
+
|
|
210
|
+
Attributes:
|
|
211
|
+
types (tuple[FilterType, ...]): Filter responses the amplifier supports.
|
|
212
|
+
An empty tuple means no filter is available.
|
|
213
|
+
enabled (Access): Whether the filter can be read and switched on or off.
|
|
214
|
+
cutoff (Access): Whether the filter cutoff frequency can be read and set.
|
|
215
|
+
The frequency domain is carried by the state snapshot, so it is not
|
|
216
|
+
enumerated here.
|
|
217
|
+
"""
|
|
218
|
+
|
|
219
|
+
types: tuple[FilterType, ...] = ()
|
|
220
|
+
enabled: Access = field(default_factory=Access)
|
|
221
|
+
cutoff: Access = field(default_factory=Access)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
@dataclass(frozen=True, slots=True)
|
|
225
|
+
class IndicationCapabilities:
|
|
226
|
+
"""
|
|
227
|
+
A read-only status flag.
|
|
228
|
+
|
|
229
|
+
Attributes:
|
|
230
|
+
read (bool): Whether the flag can be read.
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
read: bool = False
|
|
234
|
+
|
|
235
|
+
def __bool__(self) -> bool:
|
|
236
|
+
"""Return whether the flag is reported."""
|
|
237
|
+
return self.read
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
@dataclass(frozen=True, slots=True)
|
|
241
|
+
class VoltageAmplifierCapabilities:
|
|
242
|
+
"""
|
|
243
|
+
The complete capability set for one amplifier.
|
|
244
|
+
|
|
245
|
+
Every group defaults to "unsupported", so a driver declares only what it
|
|
246
|
+
actually implements.
|
|
247
|
+
|
|
248
|
+
A capability answers whether a feature is available; it does not answer
|
|
249
|
+
whether the feature is currently on. A present feature that is switched off
|
|
250
|
+
is truthy here (``bool(...) is True``) while the state flag in the snapshot
|
|
251
|
+
is ``False``. Filtering on capabilities and reading the state are therefore
|
|
252
|
+
both required to act correctly.
|
|
253
|
+
|
|
254
|
+
Attributes:
|
|
255
|
+
gain (VoltageGainCapabilities): Gain selection, in V/V.
|
|
256
|
+
input_voltage (InputVoltageCapabilities): Static input voltage range
|
|
257
|
+
limits, in V.
|
|
258
|
+
output_voltage (OutputVoltageCapabilities): Static output voltage swing
|
|
259
|
+
limits, in V.
|
|
260
|
+
filters (FilterCapabilities): Signal-filter surface.
|
|
261
|
+
output_overload (IndicationCapabilities): Output-overload flag.
|
|
262
|
+
invert_output (Access): Whether the output polarity can be read and set.
|
|
263
|
+
"""
|
|
264
|
+
|
|
265
|
+
gain: VoltageGainCapabilities = field(default_factory=VoltageGainCapabilities)
|
|
266
|
+
input_voltage: InputVoltageCapabilities = field(
|
|
267
|
+
default_factory=InputVoltageCapabilities
|
|
268
|
+
)
|
|
269
|
+
output_voltage: OutputVoltageCapabilities = field(
|
|
270
|
+
default_factory=OutputVoltageCapabilities
|
|
271
|
+
)
|
|
272
|
+
filters: FilterCapabilities = field(default_factory=FilterCapabilities)
|
|
273
|
+
output_overload: IndicationCapabilities = field(
|
|
274
|
+
default_factory=IndicationCapabilities
|
|
275
|
+
)
|
|
276
|
+
invert_output: Access = field(default_factory=Access)
|
|
277
|
+
|
|
278
|
+
def max_output_swing_v(self) -> float | None:
|
|
279
|
+
"""
|
|
280
|
+
Compute the largest symmetric output excursion.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
float | None: The smaller of ``|min_v|`` and ``|max_v|``, in V, which
|
|
284
|
+
is how far the output can swing in either direction without
|
|
285
|
+
clipping. ``None`` when either limit is unknown, because a symmetric
|
|
286
|
+
excursion then cannot be guaranteed.
|
|
287
|
+
"""
|
|
288
|
+
limits = self.output_voltage
|
|
289
|
+
if limits.min_v is None or limits.max_v is None:
|
|
290
|
+
return None
|
|
291
|
+
return min(abs(limits.min_v), abs(limits.max_v))
|
|
292
|
+
|
|
293
|
+
def max_input_voltage_v(self, gain: float) -> float | None:
|
|
294
|
+
"""
|
|
295
|
+
Compute the input voltage that drives the output to its swing limit.
|
|
296
|
+
|
|
297
|
+
The input range and the output swing divided by the gain are two
|
|
298
|
+
independent ceilings, and the smaller one binds first.
|
|
299
|
+
|
|
300
|
+
Args:
|
|
301
|
+
gain: Dimensionless voltage gain in V/V.
|
|
302
|
+
|
|
303
|
+
Returns:
|
|
304
|
+
float | None: The input-voltage ceiling in V, or ``None`` when
|
|
305
|
+
neither the input range nor the output swing is known.
|
|
306
|
+
|
|
307
|
+
Raises:
|
|
308
|
+
ValueError: If ``gain`` is zero or negative, since such a gain
|
|
309
|
+
cannot scale a voltage.
|
|
310
|
+
"""
|
|
311
|
+
if gain <= 0:
|
|
312
|
+
raise ValueError(f"gain must be positive, got {gain!r} V/V")
|
|
313
|
+
candidates: list[float] = []
|
|
314
|
+
limits = self.input_voltage
|
|
315
|
+
if limits.min_v is not None and limits.max_v is not None:
|
|
316
|
+
candidates.append(min(abs(limits.min_v), abs(limits.max_v)))
|
|
317
|
+
swing_v = self.max_output_swing_v()
|
|
318
|
+
if swing_v is not None:
|
|
319
|
+
candidates.append(swing_v / gain)
|
|
320
|
+
if not candidates:
|
|
321
|
+
return None
|
|
322
|
+
return min(candidates)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Voltage amplifier data structures.
|
|
2
|
+
|
|
3
|
+
:class:`AmplifierState` is a snapshot of what the device reports at one moment;
|
|
4
|
+
:class:`FilterSetting` is one entry of the filter list inside it.
|
|
5
|
+
|
|
6
|
+
``None`` means "this driver cannot report the field", not "zero" and not "off".
|
|
7
|
+
A field that is ``None`` has no known value at all, so a caller must check for
|
|
8
|
+
``None`` rather than treating a missing reading as an idle amplifier.
|
|
9
|
+
|
|
10
|
+
Filter fields are device-native engineering units. No unit or scale is invented
|
|
11
|
+
at this layer: a driver that speaks counts or register codes stores the value it
|
|
12
|
+
reads and documents the conversion in the driver.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import msgspec
|
|
16
|
+
|
|
17
|
+
from .capabilities import FilterType
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class FilterSetting(msgspec.Struct, frozen=True, kw_only=True):
|
|
21
|
+
"""One filter in the amplifier's signal path.
|
|
22
|
+
|
|
23
|
+
Attributes:
|
|
24
|
+
type: The filter response the entry configures.
|
|
25
|
+
enabled: Whether this filter is currently switched on. The field is a
|
|
26
|
+
plain ``bool``, not ``bool | None``: a driver either reports the
|
|
27
|
+
filter entry together with its on/off state or omits the entry.
|
|
28
|
+
low_cutoff_hz: Lower cutoff frequency in Hz, or ``None`` when the filter
|
|
29
|
+
has no lower corner or the driver does not report it. A high-pass
|
|
30
|
+
or band-pass filter may report only one corner.
|
|
31
|
+
high_cutoff_hz: Upper cutoff frequency in Hz, or ``None`` when the
|
|
32
|
+
filter has no upper corner or the driver does not report it.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
type: FilterType
|
|
36
|
+
enabled: bool = False
|
|
37
|
+
low_cutoff_hz: float | None = None
|
|
38
|
+
high_cutoff_hz: float | None = None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AmplifierState(msgspec.Struct, frozen=True, kw_only=True):
|
|
42
|
+
"""A snapshot of the values an amplifier reports.
|
|
43
|
+
|
|
44
|
+
Each field is ``None`` when the driver cannot report it. ``None`` is not
|
|
45
|
+
``0.0`` and not ``False``: an unreported gain is unknown, not unity, and an
|
|
46
|
+
unreadable overload flag is unknown, not clear.
|
|
47
|
+
|
|
48
|
+
Attributes:
|
|
49
|
+
input_voltage_v: Input voltage in V, or ``None`` when unreadable.
|
|
50
|
+
output_voltage_v: Output voltage in V, or ``None`` when unreadable.
|
|
51
|
+
output_current_a: Output current in A, or ``None`` when unreadable.
|
|
52
|
+
gain: Dimensionless voltage gain in V/V, or ``None`` when the driver
|
|
53
|
+
does not report it. ``None`` is not ``1.0``: an unreported gain may
|
|
54
|
+
be anything, while unity is only the value the device applies at
|
|
55
|
+
power-on when the driver says so.
|
|
56
|
+
filters: Configured filters, in the order the device reports them.
|
|
57
|
+
output_overload: Output-overload flag, or ``None`` when unreadable.
|
|
58
|
+
inverted: Whether the output polarity is inverted, or ``None`` when
|
|
59
|
+
unreadable.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
input_voltage_v: float | None = None
|
|
63
|
+
output_voltage_v: float | None = None
|
|
64
|
+
output_current_a: float | None = None
|
|
65
|
+
gain: float | None = None
|
|
66
|
+
filters: tuple[FilterSetting, ...] = ()
|
|
67
|
+
output_overload: bool | None = None
|
|
68
|
+
inverted: bool | None = None
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Voltage amplifier protocol.
|
|
3
|
+
|
|
4
|
+
This module defines the transport-agnostic contract every voltage amplifier
|
|
5
|
+
driver implements, plus the capability conveniences that tell a caller which
|
|
6
|
+
optional control operations a given amplifier supports. The contract is
|
|
7
|
+
deliberately minimal: a driver must be able to connect, disconnect and read the
|
|
8
|
+
state snapshot. Every control operation beyond that is advertised through
|
|
9
|
+
:attr:`VoltageAmplifierProtocol.capabilities`, so a caller can reject an
|
|
10
|
+
unsupported command before dispatching it instead of catching an exception
|
|
11
|
+
after the fact.
|
|
12
|
+
|
|
13
|
+
Every driver I/O call is a coroutine, because the shared transport is
|
|
14
|
+
asynchronous: callers must ``await`` :meth:`connect`, :meth:`close`,
|
|
15
|
+
:meth:`read_data` and every optional accessor, and an unimplemented optional
|
|
16
|
+
accessor raises ``NotImplementedError`` when awaited. The ``supports_*`` and
|
|
17
|
+
``can_*`` properties are pure capability checks over :attr:`capabilities` and
|
|
18
|
+
stay synchronous.
|
|
19
|
+
|
|
20
|
+
Two failure signals carry different meanings and must not be confused. A
|
|
21
|
+
``NotImplementedError`` raised by an optional method means "this driver does not
|
|
22
|
+
implement that capability": the feature is absent, so the caller should consult
|
|
23
|
+
the capability set and take a different path. A ``None`` field in the snapshot
|
|
24
|
+
returned by :meth:`VoltageAmplifierProtocol.read_data` means "this driver cannot
|
|
25
|
+
report that value": the feature may well exist and be controllable, but the
|
|
26
|
+
current reading is unknown, so the caller must not substitute zero or ``False``.
|
|
27
|
+
|
|
28
|
+
Output overload is an exception to the method-based surface: it is read only
|
|
29
|
+
through ``read_data()`` and has no dedicated accessor. The capability set says
|
|
30
|
+
whether the driver reports it; the snapshot says what it currently is.
|
|
31
|
+
|
|
32
|
+
Filter kinds are read from ``capabilities.filters.types``; there is no
|
|
33
|
+
per-kind ``supports_*`` property, so adding a :class:`FilterType` never changes
|
|
34
|
+
this class.
|
|
35
|
+
|
|
36
|
+
The voltage range and the output swing are static geometry rather than
|
|
37
|
+
controllable features, so they have no ``supports_*`` property either: a caller
|
|
38
|
+
reads ``capabilities.input_voltage`` and ``capabilities.output_voltage``
|
|
39
|
+
directly. The gain is the only scalable quantity: the amplifier multiplies the
|
|
40
|
+
input voltage by it, optionally filtering the signal and inverting the polarity.
|
|
41
|
+
|
|
42
|
+
Classes:
|
|
43
|
+
VoltageAmplifierProtocol: The runtime-checkable protocol every voltage
|
|
44
|
+
amplifier driver satisfies.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
from typing import ClassVar, Protocol, runtime_checkable
|
|
48
|
+
|
|
49
|
+
from .capabilities import FilterType, VoltageAmplifierCapabilities
|
|
50
|
+
from .data import AmplifierState, FilterSetting
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@runtime_checkable
|
|
54
|
+
class VoltageAmplifierProtocol(Protocol):
|
|
55
|
+
"""
|
|
56
|
+
Contract implemented by every voltage amplifier driver.
|
|
57
|
+
|
|
58
|
+
The three core methods are mandatory. Every optional method raises
|
|
59
|
+
``NotImplementedError`` by default and is advertised through
|
|
60
|
+
:attr:`capabilities`; callers must consult the capability set before
|
|
61
|
+
invoking one. Every I/O method is a coroutine; the convenience properties
|
|
62
|
+
are synchronous.
|
|
63
|
+
|
|
64
|
+
Attributes:
|
|
65
|
+
capabilities: What this amplifier can do, grouped by function. A driver
|
|
66
|
+
declares it as a class attribute, so the whole capability set is
|
|
67
|
+
known before an instance connects to hardware.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
capabilities: ClassVar[VoltageAmplifierCapabilities]
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def supports_gain(self) -> bool:
|
|
74
|
+
"""Whether the amplifier exposes adjustable voltage gain."""
|
|
75
|
+
return bool(self.capabilities.gain)
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def supports_filters(self) -> bool:
|
|
79
|
+
"""Whether the amplifier exposes any signal filter control."""
|
|
80
|
+
filters = self.capabilities.filters
|
|
81
|
+
return bool(filters.types) or bool(filters.enabled) or bool(filters.cutoff)
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def supports_output_overload(self) -> bool:
|
|
85
|
+
"""Whether the amplifier reports the output-overload flag."""
|
|
86
|
+
return bool(self.capabilities.output_overload)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def supports_invert_output(self) -> bool:
|
|
90
|
+
"""Whether the amplifier exposes output-polarity inversion."""
|
|
91
|
+
return bool(self.capabilities.invert_output)
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def can_set_gain(self) -> bool:
|
|
95
|
+
"""Whether the voltage gain can be written."""
|
|
96
|
+
return self.capabilities.gain.gain.access.write
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def can_set_filter(self) -> bool:
|
|
100
|
+
"""Whether a filter can be switched or its cutoff written."""
|
|
101
|
+
return (
|
|
102
|
+
self.capabilities.filters.enabled.write
|
|
103
|
+
or self.capabilities.filters.cutoff.write
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def can_set_invert_output(self) -> bool:
|
|
108
|
+
"""Whether output-polarity inversion can be written."""
|
|
109
|
+
return self.capabilities.invert_output.write
|
|
110
|
+
|
|
111
|
+
async def connect(self) -> None:
|
|
112
|
+
"""Open the transport to the amplifier."""
|
|
113
|
+
raise NotImplementedError(f"{type(self).__name__} must implement connect()")
|
|
114
|
+
|
|
115
|
+
async def close(self) -> None:
|
|
116
|
+
"""Close the transport to the amplifier."""
|
|
117
|
+
raise NotImplementedError(f"{type(self).__name__} must implement close()")
|
|
118
|
+
|
|
119
|
+
async def read_data(self) -> AmplifierState:
|
|
120
|
+
"""
|
|
121
|
+
Read a snapshot of the amplifier's current values.
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
AmplifierState: The current snapshot. A field is ``None`` when the
|
|
125
|
+
driver cannot report it.
|
|
126
|
+
"""
|
|
127
|
+
raise NotImplementedError(f"{type(self).__name__} must implement read_data()")
|
|
128
|
+
|
|
129
|
+
async def get_gain(self) -> float:
|
|
130
|
+
"""
|
|
131
|
+
Read the voltage gain.
|
|
132
|
+
|
|
133
|
+
Returns:
|
|
134
|
+
float: The dimensionless gain in V/V.
|
|
135
|
+
"""
|
|
136
|
+
raise NotImplementedError(
|
|
137
|
+
f"{type(self).__name__} does not implement get_gain(); capability: gain"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
async def set_gain(self, gain: float) -> None:
|
|
141
|
+
"""
|
|
142
|
+
Set the voltage gain.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
gain: The dimensionless gain in V/V, normally one of
|
|
146
|
+
``capabilities.gain.gain.options`` or inside its bounds.
|
|
147
|
+
"""
|
|
148
|
+
raise NotImplementedError(
|
|
149
|
+
f"{type(self).__name__} does not implement set_gain(); capability: gain"
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
async def get_filter(self, filter_type: FilterType) -> FilterSetting:
|
|
153
|
+
"""
|
|
154
|
+
Read one filter's settings.
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
filter_type: The filter response to read.
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
FilterSetting: The filter entry, with its cutoff corners in Hz.
|
|
161
|
+
"""
|
|
162
|
+
raise NotImplementedError(
|
|
163
|
+
f"{type(self).__name__} does not implement get_filter(); "
|
|
164
|
+
"capability: filters"
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
async def set_filter(self, setting: FilterSetting) -> None:
|
|
168
|
+
"""
|
|
169
|
+
Write one filter's settings.
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
setting: The filter entry to apply. ``setting.type`` selects the
|
|
173
|
+
filter, ``enabled`` switches it and the cutoff corners set its
|
|
174
|
+
frequency.
|
|
175
|
+
"""
|
|
176
|
+
raise NotImplementedError(
|
|
177
|
+
f"{type(self).__name__} does not implement set_filter(); "
|
|
178
|
+
"capability: filters"
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
async def get_inverted(self) -> bool:
|
|
182
|
+
"""
|
|
183
|
+
Read the output-polarity inversion state.
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
bool: Whether the output is inverted.
|
|
187
|
+
"""
|
|
188
|
+
raise NotImplementedError(
|
|
189
|
+
f"{type(self).__name__} does not implement get_inverted(); "
|
|
190
|
+
"capability: invert_output"
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
async def set_inverted(self, inverted: bool) -> None:
|
|
194
|
+
"""
|
|
195
|
+
Set the output-polarity inversion state.
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
inverted: Whether to invert the output.
|
|
199
|
+
"""
|
|
200
|
+
raise NotImplementedError(
|
|
201
|
+
f"{type(self).__name__} does not implement set_inverted(); "
|
|
202
|
+
"capability: invert_output"
|
|
203
|
+
)
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"""Vendor-neutral home for the voltage amplifier driver.
|
|
2
|
+
|
|
3
|
+
Temporary: the device behind the serial Modbus interface is not identified yet,
|
|
4
|
+
so the driver cannot be filed under ``<make>/<model>/``. Once it is identified,
|
|
5
|
+
this package renames to that path and the driver moves with it.
|
|
6
|
+
"""
|