optimize electrometer averaging

This commit is contained in:
Sebastien Bourdeauducq 2017-08-07 00:24:41 +08:00
parent cccd6e52f6
commit 9a4adb267d
1 changed files with 7 additions and 9 deletions

View File

@ -4,7 +4,7 @@ pub struct Electrometer {
range: board::ElectrometerRange,
out_of_range_count: u8,
ignore_count: u8,
ic_buffer: [f32; 512],
ic_buffer: f32,
ic_buffer_count: usize,
last_ic: Option<f32>
}
@ -20,7 +20,7 @@ impl Electrometer {
range: board::ElectrometerRange::Med,
out_of_range_count: 0,
ignore_count: 0,
ic_buffer: [0.0; 512],
ic_buffer: 0.0,
ic_buffer_count: 0,
last_ic: None
}
@ -57,6 +57,7 @@ impl Electrometer {
if new_range.is_some() {
self.ignore_count = 150;
self.ic_buffer = 0.0;
self.ic_buffer_count = 0;
self.last_ic = None;
self.range = new_range.unwrap();
@ -67,14 +68,11 @@ impl Electrometer {
board::ElectrometerRange::Med => board::IC_ADC_GAIN_MED,
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;
if self.ic_buffer_count == self.ic_buffer.len() {
let mut ic_avg: f32 = 0.0;
for ic in self.ic_buffer.iter() {
ic_avg += *ic;
}
self.last_ic = Some(ic_avg/(self.ic_buffer.len() as f32));
if self.ic_buffer_count == 512 {
self.last_ic = Some(self.ic_buffer/512.0);
self.ic_buffer = 0.0;
self.ic_buffer_count = 0;
}
}