Add RFG UDP source to rph_radar

This commit is contained in:
Ayzen
2026-10-06 19:36:00 +03:00
parent a185e61495
commit cfe81f782b
29 changed files with 817 additions and 279 deletions
@@ -39,7 +39,7 @@ TEMP_MIN_C = 15.0 # Minimum allowed laser temperature, °C
TEMP_MAX_C = 40.0 # Maximum allowed laser temperature, °C
CURRENT_MIN_MA = 15.0 # Minimum allowed laser current, mA
CURRENT_MAX_MA = 60.0 # Maximum allowed laser current, mA
CURRENT_MAX_MA = 110.0 # Maximum allowed laser current, mA (lab sweeps reach 107 mA)
# Variation step limits
CURRENT_STEP_MIN_MA = 0.002 # Minimum current variation step, mA
@@ -8,17 +8,13 @@ from .exceptions import CommunicationError
from .protocol import Protocol
logger = logging.getLogger(__name__)
DEFAULT_PI_P = 2560
DEFAULT_PI_I = 128
DEVICE_MAIN_MESSAGE_ID = 0x00FF
class LaserController:
"""Control constant setpoints or LD1 current variation through one serial port."""
def __init__(self, port: str,
pi_coeff1_p: int = DEFAULT_PI_P, pi_coeff1_i: int = DEFAULT_PI_I,
pi_coeff2_p: int = DEFAULT_PI_P, pi_coeff2_i: int = DEFAULT_PI_I):
def __init__(self, port: str, *, pi_coeff1_p: int, pi_coeff1_i: int, pi_coeff2_p: int, pi_coeff2_i: int):
self._protocol = Protocol(port)
self._pi = dict(pi_coeff1_p=pi_coeff1_p, pi_coeff1_i=pi_coeff1_i,
pi_coeff2_p=pi_coeff2_p, pi_coeff2_i=pi_coeff2_i)
@@ -39,6 +39,18 @@ def current_ma_to_n(current_ma: float) -> int:
Returns:
Integer in [0, 65535] for the DAC.
"""
n = int(ADC_BITS_16 / 2000 * RREF * current_ma)
n = max(0, min(ADC_BITS_16, n))
return n
# Rounded like the lab GUI (_device_main.py), so both send identical DAC codes.
return max(0, min(ADC_BITS_16, round(ADC_BITS_16 / 2000 * RREF * current_ma)))
def variation_codes(min_value: float, max_value: float, step: float) -> tuple[int, int, int]:
low, high, delta = (current_ma_to_n(value) for value in (min_value, max_value, step))
if delta == 0 or low >= high:
raise ValueError("Current variation range and step must be representable by the DAC")
return low, high, delta
def variation_step_count(min_value: float, max_value: float, step: float) -> int:
"""SWEEP_STEP edges per sweep: firmware loops ``while (current < max) current += delta`` on these codes."""
low, high, delta = variation_codes(min_value, max_value, step)
return math.ceil((high - low) / delta)
@@ -11,7 +11,7 @@ class LaserControlError(Exception):
pass
class ValidationError(LaserControlError):
class ValidationError(LaserControlError, ValueError):
"""Base exception for validation errors."""
pass
@@ -9,7 +9,7 @@ from operator import xor
import serial
from .constants import BAUDRATE, SERIAL_TIMEOUT_SEC
from .conversions import current_ma_to_n, temp_c_to_n
from .conversions import current_ma_to_n, temp_c_to_n, variation_codes
from .validators import validate_manual, validate_variation
from .exceptions import CommunicationError, PortBusyError, ProtocolError
@@ -116,9 +116,7 @@ class Protocol:
raise ValueError("radar.laser_control.variation.variation_type must be CHANGE_CURRENT_LD1")
validate_manual(static_temp1, static_temp2, static_current1, static_current2)
validate_variation(min_value, max_value, step, time_step, delay_time)
low, high, delta = (current_ma_to_n(value) for value in (min_value, max_value, step))
if delta == 0 or low >= high:
raise ValueError("Current variation range and step must be representable by the DAC")
low, high, delta = variation_codes(min_value, max_value, step)
return _command(
0x7777, 0x37ff, 1, low, high, delta, int(time_step * 100),
temp_c_to_n(static_temp1), current_ma_to_n(static_current2), temp_c_to_n(static_temp2),