2020-03-20 05:09:16 +08:00
|
|
|
use smoltcp::time::Instant;
|
|
|
|
use crate::{ad5680, ad7172, pid, steinhart_hart as sh};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ChannelState {
|
2020-03-20 06:39:06 +08:00
|
|
|
pub adc_data: Option<u32>,
|
2020-03-20 05:09:16 +08:00
|
|
|
pub adc_time: Instant,
|
|
|
|
pub dac_value: u32,
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ChannelState {
|
|
|
|
fn default() -> Self {
|
|
|
|
ChannelState {
|
|
|
|
adc_data: None,
|
|
|
|
adc_time: Instant::from_secs(0),
|
|
|
|
dac_value: 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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelState {
|
|
|
|
/// Update PID state on ADC input, calculate new DAC output
|
2020-05-13 06:04:55 +08:00
|
|
|
pub fn update_pid(&mut self, now: Instant, adc_data: u32) {
|
2020-03-20 05:09:16 +08:00
|
|
|
self.adc_data = Some(adc_data);
|
|
|
|
self.adc_time = now;
|
|
|
|
|
|
|
|
// Update PID controller
|
|
|
|
let input = (adc_data as f64) / (ad7172::MAX_VALUE as f64);
|
|
|
|
let temperature = self.sh.get_temperature(input);
|
|
|
|
let output = self.pid.update(temperature);
|
|
|
|
self.dac_value = (output * (ad5680::MAX_VALUE as f64)) as u32;
|
|
|
|
}
|
|
|
|
}
|