Moved to Team 9.

Fork of LineScan by Nicholas Gan

Revision:
13:c17cf029d899
Parent:
12:ce6d9f7dc76e
Child:
14:928254a609cb
Child:
21:68db1d0a5534
--- a/LineScan.cpp	Thu Apr 09 19:23:35 2015 +0000
+++ b/LineScan.cpp	Fri Apr 10 00:38:31 2015 +0000
@@ -5,9 +5,9 @@
 */
 #include "LineScan.h"
 
-#define THRESH 10000
+#define THRESH 5000
 
-#define MEAN_REF 30000      //ideal difference we should see
+#define MEAN_REF 20000      //ideal mean we should see
 #define CAM_CTRL_GAIN 0.0005 //should be small
 
 uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
@@ -32,11 +32,12 @@
 }
 
 float processFn(uint16_t *array, int arraySz, int* exposure){
-    int frameLen = NUM_PIX -  2 * SKIP;
+    const static int frameLen = NUM_PIX -  2 * SKIP;
     int avg[frameLen];
     int diff[frameLen];
     int highest = 0;
     int lowest = 0;
+    int total = 0;
     int h_idx = frameLen/2;
     int l_idx = frameLen/2;
     
@@ -44,14 +45,18 @@
     float exposureChange;
     
     //Just finds line center, does not track crossings (needs this feature)
-    if(array){
+    if(array){ 
         avg[0] = array[SKIP - 1]/2 + array[SKIP]/2;
         diff[0] = 0;
         
+        total += avg[0];    //AGC
+        
         for(int i = 1; i < frameLen; i++){
             avg[i] = array[i - 1 + SKIP]/2 + array[i + SKIP]/2;   //smoothing
             diff[i] = avg[i - 1] - avg[i];          //differential
             
+            total += avg[i];    //AGC
+            
             if(diff[i] > highest){
                 highest = diff[i];
                 h_idx = i;
@@ -61,15 +66,20 @@
                 l_idx = i;
             }
         }
-        exposureChange = ((float)(MEAN_REF - highest + lowest)) * CAM_CTRL_GAIN;    //AGC, simple proportional controller
+        //AGC, simple proportional controller
+        total = total / frameLen;
+        exposureChange = ((float)(MEAN_REF - total)) * CAM_CTRL_GAIN;    
         *exposure += (int)exposureChange;
         if(*exposure < 1)
             *exposure = 1;
         else if(*exposure > 30)
             *exposure = 30;
     }
-    
-    return ((float)(h_idx + l_idx)) / (2.0 * (float)frameLen);    //0.5 is center
+    //valid if white line on black and not blinded
+    if(h_idx > l_idx && (highest - lowest) > THRESH)
+        return ((float)(h_idx + l_idx)) / (2.0 * (float)frameLen);    //0.5 is center
+    else
+        return -1.0;  //invalid read
 }
 
 //call after integration time is done, returns index of array line is expected to be at and new exposure time