scietex.hal.voltage-amplifier 0.1.0__tar.gz
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-0.1.0/LICENSE +21 -0
- scietex_hal_voltage_amplifier-0.1.0/PKG-INFO +318 -0
- scietex_hal_voltage_amplifier-0.1.0/README.md +262 -0
- scietex_hal_voltage_amplifier-0.1.0/pyproject.toml +68 -0
- scietex_hal_voltage_amplifier-0.1.0/setup.cfg +4 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/__init__.py +10 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/base/__init__.py +56 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/base/capabilities.py +322 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/base/data.py +68 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/base/protocol.py +203 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/generic/__init__.py +6 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/generic/modbus/__init__.py +37 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/generic/modbus/client.py +384 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/generic/modbus/errors.py +49 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/generic/modbus/registers.py +94 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/py.typed +0 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/registry.py +66 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex/hal/voltage_amplifier/version.py +1 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex.hal.voltage_amplifier.egg-info/PKG-INFO +318 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex.hal.voltage_amplifier.egg-info/SOURCES.txt +22 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex.hal.voltage_amplifier.egg-info/dependency_links.txt +1 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex.hal.voltage_amplifier.egg-info/requires.txt +14 -0
- scietex_hal_voltage_amplifier-0.1.0/src/scietex.hal.voltage_amplifier.egg-info/top_level.txt +1 -0
- scietex_hal_voltage_amplifier-0.1.0/tests/test_registry.py +41 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Anton Bondarenko
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scietex.hal.voltage_amplifier
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Scietex voltage amplifier HAL package
|
|
5
|
+
Author-email: Anton Bondarenko <bond.anton@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Anton Bondarenko
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/bond-anton/scietex.hal.voltage_amplifier
|
|
29
|
+
Project-URL: Repository, https://github.com/bond-anton/scietex.hal.voltage_amplifier
|
|
30
|
+
Project-URL: Bug Tracker, https://github.com/bond-anton/scietex.hal.voltage_amplifier/issues
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Classifier: Programming Language :: Python :: 3
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
40
|
+
Classifier: Typing :: Typed
|
|
41
|
+
Requires-Python: >=3.10
|
|
42
|
+
Description-Content-Type: text/markdown
|
|
43
|
+
License-File: LICENSE
|
|
44
|
+
Requires-Dist: msgspec~=0.20.0
|
|
45
|
+
Requires-Dist: scietex.hal.serial<3.0.0,>=2.5.0
|
|
46
|
+
Requires-Dist: pymodbus<3.16,>=3.15
|
|
47
|
+
Provides-Extra: dev
|
|
48
|
+
Requires-Dist: tox>=4.46.0; extra == "dev"
|
|
49
|
+
Provides-Extra: test
|
|
50
|
+
Requires-Dist: pytest; extra == "test"
|
|
51
|
+
Requires-Dist: pytest-asyncio; extra == "test"
|
|
52
|
+
Provides-Extra: lint
|
|
53
|
+
Requires-Dist: ruff; extra == "lint"
|
|
54
|
+
Requires-Dist: ty; extra == "lint"
|
|
55
|
+
Dynamic: license-file
|
|
56
|
+
|
|
57
|
+
# scietex.hal.voltage_amplifier
|
|
58
|
+
|
|
59
|
+
A Python library for voltage amplifiers: devices that take an input voltage,
|
|
60
|
+
scale it by a selectable gain, and optionally filter the signal and invert the
|
|
61
|
+
output polarity.
|
|
62
|
+
|
|
63
|
+
The package is split in two layers. The capability model and the driver contract
|
|
64
|
+
in `scietex.hal.voltage_amplifier.base` are complete and usable now: they
|
|
65
|
+
describe what an amplifier can do, what it currently reads, and the asynchronous
|
|
66
|
+
methods a driver implements. The vendor-neutral Modbus driver in
|
|
67
|
+
`scietex.hal.voltage_amplifier.generic.modbus` is a skeleton: its structure, error
|
|
68
|
+
handling and capability mapping are in place, but the device's register map has
|
|
69
|
+
not been transcribed, so every register access raises `RegisterMapIncomplete`
|
|
70
|
+
before any bus traffic starts. The driver is therefore not advertised through the
|
|
71
|
+
`scietex.hal.voltage_amplifier.drivers` entry-point group either.
|
|
72
|
+
|
|
73
|
+
## Status
|
|
74
|
+
|
|
75
|
+
| Layer | State |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| Capability model (`base.capabilities`) | Complete |
|
|
78
|
+
| State snapshot (`base.data`) | Complete |
|
|
79
|
+
| Driver contract (`base.protocol`) | Complete |
|
|
80
|
+
| Driver registry (`registry`) | Complete, no driver advertised yet |
|
|
81
|
+
| Generic Modbus driver (`generic.modbus`) | Skeleton: register map missing |
|
|
82
|
+
|
|
83
|
+
## Install
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
pip install scietex.hal.voltage_amplifier
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The package is not on PyPI yet; install from a clone in the meantime:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
git clone https://github.com/bond-anton/scietex.hal.voltage_amplifier.git
|
|
93
|
+
cd scietex.hal.voltage_amplifier
|
|
94
|
+
pip install .
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Requirements:
|
|
98
|
+
|
|
99
|
+
- Python 3.10 or higher.
|
|
100
|
+
- `msgspec` 0.20 for the state snapshot.
|
|
101
|
+
- `scietex.hal.serial` 2.5 or newer for the asynchronous RS485 transport.
|
|
102
|
+
- `pymodbus` 3.15 for the Modbus request types.
|
|
103
|
+
|
|
104
|
+
## Usage
|
|
105
|
+
|
|
106
|
+
Every driver satisfies `VoltageAmplifierProtocol`: `connect`, `close` and
|
|
107
|
+
`read_data` are mandatory coroutines, and every optional control method is a
|
|
108
|
+
coroutine too. The `supports_*` and `can_*` properties are synchronous and read
|
|
109
|
+
straight from `capabilities`, so a caller rejects an unsupported command before
|
|
110
|
+
dispatching it.
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
import asyncio
|
|
114
|
+
|
|
115
|
+
from scietex.hal.voltage_amplifier.base import VoltageAmplifierProtocol
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def main(amp: VoltageAmplifierProtocol) -> None:
|
|
119
|
+
"""Read one snapshot and re-apply a gain the device accepts."""
|
|
120
|
+
await amp.connect()
|
|
121
|
+
try:
|
|
122
|
+
state = await amp.read_data()
|
|
123
|
+
print(state.input_voltage_v, state.output_voltage_v, state.output_current_a)
|
|
124
|
+
await apply_gain(amp, state.gain)
|
|
125
|
+
finally:
|
|
126
|
+
await amp.close()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def apply_gain(amp: VoltageAmplifierProtocol, gain: float | None) -> None:
|
|
130
|
+
if gain is None:
|
|
131
|
+
return
|
|
132
|
+
if amp.can_set_gain and amp.capabilities.gain.accepts(gain) is True:
|
|
133
|
+
await amp.set_gain(gain)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
asyncio.run(main(amplifier))
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`amplifier` is a driver instance. The repository ships no working hardware
|
|
140
|
+
driver yet, so the object is supplied by the application, by a subclass that
|
|
141
|
+
implements the optional methods, or by a test double.
|
|
142
|
+
|
|
143
|
+
The filter and polarity controls follow the same two-step guard: ask the
|
|
144
|
+
capability property, then call the coroutine.
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from scietex.hal.voltage_amplifier.base import FilterSetting, FilterType
|
|
148
|
+
|
|
149
|
+
if amp.can_set_filter:
|
|
150
|
+
await amp.set_filter(FilterSetting(type=FilterType.LOW_PASS, enabled=True))
|
|
151
|
+
|
|
152
|
+
if amp.can_set_invert_output:
|
|
153
|
+
await amp.set_inverted(True)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Where an operation is advertised, the capability set answers whether it is
|
|
157
|
+
writable; where a value is available, the snapshot carries it. The two are
|
|
158
|
+
separate questions: a feature that exists but is switched off is truthy in
|
|
159
|
+
`capabilities` and `False` in the state.
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
capabilities = amp.capabilities
|
|
163
|
+
limits = capabilities.output_voltage
|
|
164
|
+
print(limits.min_v, limits.max_v) # static swing limits, or None while unknown
|
|
165
|
+
|
|
166
|
+
state = await amp.read_data()
|
|
167
|
+
if state.gain is not None:
|
|
168
|
+
headroom = capabilities.max_input_voltage_v(state.gain)
|
|
169
|
+
swing = capabilities.max_output_swing_v()
|
|
170
|
+
print(headroom, swing)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`max_output_swing_v` returns the smaller of `|min_v|` and `|max_v|`, or `None`
|
|
174
|
+
when either limit is unknown. `max_input_voltage_v` divides that swing by the
|
|
175
|
+
gain and caps the result with the static input range; it raises `ValueError` for
|
|
176
|
+
a zero or negative gain.
|
|
177
|
+
|
|
178
|
+
## Capabilities
|
|
179
|
+
|
|
180
|
+
`VoltageAmplifierCapabilities` groups the control surface by function, not by
|
|
181
|
+
method name, and every group defaults to "unsupported". A driver declares the
|
|
182
|
+
whole set as a class attribute, so it is known before the instance connects.
|
|
183
|
+
|
|
184
|
+
| Group | Capability fields | Snapshot fields | Support query | Write query |
|
|
185
|
+
| --- | --- | --- | --- | --- |
|
|
186
|
+
| Gain | `gain.gain`, `gain.min_gain`, `gain.max_gain`, `gain.step`, `gain.default_gain` | `gain` | `supports_gain` | `can_set_gain` |
|
|
187
|
+
| Input voltage | `input_voltage.min_v`, `input_voltage.max_v` | `input_voltage_v` | static, read directly | not writable |
|
|
188
|
+
| Output voltage | `output_voltage.min_v`, `output_voltage.max_v` | `output_voltage_v` | static, read directly | not writable |
|
|
189
|
+
| Filters | `filters.types`, `filters.enabled`, `filters.cutoff` | `filters` | `supports_filters` | `can_set_filter` |
|
|
190
|
+
| Output overload | `output_overload.read` | `output_overload` | `supports_output_overload` | not writable |
|
|
191
|
+
| Invert output | `invert_output` | `inverted` | `supports_invert_output` | `can_set_invert_output` |
|
|
192
|
+
|
|
193
|
+
The input range and the output swing are static geometry rather than controllable
|
|
194
|
+
features, so they have no `supports_*` property: `capabilities.input_voltage` and
|
|
195
|
+
`capabilities.output_voltage` are read directly and hold `min_v` and `max_v`,
|
|
196
|
+
while the voltages the device currently reports are `state.input_voltage_v` and
|
|
197
|
+
`state.output_voltage_v`.
|
|
198
|
+
|
|
199
|
+
The output-overload indication is read only through `read_data()`; there is no
|
|
200
|
+
dedicated accessor. `input_voltage_v`, `output_voltage_v` and `output_current_a`
|
|
201
|
+
are always present in the snapshot and are not gated by a capability.
|
|
202
|
+
|
|
203
|
+
Some rules the model enforces:
|
|
204
|
+
|
|
205
|
+
- `SelectableCapabilities.options` is in device-native units and in the device's
|
|
206
|
+
own order. An empty tuple means the device does not enumerate the domain, which
|
|
207
|
+
is typical of a continuous setting such as a gain or a cutoff frequency; it
|
|
208
|
+
does not mean no value is accepted.
|
|
209
|
+
- `Access(read=..., write=...)` separates reading from writing, and
|
|
210
|
+
`bool(access)` is true when either direction is supported.
|
|
211
|
+
- The gain is dimensionless and expressed in V/V: the output voltage divided by
|
|
212
|
+
the input voltage. A device may enumerate its gains in `gain.gain.options`,
|
|
213
|
+
describe them as a bounded range in `gain.min_gain` and `gain.max_gain`, or do
|
|
214
|
+
neither, in which case the domain is unknown rather than empty. `gain.step`
|
|
215
|
+
documents the smallest increment the device resolves; `accepts` never rounds.
|
|
216
|
+
- `gain.default_gain` is the gain the device applies at power-on or reset, or
|
|
217
|
+
`None` when the driver does not know it. It is never assumed to be unity.
|
|
218
|
+
- `FilterType` covers `LOW_PASS`, `HIGH_PASS` and `BAND_PASS`; a filter entry is
|
|
219
|
+
a `FilterSetting` with `enabled` and the cutoff corners in Hz that apply.
|
|
220
|
+
|
|
221
|
+
## Errors
|
|
222
|
+
|
|
223
|
+
- `NotImplementedError` from an optional method means the driver does not
|
|
224
|
+
implement that capability. Consult `capabilities` and take another path; do not
|
|
225
|
+
treat it as a transport failure.
|
|
226
|
+
- A `None` field in `AmplifierState` means the driver cannot report that value.
|
|
227
|
+
It is not `0.0` and not `False`, so a caller must not substitute either. An
|
|
228
|
+
unreported gain is unknown, not unity; an unreadable overload flag is unknown,
|
|
229
|
+
not clear.
|
|
230
|
+
- `RegisterMapIncomplete` (a `RuntimeError`) means the register an operation needs
|
|
231
|
+
has no address or function code in `DEVICE_MAP`. The message names the register
|
|
232
|
+
and the file to fill in.
|
|
233
|
+
- `UnexpectedResponse` (an `OSError`) means a response cannot belong to the
|
|
234
|
+
register it answers, such as a wrong register count.
|
|
235
|
+
- `ValueError` comes from `max_input_voltage_v` for a non-positive gain, and from
|
|
236
|
+
`resolve_driver` for an unknown driver name; the message lists the available
|
|
237
|
+
names.
|
|
238
|
+
|
|
239
|
+
## The Modbus driver
|
|
240
|
+
|
|
241
|
+
`GenericModbusVoltageAmplifier` is a `scietex.hal.serial.RS485Client` subclass, so
|
|
242
|
+
the shared client opens the port once per operation and `connect()` has nothing to
|
|
243
|
+
open. Its capability set is empty and `DEVICE_MAP`, `STATUS_BITS` and
|
|
244
|
+
`FILTER_TYPE_CODES` in `scietex/hal/voltage_amplifier/generic/modbus/registers.py`
|
|
245
|
+
carry no device data yet.
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
from scietex.hal.voltage_amplifier.generic.modbus import (
|
|
249
|
+
GenericModbusVoltageAmplifier,
|
|
250
|
+
RegisterMapIncomplete,
|
|
251
|
+
)
|
|
252
|
+
from scietex.hal.serial import ModbusSerialConnectionConfig
|
|
253
|
+
|
|
254
|
+
serial_config = ModbusSerialConnectionConfig(port="/dev/ttyUSB0", framer="RTU")
|
|
255
|
+
amp = GenericModbusVoltageAmplifier(serial_config, address=1)
|
|
256
|
+
|
|
257
|
+
try:
|
|
258
|
+
await amp.read_data()
|
|
259
|
+
except RegisterMapIncomplete as error:
|
|
260
|
+
print(error)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`read_data()` resolves its three mandatory registers before the first read, so an
|
|
264
|
+
unmapped device fails without partial traffic. Transcribing the register map —
|
|
265
|
+
and filling in the capability set that the map implies — is the change that makes
|
|
266
|
+
the driver operate.
|
|
267
|
+
|
|
268
|
+
### Registers
|
|
269
|
+
|
|
270
|
+
`Register` names the ten registers the driver needs, in capability order. No
|
|
271
|
+
address or function code is assigned yet: every entry of `DEVICE_MAP` is the
|
|
272
|
+
neutral default, so every access raises `RegisterMapIncomplete` until the manual
|
|
273
|
+
is transcribed.
|
|
274
|
+
|
|
275
|
+
| Member | Purpose |
|
|
276
|
+
| --- | --- |
|
|
277
|
+
| `INPUT_VOLTAGE` | Input voltage reading |
|
|
278
|
+
| `OUTPUT_VOLTAGE` | Output voltage reading |
|
|
279
|
+
| `OUTPUT_CURRENT` | Output current reading |
|
|
280
|
+
| `GAIN` | Voltage gain setting and reading |
|
|
281
|
+
| `FILTER_ENABLE` | Filter on/off setting |
|
|
282
|
+
| `FILTER_TYPE` | Filter response selection |
|
|
283
|
+
| `FILTER_LOW_CUTOFF` | Lower cutoff frequency |
|
|
284
|
+
| `FILTER_HIGH_CUTOFF` | Upper cutoff frequency |
|
|
285
|
+
| `STATUS` | Status word carrying the overload flag |
|
|
286
|
+
| `INVERT` | Output-polarity inversion setting |
|
|
287
|
+
|
|
288
|
+
`STATUS_BITS` and `FILTER_TYPE_CODES` are empty as well: the driver reports the
|
|
289
|
+
overload as `None` and an unrecognised filter code as an unknown entry rather than
|
|
290
|
+
guessing a bit mask or a code.
|
|
291
|
+
|
|
292
|
+
Because the driver has no entry point, `registry.discover_drivers()` returns an
|
|
293
|
+
empty mapping and `registry.resolve_driver("...")` raises `ValueError`. Until the
|
|
294
|
+
device is identified the driver is reachable by direct import only.
|
|
295
|
+
|
|
296
|
+
## Testing
|
|
297
|
+
|
|
298
|
+
```bash
|
|
299
|
+
pytest tests -q
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
The suite drives in-memory amplifiers and asserts that the Modbus skeleton keeps
|
|
303
|
+
the port closed; it needs no hardware and never opens a serial port.
|
|
304
|
+
|
|
305
|
+
Other checks:
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
tox -e format,lint,type
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
`format` and `lint` run `ruff`, `type` runs `ty` over `src`. Continuous
|
|
312
|
+
integration runs the same checks plus a distribution build; its test matrix
|
|
313
|
+
covers Python 3.10, 3.12 and 3.14.
|
|
314
|
+
|
|
315
|
+
## License
|
|
316
|
+
|
|
317
|
+
This project is licensed under the **MIT License** - see the LICENSE file for
|
|
318
|
+
details.
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# scietex.hal.voltage_amplifier
|
|
2
|
+
|
|
3
|
+
A Python library for voltage amplifiers: devices that take an input voltage,
|
|
4
|
+
scale it by a selectable gain, and optionally filter the signal and invert the
|
|
5
|
+
output polarity.
|
|
6
|
+
|
|
7
|
+
The package is split in two layers. The capability model and the driver contract
|
|
8
|
+
in `scietex.hal.voltage_amplifier.base` are complete and usable now: they
|
|
9
|
+
describe what an amplifier can do, what it currently reads, and the asynchronous
|
|
10
|
+
methods a driver implements. The vendor-neutral Modbus driver in
|
|
11
|
+
`scietex.hal.voltage_amplifier.generic.modbus` is a skeleton: its structure, error
|
|
12
|
+
handling and capability mapping are in place, but the device's register map has
|
|
13
|
+
not been transcribed, so every register access raises `RegisterMapIncomplete`
|
|
14
|
+
before any bus traffic starts. The driver is therefore not advertised through the
|
|
15
|
+
`scietex.hal.voltage_amplifier.drivers` entry-point group either.
|
|
16
|
+
|
|
17
|
+
## Status
|
|
18
|
+
|
|
19
|
+
| Layer | State |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| Capability model (`base.capabilities`) | Complete |
|
|
22
|
+
| State snapshot (`base.data`) | Complete |
|
|
23
|
+
| Driver contract (`base.protocol`) | Complete |
|
|
24
|
+
| Driver registry (`registry`) | Complete, no driver advertised yet |
|
|
25
|
+
| Generic Modbus driver (`generic.modbus`) | Skeleton: register map missing |
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install scietex.hal.voltage_amplifier
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The package is not on PyPI yet; install from a clone in the meantime:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
git clone https://github.com/bond-anton/scietex.hal.voltage_amplifier.git
|
|
37
|
+
cd scietex.hal.voltage_amplifier
|
|
38
|
+
pip install .
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Requirements:
|
|
42
|
+
|
|
43
|
+
- Python 3.10 or higher.
|
|
44
|
+
- `msgspec` 0.20 for the state snapshot.
|
|
45
|
+
- `scietex.hal.serial` 2.5 or newer for the asynchronous RS485 transport.
|
|
46
|
+
- `pymodbus` 3.15 for the Modbus request types.
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
Every driver satisfies `VoltageAmplifierProtocol`: `connect`, `close` and
|
|
51
|
+
`read_data` are mandatory coroutines, and every optional control method is a
|
|
52
|
+
coroutine too. The `supports_*` and `can_*` properties are synchronous and read
|
|
53
|
+
straight from `capabilities`, so a caller rejects an unsupported command before
|
|
54
|
+
dispatching it.
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import asyncio
|
|
58
|
+
|
|
59
|
+
from scietex.hal.voltage_amplifier.base import VoltageAmplifierProtocol
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def main(amp: VoltageAmplifierProtocol) -> None:
|
|
63
|
+
"""Read one snapshot and re-apply a gain the device accepts."""
|
|
64
|
+
await amp.connect()
|
|
65
|
+
try:
|
|
66
|
+
state = await amp.read_data()
|
|
67
|
+
print(state.input_voltage_v, state.output_voltage_v, state.output_current_a)
|
|
68
|
+
await apply_gain(amp, state.gain)
|
|
69
|
+
finally:
|
|
70
|
+
await amp.close()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
async def apply_gain(amp: VoltageAmplifierProtocol, gain: float | None) -> None:
|
|
74
|
+
if gain is None:
|
|
75
|
+
return
|
|
76
|
+
if amp.can_set_gain and amp.capabilities.gain.accepts(gain) is True:
|
|
77
|
+
await amp.set_gain(gain)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
asyncio.run(main(amplifier))
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`amplifier` is a driver instance. The repository ships no working hardware
|
|
84
|
+
driver yet, so the object is supplied by the application, by a subclass that
|
|
85
|
+
implements the optional methods, or by a test double.
|
|
86
|
+
|
|
87
|
+
The filter and polarity controls follow the same two-step guard: ask the
|
|
88
|
+
capability property, then call the coroutine.
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from scietex.hal.voltage_amplifier.base import FilterSetting, FilterType
|
|
92
|
+
|
|
93
|
+
if amp.can_set_filter:
|
|
94
|
+
await amp.set_filter(FilterSetting(type=FilterType.LOW_PASS, enabled=True))
|
|
95
|
+
|
|
96
|
+
if amp.can_set_invert_output:
|
|
97
|
+
await amp.set_inverted(True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Where an operation is advertised, the capability set answers whether it is
|
|
101
|
+
writable; where a value is available, the snapshot carries it. The two are
|
|
102
|
+
separate questions: a feature that exists but is switched off is truthy in
|
|
103
|
+
`capabilities` and `False` in the state.
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
capabilities = amp.capabilities
|
|
107
|
+
limits = capabilities.output_voltage
|
|
108
|
+
print(limits.min_v, limits.max_v) # static swing limits, or None while unknown
|
|
109
|
+
|
|
110
|
+
state = await amp.read_data()
|
|
111
|
+
if state.gain is not None:
|
|
112
|
+
headroom = capabilities.max_input_voltage_v(state.gain)
|
|
113
|
+
swing = capabilities.max_output_swing_v()
|
|
114
|
+
print(headroom, swing)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`max_output_swing_v` returns the smaller of `|min_v|` and `|max_v|`, or `None`
|
|
118
|
+
when either limit is unknown. `max_input_voltage_v` divides that swing by the
|
|
119
|
+
gain and caps the result with the static input range; it raises `ValueError` for
|
|
120
|
+
a zero or negative gain.
|
|
121
|
+
|
|
122
|
+
## Capabilities
|
|
123
|
+
|
|
124
|
+
`VoltageAmplifierCapabilities` groups the control surface by function, not by
|
|
125
|
+
method name, and every group defaults to "unsupported". A driver declares the
|
|
126
|
+
whole set as a class attribute, so it is known before the instance connects.
|
|
127
|
+
|
|
128
|
+
| Group | Capability fields | Snapshot fields | Support query | Write query |
|
|
129
|
+
| --- | --- | --- | --- | --- |
|
|
130
|
+
| Gain | `gain.gain`, `gain.min_gain`, `gain.max_gain`, `gain.step`, `gain.default_gain` | `gain` | `supports_gain` | `can_set_gain` |
|
|
131
|
+
| Input voltage | `input_voltage.min_v`, `input_voltage.max_v` | `input_voltage_v` | static, read directly | not writable |
|
|
132
|
+
| Output voltage | `output_voltage.min_v`, `output_voltage.max_v` | `output_voltage_v` | static, read directly | not writable |
|
|
133
|
+
| Filters | `filters.types`, `filters.enabled`, `filters.cutoff` | `filters` | `supports_filters` | `can_set_filter` |
|
|
134
|
+
| Output overload | `output_overload.read` | `output_overload` | `supports_output_overload` | not writable |
|
|
135
|
+
| Invert output | `invert_output` | `inverted` | `supports_invert_output` | `can_set_invert_output` |
|
|
136
|
+
|
|
137
|
+
The input range and the output swing are static geometry rather than controllable
|
|
138
|
+
features, so they have no `supports_*` property: `capabilities.input_voltage` and
|
|
139
|
+
`capabilities.output_voltage` are read directly and hold `min_v` and `max_v`,
|
|
140
|
+
while the voltages the device currently reports are `state.input_voltage_v` and
|
|
141
|
+
`state.output_voltage_v`.
|
|
142
|
+
|
|
143
|
+
The output-overload indication is read only through `read_data()`; there is no
|
|
144
|
+
dedicated accessor. `input_voltage_v`, `output_voltage_v` and `output_current_a`
|
|
145
|
+
are always present in the snapshot and are not gated by a capability.
|
|
146
|
+
|
|
147
|
+
Some rules the model enforces:
|
|
148
|
+
|
|
149
|
+
- `SelectableCapabilities.options` is in device-native units and in the device's
|
|
150
|
+
own order. An empty tuple means the device does not enumerate the domain, which
|
|
151
|
+
is typical of a continuous setting such as a gain or a cutoff frequency; it
|
|
152
|
+
does not mean no value is accepted.
|
|
153
|
+
- `Access(read=..., write=...)` separates reading from writing, and
|
|
154
|
+
`bool(access)` is true when either direction is supported.
|
|
155
|
+
- The gain is dimensionless and expressed in V/V: the output voltage divided by
|
|
156
|
+
the input voltage. A device may enumerate its gains in `gain.gain.options`,
|
|
157
|
+
describe them as a bounded range in `gain.min_gain` and `gain.max_gain`, or do
|
|
158
|
+
neither, in which case the domain is unknown rather than empty. `gain.step`
|
|
159
|
+
documents the smallest increment the device resolves; `accepts` never rounds.
|
|
160
|
+
- `gain.default_gain` is the gain the device applies at power-on or reset, or
|
|
161
|
+
`None` when the driver does not know it. It is never assumed to be unity.
|
|
162
|
+
- `FilterType` covers `LOW_PASS`, `HIGH_PASS` and `BAND_PASS`; a filter entry is
|
|
163
|
+
a `FilterSetting` with `enabled` and the cutoff corners in Hz that apply.
|
|
164
|
+
|
|
165
|
+
## Errors
|
|
166
|
+
|
|
167
|
+
- `NotImplementedError` from an optional method means the driver does not
|
|
168
|
+
implement that capability. Consult `capabilities` and take another path; do not
|
|
169
|
+
treat it as a transport failure.
|
|
170
|
+
- A `None` field in `AmplifierState` means the driver cannot report that value.
|
|
171
|
+
It is not `0.0` and not `False`, so a caller must not substitute either. An
|
|
172
|
+
unreported gain is unknown, not unity; an unreadable overload flag is unknown,
|
|
173
|
+
not clear.
|
|
174
|
+
- `RegisterMapIncomplete` (a `RuntimeError`) means the register an operation needs
|
|
175
|
+
has no address or function code in `DEVICE_MAP`. The message names the register
|
|
176
|
+
and the file to fill in.
|
|
177
|
+
- `UnexpectedResponse` (an `OSError`) means a response cannot belong to the
|
|
178
|
+
register it answers, such as a wrong register count.
|
|
179
|
+
- `ValueError` comes from `max_input_voltage_v` for a non-positive gain, and from
|
|
180
|
+
`resolve_driver` for an unknown driver name; the message lists the available
|
|
181
|
+
names.
|
|
182
|
+
|
|
183
|
+
## The Modbus driver
|
|
184
|
+
|
|
185
|
+
`GenericModbusVoltageAmplifier` is a `scietex.hal.serial.RS485Client` subclass, so
|
|
186
|
+
the shared client opens the port once per operation and `connect()` has nothing to
|
|
187
|
+
open. Its capability set is empty and `DEVICE_MAP`, `STATUS_BITS` and
|
|
188
|
+
`FILTER_TYPE_CODES` in `scietex/hal/voltage_amplifier/generic/modbus/registers.py`
|
|
189
|
+
carry no device data yet.
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from scietex.hal.voltage_amplifier.generic.modbus import (
|
|
193
|
+
GenericModbusVoltageAmplifier,
|
|
194
|
+
RegisterMapIncomplete,
|
|
195
|
+
)
|
|
196
|
+
from scietex.hal.serial import ModbusSerialConnectionConfig
|
|
197
|
+
|
|
198
|
+
serial_config = ModbusSerialConnectionConfig(port="/dev/ttyUSB0", framer="RTU")
|
|
199
|
+
amp = GenericModbusVoltageAmplifier(serial_config, address=1)
|
|
200
|
+
|
|
201
|
+
try:
|
|
202
|
+
await amp.read_data()
|
|
203
|
+
except RegisterMapIncomplete as error:
|
|
204
|
+
print(error)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
`read_data()` resolves its three mandatory registers before the first read, so an
|
|
208
|
+
unmapped device fails without partial traffic. Transcribing the register map —
|
|
209
|
+
and filling in the capability set that the map implies — is the change that makes
|
|
210
|
+
the driver operate.
|
|
211
|
+
|
|
212
|
+
### Registers
|
|
213
|
+
|
|
214
|
+
`Register` names the ten registers the driver needs, in capability order. No
|
|
215
|
+
address or function code is assigned yet: every entry of `DEVICE_MAP` is the
|
|
216
|
+
neutral default, so every access raises `RegisterMapIncomplete` until the manual
|
|
217
|
+
is transcribed.
|
|
218
|
+
|
|
219
|
+
| Member | Purpose |
|
|
220
|
+
| --- | --- |
|
|
221
|
+
| `INPUT_VOLTAGE` | Input voltage reading |
|
|
222
|
+
| `OUTPUT_VOLTAGE` | Output voltage reading |
|
|
223
|
+
| `OUTPUT_CURRENT` | Output current reading |
|
|
224
|
+
| `GAIN` | Voltage gain setting and reading |
|
|
225
|
+
| `FILTER_ENABLE` | Filter on/off setting |
|
|
226
|
+
| `FILTER_TYPE` | Filter response selection |
|
|
227
|
+
| `FILTER_LOW_CUTOFF` | Lower cutoff frequency |
|
|
228
|
+
| `FILTER_HIGH_CUTOFF` | Upper cutoff frequency |
|
|
229
|
+
| `STATUS` | Status word carrying the overload flag |
|
|
230
|
+
| `INVERT` | Output-polarity inversion setting |
|
|
231
|
+
|
|
232
|
+
`STATUS_BITS` and `FILTER_TYPE_CODES` are empty as well: the driver reports the
|
|
233
|
+
overload as `None` and an unrecognised filter code as an unknown entry rather than
|
|
234
|
+
guessing a bit mask or a code.
|
|
235
|
+
|
|
236
|
+
Because the driver has no entry point, `registry.discover_drivers()` returns an
|
|
237
|
+
empty mapping and `registry.resolve_driver("...")` raises `ValueError`. Until the
|
|
238
|
+
device is identified the driver is reachable by direct import only.
|
|
239
|
+
|
|
240
|
+
## Testing
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
pytest tests -q
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
The suite drives in-memory amplifiers and asserts that the Modbus skeleton keeps
|
|
247
|
+
the port closed; it needs no hardware and never opens a serial port.
|
|
248
|
+
|
|
249
|
+
Other checks:
|
|
250
|
+
|
|
251
|
+
```bash
|
|
252
|
+
tox -e format,lint,type
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
`format` and `lint` run `ruff`, `type` runs `ty` over `src`. Continuous
|
|
256
|
+
integration runs the same checks plus a distribution build; its test matrix
|
|
257
|
+
covers Python 3.10, 3.12 and 3.14.
|
|
258
|
+
|
|
259
|
+
## License
|
|
260
|
+
|
|
261
|
+
This project is licensed under the **MIT License** - see the LICENSE file for
|
|
262
|
+
details.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "scietex.hal.voltage_amplifier"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Scietex voltage amplifier HAL package"
|
|
9
|
+
authors = [{ name = "Anton Bondarenko", email = "bond.anton@gmail.com" }]
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.10",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Typing :: Typed",
|
|
23
|
+
]
|
|
24
|
+
requires-python = ">=3.10"
|
|
25
|
+
dependencies = [
|
|
26
|
+
"msgspec ~= 0.20.0",
|
|
27
|
+
"scietex.hal.serial >= 2.5.0, < 3.0.0",
|
|
28
|
+
"pymodbus >=3.15,<3.16",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
"Homepage" = "https://github.com/bond-anton/scietex.hal.voltage_amplifier"
|
|
33
|
+
"Repository" = "https://github.com/bond-anton/scietex.hal.voltage_amplifier"
|
|
34
|
+
"Bug Tracker" = "https://github.com/bond-anton/scietex.hal.voltage_amplifier/issues"
|
|
35
|
+
|
|
36
|
+
# Registering the driver here would advertise a usable device to
|
|
37
|
+
# `discover_drivers()`, which cannot happen while `DEVICE_MAP` is empty.
|
|
38
|
+
# The driver stays reachable by direct import in the meantime.
|
|
39
|
+
[project.entry-points."scietex.hal.voltage_amplifier.drivers"]
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
dev = ["tox>=4.46.0"]
|
|
43
|
+
test = ["pytest", "pytest-asyncio"]
|
|
44
|
+
lint = ["ruff", "ty"]
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.dynamic]
|
|
47
|
+
version = { attr = "scietex.hal.voltage_amplifier.version.__version__" }
|
|
48
|
+
|
|
49
|
+
[tool.setuptools.package-data]
|
|
50
|
+
"scietex.hal.voltage_amplifier" = ["py.typed"]
|
|
51
|
+
|
|
52
|
+
[tool.setuptools.packages.find]
|
|
53
|
+
where = ["src"]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
pythonpath = ["src"]
|
|
57
|
+
testpaths = ["tests"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
line-length = 88
|
|
61
|
+
target-version = "py310"
|
|
62
|
+
|
|
63
|
+
[tool.ruff.lint]
|
|
64
|
+
select = ["E4", "E7", "E9", "F", "W", "I", "B", "C4", "SIM", "UP", "PLC0415", "PLR0913", "PLR0917"]
|
|
65
|
+
ignore = ["E501"]
|
|
66
|
+
|
|
67
|
+
[tool.ruff.format]
|
|
68
|
+
quote-style = "double"
|
|
@@ -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__"]
|