From 76477065beea95b041e96ab283e09d43c12b27ec Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 27 Feb 2024 16:26:05 +0800 Subject: [PATCH] Change f64 to f32 for SI Unit related computation - Save computational time - Improve the processing speed of TecSetI cmd --- Cargo.toml | 2 +- src/device/boot.rs | 2 +- src/laser_diode/laser_diode.rs | 6 +-- src/laser_diode/ld_ctrl.rs | 6 +-- src/laser_diode/ld_current_out_ctrl_timer.rs | 2 +- src/laser_diode/ld_pwr_exc_protector.rs | 6 +-- src/laser_diode/pd_responsitivity.rs | 6 +-- src/net/cmd_handler.rs | 44 ++++++++++---------- src/thermostat/ad7172/adc.rs | 14 +++---- src/thermostat/max1968.rs | 7 ++-- src/thermostat/pid_state.rs | 30 ++++++------- src/thermostat/steinhart_hart.rs | 4 +- src/thermostat/temp_mon.rs | 6 +-- src/thermostat/thermostat.rs | 42 ++++++++++--------- 14 files changed, 89 insertions(+), 88 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f7cc357..415ef07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ stm32f4xx-hal = { version = "0.14.0", features = ["rt", "stm32f407", "usb_fs"] } stm32-eth = { version = "0.5.2", features = ["stm32f407", "smoltcp-phy", "smoltcp"] } ieee802_3_miim = "0.8.0" smoltcp = { version = "0.10.0", default-features = false, features = ["proto-ipv4", "socket-tcp", "log", "medium-ethernet"] } -uom = { version = "0.30", default-features = false, features = ["autoconvert", "si", "f64", "use_serde"] } +uom = { version = "0.30", default-features = false, features = ["autoconvert", "si", "f32", "use_serde"] } num-traits = { version = "0.2.15", default-features = false, features = ["libm"] } usb-device = "0.2.9" usbd-serial = "0.1.1" diff --git a/src/device/boot.rs b/src/device/boot.rs index 79c1af1..43db947 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -17,7 +17,7 @@ use stm32f4xx_hal::{ }; use uom::si::electric_current::milliampere; -use uom::si::{electric_current::ampere, f64::ElectricCurrent}; +use uom::si::{electric_current::ampere, f32::ElectricCurrent}; #[cfg(not(feature = "semihosting"))] const WATCHDOG_PERIOD: u32 = 4000; diff --git a/src/laser_diode/laser_diode.rs b/src/laser_diode/laser_diode.rs index b70111c..a65952c 100644 --- a/src/laser_diode/laser_diode.rs +++ b/src/laser_diode/laser_diode.rs @@ -13,14 +13,14 @@ use serde::{Deserialize, Serialize}; use uom::si::{ electric_current::milliampere, - f64::{ElectricPotential, ElectricCurrent, Power}, + f32::{ElectricPotential, ElectricCurrent, Power}, }; use uom::{si::{ISQ, SI, Quantity}, typenum::*}; // Volt / Ampere -pub type TransimpedanceUnit = Quantity, SI, f64>; +pub type TransimpedanceUnit = Quantity, SI, f32>; // Ampere / Volt -type TransconductanceUnit = Quantity, SI, f64>; +type TransconductanceUnit = Quantity, SI, f32>; impl Settings{ pub const LD_CURRENT_MAX: ElectricCurrent = ElectricCurrent { diff --git a/src/laser_diode/ld_ctrl.rs b/src/laser_diode/ld_ctrl.rs index 1d17c22..d1b07b2 100644 --- a/src/laser_diode/ld_ctrl.rs +++ b/src/laser_diode/ld_ctrl.rs @@ -8,7 +8,7 @@ use stm32f4xx_hal::{ use uom::si::{ ratio::ratio, - f64::{ElectricPotential, ElectricCurrent}, + f32::{ElectricPotential, ElectricCurrent}, }; use crate::laser_diode::max5719::{self, Dac}; @@ -79,9 +79,9 @@ impl LdCtrl { pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential { let value = ((voltage / dac_out_v_max).get::() - * (max5719::MAX_VALUE as f64)) as u32; + * (max5719::MAX_VALUE as f32)) as u32; self.phy.dac.set(value).unwrap(); - value as f64 * dac_out_v_max / max5719::MAX_VALUE as f64 + value as f32 * dac_out_v_max / max5719::MAX_VALUE as f32 } pub fn set_i(&mut self, current: ElectricCurrent, transimpedance: TransimpedanceUnit, dac_out_v_max: ElectricPotential) -> ElectricCurrent { diff --git a/src/laser_diode/ld_current_out_ctrl_timer.rs b/src/laser_diode/ld_current_out_ctrl_timer.rs index b28c4d9..780d787 100644 --- a/src/laser_diode/ld_current_out_ctrl_timer.rs +++ b/src/laser_diode/ld_current_out_ctrl_timer.rs @@ -1,6 +1,6 @@ use stm32f4xx_hal::timer::{CounterUs, Event}; use stm32f4xx_hal::pac::{interrupt, Interrupt, TIM2}; -use uom::si::{f64::ElectricCurrent, electric_current::ampere}; +use uom::si::{f32::ElectricCurrent, electric_current::ampere}; use fugit::{TimerDurationU32, KilohertzU32}; use core::marker::PhantomData; diff --git a/src/laser_diode/ld_pwr_exc_protector.rs b/src/laser_diode/ld_pwr_exc_protector.rs index 512a709..af43d1f 100644 --- a/src/laser_diode/ld_pwr_exc_protector.rs +++ b/src/laser_diode/ld_pwr_exc_protector.rs @@ -7,7 +7,7 @@ use stm32f4xx_hal::{ }; use uom::si::{ electric_potential::millivolt, - f64::ElectricPotential, + f32::ElectricPotential, ratio::ratio }; @@ -134,14 +134,14 @@ impl LdPwrExcProtector { fn convert_sample_to_volt(sample :u16) -> ElectricPotential { if let Some(ref mut wdg ) = LdPwrExcProtector::get() { - return ElectricPotential::new::(((u32::from(sample) * wdg.calibrated_vdda) / u32::from(MAX_SAMPLE)) as f64) + return ElectricPotential::new::(((u32::from(sample) * wdg.calibrated_vdda) / u32::from(MAX_SAMPLE)) as f32) } ElectricPotential::new::(0.0) } pub fn set_trigger_threshold_v(htr: ElectricPotential){ if let Some(ref mut wdg ) = LdPwrExcProtector::get() { - let code: u32 = ((htr / (ElectricPotential::new::(wdg.calibrated_vdda as f64))).get::() * (MAX_SAMPLE as f64)) as u32; + let code: u32 = ((htr / (ElectricPotential::new::(wdg.calibrated_vdda as f32))).get::() * (MAX_SAMPLE as f32)) as u32; wdg.pac.htr.write(|w| unsafe {w.bits(code)}); } } diff --git a/src/laser_diode/pd_responsitivity.rs b/src/laser_diode/pd_responsitivity.rs index 2933827..2d10581 100644 --- a/src/laser_diode/pd_responsitivity.rs +++ b/src/laser_diode/pd_responsitivity.rs @@ -1,7 +1,7 @@ -use core::{f64::NAN, marker::PhantomData}; +use core::{f32::NAN, marker::PhantomData}; use serde::{Deserialize, Serialize}; use uom::si::{ - f64::{ + f32::{ ElectricCurrent, Power }, @@ -11,7 +11,7 @@ use uom::{si::{ISQ, SI, Quantity}, typenum::*}; use miniconf::Tree; // Ampere / Watt -pub type ResponsitivityUnit = Quantity, SI, f64>; +pub type ResponsitivityUnit = Quantity, SI, f32>; #[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Tree)] pub struct Parameters { diff --git a/src/net/cmd_handler.rs b/src/net/cmd_handler.rs index d7b5460..9ddec5e 100644 --- a/src/net/cmd_handler.rs +++ b/src/net/cmd_handler.rs @@ -156,7 +156,7 @@ pub fn send_status_report(buffer: &mut [u8], laser: &mut LdDrive, tec: &mut Ther // Use a minimal struct for high speed cmd ctrl to reduce processing overhead #[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)] pub struct TecSetICmdJson { - tec_set_i: f64 + tec_set_i: f32 } #[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)] pub struct TecSetICmd { @@ -229,7 +229,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu laser.ld_open(); } Some(LdCmdEnum::SetI) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { laser.ld_set_i(ElectricCurrent::new::(val)); } @@ -239,7 +239,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(LdCmdEnum::SetISoftLimit) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { laser.set_ld_drive_current_limit(ElectricCurrent::new::(val)) } @@ -249,7 +249,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(LdCmdEnum::SetPdResponsitivity) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { laser.set_pd_responsitivity(ResponsitivityUnit {dimension: PhantomData, units: PhantomData, value: val}) } @@ -259,7 +259,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(LdCmdEnum::SetPdDarkCurrent) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { laser.set_pd_dark_current(ElectricCurrent::new::(val)) } @@ -269,7 +269,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(LdCmdEnum::SetLdPwrLimit) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { laser.set_ld_power_limit(Power::new::(val)) } @@ -295,7 +295,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu tec.power_down() } Some(ThermostatCmdEnum::SetTecMaxV) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_max_v(ElectricPotential::new::(val)); } @@ -305,7 +305,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTecMaxIPos) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_max_i_pos(ElectricCurrent::new::(val)); } @@ -315,7 +315,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTecMaxINeg) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_max_i_pos(ElectricCurrent::new::(val)); } @@ -325,7 +325,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTecIOut) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_i(ElectricCurrent::new::(val)); } @@ -335,7 +335,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTemperatureSetpoint) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_temperature_setpoint(ThermodynamicTemperature::new::(val)); } @@ -351,7 +351,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu tec.set_pid_engaged(false); } Some(ThermostatCmdEnum::SetPidKp) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_pid(Kp, val); } @@ -361,7 +361,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetPidKi) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_pid(Ki, val); } @@ -371,7 +371,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetPidKd) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_pid(Kd, val); } @@ -381,7 +381,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetPidOutMin) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_pid(Min, val); } @@ -391,7 +391,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetPidOutMax) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_pid(Max, val); } @@ -433,7 +433,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu tec.set_temp_adc_sinc3_fine_filter(0, val2); } None => { - info!("data_f64 field needs to be set for configuration"); + info!("data_f32 field needs to be set for configuration"); } } } @@ -455,7 +455,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTempMonUpperLimit) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_temp_mon_upper_limit(ThermodynamicTemperature::new::(val)); } @@ -465,7 +465,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetTempMonLowerLimit) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_temp_mon_lower_limit(ThermodynamicTemperature::new::(val)); } @@ -478,7 +478,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu tec.clear_temp_mon_alarm(); } Some(ThermostatCmdEnum::SetShT0) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_sh_t0(ThermodynamicTemperature::new::(val)); } @@ -488,7 +488,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetShR0) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_sh_r0(ElectricalResistance::new::(val)); } @@ -498,7 +498,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mu } } Some(ThermostatCmdEnum::SetShBeta) => { - match cmd.json.data_f64 { + match cmd.json.data_f32 { Some(val) => { tec.set_sh_beta(val); } diff --git a/src/thermostat/ad7172/adc.rs b/src/thermostat/ad7172/adc.rs index 2946eb4..bd515be 100644 --- a/src/thermostat/ad7172/adc.rs +++ b/src/thermostat/ad7172/adc.rs @@ -11,7 +11,7 @@ use stm32f4xx_hal:: }, }; use uom::si::{ - f64::ElectricPotential, + f32::ElectricPotential, electric_potential::volt, }; use super::{ @@ -325,15 +325,15 @@ impl Default for ChannelCalibration { impl ChannelCalibration { pub fn convert_data(&self, data: u32) -> ElectricPotential { let data = if self.bipolar { - (data as i32 - 0x80_0000) as f64 + (data as i32 - 0x80_0000) as f32 } else { - data as f64 / 2.0 + data as f32 / 2.0 }; - let data = data / (self.gain as f64 / (0x40_0000 as f64)); - let data = data + (self.offset as i32 - 0x80_0000) as f64; - let data = data / (2 << 23) as f64; + let data = data / (self.gain as f32 / (0x40_0000 as f32)); + let data = data + (self.offset as i32 - 0x80_0000) as f32; + let data = data / (2 << 23) as f32; - const V_REF: f64 = 3.3; + const V_REF: f32 = 3.3; ElectricPotential::new::(data * V_REF / 0.75) } } diff --git a/src/thermostat/max1968.rs b/src/thermostat/max1968.rs index a5bebef..d217af9 100644 --- a/src/thermostat/max1968.rs +++ b/src/thermostat/max1968.rs @@ -15,7 +15,7 @@ use stm32f4xx_hal::{ use uom::si::{ electric_potential::millivolt, - f64::ElectricPotential, + f32::ElectricPotential, ratio::ratio, }; @@ -143,9 +143,8 @@ impl MAX1968 { pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential { let value = ((voltage / dac_out_v_max).get::() - * (ad5680::MAX_VALUE as f64)) as u32; + * (ad5680::MAX_VALUE as f32)) as u32; self.phy.dac.set(value).unwrap(); - // TODO: Store the set-ed DAC Voltage Value voltage } @@ -192,7 +191,7 @@ impl MAX1968 { } }; let mv = self.pins_adc.sample_to_millivolts(sample as u16); - ElectricPotential::new::(mv as f64) + ElectricPotential::new::(mv as f32) } pub fn set_pwm(&mut self, pwm_pin: PwmPinsEnum, duty: f64, max_duty: f64) -> f64 { diff --git a/src/thermostat/pid_state.rs b/src/thermostat/pid_state.rs index 9a5e2e1..bbaea5b 100644 --- a/src/thermostat/pid_state.rs +++ b/src/thermostat/pid_state.rs @@ -1,7 +1,7 @@ use miniconf::Tree; use serde::{Deserialize, Serialize}; use uom::si::{ - electric_potential::volt, electrical_resistance::ohm, f64::{ + electric_potential::volt, electrical_resistance::ohm, f32::{ ElectricPotential, ElectricalResistance, ThermodynamicTemperature }, thermodynamic_temperature::degree_celsius }; @@ -9,8 +9,8 @@ use crate::thermostat::{ ad7172, steinhart_hart as sh, }; -const R_INNER: f64 = 2.0 * 5100.0; -const VREF_SENS: f64 = 3.3 / 2.0; +const R_INNER: f32 = 2.0 * 5100.0; +const VREF_SENS: f32 = 3.3 / 2.0; #[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Tree)] pub struct Parameters { @@ -105,11 +105,11 @@ impl PidState { pub fn update_pid(&mut self) -> Option { let input = self.get_temperature()?.get::(); let setpoint = self.set_point.get::(); - let mut output: f64 = self.controller.y1 - setpoint * f64::from(self.controller.parameters.ki) - + input * f64::from(self.controller.parameters.kp + self.controller.parameters.ki + self.controller.parameters.kd) + let mut output: f64 = self.controller.y1 - setpoint as f64 * f64::from(self.controller.parameters.ki) + + input as f64 * f64::from(self.controller.parameters.kp + self.controller.parameters.ki + self.controller.parameters.kd) - self.controller.x1 * f64::from(self.controller.parameters.kp + 2.0 * self.controller.parameters.kd) + self.controller.x2 * f64::from(self.controller.parameters.kd) - + f64::from(self.controller.parameters.kp) * (setpoint - self.controller.u1); + + f64::from(self.controller.parameters.kp) * (setpoint as f64 - self.controller.u1); if output < self.controller.parameters.output_min.into() { output = self.controller.parameters.output_min.into(); } @@ -117,8 +117,8 @@ impl PidState { output = self.controller.parameters.output_max.into(); } self.controller.x2 = self.controller.x1; - self.controller.x1 = input; - self.controller.u1 = setpoint; + self.controller.x1 = input as f64; + self.controller.u1 = setpoint as f64; self.controller.y1 = output; Some(output) } @@ -142,22 +142,22 @@ impl PidState { Some(temperature) } - pub fn set_pid_params(&mut self, param: PidSettings, val: f64){ + pub fn set_pid_params(&mut self, param: PidSettings, val: f32){ match param { PidSettings::Kp => { - self.controller.parameters.kp = val as f32; + self.controller.parameters.kp = val; } PidSettings::Ki => { - self.controller.parameters.ki = val as f32; + self.controller.parameters.ki = val; } PidSettings::Kd => { - self.controller.parameters.kd = val as f32; + self.controller.parameters.kd = val; } PidSettings::Min => { - self.controller.parameters.output_min = val as f32; + self.controller.parameters.output_min = val; } PidSettings::Max => { - self.controller.parameters.output_max = val as f32; + self.controller.parameters.output_max = val; } } } @@ -185,7 +185,7 @@ impl PidState { self.sh.r0 = r0 } - pub fn set_sh_beta(&mut self, beta: f64){ + pub fn set_sh_beta(&mut self, beta: f32){ self.sh.b = beta } diff --git a/src/thermostat/steinhart_hart.rs b/src/thermostat/steinhart_hart.rs index d00e3a1..d6298a2 100644 --- a/src/thermostat/steinhart_hart.rs +++ b/src/thermostat/steinhart_hart.rs @@ -1,7 +1,7 @@ use num_traits::float::Float; use serde::{Deserialize, Serialize}; use uom::si::{ - f64::{ + f32::{ ElectricalResistance, ThermodynamicTemperature, }, @@ -19,7 +19,7 @@ pub struct Parameters { /// Base resistance pub r0: ElectricalResistance, /// Beta - pub b: f64, + pub b: f32, } impl Parameters { diff --git a/src/thermostat/temp_mon.rs b/src/thermostat/temp_mon.rs index 79dee68..4ca7f06 100644 --- a/src/thermostat/temp_mon.rs +++ b/src/thermostat/temp_mon.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use miniconf::Tree; use uom::si::{ - f64::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius + f32::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius }; use num_traits::Float; #[derive(PartialEq, Deserialize, Serialize, Copy, Clone, Default, Debug)] @@ -21,8 +21,8 @@ pub struct TempStatus { #[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)] pub struct TempMonSettings { - pub upper_limit: f64, - pub lower_limit: f64, + pub upper_limit: f32, + pub lower_limit: f32, } pub struct TempMon { diff --git a/src/thermostat/thermostat.rs b/src/thermostat/thermostat.rs index d3505c7..52aea8b 100644 --- a/src/thermostat/thermostat.rs +++ b/src/thermostat/thermostat.rs @@ -13,7 +13,7 @@ use uom::si::{ electric_potential::volt, electrical_resistance::ohm, thermodynamic_temperature::degree_celsius, - f64::{ThermodynamicTemperature, ElectricCurrent, ElectricPotential, ElectricalResistance}, + f32::{ThermodynamicTemperature, ElectricCurrent, ElectricPotential, ElectricalResistance}, ratio::ratio, }; use miniconf::Tree; @@ -66,7 +66,7 @@ impl TecSettings{ units: PhantomData, value: 5.0, }; - const MAX_V_DUTY_MAX: f64 = TecSettings::MAX_V_MAX.value / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE.value; + const MAX_V_DUTY_MAX: f64 = TecSettings::MAX_V_MAX.value as f64 / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE.value as f64; const MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE: ElectricCurrent = ElectricCurrent { dimension: PhantomData, units: PhantomData, @@ -83,8 +83,8 @@ impl TecSettings{ value: 1.0, }; // .get::() is not implemented for const - const MAX_I_POS_DUTY_MAX: f64 = TecSettings::MAX_I_POS_CURRENT.value / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value; - const MAX_I_NEG_DUTY_MAX: f64 = TecSettings::MAX_I_NEG_CURRENT.value / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value; + const MAX_I_POS_DUTY_MAX: f64 = TecSettings::MAX_I_POS_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64; + const MAX_I_NEG_DUTY_MAX: f64 = TecSettings::MAX_I_NEG_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64; } impl Default for TecSettings { @@ -176,7 +176,7 @@ impl Thermostat{ if pid_engaged { match state.update_pid() { Some(pid_output) => { - self.set_i(ElectricCurrent::new::(pid_output)); + self.set_i(ElectricCurrent::new::(pid_output as f32)); debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::()); } None => { } @@ -213,40 +213,42 @@ impl Thermostat{ pub fn set_max_v(&mut self, max_v: ElectricPotential) -> ElectricPotential { let duty = (max_v / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE).get::(); - let duty = self.max1968.set_pwm(PwmPinsEnum::MaxV, duty, TecSettings::MAX_V_DUTY_MAX); - self.tec_settings.max_v_set = duty * TecSettings::MAX_V_DUTY_TO_CURRENT_RATE; + let duty = self.max1968.set_pwm(PwmPinsEnum::MaxV, duty as f64, TecSettings::MAX_V_DUTY_MAX); + self.tec_settings.max_v_set = duty as f32 * TecSettings::MAX_V_DUTY_TO_CURRENT_RATE; self.tec_settings.max_v_set } pub fn set_max_i_pos(&mut self, max_i_pos: ElectricCurrent) -> ElectricCurrent { let duty = (max_i_pos / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::(); - let duty = self.max1968.set_pwm(PwmPinsEnum::MaxPosI, duty, TecSettings::MAX_I_POS_DUTY_MAX); - self.tec_settings.max_i_pos_set = duty * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE; + let duty = self.max1968.set_pwm(PwmPinsEnum::MaxPosI, duty as f64, TecSettings::MAX_I_POS_DUTY_MAX); + self.tec_settings.max_i_pos_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE; self.tec_settings.max_i_pos_set } pub fn set_max_i_neg(&mut self, max_i_neg: ElectricCurrent) -> ElectricCurrent { let duty = (max_i_neg / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::(); - let duty = self.max1968.set_pwm(PwmPinsEnum::MaxNegI, duty, TecSettings::MAX_I_NEG_DUTY_MAX); - self.tec_settings.max_i_neg_set = duty * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE; + let duty = self.max1968.set_pwm(PwmPinsEnum::MaxNegI, duty as f64, TecSettings::MAX_I_NEG_DUTY_MAX); + self.tec_settings.max_i_neg_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE; self.tec_settings.max_i_neg_set } pub fn get_dac_vfb(&mut self) -> ElectricPotential { - self.max1968.adc_read(AdcReadTarget::DacVfb, 16) + ElectricPotential::new::(NAN) + // self.max1968.adc_read(AdcReadTarget::DacVfb, 16) } pub fn get_vref(&mut self) -> ElectricPotential { - self.max1968.adc_read(AdcReadTarget::VREF, 16) + // ElectricPotential::new::(NAN) + self.max1968.adc_read(AdcReadTarget::VREF, 1) } pub fn get_tec_i(&mut self) -> ElectricCurrent { let vref = self.get_vref(); - (self.max1968.adc_read(AdcReadTarget::ITec, 16) - vref) / ElectricalResistance::new::(0.4) + (self.max1968.adc_read(AdcReadTarget::ITec, 1) - vref) / ElectricalResistance::new::(0.4) } pub fn get_tec_v(&mut self) -> ElectricPotential { - (self.max1968.adc_read(AdcReadTarget::VTec, 16) - TecSettings::TEC_VSEC_BIAS_V) * 4.0 + (self.max1968.adc_read(AdcReadTarget::VTec, 1) - TecSettings::TEC_VSEC_BIAS_V) * 4.0 } /// Calibrates the DAC output to match vref of the MAX driver to reduce zero-current offset of the MAX driver output. @@ -285,7 +287,7 @@ impl Thermostat{ best_error = error; start_value = prev_value; - let vref = (value as f64 / ad5680::MAX_VALUE as f64) * TecSettings::DAC_OUT_V_MAX; + let vref = (value as f32 / ad5680::MAX_VALUE as f32) * TecSettings::DAC_OUT_V_MAX; self.set_center_pt(vref); } prev_value = value; @@ -349,11 +351,11 @@ impl Thermostat{ self.max1968.get_calibrated_vdda() } - pub fn set_pid(&mut self, param: PidSettings, val: f64){ + pub fn set_pid(&mut self, param: PidSettings, val: f32){ self.pid_ctrl_ch0.set_pid_params(param, val); } - pub fn set_sh_beta(&mut self, beta: f64) { + pub fn set_sh_beta(&mut self, beta: f32) { self.pid_ctrl_ch0.set_sh_beta(beta); } @@ -441,7 +443,7 @@ pub struct TecSettingSummary { #[derive(Deserialize, Serialize, Clone, Copy, Debug, Tree)] pub struct ThermistorParams { - t0: f64, + t0: f32, r0: ElectricalResistance, - b: f64 + b: f32 }