]> www.fi.muni.cz Git - openparking.git/commitdiff
measurements: running avg
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 21:13:25 +0000 (23:13 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 22 May 2015 21:14:23 +0000 (23:14 +0200)
firmware/firmware.c

index f77e67a570377132a7bcaf815c207be5ed3dad05..c9c4317db1de3d1571498dde1ee45f975488aca4 100644 (file)
@@ -7,6 +7,7 @@
 
 #define ECHO_TIMEOUT           (CLOCK_HZ/20)   // 50 ms
 #define MEASUREMENT_WAIT       (4*ECHO_TIMEOUT)
+#define MEASUREMENT_SHIFT      2               // running avg (1 << M_SHIFT)
 
 #define N_TRIGGERS 3
 #define N_SENSORS 12
@@ -70,10 +71,23 @@ static void do_measurement(unsigned char trig)
                                to_start &= ~mask;
                                to_measure |= mask;
                        } else if ((to_measure & mask) && !(bits & mask)) {
+                               uint16_t old_d, new_d;
+                               uint8_t idx = trig*N_TRIG_SENSORS+i;
                                // echo end
                                to_measure &= ~mask;
-                               distances[trig*N_TRIG_SENSORS + i]
-                                       = now - starttimes[i];
+                               new_d = now - starttimes[i];
+                               old_d = distances[idx];
+
+                               if (old_d == 0
+                                       || old_d == -1) {
+                                       distances[idx] = new_d;
+                               } else {
+                                       distances[idx] = (
+                                               (old_d << MEASUREMENT_SHIFT)
+                                               + new_d
+                                               - old_d
+                                               ) >> MEASUREMENT_SHIFT;
+                               }
                        }
                }
        }