2020-03-20 02:51:59 +08:00
|
|
|
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,
|
2023-08-28 14:52:09 +08:00
|
|
|
f64::{ElectricalResistance, TemperatureInterval, ThermodynamicTemperature},
|
2020-09-17 00:40:07 +08:00
|
|
|
ratio::ratio,
|
2023-08-28 14:52:09 +08:00
|
|
|
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
|
|
|
|
2023-08-25 17:34:07 +08:00
|
|
|
/// B-Parameter equation parameters
|
2020-12-12 08:25:07 +08:00
|
|
|
#[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,
|
2023-08-25 17:34:07 +08:00
|
|
|
/// Thermistor resistance at base temperature
|
2020-09-17 00:40:07 +08:00
|
|
|
pub r0: ElectricalResistance,
|
2023-08-25 17:34:07 +08:00
|
|
|
/// Beta (average slope of the function ln R vs. 1/T)
|
2023-08-28 14:52:09 +08:00
|
|
|
pub b: TemperatureInterval,
|
2020-03-19 04:51:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameters {
|
2023-08-28 14:52:09 +08:00
|
|
|
/// Perform the resistance to temperature conversion.
|
2020-09-17 00:40:07 +08:00
|
|
|
pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature {
|
2023-08-28 14:52:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-03-19 07:23:41 +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),
|
2023-08-28 14:52:09 +08:00
|
|
|
b: TemperatureInterval::new::<kelvin_interval>(3800.0),
|
2020-03-20 05:56:14 +08:00
|
|
|
}
|
2020-03-19 07:23:41 +08:00
|
|
|
}
|
|
|
|
}
|