1
0
Fork 0

Thermostat: Add pid_ctrl parms & its related fns

- setup: include the pid_ctrl
- poll_adc: Fetch Temperature and Update PID to set the current output
This commit is contained in:
linuswck 2024-01-17 16:06:07 +08:00
parent d0f226ce03
commit ca110962f7
2 changed files with 56 additions and 7 deletions

View File

@ -41,7 +41,7 @@ pub fn bootup(
sys_timer::setup(core_perif.SYST, clocks);
let (_eth_pins, usb, current_source_phy, _ad7172_phy, max1968_phy) = gpio::setup(
let (_eth_pins, usb, current_source_phy, ad7172_phy, max1968_phy) = gpio::setup(
clocks,
perif.TIM4,
perif.GPIOA,
@ -73,7 +73,7 @@ pub fn bootup(
let tec_driver = MAX1968::new(max1968_phy, perif.ADC1);
let mut thermostat = Thermostat::new(tec_driver);
let mut thermostat = Thermostat::new(tec_driver, ad7172_phy);
thermostat.setup();
thermostat.power_up();

View File

@ -1,13 +1,16 @@
use core::marker::PhantomData;
use smoltcp::time::Instant;
use crate::sys_timer;
use crate::thermostat::ad5680;
use crate::thermostat::max1968::{MAX1968, AdcReadTarget, PwmPinsEnum};
use crate::thermostat::ad7172;
use crate::thermostat::channel_state::ChannelState;
use log::info;
use uom::si::{
electric_current::ampere,
electric_potential::volt,
electrical_resistance::ohm,
f64::{ElectricCurrent, ElectricPotential, ElectricalResistance},
f64::{ElectricCurrent, ElectricPotential, ElectricalResistance, Time},
ratio::ratio,
};
use miniconf::Miniconf;
@ -88,18 +91,27 @@ impl Default for Settings {
pub struct Thermostat {
max1968: MAX1968,
ad7172: ad7172::AdcPhy,
pub tec_setting: Settings,
// TADC
pid_ctrl_ch0: ChannelState,
}
impl Thermostat{
pub fn new (max1968: MAX1968) -> Self {
pub fn new (max1968: MAX1968, ad7172: ad7172::AdcPhy) -> Self {
Thermostat{
max1968: max1968,
ad7172: ad7172,
tec_setting: Settings::default(),
pid_ctrl_ch0: ChannelState::default(),
}
}
pub fn setup(&mut self){
self.tec_setup();
let t_adc_ch0_cal = self.t_adc_setup();
self.pid_ctrl_ch0.adc_calibration = t_adc_ch0_cal;
}
fn tec_setup(&mut self) {
self.power_down();
self.tec_setting = Settings::default();
@ -111,6 +123,36 @@ impl Thermostat{
self.set_max_i_neg(self.tec_setting.max_i_neg_set);
}
fn t_adc_setup(&mut self)->ad7172::ChannelCalibration{
self.ad7172.set_sync_enable(false).unwrap();
self.ad7172.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
let adc_calibration0 = self.ad7172.get_calibration(0)
.expect("adc_calibration0");
self.ad7172.start_continuous_conversion().unwrap();
adc_calibration0
}
pub fn poll_adc(&mut self, instant: Instant) -> Option<u8> {
self.ad7172.data_ready().unwrap().map(|channel| {
let data = self.ad7172.read_data().unwrap();
let state: &mut ChannelState = &mut self.pid_ctrl_ch0;
state.update(instant, data);
match state.update_pid() {
Some(pid_output) if state.pid_engaged => {
// Forward PID output to i_set DAC
self.set_i(ElectricCurrent::new::<ampere>(pid_output));
self.power_up();
}
None if state.pid_engaged => {
self.power_down();
}
_ => {}
}
channel
})
}
pub fn power_up(&mut self){
self.max1968.power_up();
}
@ -216,4 +258,11 @@ impl Thermostat{
info!("Best Error: {:?}", best_error);
}
pub fn pid_engaged(&mut self) -> bool {
if self.pid_ctrl_ch0.pid_engaged {
return true;
}
false
}
}