Moved to Team 9.

Fork of LineScan by Nicholas Gan

Revision:
3:f31986cb68fd
Parent:
2:6a87b2348245
Child:
5:9c19face64a7
Child:
6:984ccef5ca19
diff -r 6a87b2348245 -r f31986cb68fd LineScan.cpp
--- a/LineScan.cpp	Thu Mar 19 19:13:34 2015 +0000
+++ b/LineScan.cpp	Mon Mar 30 22:07:14 2015 +0000
@@ -5,6 +5,9 @@
 */
 #include "LineScan.h"
 
+#define MEAN_REF 15000      //ideal difference we should see
+#define CAM_CTRL_GAIN 0.001 //should be small
+
 uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
     uint16_t pixel;
     
@@ -26,7 +29,7 @@
     *camClk = 0;
 }
 
-int processFn(uint16_t *array, int arraySz){
+int processFn(uint16_t *array, int arraySz, int* exposure){
     int avg[NUM_PIX];
     int diff[NUM_PIX];
     int highest = 0;
@@ -34,14 +37,17 @@
     int h_idx = NUM_PIX/2;
     int l_idx = NUM_PIX/2;
     
-    //Just finds line center, does not track crossings
+    //for AGC
+    float exposureChange;
+    
+    //Just finds line center, does not track crossings (needs this feature)
     if(array){
         avg[0] = array[0];
         diff[0] = 0;
         
         for(int i = 1; i < NUM_PIX; i++){
-            avg[i] = array[i - 1]/2 + array[i]/2;
-            diff[i] = avg[i - 1] - avg[i];
+            avg[i] = array[i - 1]/2 + array[i]/2;   //smoothing
+            diff[i] = avg[i - 1] - avg[i];          //differential
             
             if(diff[i] > highest){
                 highest = diff[i];
@@ -52,14 +58,20 @@
                 l_idx = i;
             }
         }
+        exposureChange = ((float)(MEAN_REF - highest)) * CAM_CTRL_GAIN;    //AGC, simple proportional controller
+        *exposure += (int)exposureChange;
+        if(*exposure <= 0)
+            *exposure = 1;
+        else if(*exposure > 40)
+            *exposure = 40;
     }
     
     return (h_idx + l_idx)/2;
 }
 
-//call after integration time is done, returns index of array line is expected to be at
-int getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk){
-    uint16_t lineAry[NUM_PIX];          // It might be nice to make this static and add double buffering and export of pointers like I had. 
+//call after integration time is done, returns index of array line is expected to be at and new exposure time
+int getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk, int *exposure){
+    uint16_t lineAry[NUM_PIX];
     int position;
     
     //read 
@@ -69,7 +81,7 @@
     }
     
     //process line scan data
-    position  = processFn(lineAry, NUM_PIX);
+    position  = processFn(lineAry, NUM_PIX, exposure);
     
     return position;
 }
@@ -78,7 +90,7 @@
 sample call (handler thread):
 
 while(1){
-   linePos  = getLinePos(cameraIn1, si, clk);   //volatile linePos, replace with steering angle and read 2 cameras?
-   Thread::wait(14);    //sleep for 14 us
+   linePos  = getLinePos(cameraIn1, si, clk, &exposureTime);   //volatile linePos, replace with steering angle and read 2 cameras?
+   Thread::wait(exposureTime);    //sleep for 14 us
 }
 */