From 2e72a03b9368bac8635e760fb0d62dd7ff388f0f Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Mar 2020 22:56:14 +0100 Subject: [PATCH] steinhart_hart: fix equation --- src/command_parser.rs | 4 ++-- src/main.rs | 5 ++--- src/steinhart_hart.rs | 22 ++++++---------------- 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/command_parser.rs b/src/command_parser.rs index 3f3adae..b82e54f 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -110,7 +110,7 @@ pub enum PidParameter { #[derive(Debug, Clone, PartialEq)] pub enum ShParameter { T0, - R, + B, R0, } @@ -353,7 +353,7 @@ fn steinhart_hart_parameter(input: &[u8]) -> IResult<&[u8], Result ! { channel, ); let _ = writeln!(socket, "- t0={}", state.sh.t0); - let _ = writeln!(socket, "- r={}", state.sh.r); + let _ = writeln!(socket, "- b={}", state.sh.b); let _ = writeln!(socket, "- r0={}", state.sh.r0); let _ = writeln!(socket, ""); } @@ -355,10 +355,9 @@ fn main() -> ! { use command_parser::ShParameter::*; match parameter { T0 => sh.t0 = value, - R => sh.r = value, + B => sh.b = value, R0 => sh.r0 = value, } - sh.update(); let _ = writeln!(socket, "Steinhart-Hart equation parameter updated"); } Command::PostFilter { channel, rate } => { diff --git a/src/steinhart_hart.rs b/src/steinhart_hart.rs index fd3b099..de4d643 100644 --- a/src/steinhart_hart.rs +++ b/src/steinhart_hart.rs @@ -4,38 +4,28 @@ use num_traits::float::Float; #[derive(Clone, Debug)] pub struct Parameters { pub t0: f64, - pub r: f64, + pub b: f64, pub r0: f64, - - r_fact: f64, } impl Parameters { - /// Update the cached r_fact - pub fn update(&mut self) { - self.r_fact = (self.r / self.r0).ln(); - } - /// Perform the voltage to temperature conversion. /// /// Result unit: Kelvin /// /// TODO: verify - pub fn get_temperature(&self, b: f64) -> f64 { - let inv_temp = 1.0 / self.t0 + self.r_fact / b; + 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 } } impl Default for Parameters { fn default() -> Self { - let mut p = Parameters { + Parameters { t0: 0.001_4, - r: 0.000_000_099, + b: 0.000_000_099, r0: 5_110.0, - r_fact: 0.0, - }; - p.update(); - p + } } }