Richard Hoekstra
/
TLS2-Sensorcontroller
De sensorcontroller van het TLS2 project.
Diff: main.cpp
- Revision:
- 2:4c5952cf26d0
- Parent:
- 1:c9fae063e6f3
- Child:
- 3:8e50fc7b82b7
diff -r c9fae063e6f3 -r 4c5952cf26d0 main.cpp --- a/main.cpp Mon Nov 14 18:27:21 2016 +0000 +++ b/main.cpp Mon Nov 14 18:39:42 2016 +0000 @@ -31,21 +31,31 @@ bool smoothing = true; //Determine to activate the moving average - +//In: nieuwe waarde en hoeveel samples er worden gebruikt +//Out: moving average van x elementen +//Explanation: deze functie 'smooth' de waardes die binnenkomen van +// bijvoorbeeld de druksensor. Zoals een condensator, +// de smoothing factor kan worden ingesteld door de hoeveelheid +// samples in de moving average aan te passen. +// Met maximaal 100 samples. float calc_moving_average(float val, int samples = 10){ - static float sample_arr[samples] = {0}; //[0] is the newest + static float sample_arr[100] = {0}; //[0] is the newest float moving_average = 0; - //Put the new val into the sample_arr and push out the oldest one - for(int i=samples-1; i>0; i--){ - //[9]<-[8]<-[7] - sample_arr[i] = sample_arr[i-1]; + if(samples > 0 && samples < 100){ //Sanity check + //Put the new val into the sample_arr and push out the oldest one + for(int i=samples-1; i>0; i--){ + //[9]<-[8]<-[7] + sample_arr[i] = sample_arr[i-1]; + } + sample_arr[0] = val; + //Calculate the moving average + for(int i=0; i<samples; i++){ + moving_average += sample_arr[i]; + } + return moving_average/(float)samples; + } else { + return 3.1415926; //Improv error code } - sample_arr[0] = val; - //Calculate the moving average - for(int i=0; i<samples; i++){ - moving_average += sample_arr[i] - } - return moving_average/(float)samples; } int main() {