thermostat/src/b_parameter.rs

39 lines
1.3 KiB
Rust
Raw Normal View History

use num_traits::float::Float;
2024-10-14 11:52:15 +08:00
use serde::{Deserialize, Serialize};
2020-09-17 00:40:07 +08:00
use uom::si::{
electrical_resistance::ohm,
f64::{ElectricalResistance, TemperatureInterval, ThermodynamicTemperature},
2020-09-17 00:40:07 +08:00
ratio::ratio,
temperature_interval::kelvin as kelvin_interval,
2020-09-17 00:40:07 +08:00
thermodynamic_temperature::{degree_celsius, kelvin},
};
2020-03-19 04:51:30 +08:00
/// B-Parameter equation parameters
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
2020-03-19 04:51:30 +08:00
pub struct Parameters {
2020-09-17 00:40:07 +08:00
/// Base temperature
pub t0: ThermodynamicTemperature,
/// Thermistor resistance at base temperature
2020-09-17 00:40:07 +08:00
pub r0: ElectricalResistance,
/// Beta (average slope of the function ln R vs. 1/T)
pub b: TemperatureInterval,
2020-03-19 04:51:30 +08:00
}
impl Parameters {
/// Perform the resistance to temperature conversion.
2020-09-17 00:40:07 +08:00
pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature {
let temp = (self.t0.recip() + (r / self.r0).get::<ratio>().ln() / self.b).recip();
ThermodynamicTemperature::new::<kelvin>(temp.get::<kelvin_interval>())
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: TemperatureInterval::new::<kelvin_interval>(3800.0),
2020-03-20 05:56:14 +08:00
}
}
}