steinhart_hart: fix calculation, rename parameters

This commit is contained in:
Astro 2020-03-19 00:23:41 +01:00
parent 0f4442b124
commit cee0a1fcab
1 changed files with 25 additions and 14 deletions

View File

@ -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
}
}