thermostat/src/channel_state.rs

67 lines
1.8 KiB
Rust
Raw Normal View History

use smoltcp::time::Instant;
2020-09-14 04:52:20 +08:00
use uom::si::{
2020-09-17 00:40:07 +08:00
f64::{
ElectricPotential,
ElectricalResistance,
ThermodynamicTemperature,
},
2020-09-14 04:52:20 +08:00
electric_potential::volt,
2020-09-17 00:40:07 +08:00
electrical_resistance::ohm,
temperature_interval::kelvin,
2020-09-14 04:52:20 +08:00
};
2020-05-17 07:23:35 +08:00
use crate::{
ad7172,
pid,
steinhart_hart as sh,
};
pub struct ChannelState {
2020-03-20 06:39:06 +08:00
pub adc_data: Option<u32>,
pub adc_calibration: ad7172::ChannelCalibration,
pub adc_time: Instant,
2020-09-14 04:52:20 +08:00
pub dac_value: ElectricPotential,
2020-05-13 06:04:55 +08:00
pub pid_engaged: bool,
pub pid: pid::Controller,
pub sh: sh::Parameters,
}
impl ChannelState {
pub fn new(adc_calibration: ad7172::ChannelCalibration) -> Self {
ChannelState {
adc_data: None,
adc_calibration,
adc_time: Instant::from_secs(0),
2020-09-14 04:52:20 +08:00
dac_value: ElectricPotential::new::<volt>(0.0),
2020-05-13 06:04:55 +08:00
pid_engaged: false,
pid: pid::Controller::new(pid::Parameters::default()),
sh: sh::Parameters::default(),
}
}
pub fn update(&mut self, now: Instant, adc_data: u32) {
self.adc_data = Some(adc_data);
self.adc_time = now;
}
/// Update PID state on ADC input, calculate new DAC output
2020-09-17 00:40:07 +08:00
pub fn update_pid(&mut self) {
// Update PID controller
2020-09-17 00:40:07 +08:00
// self.pid.update(self.get_temperature().unwrap().get::<kelvin>())
// TODO: add output field
}
pub fn get_adc(&self) -> Option<ElectricPotential> {
let volts = self.adc_calibration.convert_data(self.adc_data?);
Some(ElectricPotential::new::<volt>(volts))
}
2020-09-17 00:40:07 +08:00
pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
let r = self.get_adc()?.get::<volt>();
2020-09-17 00:40:07 +08:00
// TODO:
let r = ElectricalResistance::new::<ohm>(r);
let temperature = self.sh.get_temperature(r);
Some(temperature)
}
}