zach_thesholding

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_notifications_with_orig_mbed by Nicholas Kosarek

Files at this revision

API Documentation at this revision

Comitter:
znew711
Date:
Thu May 04 03:27:26 2017 +0000
Parent:
17:09ceae7cb00e
Commit message:
better noise tolerance

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 03 20:41:43 2017 +0000
+++ b/main.cpp	Thu May 04 03:27:26 2017 +0000
@@ -96,10 +96,10 @@
 #define QUEUE_SIZE 100
 
 #define Z_THRESHOLD 5000
-#define ACCEL_ONE_RESTING 5550
-#define ACCEL_TWO_RESTING 5135
-#define ACCEL_ONE_EPSILON 336
-#define ACCEL_TWO_EPSILON 369
+#define ACCEL_ONE_RESTING 5100
+#define ACCEL_TWO_RESTING 5500
+#define ACCEL_ONE_EPSILON 600
+#define ACCEL_TWO_EPSILON 600
  
 const static char     DEVICE_NAME[] = "LUMBERJACK_NANO";
 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
@@ -452,9 +452,12 @@
     pq.liveSamples = 0;
     
     uint8_t lastThreePackets[3][20];
+    uint8_t firstThreePackets[3][20];
+    uint8_t streakIndex = 0;
     uint8_t lastThreeIndex = 0;
     uint8_t underThresholdCount = 0;
-    bool inAKeyStroke = false;
+    uint8_t aboveThresholdCount = 0;
+    bool inAStreak = false;
     uint16_t counter = 0;
    
     while(1)
@@ -476,38 +479,82 @@
                               (uint8_t)(x2 >> 8), (uint8_t)x2, (uint8_t)(y2 >> 8), (uint8_t)y2, (uint8_t)(z2 >> 8), (uint8_t)z2,
                               (uint8_t)(counter >> 8), (uint8_t) counter, 0, 0, 0, 0, (uint8_t) triggerOne, (uint8_t) triggerTwo};
         //TODO: handle negative accels
-        if (z1 > ACCEL_ONE_RESTING + ACCEL_ONE_EPSILON || z1 < ACCEL_ONE_RESTING - ACCEL_ONE_EPSILON ||
-            z2 > ACCEL_TWO_RESTING + ACCEL_TWO_EPSILON || z2 < ACCEL_TWO_RESTING - ACCEL_TWO_EPSILON) {
+        if (triggerOne || triggerTwo) {
+            inAStreak = true;
             underThresholdCount = 0;
-            if (!inAKeyStroke) {
-                //start transmitting
-                inAKeyStroke = true;
+            if (streakIndex < 3) {
+                for (int i = 0; i < 20; i++) {
+                    firstThreePackets[streakIndex][i] = values[i];
+                }
+                streakIndex++;
+                aboveThresholdCount++;
+            } else if (streakIndex == 3) {
+                streakIndex++;
+                uint8_t temp[20];
                 for (int i = 0; i < 3; i++) {
-                    uint8_t temp[20];
                     for (int j = 0; j < 20; j++) {
                         temp[j] = lastThreePackets[(lastThreeIndex + i) % 3][j];
                     }
                     addToQueue(temp);
                 }
+                for (int i = 0; i < 3; i++) {
+                    for (int j = 0; j < 20; j++) {
+                        temp[j] = firstThreePackets[i][j];
+                    }
+                    addToQueue(temp);
+                }
                 addToQueue(values);
                 startTransmission();
             } else {
+                streakIndex++;
                 addToQueue(values);
             }
-        } else if (underThresholdCount < 3 && inAKeyStroke) {
-            underThresholdCount++;
-            addToQueue(values);
-            //todo
         } else {
-            for (int i = 0; i < 20; i++) {
-                lastThreePackets[lastThreeIndex][i] = values[i];
+            if (streakIndex < 3 && inAStreak) {
+                underThresholdCount += 1;
+                for (int i = 0; i < 20; i++) {
+                    firstThreePackets[streakIndex][i] = values[i];
+                }
+                streakIndex++;
+            } else if (streakIndex == 3) {
+                underThresholdCount++;
+                if (aboveThresholdCount > 1) {
+                    uint8_t temp[20];
+                    for (int i = 0; i < 3; i++) {
+                        for (int j = 0; j < 20; j++) {
+                            temp[j] = lastThreePackets[(lastThreeIndex + i) % 3][j];
+                        }
+                        addToQueue(temp);
+                    }
+                    for (int i = 0; i < 3; i++) {
+                        for (int j = 0; j < 20; j++) {
+                            temp[j] = firstThreePackets[i][j];
+                        }
+                        addToQueue(temp);
+                    }
+                    addToQueue(values);
+                    streakIndex++;
+                    startTransmission();
+                } else {
+                    streakIndex = 0;
+                    inAStreak = false;
+                    aboveThresholdCount = 0;
+                }
+            } else if (underThresholdCount < 3 && inAStreak) {
+                underThresholdCount++;
+                streakIndex++;
+                addToQueue(values);
+            } else {
+                inAStreak = false;
+                streakIndex = 0;
+                aboveThresholdCount = 0;
+                for (int i = 0; i < 20; i++) {
+                    lastThreePackets[lastThreeIndex][i] = values[i];
+                }
+                lastThreeIndex = (lastThreeIndex + 1) % 3;
             }
-            lastThreeIndex = (lastThreeIndex + 1) % 3;
-            inAKeyStroke = false;
         }
-        counter++;
- 
-        wait_ms(5);
+    counter++;
+    wait_ms(5);
     }
- 
 }
\ No newline at end of file