From 431666046f42c3f4610373c000ff31fda8018498 Mon Sep 17 00:00:00 2001 From: Ayzen Date: Wed, 6 May 2026 14:52:31 +0300 Subject: [PATCH] Added ADC measurements --- laser_control/constants.py | 1 + laser_control/conversions.py | 14 +++++++++++++- laser_control/example_usage.py | 2 ++ laser_control/gui/sections.py | 2 ++ laser_control/gui/window.py | 2 ++ laser_control/models.py | 8 ++++---- laser_control/protocol.py | 5 +++-- 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/laser_control/constants.py b/laser_control/constants.py index ba483d2..f316049 100644 --- a/laser_control/constants.py +++ b/laser_control/constants.py @@ -142,6 +142,7 @@ R0 = 10000 ADC_BITS_16 = 65535 ADC_BITS_12 = 4095 +STM32_ADC_PIN_VREF = 3.3 U3V3_COEFF = 1.221e-3 U5V_COEFF = 1.8315e-3 diff --git a/laser_control/conversions.py b/laser_control/conversions.py index a0eced7..8c4fbbf 100644 --- a/laser_control/conversions.py +++ b/laser_control/conversions.py @@ -14,6 +14,7 @@ from .constants import ( RREF, BETA_INTERNAL, BETA_EXTERNAL, T0_K, R0, ADC_BITS_16, ADC_BITS_12, + STM32_ADC_PIN_VREF, U3V3_COEFF, U5V_COEFF, U7V_COEFF, ) @@ -111,4 +112,15 @@ def voltage_5v_n_to_v(n: int) -> float: def voltage_7v_n_to_v(n: int) -> float: """Convert 7V rail ADC count to volts.""" - return n * U7V_COEFF \ No newline at end of file + return n * U7V_COEFF + + +def stm32_adc_pin_n_to_v(n: int) -> float: + """ + Convert a raw 12-bit STM32 ADC code from a direct MCU input pin to volts. + + The new auxiliary telemetry channels are measured directly on PF3/PF4 + without an external divider in the firmware path, so the conversion is a + plain ratio against the MCU analog reference rail. + """ + return n * STM32_ADC_PIN_VREF / ADC_BITS_12 diff --git a/laser_control/example_usage.py b/laser_control/example_usage.py index eb8180e..5620423 100644 --- a/laser_control/example_usage.py +++ b/laser_control/example_usage.py @@ -26,6 +26,8 @@ def example_manual_mode(port: str = None): print(f" Temp2: {data.temp2:.2f} °C") print(f" I1: {data.current1:.3f} mA") print(f" I2: {data.current2:.3f} mA") + print(f" PF3: {data.adc_pf3_voltage:.3f} V") + print(f" PF4: {data.adc_pf4_voltage:.3f} V") print(f" 3.3V: {data.voltage_3v3:.3f} V") print(f" 5V: {data.voltage_5v1:.3f} V") print(f" 7V: {data.voltage_7v0:.3f} V") diff --git a/laser_control/gui/sections.py b/laser_control/gui/sections.py index 6d7e428..180ad75 100644 --- a/laser_control/gui/sections.py +++ b/laser_control/gui/sections.py @@ -568,6 +568,8 @@ def build_status_group(owner) -> QGroupBox: ("Температура 2", "_telemetry_temp2"), ("Фотодиод 1", "_telemetry_current1"), ("Фотодиод 2", "_telemetry_current2"), + ("Напряжение PF3", "_telemetry_adc_pf3"), + ("Напряжение PF4", "_telemetry_adc_pf4"), ("Внешняя температура 1", "_telemetry_temp_ext1"), ("Внешняя температура 2", "_telemetry_temp_ext2"), ("Питание 3.3 В", "_telemetry_3v3"), diff --git a/laser_control/gui/window.py b/laser_control/gui/window.py index 117c193..31aa42d 100644 --- a/laser_control/gui/window.py +++ b/laser_control/gui/window.py @@ -603,6 +603,8 @@ class MainWindow(QMainWindow): self._telemetry_temp2.setText(f"{measurements.temp2:.2f} °C") self._telemetry_current1.setText(f"{measurements.current1:.3f} мА") self._telemetry_current2.setText(f"{measurements.current2:.3f} мА") + self._telemetry_adc_pf3.setText(f"{measurements.adc_pf3_voltage:.3f} В") + self._telemetry_adc_pf4.setText(f"{measurements.adc_pf4_voltage:.3f} В") self._telemetry_temp_ext1.setText(f"{measurements.temp_ext1:.2f} °C") self._telemetry_temp_ext2.setText(f"{measurements.temp_ext2:.2f} °C") self._telemetry_3v3.setText(f"{measurements.voltage_3v3:.3f} В") diff --git a/laser_control/models.py b/laser_control/models.py index 3f3d311..381c3bd 100644 --- a/laser_control/models.py +++ b/laser_control/models.py @@ -35,6 +35,8 @@ class Measurements: current2: float temp1: float temp2: float + adc_pf3_voltage: float = 0.0 + adc_pf4_voltage: float = 0.0 temp_ext1: float | None = None temp_ext2: float | None = None voltage_3v3: float = 0.0 @@ -42,8 +44,6 @@ class Measurements: voltage_5v2: float = 0.0 voltage_7v0: float = 0.0 message_id: int | None = None - to6_counter_lsb: int | None = None - to6_counter_msb: int | None = None timestamp: datetime = field(default_factory=datetime.now) def to_dict(self) -> dict[str, Any]: @@ -51,6 +51,8 @@ class Measurements: return { "current1": self.current1, "current2": self.current2, + "adc_pf3_voltage": self.adc_pf3_voltage, + "adc_pf4_voltage": self.adc_pf4_voltage, "temp1": self.temp1, "temp2": self.temp2, "temp_ext1": self.temp_ext1, @@ -60,8 +62,6 @@ class Measurements: "voltage_5v2": self.voltage_5v2, "voltage_7v0": self.voltage_7v0, "message_id": self.message_id, - "to6_counter_lsb": self.to6_counter_lsb, - "to6_counter_msb": self.to6_counter_msb, "timestamp": self.timestamp.isoformat(), } diff --git a/laser_control/protocol.py b/laser_control/protocol.py index 5ee265f..40e2b8a 100644 --- a/laser_control/protocol.py +++ b/laser_control/protocol.py @@ -56,6 +56,7 @@ from .constants import ( from .conversions import ( current_ma_to_n, current_n_to_ma, + stm32_adc_pin_n_to_v, temp_c_to_n, temp_ext_n_to_c, temp_n_to_c, @@ -456,6 +457,8 @@ class Protocol: return Measurements( current1=current_n_to_ma(words[1]), current2=current_n_to_ma(words[2]), + adc_pf3_voltage=stm32_adc_pin_n_to_v(words[3]), + adc_pf4_voltage=stm32_adc_pin_n_to_v(words[4]), temp1=temp_n_to_c(words[5]), temp2=temp_n_to_c(words[6]), temp_ext1=temp_ext_n_to_c(words[7]), @@ -465,8 +468,6 @@ class Protocol: voltage_5v2=voltage_5v_n_to_v(words[11]), voltage_7v0=voltage_7v_n_to_v(words[12]), message_id=words[13], - to6_counter_lsb=words[3], - to6_counter_msb=words[4], timestamp=datetime.now(), )