Initial proof of concept
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
use crate::quantity::{Kelvins, Ohms};
|
||||
use libm::{log, logf};
|
||||
|
||||
/// Convert thermistor resistance to a temperature using beta parameter equation
|
||||
pub fn convert_beta(
|
||||
resistance: Ohms<f32>,
|
||||
beta: f32,
|
||||
reference_temp: Kelvins<f32>,
|
||||
reference_resist: Ohms<f32>,
|
||||
) -> Kelvins<f32> {
|
||||
let kelvins = 1.0 / ((logf(resistance.0 / reference_resist.0) / beta) + 1.0 / reference_temp.0);
|
||||
Kelvins(kelvins)
|
||||
}
|
||||
|
||||
/// Convert thermistor resistance to a temperature using Steinhart-Hart equation
|
||||
pub fn convert_steinhart(resistance: Ohms<f64>, a: f64, b: f64, c: f64) -> Kelvins<f32> {
|
||||
let log_omhs = log(resistance.0);
|
||||
let kelvins = 1.0 / (a + b * log_omhs + c * log_omhs * log_omhs * log_omhs);
|
||||
Kelvins(kelvins as f32)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Tests ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use float_cmp::assert_approx_eq;
|
||||
|
||||
use crate::quantity::{OhmsVal, KelvinsVal};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn convert_beta_test() {
|
||||
let temperature = convert_beta(1538.462.ohms(), 3950.0, 298.15.kelvins(), 100_000.0.ohms());
|
||||
|
||||
assert_approx_eq!(f32, 435.31073, temperature.0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user