sn9000 support
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"""Raw acquisition producer for matrix-mode radars (LibreVNA multi-device, SN9000)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import threading
|
||||
import time
|
||||
|
||||
from python_app.hardware_full.matrix_radar_service import create_matrix_radar_service
|
||||
from python_app.models.run_config_model import RunConfigModel
|
||||
from python_app.orchestration.shm import ShmRingWriter
|
||||
from python_app.storage.npz.serialize import RAW_MAGIC, serialize_trace_collection
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Run producer process until config or signal requests exit."""
|
||||
parser = argparse.ArgumentParser(description="Publish matrix-radar raw sweeps to SHM rings")
|
||||
parser.add_argument("--config", required=True, type=Path, help="Path to run_config.json")
|
||||
args = parser.parse_args()
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
||||
stop_requested = threading.Event()
|
||||
|
||||
def request_stop(_signum: int, _frame: object) -> None:
|
||||
stop_requested.set()
|
||||
|
||||
signal.signal(signal.SIGINT, request_stop)
|
||||
signal.signal(signal.SIGTERM, request_stop)
|
||||
|
||||
config = RunConfigModel.load_from_path(args.config)
|
||||
config.apply_device_model_constraints()
|
||||
if not config.is_matrix_radar:
|
||||
raise RuntimeError(
|
||||
"matrix_raw_producer requires a matrix-mode radar.model "
|
||||
"(librevna_multi or sn9000)"
|
||||
)
|
||||
|
||||
raw_writer = ShmRingWriter(
|
||||
config.rings.raw.name,
|
||||
config.rings.raw.capacity,
|
||||
config.rings.raw.slot_size_bytes,
|
||||
)
|
||||
raw_tap_writer = ShmRingWriter(
|
||||
config.rings.raw_tap.name,
|
||||
config.rings.raw_tap.capacity,
|
||||
config.rings.raw_tap.slot_size_bytes,
|
||||
)
|
||||
radar = create_matrix_radar_service(config)
|
||||
|
||||
try:
|
||||
radar.open()
|
||||
radar.configure(config.radar.sweep)
|
||||
collection_id = 1
|
||||
while not stop_requested.is_set():
|
||||
collection_start = time.monotonic()
|
||||
collection = radar.acquire_collection(collection_id=collection_id)
|
||||
|
||||
payload = serialize_trace_collection(collection, RAW_MAGIC)
|
||||
if not raw_writer.push(payload):
|
||||
raise RuntimeError(
|
||||
f"Raw payload size {len(payload)} exceeds ring slot size {raw_writer.slot_size_bytes}"
|
||||
)
|
||||
if not raw_tap_writer.push(payload):
|
||||
raise RuntimeError(
|
||||
f"Raw tap payload size {len(payload)} exceeds ring slot size {raw_tap_writer.slot_size_bytes}"
|
||||
)
|
||||
if not config.runtime.continuous:
|
||||
break
|
||||
collection_duration_s = time.monotonic() - collection_start
|
||||
if collection_id == 1 or collection_id % 20 == 0 or collection_duration_s > 2.0:
|
||||
logger.info(
|
||||
"matrix radar collection %d acquired in %.3f s",
|
||||
collection_id,
|
||||
collection_duration_s,
|
||||
)
|
||||
collection_id += 1
|
||||
finally:
|
||||
radar.close()
|
||||
raw_tap_writer.close()
|
||||
raw_writer.close()
|
||||
|
||||
logger.info("matrix radar raw producer stopped")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Standalone smoke test for the SN9000 multi-port VISA HiSLIP acquisition path."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
|
||||
import numpy as np
|
||||
|
||||
from python_app.hardware_full.sn9000_service import Sn9000Service
|
||||
from python_app.models.dataset_model import SweepCollection
|
||||
from python_app.models.run_config_model import RadarSweepModel, RunConfigModel
|
||||
|
||||
|
||||
def _parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description="Acquire one SN9000 collection through VISA HiSLIP")
|
||||
parser.add_argument(
|
||||
"--host",
|
||||
default="127.0.0.1",
|
||||
help="SNVNA HiSLIP server host (default: 127.0.0.1)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--port",
|
||||
type=int,
|
||||
default=4880,
|
||||
help="SNVNA HiSLIP server TCP port (default: 4880)",
|
||||
)
|
||||
parser.add_argument("--start-hz", type=float, default=1_000_000.0)
|
||||
parser.add_argument("--stop-hz", type=float, default=3_000_000_000.0)
|
||||
parser.add_argument("--points", type=int, default=201)
|
||||
parser.add_argument("--ifbw-hz", type=float, default=10_000.0)
|
||||
parser.add_argument("--power-dbm", type=float, default=-10.0)
|
||||
parser.add_argument("--timeout-ms", type=int, default=20_000)
|
||||
parser.add_argument("--no-preset", action="store_true")
|
||||
parser.add_argument(
|
||||
"--visa-library",
|
||||
default="@ivi",
|
||||
help="PyVISA IVI backend specification, e.g. @ivi or /usr/lib/x86_64-linux-gnu/libvisa.so",
|
||||
)
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def _validate_collection(collection: SweepCollection, expected_points: int) -> None:
|
||||
expected_traces = (
|
||||
RunConfigModel.MULTI_DEVICE_INPUT_POSITIONS * RunConfigModel.MULTI_DEVICE_OUTPUT_POSITIONS
|
||||
)
|
||||
if len(collection.traces) != expected_traces:
|
||||
raise RuntimeError(
|
||||
f"SN9000 collection has {len(collection.traces)} traces, expected {expected_traces}"
|
||||
)
|
||||
|
||||
reference_frequency = collection.traces[0].frequency_hz
|
||||
if reference_frequency.shape != (expected_points,):
|
||||
raise RuntimeError(f"Unexpected frequency shape: {reference_frequency.shape}")
|
||||
if not np.all(np.isfinite(reference_frequency)):
|
||||
raise RuntimeError("Frequency axis contains non-finite values")
|
||||
if np.any(np.diff(reference_frequency) < 0.0):
|
||||
raise RuntimeError("Frequency axis is not monotonic")
|
||||
|
||||
expected_combos = RunConfigModel.build_matrix_radar_virtual_combos()
|
||||
for trace, expected_combo in zip(collection.traces, expected_combos, strict=True):
|
||||
if (
|
||||
int(trace.combo.input) != int(expected_combo.input)
|
||||
or int(trace.combo.output) != int(expected_combo.output)
|
||||
):
|
||||
raise RuntimeError(
|
||||
f"Trace combo mismatch: got ({trace.combo.input},{trace.combo.output}), "
|
||||
f"expected ({expected_combo.input},{expected_combo.output})"
|
||||
)
|
||||
if trace.s11.shape != (expected_points,) or trace.s21.shape != (expected_points,):
|
||||
raise RuntimeError(
|
||||
f"Trace ({trace.combo.input},{trace.combo.output}) has shapes "
|
||||
f"s11={trace.s11.shape}, s21={trace.s21.shape}"
|
||||
)
|
||||
if not np.all(np.isfinite(trace.s11.real)) or not np.all(np.isfinite(trace.s11.imag)):
|
||||
raise RuntimeError(
|
||||
f"Trace ({trace.combo.input},{trace.combo.output}) S11 contains non-finite values"
|
||||
)
|
||||
if not np.all(np.isfinite(trace.s21.real)) or not np.all(np.isfinite(trace.s21.imag)):
|
||||
raise RuntimeError(
|
||||
f"Trace ({trace.combo.input},{trace.combo.output}) S21 contains non-finite values"
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = _parse_args()
|
||||
sweep = RadarSweepModel(
|
||||
start_hz=args.start_hz,
|
||||
stop_hz=args.stop_hz,
|
||||
points=args.points,
|
||||
if_bandwidth_hz=args.ifbw_hz,
|
||||
power_dbm=args.power_dbm,
|
||||
)
|
||||
|
||||
service = Sn9000Service(
|
||||
host=args.host,
|
||||
port=args.port,
|
||||
timeout_ms=args.timeout_ms,
|
||||
preset_on_open=not args.no_preset,
|
||||
visa_library=args.visa_library,
|
||||
)
|
||||
try:
|
||||
service.open()
|
||||
print(f"SN9000 IDN: {service.query_identity()}")
|
||||
service.configure(sweep)
|
||||
collection = service.acquire_collection(collection_id=1)
|
||||
_validate_collection(collection, args.points)
|
||||
system_error = service.query_system_error()
|
||||
if not system_error.startswith("0,"):
|
||||
raise RuntimeError(f"SN9000 SCPI error after sweep: {system_error}")
|
||||
mean_abs_s21 = np.mean([float(np.mean(np.abs(trace.s21))) for trace in collection.traces])
|
||||
print(
|
||||
"SN9000 collection OK: "
|
||||
f"traces={len(collection.traces)}, points={args.points}, "
|
||||
f"first_hz={collection.traces[0].frequency_hz[0]:.3f}, "
|
||||
f"last_hz={collection.traces[0].frequency_hz[-1]:.3f}, "
|
||||
f"mean_abs_s21={mean_abs_s21:.6g}"
|
||||
)
|
||||
finally:
|
||||
service.close()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user