From 9d8a553669a1fdeb211b1e9367de040d1ec63976 Mon Sep 17 00:00:00 2001 From: linuswck Date: Fri, 26 Jan 2024 12:10:52 +0800 Subject: [PATCH] pd_mon: add fns to convert between pwr and current --- src/laser_diode/laser_diode.rs | 21 ++++++++++++--------- src/laser_diode/pd_mon.rs | 9 ++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/laser_diode/laser_diode.rs b/src/laser_diode/laser_diode.rs index 40954b6..5c4e7cb 100644 --- a/src/laser_diode/laser_diode.rs +++ b/src/laser_diode/laser_diode.rs @@ -2,7 +2,8 @@ use miniconf::Miniconf; use stm32f4xx_hal::pac::ADC2; use crate::laser_diode::ld_ctrl::LdCtrl; use crate::laser_diode::ld_pwr_exc_protector::{LdPwrExcProtector, self}; -use core::{marker::PhantomData, f64::NAN}; +use crate::laser_diode::pd_mon; +use core::marker::PhantomData; use uom::si::{ electric_current::milliampere, @@ -43,7 +44,8 @@ impl Settings{ pub struct Settings { ld_drive_current: ElectricCurrent, ld_drive_current_limit: ElectricCurrent, - pd_i_to_out_pwr: IToPowerUnit, + #[miniconf(defer)] + pd_responsitivity: pd_mon::Parameters, } impl Default for Settings { @@ -51,7 +53,7 @@ impl Default for Settings { Self { ld_drive_current: ElectricCurrent::new::(0.0), ld_drive_current_limit: ElectricCurrent::new::(0.0), - pd_i_to_out_pwr: IToPowerUnit {dimension: PhantomData, units: PhantomData, value: NAN} + pd_responsitivity: pd_mon::Parameters::default(), } } } @@ -125,16 +127,17 @@ impl LdDrive{ LdPwrExcProtector::clear_alarm_status(); } + pub fn set_pd_responsitivity(&mut self, responsitivity: pd_mon::ResponsitivityUnit){ + self.settings.pd_responsitivity.responsitivity = responsitivity; + } + pub fn set_ld_power_limit(&mut self, pwr_limit: Power){ - // LdPwrExcProtector::set_trigger_threshold_v(convert pwr_limit to raw adc code) - unimplemented!() + LdPwrExcProtector::set_trigger_threshold_v(self.settings.pd_responsitivity + .get_ld_i_from_ld_pwr(pwr_limit) / Settings::PD_MON_TRANSCONDUCTANCE + ); } pub fn set_pd_i_limit(&mut self, i: ElectricCurrent){ LdPwrExcProtector::set_trigger_threshold_v(i / Settings::PD_MON_TRANSCONDUCTANCE); } - - pub fn set_pd_v_limit(&mut self, v: ElectricPotential){ - LdPwrExcProtector::set_trigger_threshold_v(v); - } } diff --git a/src/laser_diode/pd_mon.rs b/src/laser_diode/pd_mon.rs index fe69219..e09cf8d 100644 --- a/src/laser_diode/pd_mon.rs +++ b/src/laser_diode/pd_mon.rs @@ -12,19 +12,22 @@ use miniconf::Miniconf; // Ampere / Watt pub type ResponsitivityUnit = Quantity, SI, f64>; -/// Steinhart-Hart equation Photodiode #[derive(Clone, Debug, PartialEq, Miniconf)] pub struct Parameters { - /// Responsitivity pub responsitivity: ResponsitivityUnit, pub i_dark: ElectricCurrent, } impl Parameters { - pub fn get_ld_output_power(&self, i: ElectricCurrent) -> Power { + pub fn get_ld_pwr_from_ld_i(&self, i: ElectricCurrent) -> Power { let ld_power = (i - self.i_dark) / self.responsitivity; ld_power } + + pub fn get_ld_i_from_ld_pwr(&self, pwr: Power) -> ElectricCurrent { + let ld_i = pwr * self.responsitivity + self.i_dark; + ld_i + } } impl Default for Parameters {