thermostat/src/steinhart_hart.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

use num_traits::float::Float;
2020-09-17 00:40:07 +08:00
use uom::si::{
f64::{
ElectricalResistance,
ThermodynamicTemperature,
},
electrical_resistance::ohm,
ratio::ratio,
thermodynamic_temperature::{degree_celsius, kelvin},
};
2020-03-19 04:51:30 +08:00
/// Steinhart-Hart equation parameters
#[derive(Clone, Debug)]
pub struct Parameters {
2020-09-17 00:40:07 +08:00
/// Base temperature
pub t0: ThermodynamicTemperature,
/// Base resistance
pub r0: ElectricalResistance,
/// Beta
2020-03-20 05:56:14 +08:00
pub b: f64,
2020-03-19 04:51:30 +08:00
}
impl Parameters {
/// Perform the voltage to temperature conversion.
///
/// Result unit: Kelvin
///
/// TODO: verify
2020-09-17 00:40:07 +08:00
pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature {
let inv_temp = 1.0 / self.t0.get::<kelvin>() + (r / self.r0).get::<ratio>().ln() / self.b;
ThermodynamicTemperature::new::<kelvin>(1.0 / inv_temp)
2020-03-19 04:51:30 +08:00
}
}
impl Default for Parameters {
fn default() -> Self {
2020-03-20 05:56:14 +08:00
Parameters {
2020-09-17 00:40:07 +08:00
t0: ThermodynamicTemperature::new::<degree_celsius>(25.0),
r0: ElectricalResistance::new::<ohm>(10_000.0),
b: 3800.0,
2020-03-20 05:56:14 +08:00
}
}
}