Moved to Team 9.
Fork of LineScan by
LineScan.cpp@7:7d3b6edd783e, 2015-03-31 (annotated)
- Committer:
- ikrase
- Date:
- Tue Mar 31 23:34:18 2015 +0000
- Revision:
- 7:7d3b6edd783e
- Parent:
- 6:984ccef5ca19
- Child:
- 8:b9ec2f3e12b6
Test version with all telemetry commented out
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:f31986cb68fd | 8 | #define MEAN_REF 15000 //ideal difference we should see |
ng3600 | 3:f31986cb68fd | 9 | #define CAM_CTRL_GAIN 0.001 //should be small |
ng3600 | 3:f31986cb68fd | 10 | |
ng3600 | 0:2d546112b0b8 | 11 | uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){ |
ng3600 | 0:2d546112b0b8 | 12 | uint16_t pixel; |
ng3600 | 0:2d546112b0b8 | 13 | |
ng3600 | 0:2d546112b0b8 | 14 | //read pixel n |
ng3600 | 0:2d546112b0b8 | 15 | pixel = cam.read_u16(); |
ng3600 | 0:2d546112b0b8 | 16 | |
ng3600 | 0:2d546112b0b8 | 17 | //clock pulse for next pixel n + 1 |
ng3600 | 0:2d546112b0b8 | 18 | *camClk = 1; |
ikrase | 1:f10ec868cd71 | 19 | *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 | 20 | |
ng3600 | 0:2d546112b0b8 | 21 | return pixel; //return result as an uint16_t (16 bit integer) |
ng3600 | 0:2d546112b0b8 | 22 | } |
ng3600 | 0:2d546112b0b8 | 23 | |
ng3600 | 0:2d546112b0b8 | 24 | void startRead(DigitalOut *camSi, DigitalOut *camClk){ |
ng3600 | 0:2d546112b0b8 | 25 | //pulse first clock cycle and start integral pin |
ng3600 | 0:2d546112b0b8 | 26 | *camSi = 1; |
ng3600 | 0:2d546112b0b8 | 27 | *camClk = 1; |
ng3600 | 0:2d546112b0b8 | 28 | *camSi = 0; |
ng3600 | 0:2d546112b0b8 | 29 | *camClk = 0; |
ng3600 | 0:2d546112b0b8 | 30 | } |
ng3600 | 0:2d546112b0b8 | 31 | |
ng3600 | 3:f31986cb68fd | 32 | int processFn(uint16_t *array, int arraySz, int* exposure){ |
ng3600 | 2:6a87b2348245 | 33 | int avg[NUM_PIX]; |
ng3600 | 2:6a87b2348245 | 34 | int diff[NUM_PIX]; |
ng3600 | 2:6a87b2348245 | 35 | int highest = 0; |
ng3600 | 2:6a87b2348245 | 36 | int lowest = 0; |
ng3600 | 2:6a87b2348245 | 37 | int h_idx = NUM_PIX/2; |
ng3600 | 2:6a87b2348245 | 38 | int l_idx = NUM_PIX/2; |
ng3600 | 0:2d546112b0b8 | 39 | |
ng3600 | 3:f31986cb68fd | 40 | //for AGC |
ng3600 | 3:f31986cb68fd | 41 | float exposureChange; |
ng3600 | 3:f31986cb68fd | 42 | |
ng3600 | 3:f31986cb68fd | 43 | //Just finds line center, does not track crossings (needs this feature) |
ng3600 | 2:6a87b2348245 | 44 | if(array){ |
ng3600 | 2:6a87b2348245 | 45 | avg[0] = array[0]; |
ng3600 | 2:6a87b2348245 | 46 | diff[0] = 0; |
ng3600 | 2:6a87b2348245 | 47 | |
ng3600 | 2:6a87b2348245 | 48 | for(int i = 1; i < NUM_PIX; i++){ |
ng3600 | 3:f31986cb68fd | 49 | avg[i] = array[i - 1]/2 + array[i]/2; //smoothing |
ng3600 | 3:f31986cb68fd | 50 | diff[i] = avg[i - 1] - avg[i]; //differential |
ng3600 | 2:6a87b2348245 | 51 | |
ng3600 | 2:6a87b2348245 | 52 | if(diff[i] > highest){ |
ng3600 | 2:6a87b2348245 | 53 | highest = diff[i]; |
ng3600 | 2:6a87b2348245 | 54 | h_idx = i; |
ng3600 | 2:6a87b2348245 | 55 | } |
ng3600 | 2:6a87b2348245 | 56 | else if(diff[i] < lowest){ |
ng3600 | 2:6a87b2348245 | 57 | lowest = diff[i]; |
ng3600 | 2:6a87b2348245 | 58 | l_idx = i; |
ng3600 | 2:6a87b2348245 | 59 | } |
ng3600 | 2:6a87b2348245 | 60 | } |
ng3600 | 3:f31986cb68fd | 61 | exposureChange = ((float)(MEAN_REF - highest)) * CAM_CTRL_GAIN; //AGC, simple proportional controller |
ng3600 | 3:f31986cb68fd | 62 | *exposure += (int)exposureChange; |
ng3600 | 3:f31986cb68fd | 63 | if(*exposure <= 0) |
ng3600 | 3:f31986cb68fd | 64 | *exposure = 1; |
ng3600 | 3:f31986cb68fd | 65 | else if(*exposure > 40) |
ng3600 | 3:f31986cb68fd | 66 | *exposure = 40; |
ng3600 | 0:2d546112b0b8 | 67 | } |
ng3600 | 0:2d546112b0b8 | 68 | |
ng3600 | 2:6a87b2348245 | 69 | return (h_idx + l_idx)/2; |
ng3600 | 0:2d546112b0b8 | 70 | } |
ng3600 | 0:2d546112b0b8 | 71 | |
ng3600 | 3:f31986cb68fd | 72 | //call after integration time is done, returns index of array line is expected to be at and new exposure time |
ikrase | 7:7d3b6edd783e | 73 | int getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk, int *exposure){ // telemetry::NumericArray<uint16_t, 128> &tele_linescan){ |
ng3600 | 3:f31986cb68fd | 74 | uint16_t lineAry[NUM_PIX]; |
ng3600 | 0:2d546112b0b8 | 75 | int position; |
ng3600 | 0:2d546112b0b8 | 76 | |
ng3600 | 0:2d546112b0b8 | 77 | //read |
ng3600 | 0:2d546112b0b8 | 78 | startRead(camSi, camClk); |
ng3600 | 0:2d546112b0b8 | 79 | for(int i = 0; i < NUM_PIX; i++){ |
ng3600 | 0:2d546112b0b8 | 80 | lineAry[i] = read1Bit(cam, camClk); |
ikrase | 7:7d3b6edd783e | 81 | //tele_linescan[i] = lineAry[i]; |
ng3600 | 0:2d546112b0b8 | 82 | } |
ng3600 | 0:2d546112b0b8 | 83 | |
ng3600 | 0:2d546112b0b8 | 84 | //process line scan data |
ng3600 | 3:f31986cb68fd | 85 | position = processFn(lineAry, NUM_PIX, exposure); |
ng3600 | 0:2d546112b0b8 | 86 | |
ng3600 | 0:2d546112b0b8 | 87 | return position; |
ng3600 | 0:2d546112b0b8 | 88 | } |
ng3600 | 0:2d546112b0b8 | 89 | |
ng3600 | 0:2d546112b0b8 | 90 | /* |
ng3600 | 0:2d546112b0b8 | 91 | sample call (handler thread): |
ng3600 | 0:2d546112b0b8 | 92 | |
ng3600 | 0:2d546112b0b8 | 93 | while(1){ |
ng3600 | 3:f31986cb68fd | 94 | linePos = getLinePos(cameraIn1, si, clk, &exposureTime); //volatile linePos, replace with steering angle and read 2 cameras? |
ng3600 | 3:f31986cb68fd | 95 | Thread::wait(exposureTime); //sleep for 14 us |
ng3600 | 0:2d546112b0b8 | 96 | } |
ng3600 | 2:6a87b2348245 | 97 | */ |