Average out reads instead of waiting for stability

Reading until the error falls into a certain tolerance might cause
bootlooping as the ADC values just might never settle. Average it out
instead.
This commit is contained in:
atse 2024-01-26 13:04:20 +08:00
parent 2591bff0e2
commit 2e9c1d1467

View File

@ -169,17 +169,6 @@ impl<'a> Channels<'a> {
}
}
pub fn read_dac_feedback_until_stable(&mut self, channel: usize, tolerance: ElectricPotential) -> ElectricPotential {
let mut prev = self.read_dac_feedback(channel);
loop {
let current = self.read_dac_feedback(channel);
if (current - prev).abs() < tolerance {
return current;
}
prev = current;
}
}
pub fn read_itec(&mut self, channel: usize) -> ElectricPotential {
match channel {
0 => {
@ -265,11 +254,14 @@ impl<'a> Channels<'a> {
/// thermostat.
pub fn calibrate_dac_value(&mut self, channel: usize) {
let samples = 50;
let target_voltage = {
let mut target_voltage = ElectricPotential::new::<volt>(0.0);
for _ in 0..samples {
target_voltage += self.read_vref(channel);
}
target_voltage /= samples as f64;
target_voltage
};
let mut start_value = 1;
let mut best_error = ElectricPotential::new::<volt>(100.0);
@ -287,7 +279,14 @@ impl<'a> Channels<'a> {
_ => unreachable!(),
}
let dac_feedback = self.read_dac_feedback_until_stable(channel, ElectricPotential::new::<volt>(0.001));
let dac_feedback = {
let mut dac_feedback = ElectricPotential::new::<volt>(0.0);
for _ in 0..samples {
dac_feedback += self.read_dac_feedback(channel);
}
dac_feedback /= samples as f64;
dac_feedback
};
let error = target_voltage - dac_feedback;
if error < ElectricPotential::new::<volt>(0.0) {
break;