2019-10-03 01:55:33 +08:00
|
|
|
use libm::F32Ext;
|
|
|
|
|
|
|
|
/// Steinhart-Hart equation parameters
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Parameters {
|
|
|
|
pub a: f32,
|
|
|
|
pub b: f32,
|
|
|
|
pub c: f32,
|
|
|
|
/// Parallel resistance
|
|
|
|
///
|
|
|
|
/// Not truly part of the equation but required to calculate
|
|
|
|
/// resistance from voltage.
|
|
|
|
pub parallel_r: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameters {
|
2019-10-03 03:28:14 +08:00
|
|
|
/// Perform the voltage to temperature conversion.
|
2019-10-03 01:55:33 +08:00
|
|
|
///
|
|
|
|
/// Result unit: Kelvin
|
2019-10-03 03:28:14 +08:00
|
|
|
///
|
|
|
|
/// TODO: verify
|
|
|
|
pub fn get_temperature(&self, voltage: f32) -> f32 {
|
|
|
|
let r = self.parallel_r * voltage;
|
|
|
|
let ln_r = r.abs().ln();
|
2019-10-03 01:55:33 +08:00
|
|
|
let inv_temp = self.a +
|
|
|
|
self.b * ln_r +
|
|
|
|
self.c * ln_r * ln_r * ln_r;
|
|
|
|
1.0 / inv_temp
|
|
|
|
}
|
|
|
|
}
|