]> www.fi.muni.cz Git - openparking.git/commitdiff
Maximum distance is not an error
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 23 May 2015 15:28:06 +0000 (17:28 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 23 May 2015 15:50:27 +0000 (17:50 +0200)
Try to handle the too-distant object gracefully - treat them as if the
pulse ended at the ECHO_TIMEOUT time.

Now -1 means error (the echo pulse has not started), while too long
value is simply recorded as maximum value.

firmware/firmware.c

index 5400714f48b10645dc196673493eb88d7a321871..8e973510ae55d1577ecc28b2ec2b413d85d59f43 100644 (file)
@@ -49,6 +49,7 @@ static void do_measurement(unsigned char trig)
        uint16_t starttimes[N_TRIG_SENSORS], starttime;
        uint8_t to_start = (1 << N_TRIG_SENSORS) - 1;
        uint8_t to_measure = 0, i;
+       uint16_t now;
 
        pull_trigger(trig);
 
@@ -56,7 +57,7 @@ static void do_measurement(unsigned char trig)
 
        while (to_start || to_measure) {
                uint8_t bits = 0;
-               uint16_t now = get_clock();
+               now = get_clock();
 
                if (now-starttime >= ECHO_TIMEOUT)
                        break;
@@ -107,7 +108,12 @@ static void do_measurement(unsigned char trig)
                if (to_start & (1 << i))
                        distances[trig*N_TRIG_SENSORS + i] = -1;
                else if (to_measure & (1 << i))
-                       distances[trig*N_TRIG_SENSORS + i] = 0;
+                       /*
+                        * If the echo pulse is too long, do not treat it
+                        * as error, just count it as maximum length.
+                        */
+                       distances[trig*N_TRIG_SENSORS + i]
+                               = now - starttimes[i];
 }
 
 static void led_set(uint8_t led, uint8_t state)
@@ -179,7 +185,7 @@ static void eval_bitmaps()
                mask = 1 << i;
 
                if (thresholds[i]) {
-                       if (distances[i] == -1 || distances[i] == 0) {
+                       if (distances[i] == -1) {
                                err_b |= mask;
                        } else if (distances[i] > thresholds[i]) {
                                free_b |= mask;