steinhart_hart: Beta Parameter uom dimensions #117

Merged
sb10q merged 1 commits from atse/thermostat:b-parameter-uom into master 2024-10-21 11:12:47 +08:00
2 changed files with 14 additions and 9 deletions

View File

@ -19,7 +19,11 @@ use uom::si::{
electric_current::ampere,
electric_potential::volt,
electrical_resistance::ohm,
f64::{ElectricCurrent, ElectricPotential, ElectricalResistance, ThermodynamicTemperature},
f64::{
ElectricCurrent, ElectricPotential, ElectricalResistance, TemperatureInterval,
ThermodynamicTemperature,
},
temperature_interval::kelvin,
thermodynamic_temperature::degree_celsius,
};
@ -248,7 +252,7 @@ impl Handler {
use super::command_parser::ShParameter::*;
match parameter {
T0 => sh.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
B => sh.b = value,
B => sh.b = TemperatureInterval::new::<kelvin>(value),
R0 => sh.r0 = ElectricalResistance::new::<ohm>(value),
}
send_line(socket, b"{}");

View File

@ -2,8 +2,9 @@ use num_traits::float::Float;
use serde::{Deserialize, Serialize};
use uom::si::{
electrical_resistance::ohm,
f64::{ElectricalResistance, ThermodynamicTemperature},
f64::{ElectricalResistance, TemperatureInterval, ThermodynamicTemperature},
ratio::ratio,
temperature_interval::kelvin as kelvin_interval,
thermodynamic_temperature::{degree_celsius, kelvin},
};
@ -12,17 +13,17 @@ use uom::si::{
pub struct Parameters {
/// Base temperature
pub t0: ThermodynamicTemperature,
/// Base resistance
/// Resistance at base temperature
pub r0: ElectricalResistance,
/// Beta
pub b: f64,
pub b: TemperatureInterval,
}
impl Parameters {
/// Perform the voltage to temperature conversion.
/// Perform the resistance to temperature conversion.
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)
let temp = (self.t0.recip() + (r / self.r0).get::<ratio>().ln() / self.b).recip();
ThermodynamicTemperature::new::<kelvin>(temp.get::<kelvin_interval>())
}

if you add kelvin unit to b, don't use self.b.get::<kelvin>(). The unit is correct now.
maybe simplify it into one line?

if you add kelvin unit to `b`, don't use `self.b.get::<kelvin>()`. The unit is correct now. maybe simplify it into one line?
Outdated
Review

ThermodynamicTemperatures couldn't be added together, and the same applies to its reciprocal, which is why I did that initially.

Upon further inspection of the uom crate though, turns out I've used the wrong type for Parameters::b, and TemperatureInterval should be used instead, albeit with weird ergonomics, and probably can't be simplified onto just one line.

`ThermodynamicTemperature`s [couldn't be added together](https://github.com/iliekturtles/uom/issues/403), and the same applies to its reciprocal, which is why I did that initially. Upon further inspection of the uom crate though, turns out I've used the wrong type for `Parameters::b`, and `TemperatureInterval` should be used instead, albeit with weird ergonomics, and probably can't be simplified onto just one line.
}
@ -31,7 +32,7 @@ impl Default for Parameters {
Parameters {
t0: ThermodynamicTemperature::new::<degree_celsius>(25.0),
r0: ElectricalResistance::new::<ohm>(10_000.0),
b: 3800.0,
b: TemperatureInterval::new::<kelvin_interval>(3800.0),
}
}
}