thermostat: split poll_adc & update_pid into 2 fns

This commit is contained in:
linuswck 2024-02-23 12:58:35 +08:00
parent c09ccc29cd
commit d1cc677668
2 changed files with 18 additions and 12 deletions

View File

@ -92,7 +92,8 @@ fn main() -> ! {
laser.poll_and_update_output_current(); laser.poll_and_update_output_current();
if thermostat.poll_adc_and_update_pid() { if thermostat.poll_adc() {
thermostat.update_pid();
if thermostat.get_temp_mon_status().over_temp_alarm { if thermostat.get_temp_mon_status().over_temp_alarm {
laser.power_down(); laser.power_down();
thermostat.set_pid_engaged(false); thermostat.set_pid_engaged(false);

View File

@ -151,31 +151,36 @@ impl Thermostat{
adc_calibration0 adc_calibration0
} }
pub fn poll_adc_and_update_pid(&mut self) -> bool { pub fn poll_adc(&mut self) -> bool {
let mut data_rdy = false; let mut data_rdy = false;
self.ad7172.data_ready().unwrap().map(|_ch| { self.ad7172.data_ready().unwrap().map(|_ch| {
let data = self.ad7172.read_data().unwrap(); let data = self.ad7172.read_data().unwrap();
let state: &mut PidState = &mut self.pid_ctrl_ch0; let state: &mut PidState = &mut self.pid_ctrl_ch0;
state.update(data); state.update(data);
debug!("state.get_pid_engaged(): {:?}", state.get_pid_engaged());
let pid_engaged = state.get_pid_engaged(); let pid_engaged = state.get_pid_engaged();
if pid_engaged {
match state.update_pid() {
Some(pid_output) => {
self.set_i(ElectricCurrent::new::<ampere>(pid_output));
debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::<degree_celsius>());
}
None => { }
}
}
let temp = self.get_temperature(); let temp = self.get_temperature();
self.temp_mon.update_status(pid_engaged, temp); self.temp_mon.update_status(pid_engaged, temp);
debug!("state.get_pid_engaged(): {:?}", pid_engaged);
debug!("Temperature: {:?} degree", temp.get::<degree_celsius>()); debug!("Temperature: {:?} degree", temp.get::<degree_celsius>());
data_rdy = true; data_rdy = true;
}); });
data_rdy data_rdy
} }
pub fn update_pid(&mut self) {
let state: &mut PidState = &mut self.pid_ctrl_ch0;
let pid_engaged = state.get_pid_engaged();
if pid_engaged {
match state.update_pid() {
Some(pid_output) => {
self.set_i(ElectricCurrent::new::<ampere>(pid_output));
debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::<degree_celsius>());
}
None => { }
}
}
}
pub fn get_temp_mon_status(&mut self) -> TempStatus { pub fn get_temp_mon_status(&mut self) -> TempStatus {
self.temp_mon.get_status() self.temp_mon.get_status()
} }