38 lines
1.7 KiB
Python
38 lines
1.7 KiB
Python
"""Apply constant laser settings or LD1 current variation before ADC acquisition."""
|
|
|
|
from dataclasses import asdict
|
|
|
|
from python_app.hardware_full.laser_control.controller import DEVICE_MAIN_MESSAGE_ID, LaserController
|
|
from python_app.hardware_full.laser_control.protocol import Protocol
|
|
from python_app.models.run_config_schema import RunConfigModel
|
|
|
|
|
|
def apply_rph_radar_laser_control(config: RunConfigModel) -> bool:
|
|
"""Validate every command before connecting/resetting; leave a started sweep running."""
|
|
laser = config.radar.laser_control
|
|
if not laser.enabled:
|
|
return False
|
|
if not laser.port:
|
|
raise ValueError("radar.laser_control.port is required when laser_control is enabled")
|
|
mode = laser.mode.strip().lower()
|
|
if mode not in {"manual", "variation"}:
|
|
raise ValueError("radar.laser_control.mode must be 'manual' or 'variation'")
|
|
pi = {name: getattr(laser, name) for name in ("pi_coeff1_p", "pi_coeff1_i", "pi_coeff2_p", "pi_coeff2_i")}
|
|
manual = (asdict(laser.manual) if mode == "manual" else
|
|
{name: getattr(laser.variation, "static_" + name) for name in ("temp1", "temp2", "current1", "current2")})
|
|
variation = asdict(laser.variation)
|
|
Protocol.encode_decode_enable(**manual, **pi, message_id=DEVICE_MAIN_MESSAGE_ID)
|
|
if mode == "variation":
|
|
Protocol.encode_task_enable(**variation, **pi)
|
|
|
|
controller = LaserController(port=laser.port, **pi)
|
|
try:
|
|
controller.connect()
|
|
controller.reset()
|
|
controller.set_manual_mode(**manual, message_id=DEVICE_MAIN_MESSAGE_ID)
|
|
if mode == "variation":
|
|
controller.start_variation(params=variation)
|
|
return True
|
|
finally:
|
|
controller.disconnect()
|