forked from M-Labs/ionpak-thermostat
optimize electrometer averaging
This commit is contained in:
parent
cccd6e52f6
commit
9a4adb267d
|
@ -4,7 +4,7 @@ pub struct Electrometer {
|
||||||
range: board::ElectrometerRange,
|
range: board::ElectrometerRange,
|
||||||
out_of_range_count: u8,
|
out_of_range_count: u8,
|
||||||
ignore_count: u8,
|
ignore_count: u8,
|
||||||
ic_buffer: [f32; 512],
|
ic_buffer: f32,
|
||||||
ic_buffer_count: usize,
|
ic_buffer_count: usize,
|
||||||
last_ic: Option<f32>
|
last_ic: Option<f32>
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ impl Electrometer {
|
||||||
range: board::ElectrometerRange::Med,
|
range: board::ElectrometerRange::Med,
|
||||||
out_of_range_count: 0,
|
out_of_range_count: 0,
|
||||||
ignore_count: 0,
|
ignore_count: 0,
|
||||||
ic_buffer: [0.0; 512],
|
ic_buffer: 0.0,
|
||||||
ic_buffer_count: 0,
|
ic_buffer_count: 0,
|
||||||
last_ic: None
|
last_ic: None
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,7 @@ impl Electrometer {
|
||||||
|
|
||||||
if new_range.is_some() {
|
if new_range.is_some() {
|
||||||
self.ignore_count = 150;
|
self.ignore_count = 150;
|
||||||
|
self.ic_buffer = 0.0;
|
||||||
self.ic_buffer_count = 0;
|
self.ic_buffer_count = 0;
|
||||||
self.last_ic = None;
|
self.last_ic = None;
|
||||||
self.range = new_range.unwrap();
|
self.range = new_range.unwrap();
|
||||||
|
@ -67,14 +68,11 @@ impl Electrometer {
|
||||||
board::ElectrometerRange::Med => board::IC_ADC_GAIN_MED,
|
board::ElectrometerRange::Med => board::IC_ADC_GAIN_MED,
|
||||||
board::ElectrometerRange::High => board::IC_ADC_GAIN_HIGH
|
board::ElectrometerRange::High => board::IC_ADC_GAIN_HIGH
|
||||||
};
|
};
|
||||||
self.ic_buffer[self.ic_buffer_count] = ((ic_sample as f32) - board::IC_ADC_OFFSET)/gain;
|
self.ic_buffer += ((ic_sample as f32) - board::IC_ADC_OFFSET)/gain;
|
||||||
self.ic_buffer_count += 1;
|
self.ic_buffer_count += 1;
|
||||||
if self.ic_buffer_count == self.ic_buffer.len() {
|
if self.ic_buffer_count == 512 {
|
||||||
let mut ic_avg: f32 = 0.0;
|
self.last_ic = Some(self.ic_buffer/512.0);
|
||||||
for ic in self.ic_buffer.iter() {
|
self.ic_buffer = 0.0;
|
||||||
ic_avg += *ic;
|
|
||||||
}
|
|
||||||
self.last_ic = Some(ic_avg/(self.ic_buffer.len() as f32));
|
|
||||||
self.ic_buffer_count = 0;
|
self.ic_buffer_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue