From 9381feec1bd9a438a5bbc037314b892a758f1f3e Mon Sep 17 00:00:00 2001 From: atse Date: Fri, 16 Aug 2024 16:26:01 +0800 Subject: [PATCH] Constants uom --- src/ad7172/adc.rs | 14 ++++++++------ src/channel_state.rs | 20 ++++++++++++++------ src/channels.rs | 11 +++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/ad7172/adc.rs b/src/ad7172/adc.rs index 68ebab3..f81627b 100644 --- a/src/ad7172/adc.rs +++ b/src/ad7172/adc.rs @@ -1,13 +1,11 @@ +use core::marker::PhantomData; use core::fmt; use log::{info, warn}; use stm32f4xx_hal::hal::{ blocking::spi::Transfer, digital::v2::OutputPin, }; -use uom::si::{ - f64::ElectricPotential, - electric_potential::volt, -}; +use uom::si::f64::ElectricPotential; use super::{ regs::{self, Register, RegisterData}, checksum::{ChecksumMode, Checksum}, @@ -270,7 +268,11 @@ impl ChannelCalibration { let data = data + (self.offset as i32 - 0x80_0000) as f64; let data = data / (2 << 23) as f64; - const V_REF: f64 = 3.3; - ElectricPotential::new::(data * V_REF / 0.75) + const V_REF: ElectricPotential = ElectricPotential { + dimension: PhantomData, + units: PhantomData, + value: 3.3, + }; + data * V_REF / 0.75 } } diff --git a/src/channel_state.rs b/src/channel_state.rs index 4820131..664848d 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -1,3 +1,5 @@ +use core::marker::PhantomData; + use smoltcp::time::{Duration, Instant}; use uom::si::{ f64::{ @@ -9,7 +11,6 @@ use uom::si::{ }, electric_potential::volt, electric_current::ampere, - electrical_resistance::ohm, thermodynamic_temperature::degree_celsius, time::millisecond, }; @@ -20,8 +21,16 @@ use crate::{ command_parser::CenterPoint, }; -const R_INNER: f64 = 2.0 * 5100.0; -const VREF_SENS: f64 = 3.3 / 2.0; +const R_INNER: ElectricalResistance = ElectricalResistance { + dimension: PhantomData, + units: PhantomData, + value: 2.0 * 5100.0, +}; +const VREF_SENS: ElectricPotential = ElectricPotential { + dimension: PhantomData, + units: PhantomData, + value: 3.3 / 2.0, +}; pub struct ChannelState { pub adc_data: Option, @@ -87,10 +96,9 @@ impl ChannelState { /// Get `SENS[01]` input resistance pub fn get_sens(&self) -> Option { - let r_inner = ElectricalResistance::new::(R_INNER); - let vref = ElectricPotential::new::(VREF_SENS); + let vref = VREF_SENS; let adc_input = self.get_adc()?; - let r = r_inner * adc_input / (vref - adc_input); + let r = R_INNER * adc_input / (vref - adc_input); Some(r) } diff --git a/src/channels.rs b/src/channels.rs index a428dd1..42d1011 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -31,7 +31,11 @@ pub enum PinsAdcReadTarget { } pub const CHANNELS: usize = 2; -pub const R_SENSE: f64 = 0.05; +pub const R_SENSE: ElectricalResistance = ElectricalResistance { + dimension: PhantomData, + units: PhantomData, + value: 0.05, +}; // From design specs pub const MAX_TEC_I: ElectricCurrent = ElectricCurrent { @@ -159,10 +163,9 @@ impl Channels { _ => unreachable!(), }; let center_point = vref_meas; - let r_sense = ElectricalResistance::new::(R_SENSE); - let voltage = i_set * 10.0 * r_sense + center_point; + let voltage = i_set * 10.0 * R_SENSE + center_point; let voltage = self.set_dac(channel, voltage); - let i_set = (voltage - center_point) / (10.0 * r_sense); + let i_set = (voltage - center_point) / (10.0 * R_SENSE); self.channel_state(channel).i_set = i_set; i_set }