From dda1f2f0b4c78742ae7f173c6443183e723443dc Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 16 Sep 2020 18:40:07 +0200 Subject: [PATCH] use ThermodynamicTemperature --- src/channel_state.rs | 17 +++++++++++++---- src/channels.rs | 3 ++- src/main.rs | 20 ++++++++++++++++---- src/steinhart_hart.rs | 28 ++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/channel_state.rs b/src/channel_state.rs index 8152161..5563150 100644 --- a/src/channel_state.rs +++ b/src/channel_state.rs @@ -1,7 +1,13 @@ use smoltcp::time::Instant; use uom::si::{ - f64::ElectricPotential, + f64::{ + ElectricPotential, + ElectricalResistance, + ThermodynamicTemperature, + }, electric_potential::volt, + electrical_resistance::ohm, + temperature_interval::kelvin, }; use crate::{ ad7172, @@ -39,9 +45,10 @@ impl ChannelState { } /// Update PID state on ADC input, calculate new DAC output - pub fn update_pid(&mut self) -> f64 { + pub fn update_pid(&mut self) { // Update PID controller - self.pid.update(self.get_temperature().unwrap()) + // self.pid.update(self.get_temperature().unwrap().get::()) + // TODO: add output field } pub fn get_adc(&self) -> Option { @@ -49,8 +56,10 @@ impl ChannelState { Some(ElectricPotential::new::(volts)) } - pub fn get_temperature(&self) -> Option { + pub fn get_temperature(&self) -> Option { let r = self.get_adc()?.get::(); + // TODO: + let r = ElectricalResistance::new::(r); let temperature = self.sh.get_temperature(r); Some(temperature) } diff --git a/src/channels.rs b/src/channels.rs index f82d778..c4b0512 100644 --- a/src/channels.rs +++ b/src/channels.rs @@ -76,7 +76,8 @@ impl Channels { }; if let Some(dac_value) = dac_value { // Forward PID output to i_set DAC - self.set_dac(channel.into(), ElectricPotential::new::(dac_value)); + // TODO: + // self.set_dac(channel.into(), ElectricPotential::new::(dac_value)); } channel diff --git a/src/main.rs b/src/main.rs index a08747a..383a4cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,10 +34,12 @@ use uom::{ f64::{ ElectricPotential, ElectricalResistance, + ThermodynamicTemperature, }, electric_current::ampere, electric_potential::volt, electrical_resistance::ohm, + thermodynamic_temperature::degree_celsius, }, }; @@ -260,9 +262,19 @@ fn main() -> ! { socket, "channel {}: Steinhart-Hart equation parameters", channel, ); - let _ = writeln!(socket, "- t0={}", state.sh.t0); + let _ = writeln!(socket, "- t0={}", state.sh.t0.into_format_args(degree_celsius, Abbreviation)); let _ = writeln!(socket, "- b={}", state.sh.b); - let _ = writeln!(socket, "- r0={}", state.sh.r0); + let _ = writeln!(socket, "- r0={}", state.sh.r0.into_format_args(ohm, Abbreviation)); + match (state.get_adc(), state.get_temperature()) { + (Some(adc), Some(temp)) => { + let _ = writeln!( + socket, "- adc={} temp={:.3}K", + adc.into_format_args(volt, Abbreviation), + temp.into_format_args(degree_celsius, Abbreviation), + ); + } + _ => {} + } let _ = writeln!(socket, ""); } } @@ -361,9 +373,9 @@ fn main() -> ! { let sh = &mut channels.channel_state(channel).sh; use command_parser::ShParameter::*; match parameter { - T0 => sh.t0 = value, + T0 => sh.t0 = ThermodynamicTemperature::new::(value), B => sh.b = value, - R0 => sh.r0 = value, + R0 => sh.r0 = ElectricalResistance::new::(value), } let _ = writeln!(socket, "Steinhart-Hart equation parameter updated"); } diff --git a/src/steinhart_hart.rs b/src/steinhart_hart.rs index de4d643..f0b231f 100644 --- a/src/steinhart_hart.rs +++ b/src/steinhart_hart.rs @@ -1,11 +1,23 @@ use num_traits::float::Float; +use uom::si::{ + f64::{ + ElectricalResistance, + ThermodynamicTemperature, + }, + electrical_resistance::ohm, + ratio::ratio, + thermodynamic_temperature::{degree_celsius, kelvin}, +}; /// Steinhart-Hart equation parameters #[derive(Clone, Debug)] pub struct Parameters { - pub t0: f64, + /// Base temperature + pub t0: ThermodynamicTemperature, + /// Base resistance + pub r0: ElectricalResistance, + /// Beta pub b: f64, - pub r0: f64, } impl Parameters { @@ -14,18 +26,18 @@ impl Parameters { /// Result unit: Kelvin /// /// TODO: verify - pub fn get_temperature(&self, r: f64) -> f64 { - let inv_temp = 1.0 / self.t0 + (r / self.r0).ln() / self.b; - 1.0 / inv_temp + pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature { + let inv_temp = 1.0 / self.t0.get::() + (r / self.r0).get::().ln() / self.b; + ThermodynamicTemperature::new::(1.0 / inv_temp) } } impl Default for Parameters { fn default() -> Self { Parameters { - t0: 0.001_4, - b: 0.000_000_099, - r0: 5_110.0, + t0: ThermodynamicTemperature::new::(25.0), + r0: ElectricalResistance::new::(10_000.0), + b: 3800.0, } } }