car using PID from centre line

Dependencies:   FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q

Fork of KL25Z_Camera_Test by GDP 4

Revision:
22:973b95478663
Parent:
21:0b69fada7c5f
Child:
26:f3d770f3eda1
diff -r 0b69fada7c5f -r 973b95478663 main.cpp
--- a/main.cpp	Fri Dec 09 13:19:28 2016 +0000
+++ b/main.cpp	Mon Dec 12 09:50:39 2016 +0000
@@ -65,7 +65,7 @@
             handleStartStop();
             
             // Send the line scan image over serial
-            //sendImage();
+            sendImage();
             
             
             //Reset image ready flag
@@ -316,12 +316,13 @@
 }
 
 inline void handleStartStop() {
+    
+    //v1:
     //Hacky way to detect the start/stop signal
     if(right - left < 60) {
-        sendString("START STOP!!");//do you mean %d? - YES!!!!
-    
-        //lapTime(); 
-        //testSpeed(speed) HOLY SHIT ITS DAT BOI!!!!!!!!
+        sendString("START STOP!!");
+        //lapTime();
+        //testSpeed(speed)
         /*if(seen) {
             seen = false;
         } else {
@@ -329,12 +330,73 @@
             seen = true;    
         }    
 
-        if(startstop >= 1) { */
-            TFC_SetMotorPWM(0.f,0.f);
-            TFC_HBRIDGE_DISABLE;
-            startstop = 0;      
+        if(startstop >= 1) {
+            */
+        TFC_SetMotorPWM(0.f,0.f);
+        TFC_HBRIDGE_DISABLE;
+        startstop = 0;
+    }
         
-    }    
+    //----------------------------START/STOP v2-----------------------------
+    //New method plan:
+    //Start at the centre of the image
+    //Look for 2 transitions (B->W OR W->B) - ignoring whether the centre pixel was black or white
+    //this should efficiently detect whether the marker is visible, and shouldn't give too many false positives
+    //May need to fiddle with the track width initial check, or disbale it entirely.
+    //NB: May want to incorporate this into findCentreValue(), it looks like they will be operating in similar ways on the exact same data...
+    /*
+    uint8_t lastPixel, currentPixel;
+    lastPixel = -1; //or 0?
+    bool startStopLeft = false;
+    bool startStopRight = false
+    //So:
+    //1. Starting at the middle, step left, looking for 2 transitions
+    for(int i = 63; i > 0; i--) {
+        currentPixel = (uint8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF; //Cast to signed or unsigned? Edge detection code has signed, but it puts it in an unsigned variable...
+        if((lastPixel - currentPixel) > 10) { //1st transition
+            for (int j = i; j > 0; j--) { //Keep going until 2nd transition
+                if((lastPixel - currentPixel) > 10) { //2nd transition
+                    //Set flag that 2 transitions on the left side are identified
+                    startStopLeft = true;
+                    //goto finishLeft;
+                }
+            }
+            
+        }
+        startStopLeft = false;
+        lastPixel = currentPixel;
+    }
+    //finishLeft:
+    */
+    
+    //v2.5:
+    /*
+    int slower = 0;
+    uint8_t difference = 0;
+    uint8_t lastPixel, currentPixel, transitionsSeen;
+    lastPixel = -1;
+    transitionsSeen = 0;
+    //Starting  near the left edge, step right, counting transitions. If there are 4, it is the marker (what about 3?)
+    for(int i = 30; i < 98; i++) {
+        currentPixel = (int8_t)(CLOSE_CAMERA[i] >> 4) & 0xFF;
+        difference = lastPixel - currentPixel;
+        if(abs(difference) > 150 && lastPixel != -1){ //transition seen, increment counter
+            transitionsSeen++;
+        }
+        lastPixel = currentPixel;
+    }
+    if (slower%100 == 0) {
+        sendString("Transitions seen: %d", transitionsSeen);
+    }
+    //if(transitionsSeen >= 3) {
+        //Stop the car!
+        //sendString("Start/stop seen");
+        //TFC_SetMotorPWM(0.f,0.f);
+        //TFC_HBRIDGE_DISABLE;
+    //}
+    transitionsSeen = 0;
+    slower++;
+    */
 }