Moved to Team 9.

Fork of LineScan by Nicholas Gan

Committer:
ng3600
Date:
Fri Apr 03 00:27:53 2015 +0000
Revision:
9:5226617d2019
Parent:
8:b9ec2f3e12b6
Child:
10:6b6c3db74538
Child:
11:5e66d0531053
Minor tuning adjustments, added saturation point for AGC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ng3600 0:2d546112b0b8 1 /*
ng3600 0:2d546112b0b8 2 AnalogIn cam1(CAM1_IN);
ng3600 0:2d546112b0b8 3 DigitalOut camSi(CAM_SI);
ikrase 1:f10ec868cd71 4 DigitalOut camClk(CAM_CLK); // Definining camera pins. These are actually defined in Main...
ng3600 0:2d546112b0b8 5 */
ng3600 0:2d546112b0b8 6 #include "LineScan.h"
ng3600 0:2d546112b0b8 7
ng3600 8:b9ec2f3e12b6 8 #define THRESH 10000
ng3600 8:b9ec2f3e12b6 9
ng3600 8:b9ec2f3e12b6 10 #define MEAN_REF 30000 //ideal difference we should see
ng3600 9:5226617d2019 11 #define CAM_CTRL_GAIN 0.0005 //should be small
ng3600 3:f31986cb68fd 12
ng3600 0:2d546112b0b8 13 uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
ng3600 0:2d546112b0b8 14 uint16_t pixel;
ng3600 0:2d546112b0b8 15
ng3600 0:2d546112b0b8 16 //read pixel n
ng3600 0:2d546112b0b8 17 pixel = cam.read_u16();
ng3600 0:2d546112b0b8 18
ng3600 0:2d546112b0b8 19 //clock pulse for next pixel n + 1
ng3600 0:2d546112b0b8 20 *camClk = 1;
ikrase 1:f10ec868cd71 21 *camClk = 0; // Apparently there is no need for any delay. It may be desireable to flatten this, or perhaps the compiler does it.
ng3600 0:2d546112b0b8 22
ng3600 0:2d546112b0b8 23 return pixel; //return result as an uint16_t (16 bit integer)
ng3600 0:2d546112b0b8 24 }
ng3600 0:2d546112b0b8 25
ng3600 0:2d546112b0b8 26 void startRead(DigitalOut *camSi, DigitalOut *camClk){
ng3600 0:2d546112b0b8 27 //pulse first clock cycle and start integral pin
ng3600 0:2d546112b0b8 28 *camSi = 1;
ng3600 0:2d546112b0b8 29 *camClk = 1;
ng3600 0:2d546112b0b8 30 *camSi = 0;
ng3600 0:2d546112b0b8 31 *camClk = 0;
ng3600 0:2d546112b0b8 32 }
ng3600 0:2d546112b0b8 33
ng3600 8:b9ec2f3e12b6 34 float processFn(uint16_t *array, int arraySz, int* exposure){
ng3600 8:b9ec2f3e12b6 35 int frameLen = NUM_PIX - 2 * SKIP;
ng3600 8:b9ec2f3e12b6 36 int avg[frameLen];
ng3600 8:b9ec2f3e12b6 37 int diff[frameLen];
ng3600 2:6a87b2348245 38 int highest = 0;
ng3600 2:6a87b2348245 39 int lowest = 0;
ng3600 8:b9ec2f3e12b6 40 int h_idx = frameLen/2;
ng3600 8:b9ec2f3e12b6 41 int l_idx = frameLen/2;
ng3600 0:2d546112b0b8 42
ng3600 3:f31986cb68fd 43 //for AGC
ng3600 3:f31986cb68fd 44 float exposureChange;
ng3600 3:f31986cb68fd 45
ng3600 3:f31986cb68fd 46 //Just finds line center, does not track crossings (needs this feature)
ng3600 2:6a87b2348245 47 if(array){
ng3600 8:b9ec2f3e12b6 48 avg[0] = array[SKIP - 1]/2 + array[SKIP]/2;
ng3600 2:6a87b2348245 49 diff[0] = 0;
ng3600 2:6a87b2348245 50
ng3600 8:b9ec2f3e12b6 51 for(int i = 1; i < frameLen; i++){
ng3600 8:b9ec2f3e12b6 52 avg[i] = array[i - 1 + SKIP]/2 + array[i + SKIP]/2; //smoothing
ng3600 3:f31986cb68fd 53 diff[i] = avg[i - 1] - avg[i]; //differential
ng3600 2:6a87b2348245 54
ng3600 2:6a87b2348245 55 if(diff[i] > highest){
ng3600 2:6a87b2348245 56 highest = diff[i];
ng3600 2:6a87b2348245 57 h_idx = i;
ng3600 2:6a87b2348245 58 }
ng3600 2:6a87b2348245 59 else if(diff[i] < lowest){
ng3600 2:6a87b2348245 60 lowest = diff[i];
ng3600 2:6a87b2348245 61 l_idx = i;
ng3600 2:6a87b2348245 62 }
ng3600 2:6a87b2348245 63 }
ng3600 8:b9ec2f3e12b6 64 exposureChange = ((float)(MEAN_REF - highest + lowest)) * CAM_CTRL_GAIN; //AGC, simple proportional controller
ng3600 3:f31986cb68fd 65 *exposure += (int)exposureChange;
ng3600 9:5226617d2019 66 if(*exposure < 10)
ng3600 9:5226617d2019 67 *exposure = 10;
ng3600 9:5226617d2019 68 else if(*exposure > 30)
ng3600 9:5226617d2019 69 *exposure = 30;
ng3600 0:2d546112b0b8 70 }
ng3600 0:2d546112b0b8 71
ng3600 8:b9ec2f3e12b6 72 return ((float)(h_idx + l_idx)) / (2.0 * (float)frameLen); //0 is center
ng3600 0:2d546112b0b8 73 }
ng3600 0:2d546112b0b8 74
ng3600 3:f31986cb68fd 75 //call after integration time is done, returns index of array line is expected to be at and new exposure time
ng3600 8:b9ec2f3e12b6 76 float getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk, int *exposure){ // telemetry::NumericArray<uint16_t, 128> &tele_linescan){
ng3600 3:f31986cb68fd 77 uint16_t lineAry[NUM_PIX];
ng3600 8:b9ec2f3e12b6 78 float position;
ng3600 0:2d546112b0b8 79
ng3600 0:2d546112b0b8 80 //read
ng3600 0:2d546112b0b8 81 startRead(camSi, camClk);
ng3600 0:2d546112b0b8 82 for(int i = 0; i < NUM_PIX; i++){
ng3600 0:2d546112b0b8 83 lineAry[i] = read1Bit(cam, camClk);
ikrase 7:7d3b6edd783e 84 //tele_linescan[i] = lineAry[i];
ng3600 0:2d546112b0b8 85 }
ng3600 0:2d546112b0b8 86
ng3600 0:2d546112b0b8 87 //process line scan data
ng3600 3:f31986cb68fd 88 position = processFn(lineAry, NUM_PIX, exposure);
ng3600 0:2d546112b0b8 89
ng3600 0:2d546112b0b8 90 return position;
ng3600 0:2d546112b0b8 91 }
ng3600 0:2d546112b0b8 92
ng3600 0:2d546112b0b8 93 /*
ng3600 0:2d546112b0b8 94 sample call (handler thread):
ng3600 0:2d546112b0b8 95
ng3600 0:2d546112b0b8 96 while(1){
ng3600 3:f31986cb68fd 97 linePos = getLinePos(cameraIn1, si, clk, &exposureTime); //volatile linePos, replace with steering angle and read 2 cameras?
ng3600 3:f31986cb68fd 98 Thread::wait(exposureTime); //sleep for 14 us
ng3600 0:2d546112b0b8 99 }
ng3600 2:6a87b2348245 100 */