kirdy/src/laser_diode/laser_diode.rs

182 lines
5.7 KiB
Rust

use miniconf::Miniconf;
use stm32f4xx_hal::pac::ADC2;
use uom::si::electric_current::ampere;
use crate::laser_diode::ld_ctrl::LdCtrl;
use crate::laser_diode::ld_pwr_exc_protector::{LdPwrExcProtector, self};
use crate::laser_diode::pd_responsitivity;
use core::marker::PhantomData;
use crate::device::sys_timer::sleep;
use uom::si::{
electric_current::milliampere,
f64::{ElectricPotential, ElectricCurrent, Power},
};
use num_traits::Float;
use uom::{si::{ISQ, SI, Quantity, ratio::ratio}, typenum::*};
// Volt / Ampere
pub type TransimpedanceUnit = Quantity<ISQ<P2, P1, N3, N2, Z0, Z0, Z0>, SI<f64>, f64>;
// Ampere / Volt
type TransconductanceUnit = Quantity<ISQ<N2, N1, P3, P2, Z0, Z0, Z0>, SI<f64>, f64>;
impl Settings{
pub const DAC_OUT_V_MAX: ElectricPotential = ElectricPotential {
dimension: PhantomData,
units: PhantomData,
value: 4.096,
};
// Unit: A/V
const PD_MON_TRANSCONDUCTANCE: TransconductanceUnit = TransconductanceUnit {
dimension: PhantomData,
units: PhantomData,
value: 0.001,
};
const LD_DRIVE_TRANSIMPEDANCE: TransimpedanceUnit = TransimpedanceUnit {
dimension: PhantomData,
units: PhantomData,
value: 10.0 / 0.75,
};
const LD_CURRENT_STEP_SIZE: ElectricCurrent = ElectricCurrent {
dimension: PhantomData,
units: PhantomData,
value: 0.0001,
};
const LD_CURRENT_TIME_STEP_MS: u32 = 1;
}
#[derive(Clone, Debug, Miniconf)]
pub struct Settings {
ld_drive_current: ElectricCurrent,
ld_drive_current_limit: ElectricCurrent,
#[miniconf(defer)]
pd_responsitivity: pd_responsitivity::Parameters,
}
impl Default for Settings {
fn default() -> Self {
Self {
ld_drive_current: ElectricCurrent::new::<milliampere>(0.0),
ld_drive_current_limit: ElectricCurrent::new::<milliampere>(0.0),
pd_responsitivity: pd_responsitivity::Parameters::default(),
}
}
}
pub struct LdDrive{
ctrl: LdCtrl,
settings: Settings,
}
impl LdDrive{
pub fn new(current_source: LdCtrl, pins_adc: ADC2, phy: ld_pwr_exc_protector::LdPwrExcProtectorPhy)-> Self {
LdPwrExcProtector::setup(pins_adc, phy);
LdDrive {
ctrl: current_source,
settings: Settings::default()
}
}
pub fn setup(&mut self) {
LdPwrExcProtector::pwr_off();
self.ld_set_i(ElectricCurrent::new::<milliampere>(0.0));
self.ld_short();
}
pub fn get_ld_drive_current(&mut self) -> ElectricCurrent{
self.settings.ld_drive_current
}
pub fn set_ld_drive_current_limit(&mut self, i_limit: ElectricCurrent){
self.settings.ld_drive_current_limit = i_limit;
}
pub fn ld_short(&mut self) {
self.ctrl.ld_short_enable();
}
pub fn ld_open(&mut self) {
self.ctrl.ld_short_disable();
}
pub fn power_up(&mut self){
let _ = self.ctrl.set_i(ElectricCurrent::new::<milliampere>(0.0), Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
LdPwrExcProtector::pwr_on_and_arm_protection();
// Wait for LD Power Supply to start up before driving current to laser diode
sleep(30);
self.ld_set_i(self.settings.ld_drive_current);
}
pub fn power_down(&mut self){
LdPwrExcProtector::pwr_off();
}
pub fn get_pd_i(&mut self) -> ElectricCurrent {
LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE
}
// Ramping up or down laser diode current according to preset current step size and time step.
pub fn ld_set_i(&mut self, i: ElectricCurrent) -> ElectricCurrent {
let mut prev_i_set = self.settings.ld_drive_current;
let final_i_set = i.min(self.settings.ld_drive_current_limit).max(ElectricCurrent::new::<ampere>(0.0));
let num_of_step = ((final_i_set - prev_i_set)/Settings::LD_CURRENT_STEP_SIZE).get::<ratio>().floor() as i32;
let current_step = if num_of_step.is_positive() {
Settings::LD_CURRENT_STEP_SIZE
} else {
-Settings::LD_CURRENT_STEP_SIZE
};
for _ in 0..num_of_step.abs() {
prev_i_set = prev_i_set + current_step;
let _ = self.ctrl.set_i(prev_i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
sleep(Settings::LD_CURRENT_TIME_STEP_MS);
}
let prev_i_set = self.ctrl.set_i(final_i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
self.settings.ld_drive_current = prev_i_set;
prev_i_set
}
// Set the calibrated VDDA value obtained from ADC1 calibration
pub fn set_pd_mon_calibrated_vdda(&mut self, val_cal: u32) {
LdPwrExcProtector::set_calibrated_vdda(val_cal)
}
pub fn pd_mon_status(&mut self) -> ld_pwr_exc_protector::Status {
LdPwrExcProtector::get_status()
}
pub fn pd_mon_clear_alarm(&mut self) {
LdPwrExcProtector::clear_alarm_status();
}
pub fn set_pd_responsitivity(&mut self, responsitivity: pd_responsitivity::ResponsitivityUnit){
self.settings.pd_responsitivity.responsitivity = responsitivity;
}
pub fn set_pd_dark_current(&mut self, i_dark: ElectricCurrent){
self.settings.pd_responsitivity.i_dark = i_dark;
}
pub fn set_ld_power_limit(&mut self, pwr_limit: Power){
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 get_term_status(&mut self)->bool{
self.ctrl.get_term_status()
}
}