fix tec_i calculation, add units::{Amps, Ohms}

This commit is contained in:
Astro 2020-05-17 00:54:37 +02:00
parent fb5c7a84e9
commit 38a220ce4e
2 changed files with 36 additions and 8 deletions

View File

@ -43,6 +43,7 @@ mod command_parser;
use command_parser::{Command, ShowCommand, PwmPin}; use command_parser::{Command, ShowCommand, PwmPin};
mod timer; mod timer;
mod units; mod units;
use units::{Amps, Ohms};
mod pid; mod pid;
mod steinhart_hart; mod steinhart_hart;
mod channels; mod channels;
@ -143,17 +144,17 @@ fn main() -> ! {
for channel in 0..CHANNELS { for channel in 0..CHANNELS {
if let Some(adc_data) = channels.channel_state(channel).adc_data { if let Some(adc_data) = channels.channel_state(channel).adc_data {
let dac_loopback = channels.read_dac_loopback(channel); 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 = channels.read_itec(channel);
let itec_u = itec.0; let tec_i = Amps((itec.0 - 1.5) / 8.0);
let tec_u = (itec_u - 1.5) / 8.0;
let tec_r = 5.0;
let tec_i = tec_u / tec_r;
let state = channels.channel_state(channel); let state = channels.channel_state(channel);
let _ = writeln!( 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, state.adc_time, channel, adc_data,
dac_loopback, itec, dac_loopback, dac_i,
tec_u, tec_i, itec, tec_i,
); );
} }
} }

View File

@ -1,4 +1,7 @@
use core::fmt; use core::{
fmt,
ops::Div,
};
#[derive(Debug, Clone, PartialEq, PartialOrd)] #[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Volts(pub f64); pub struct Volts(pub f64);
@ -8,3 +11,27 @@ impl fmt::Display for Volts {
write!(f, "{:.3}V", self.0) write!(f, "{:.3}V", self.0)
} }
} }
impl Div<Ohms> 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)
}
}