From 38a220ce4e45a6aa6e5f25269f1903dee98cab8c Mon Sep 17 00:00:00 2001 From: Astro Date: Sun, 17 May 2020 00:54:37 +0200 Subject: [PATCH] fix tec_i calculation, add units::{Amps, Ohms} --- src/main.rs | 15 ++++++++------- src/units.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9ead14b..7e40834 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ mod command_parser; use command_parser::{Command, ShowCommand, PwmPin}; mod timer; mod units; +use units::{Amps, Ohms}; mod pid; mod steinhart_hart; mod channels; @@ -143,17 +144,17 @@ fn main() -> ! { for channel in 0..CHANNELS { if let Some(adc_data) = channels.channel_state(channel).adc_data { let dac_loopback = channels.read_dac_loopback(channel); + let dac_i = dac_loopback.clone() / Ohms(5.0); + let itec = channels.read_itec(channel); - let itec_u = itec.0; - let tec_u = (itec_u - 1.5) / 8.0; - let tec_r = 5.0; - let tec_i = tec_u / tec_r; + let tec_i = Amps((itec.0 - 1.5) / 8.0); + let state = channels.channel_state(channel); let _ = writeln!( - socket, "t={} raw{}=0x{:06X} dac_loopback={} itec={} tec={:.3}V/{:.3}A", + socket, "t={} raw{}=0x{:06X} dac_loopback={}/{} itec={} tec={}", state.adc_time, channel, adc_data, - dac_loopback, itec, - tec_u, tec_i, + dac_loopback, dac_i, + itec, tec_i, ); } } diff --git a/src/units.rs b/src/units.rs index 3a894cb..b4c0d5d 100644 --- a/src/units.rs +++ b/src/units.rs @@ -1,4 +1,7 @@ -use core::fmt; +use core::{ + fmt, + ops::Div, +}; #[derive(Debug, Clone, PartialEq, PartialOrd)] pub struct Volts(pub f64); @@ -8,3 +11,27 @@ impl fmt::Display for Volts { write!(f, "{:.3}V", self.0) } } + +impl Div for Volts { + type Output = Amps; + fn div(self, rhs: Ohms) -> Amps { + Amps(self.0 / rhs.0) + } +} + +#[derive(Debug, Clone, PartialEq, PartialOrd)] +pub struct Amps(pub f64); + +impl fmt::Display for Amps { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:.3}A", self.0) + } +} +#[derive(Debug, Clone, PartialEq, PartialOrd)] +pub struct Ohms(pub f64); + +impl fmt::Display for Ohms { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:.3}Ω", self.0) + } +}