use ThermodynamicTemperature

This commit is contained in:
Astro 2020-09-16 18:40:07 +02:00
parent b1b6d1ea94
commit dda1f2f0b4
4 changed files with 51 additions and 17 deletions

View File

@ -1,7 +1,13 @@
use smoltcp::time::Instant; use smoltcp::time::Instant;
use uom::si::{ use uom::si::{
f64::ElectricPotential, f64::{
ElectricPotential,
ElectricalResistance,
ThermodynamicTemperature,
},
electric_potential::volt, electric_potential::volt,
electrical_resistance::ohm,
temperature_interval::kelvin,
}; };
use crate::{ use crate::{
ad7172, ad7172,
@ -39,9 +45,10 @@ impl ChannelState {
} }
/// Update PID state on ADC input, calculate new DAC output /// Update PID state on ADC input, calculate new DAC output
pub fn update_pid(&mut self) -> f64 { pub fn update_pid(&mut self) {
// Update PID controller // Update PID controller
self.pid.update(self.get_temperature().unwrap()) // self.pid.update(self.get_temperature().unwrap().get::<kelvin>())
// TODO: add output field
} }
pub fn get_adc(&self) -> Option<ElectricPotential> { pub fn get_adc(&self) -> Option<ElectricPotential> {
@ -49,8 +56,10 @@ impl ChannelState {
Some(ElectricPotential::new::<volt>(volts)) Some(ElectricPotential::new::<volt>(volts))
} }
pub fn get_temperature(&self) -> Option<f64> { pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
let r = self.get_adc()?.get::<volt>(); let r = self.get_adc()?.get::<volt>();
// TODO:
let r = ElectricalResistance::new::<ohm>(r);
let temperature = self.sh.get_temperature(r); let temperature = self.sh.get_temperature(r);
Some(temperature) Some(temperature)
} }

View File

@ -76,7 +76,8 @@ impl Channels {
}; };
if let Some(dac_value) = dac_value { if let Some(dac_value) = dac_value {
// Forward PID output to i_set DAC // Forward PID output to i_set DAC
self.set_dac(channel.into(), ElectricPotential::new::<volt>(dac_value)); // TODO:
// self.set_dac(channel.into(), ElectricPotential::new::<volt>(dac_value));
} }
channel channel

View File

@ -34,10 +34,12 @@ use uom::{
f64::{ f64::{
ElectricPotential, ElectricPotential,
ElectricalResistance, ElectricalResistance,
ThermodynamicTemperature,
}, },
electric_current::ampere, electric_current::ampere,
electric_potential::volt, electric_potential::volt,
electrical_resistance::ohm, electrical_resistance::ohm,
thermodynamic_temperature::degree_celsius,
}, },
}; };
@ -260,9 +262,19 @@ fn main() -> ! {
socket, "channel {}: Steinhart-Hart equation parameters", socket, "channel {}: Steinhart-Hart equation parameters",
channel, channel,
); );
let _ = writeln!(socket, "- t0={}", state.sh.t0); let _ = writeln!(socket, "- t0={}", state.sh.t0.into_format_args(degree_celsius, Abbreviation));
let _ = writeln!(socket, "- b={}", state.sh.b); let _ = writeln!(socket, "- b={}", state.sh.b);
let _ = writeln!(socket, "- r0={}", state.sh.r0); let _ = writeln!(socket, "- r0={}", state.sh.r0.into_format_args(ohm, Abbreviation));
match (state.get_adc(), state.get_temperature()) {
(Some(adc), Some(temp)) => {
let _ = writeln!(
socket, "- adc={} temp={:.3}K",
adc.into_format_args(volt, Abbreviation),
temp.into_format_args(degree_celsius, Abbreviation),
);
}
_ => {}
}
let _ = writeln!(socket, ""); let _ = writeln!(socket, "");
} }
} }
@ -361,9 +373,9 @@ fn main() -> ! {
let sh = &mut channels.channel_state(channel).sh; let sh = &mut channels.channel_state(channel).sh;
use command_parser::ShParameter::*; use command_parser::ShParameter::*;
match parameter { match parameter {
T0 => sh.t0 = value, T0 => sh.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
B => sh.b = value, B => sh.b = value,
R0 => sh.r0 = value, R0 => sh.r0 = ElectricalResistance::new::<ohm>(value),
} }
let _ = writeln!(socket, "Steinhart-Hart equation parameter updated"); let _ = writeln!(socket, "Steinhart-Hart equation parameter updated");
} }

View File

@ -1,11 +1,23 @@
use num_traits::float::Float; use num_traits::float::Float;
use uom::si::{
f64::{
ElectricalResistance,
ThermodynamicTemperature,
},
electrical_resistance::ohm,
ratio::ratio,
thermodynamic_temperature::{degree_celsius, kelvin},
};
/// Steinhart-Hart equation parameters /// Steinhart-Hart equation parameters
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Parameters { pub struct Parameters {
pub t0: f64, /// Base temperature
pub t0: ThermodynamicTemperature,
/// Base resistance
pub r0: ElectricalResistance,
/// Beta
pub b: f64, pub b: f64,
pub r0: f64,
} }
impl Parameters { impl Parameters {
@ -14,18 +26,18 @@ impl Parameters {
/// Result unit: Kelvin /// Result unit: Kelvin
/// ///
/// TODO: verify /// TODO: verify
pub fn get_temperature(&self, r: f64) -> f64 { pub fn get_temperature(&self, r: ElectricalResistance) -> ThermodynamicTemperature {
let inv_temp = 1.0 / self.t0 + (r / self.r0).ln() / self.b; let inv_temp = 1.0 / self.t0.get::<kelvin>() + (r / self.r0).get::<ratio>().ln() / self.b;
1.0 / inv_temp ThermodynamicTemperature::new::<kelvin>(1.0 / inv_temp)
} }
} }
impl Default for Parameters { impl Default for Parameters {
fn default() -> Self { fn default() -> Self {
Parameters { Parameters {
t0: 0.001_4, t0: ThermodynamicTemperature::new::<degree_celsius>(25.0),
b: 0.000_000_099, r0: ElectricalResistance::new::<ohm>(10_000.0),
r0: 5_110.0, b: 3800.0,
} }
} }
} }