From cee0a1fcabfb83764a9be3cc19398dd8fed3d390 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 19 Mar 2020 00:23:41 +0100 Subject: [PATCH] steinhart_hart: fix calculation, rename parameters --- src/steinhart_hart.rs | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/steinhart_hart.rs b/src/steinhart_hart.rs index 5a2444f..792e857 100644 --- a/src/steinhart_hart.rs +++ b/src/steinhart_hart.rs @@ -3,28 +3,39 @@ use lexical_core::Float; /// Steinhart-Hart equation parameters #[derive(Clone, Debug)] pub struct Parameters { - pub a: f64, - pub b: f64, - pub c: f64, - /// Parallel resistance - /// - /// Not truly part of the equation but required to calculate - /// resistance from voltage. - pub parallel_r: f64, + pub t0: f64, + pub r: 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, voltage: f64) -> f64 { - let r = self.parallel_r * voltage; - let ln_r = r.abs().ln(); - let inv_temp = self.a + - self.b * ln_r + - self.c * ln_r * ln_r * ln_r; + pub fn get_temperature(&self, b: f64) -> f64 { + let inv_temp = 1.0 / self.t0 + self.r_fact / b; 1.0 / inv_temp } } + +impl Default for Parameters { + fn default() -> Self { + let mut p = Parameters { + t0: 0.001_4, + r: 0.000_000_099, + r0: 5_110.0, + r_fact: 0.0, + }; + p.update(); + p + } +}