From 3ac287ace24fdfd57b4e096afd70ee1f7dadaa10 Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 17 Jan 2024 12:18:32 +0800 Subject: [PATCH] steinhart_hart: Add Steinhart-Hart eq fns - Port from thermostat firmware --- src/thermostat/mod.rs | 3 ++- src/thermostat/steinhart_hart.rs | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/thermostat/steinhart_hart.rs diff --git a/src/thermostat/mod.rs b/src/thermostat/mod.rs index e066de0..63a58c4 100644 --- a/src/thermostat/mod.rs +++ b/src/thermostat/mod.rs @@ -1,4 +1,5 @@ pub mod ad5680; pub mod max1968; pub mod thermostat; -pub mod ad7172; \ No newline at end of file +pub mod ad7172; +pub mod steinhart_hart; diff --git a/src/thermostat/steinhart_hart.rs b/src/thermostat/steinhart_hart.rs new file mode 100644 index 0000000..fb28777 --- /dev/null +++ b/src/thermostat/steinhart_hart.rs @@ -0,0 +1,40 @@ +use num_traits::float::Float; +use uom::si::{ + f64::{ + ElectricalResistance, + ThermodynamicTemperature, + }, + electrical_resistance::ohm, + ratio::ratio, + thermodynamic_temperature::{degree_celsius, kelvin}, +}; +use miniconf::Miniconf; + +/// Steinhart-Hart equation parameters +#[derive(Clone, Debug, PartialEq, Miniconf)] +pub struct Parameters { + /// Base temperature + pub t0: ThermodynamicTemperature, + /// Base resistance + pub r0: ElectricalResistance, + /// Beta + pub b: f64, +} + +impl Parameters { + /// Perform the voltage to temperature conversion. + pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature { + let inv_temp = 1.0 / self.t0.get::() + (r / self.r0).get::().ln() / self.b; + ThermodynamicTemperature::new::(1.0 / inv_temp) + } +} + +impl Default for Parameters { + fn default() -> Self { + Parameters { + t0: ThermodynamicTemperature::new::(25.0), + r0: ElectricalResistance::new::(10_000.0), + b: 3800.0, + } + } +}