2020-12-18 22:40:05 +08:00
|
|
|
use smoltcp::time::{Duration, 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-12-18 22:40:05 +08:00
|
|
|
Time,
|
2020-09-17 00:40:07 +08:00
|
|
|
},
|
2020-09-14 04:52:20 +08:00
|
|
|
electric_potential::volt,
|
2020-09-17 00:40:07 +08:00
|
|
|
electrical_resistance::ohm,
|
2020-09-18 06:09:30 +08:00
|
|
|
thermodynamic_temperature::degree_celsius,
|
2020-12-18 22:40:05 +08:00
|
|
|
time::millisecond,
|
2020-09-14 04:52:20 +08:00
|
|
|
};
|
2020-05-17 07:23:35 +08:00
|
|
|
use crate::{
|
|
|
|
ad7172,
|
|
|
|
pid,
|
|
|
|
steinhart_hart as sh,
|
2020-09-24 04:30:04 +08:00
|
|
|
command_parser::CenterPoint,
|
2020-05-17 07:23:35 +08:00
|
|
|
};
|
2020-03-20 05:09:16 +08:00
|
|
|
|
2020-09-17 02:49:24 +08:00
|
|
|
const R_INNER: f64 = 2.0 * 5100.0;
|
2020-09-17 05:31:49 +08:00
|
|
|
const VREF_SENS: f64 = 3.3 / 2.0;
|
2020-03-20 05:09:16 +08:00
|
|
|
|
|
|
|
pub struct ChannelState {
|
2020-03-20 06:39:06 +08:00
|
|
|
pub adc_data: Option<u32>,
|
2020-09-14 05:15:48 +08:00
|
|
|
pub adc_calibration: ad7172::ChannelCalibration,
|
2020-03-20 05:09:16 +08:00
|
|
|
pub adc_time: Instant,
|
2020-12-18 22:40:05 +08:00
|
|
|
pub adc_interval: Duration,
|
2020-09-24 04:30:04 +08:00
|
|
|
/// i_set 0A center point
|
|
|
|
pub center: CenterPoint,
|
2020-09-14 04:52:20 +08:00
|
|
|
pub dac_value: ElectricPotential,
|
2020-05-13 06:04:55 +08:00
|
|
|
pub pid_engaged: bool,
|
2020-03-20 05:09:16 +08:00
|
|
|
pub pid: pid::Controller,
|
|
|
|
pub sh: sh::Parameters,
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:15:48 +08:00
|
|
|
impl ChannelState {
|
|
|
|
pub fn new(adc_calibration: ad7172::ChannelCalibration) -> Self {
|
2020-03-20 05:09:16 +08:00
|
|
|
ChannelState {
|
|
|
|
adc_data: None,
|
2020-09-14 05:15:48 +08:00
|
|
|
adc_calibration,
|
2020-03-20 05:09:16 +08:00
|
|
|
adc_time: Instant::from_secs(0),
|
2020-12-18 22:40:05 +08:00
|
|
|
// default: 10 Hz
|
|
|
|
adc_interval: Duration::from_millis(100),
|
2020-09-24 04:30:04 +08:00
|
|
|
center: CenterPoint::Vref,
|
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,
|
2020-03-20 05:09:16 +08:00
|
|
|
pid: pid::Controller::new(pid::Parameters::default()),
|
|
|
|
sh: sh::Parameters::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:15:48 +08:00
|
|
|
pub fn update(&mut self, now: Instant, adc_data: u32) {
|
2020-09-25 06:01:08 +08:00
|
|
|
self.adc_data = if adc_data == ad7172::MAX_VALUE {
|
|
|
|
// this means there is no thermistor plugged into the ADC.
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(adc_data)
|
|
|
|
};
|
2020-12-18 22:40:05 +08:00
|
|
|
self.adc_interval = now - self.adc_time;
|
2020-03-20 05:09:16 +08:00
|
|
|
self.adc_time = now;
|
2020-09-14 05:15:48 +08:00
|
|
|
}
|
2020-03-20 05:09:16 +08:00
|
|
|
|
2020-09-14 05:15:48 +08:00
|
|
|
/// Update PID state on ADC input, calculate new DAC output
|
2022-02-24 20:16:47 +08:00
|
|
|
pub fn update_pid(&mut self) -> Option<f64> {
|
2020-09-18 06:09:30 +08:00
|
|
|
let temperature = self.get_temperature()?
|
|
|
|
.get::<degree_celsius>();
|
2022-02-24 20:16:47 +08:00
|
|
|
let pid_output = self.pid.update(temperature);
|
2020-09-18 06:09:30 +08:00
|
|
|
Some(pid_output)
|
2020-09-14 05:15:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-18 22:40:05 +08:00
|
|
|
pub fn get_adc_time(&self) -> Time {
|
|
|
|
Time::new::<millisecond>(self.adc_time.total_millis() as f64)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_adc_interval(&self) -> Time {
|
|
|
|
Time::new::<millisecond>(self.adc_interval.total_millis() as f64)
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:15:48 +08:00
|
|
|
pub fn get_adc(&self) -> Option<ElectricPotential> {
|
2020-09-25 06:01:08 +08:00
|
|
|
Some(self.adc_calibration.convert_data(self.adc_data?))
|
2020-09-17 02:49:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get `SENS[01]` input resistance
|
|
|
|
pub fn get_sens(&self) -> Option<ElectricalResistance> {
|
|
|
|
let r_inner = ElectricalResistance::new::<ohm>(R_INNER);
|
2020-09-17 05:31:49 +08:00
|
|
|
let vref = ElectricPotential::new::<volt>(VREF_SENS);
|
2020-09-17 02:49:24 +08:00
|
|
|
let adc_input = self.get_adc()?;
|
2020-09-17 05:31:49 +08:00
|
|
|
let r = r_inner * adc_input / (vref - adc_input);
|
2020-09-17 02:49:24 +08:00
|
|
|
Some(r)
|
2020-09-14 05:15:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 00:40:07 +08:00
|
|
|
pub fn get_temperature(&self) -> Option<ThermodynamicTemperature> {
|
2020-09-17 02:49:24 +08:00
|
|
|
let r = self.get_sens()?;
|
2020-09-14 05:15:48 +08:00
|
|
|
let temperature = self.sh.get_temperature(r);
|
|
|
|
Some(temperature)
|
2020-03-20 05:09:16 +08:00
|
|
|
}
|
|
|
|
}
|