De sensorcontroller van het TLS2 project.

Dependencies:   mbed

Revision:
1:c9fae063e6f3
Parent:
0:eea3cc9d2701
Child:
2:4c5952cf26d0
--- a/main.cpp	Tue Nov 08 18:49:33 2016 +0000
+++ b/main.cpp	Mon Nov 14 18:27:21 2016 +0000
@@ -13,6 +13,7 @@
         3. Moving Average van 'Hoofd'sensor
 */
 
+
 void i2c_request(){
     //To be written    
 }
@@ -24,8 +25,29 @@
 //Index 2: temperatuursenor waarde
 #define druksensor 0
 #define flowsensor 1
-#define tempsensor 2                        
-        
+#define tempsensor 2
+
+bool pressure_is_main = true; //Determine the most important sensor as in, on which value is the motor controller regulating
+bool smoothing = true; //Determine to activate the moving average         
+
+
+
+float calc_moving_average(float val, int samples = 10){
+    static float sample_arr[samples] = {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];
+    }
+    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() {
     //Pins
     AnalogIn drukSensor(A0);
@@ -47,13 +69,21 @@
     t_temp.start();
     while(1) {
         if(t_druk.read_ms() >= tick_ms_druksensor){
-            //Lees de druksensor uit
-            sensorVal[druksensor] = drukSensor.read();
+            //Lees de druksensor uit            
+            if(pressure_is_main == true && smoothing == true){
+                calc_moving_average(drukSensor.read());
+            } else {
+                sensorVal[druksensor] = drukSensor.read();   
+            }
             t_druk.reset(); 
         }
         if(t_flow.read_ms() >= tick_ms_flowsensor){
             //Lees de flowsensor uit
-            sensorVal[flowsensor] = flowSensor.read();
+            if(pressure_is_main == false && smoothing == true){
+                calc_moving_average(flowSensor.read());
+            } else {
+                sensorVal[flowsensor] = flowSensor.read();
+            }
             t_flow.reset();    
         }
         if(t_temp.read_ms() >= tick_ms_tempsensor){