Moved to Team 9.

Fork of LineScan by Nicholas Gan

Revision:
22:0136520fe249
Parent:
21:68db1d0a5534
Child:
23:a1a671d3c9e5
--- a/LineScan.cpp	Sat Apr 18 07:03:00 2015 +0000
+++ b/LineScan.cpp	Sat Apr 18 09:00:19 2015 +0000
@@ -8,6 +8,7 @@
 #define THRESH 5000
 
 #define MEAN_REF 20000      //ideal mean we should see
+#define BRIGHTEST_REF 40000
 #define CAM_CTRL_GAIN 0.0005 //should be small
 
 uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
@@ -42,6 +43,7 @@
     int l_idx = frameLen/2;
     int left_bound = SKIP;
     int right_bound = NUM_PIX - SKIP;
+    uint16_t brightest = 0;
     
     //for AGC
     float exposureChange;
@@ -50,14 +52,17 @@
     if(array){ 
         avg[0] = array[SKIP - 1]/2 + array[SKIP]/2;
         diff[0] = 0;
-        
+        brightest = avg[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 (avg[i] > brightest) {
+                total = avg[i];    //AGC
+                brightest = avg[i];
+            }
             
             if(diff[i] > highest){
                 highest = diff[i];
@@ -70,7 +75,7 @@
         }
         //AGC, simple proportional controller
         total = total / frameLen;
-        exposureChange = ((float)(MEAN_REF - total)) * CAM_CTRL_GAIN;    
+        exposureChange = ((float)(BRIGHTEST_REF - total)) * CAM_CTRL_GAIN;    
         *exposure += (int)exposureChange;
         if(*exposure < 1)
             *exposure = 1;
@@ -81,9 +86,9 @@
     if (highest > THRESH && -1 * lowest > THRESH)
         return ((float) h_idx + (float) l_idx) / (2*(float)frameLen);
     else if(highest > THRESH && -1 * lowest < THRESH )
-        return ((float) h_idx + left_bound) / (2*(float)frameLen);    //0.5 is center
+        return ((float) h_idx + (float)left_bound - (float)(right_bound - h_idx)) / (2*(float)frameLen);    //0.5 is center
     else if (-1*lowest > THRESH && highest < THRESH )
-        return ((float) l_idx + right_bound) / (2*(float)frameLen);
+        return ((float) l_idx + (float)right_bound + (float)(l_idx - left_bound)) / (2*(float)frameLen);
     else
         return -1.0;  //invalid read
 }