NXP Group 13 / Mbed 2 deprecated Car3

Dependencies:   camera mbed tsi_sensor

Fork of Car2 by Zach Matthews

Revision:
24:6219b8ce421f
Parent:
23:6e1e142b7baf
Child:
25:74c12b0acf0c
--- a/main.cpp	Mon Apr 17 01:08:11 2017 +0000
+++ b/main.cpp	Mon Apr 17 23:37:02 2017 +0000
@@ -9,8 +9,8 @@
 #define MIN_SPEED 0.17  //.15 seems to be optimal
 #define MAX_SPEED 0.45  //.5
 #define TURN_TIME 0
-#define STRAIGHT_TIME 5
-#define START_FINISH_TIME 30
+#define STRAIGHT_TIME 20
+#define START_FINISH_TIME 60
 #define DEFAULT_THRESHOLD 65
 #define BLIND_LENGTH 30
 #define DIFF_RATIO 0.5
@@ -35,7 +35,6 @@
 Camera cam(PTE23, PTE21, PTB3);
 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
 int turnCounter = 0;
-int startFinishCounter = 0;
 int threshold = DEFAULT_THRESHOLD;
 float wheelPos = STRAIGHT;
 bool idle = true;
@@ -43,6 +42,12 @@
 int rightBlind = 0;
 float lastSlide;
 int numDarks = 0;
+int minLightGap = 1;
+int maxLightGap = 55;
+int minDarkBlock = 4;
+int maxDarkBlock = 40;
+int positionOffThreshold = 3;
+struct darkBlock *darkBlockHead;
 PwmOut led(LED_GREEN);
 PwmOut redLed(LED_RED);
 
@@ -116,19 +121,21 @@
     servo.pulsewidth(wheelPos);
 }
 
+struct darkBlock{
+    int position;
+    int length;
+    int TTL; //time to live
+    struct darkBlock *next;
+    struct darkBlock *prev;
+};
+
 /*
     Function: detectStartFinish
     Description: detects the mark on the track that represents the start/finish line and toggles RUNNING
 */
 void detectStartFinish(int frame[]){
-    bool lookForDark = false;
-    int darkBlockSize = 0;
-    int darkBlocks = 0;
-    int lightGapSize = 0;
-    int minLightGap = 1;
-    int maxLightGap = 55;
-    int minDarkBlock = 4;
-    int maxDarkBlock = 40;
+    
+    //idle override by touching the slider
     if(tsi.readPercentage() != lastSlide){
         idle = !idle;
         led = 0.0;
@@ -136,49 +143,16 @@
     }
     if(numDarks <= 15) return;
     for(int i = 0; i < 128; i++){
-        if(lookForDark){
-            if(frame[i] < threshold){
-                darkBlockSize++;
-            }
-            else{
-                if(darkBlockSize > minDarkBlock && darkBlockSize < maxDarkBlock){
-                    darkBlocks++;
-                    lookForDark = false;
-                    darkBlockSize = 0;
-                    lightGapSize = 1;
-                }
-                else{
-                    darkBlockSize = 0;
-                }
-            }
+        
+    }
+    
+    idle = !idle; //toggle idle
+        if(!idle){ 
+            led = 1.0 - led;
+            servo.pulsewidth(STRAIGHT);
+            wait(3);
         }
-        if(!lookForDark){
-            if(frame[i] > threshold){
-                lightGapSize++;
-            }
-            else{
-                if(lightGapSize > minLightGap && lightGapSize < maxLightGap){
-                    lookForDark = true;
-                    minLightGap = 20;
-                    lightGapSize = 0;
-                    darkBlockSize = 1;
-                }
-                else{
-                    lightGapSize = 0;
-                }
-            }
-        }
-    }
-        if(darkBlocks > 1){
-            idle = !idle; //toggle idle
-            startFinishCounter = 1;
-            if(!idle){ 
-                led = 1.0 - led;
-                servo.pulsewidth(STRAIGHT);
-                wait(5);
-            }
-            else led = 1.0;
-        }
+        else led = 1.0;
 }
 
 /*
@@ -218,7 +192,7 @@
         if(cam.imageData[i] < low) low = cam.imageData[i];
     }
 //    threshold = low + (high - low) * 0.35;        //(high + 2 * low) / 3;
-    threshold = (low + high) / 2;
+    threshold = (3 * low + high) / 4;
 }
 
 int main() {    
@@ -233,7 +207,7 @@
     idle = true;
     lastSlide = tsi.readPercentage();
     while(1){
-        wait_ms(5);
+        wait_ms(1);
         cam.capture();
         //display(cam.imageData);
         turnWheels(cam.imageData);