Moved to Team 9.

Fork of LineScan by Nicholas Gan

Committer:
ikrase
Date:
Fri Mar 13 08:18:05 2015 +0000
Revision:
1:f10ec868cd71
Parent:
0:2d546112b0b8
Child:
2:6a87b2348245
Child:
4:91e7f2c8999f
checkpoint 7: benchtop tracking working

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 0:2d546112b0b8 8 uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
ng3600 0:2d546112b0b8 9 uint16_t pixel;
ng3600 0:2d546112b0b8 10
ng3600 0:2d546112b0b8 11 //read pixel n
ng3600 0:2d546112b0b8 12 pixel = cam.read_u16();
ng3600 0:2d546112b0b8 13
ng3600 0:2d546112b0b8 14 //clock pulse for next pixel n + 1
ng3600 0:2d546112b0b8 15 *camClk = 1;
ikrase 1:f10ec868cd71 16 *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 17
ng3600 0:2d546112b0b8 18 return pixel; //return result as an uint16_t (16 bit integer)
ng3600 0:2d546112b0b8 19 }
ng3600 0:2d546112b0b8 20
ng3600 0:2d546112b0b8 21 void startRead(DigitalOut *camSi, DigitalOut *camClk){
ng3600 0:2d546112b0b8 22 //pulse first clock cycle and start integral pin
ng3600 0:2d546112b0b8 23 *camSi = 1;
ng3600 0:2d546112b0b8 24 *camClk = 1;
ng3600 0:2d546112b0b8 25 *camSi = 0;
ng3600 0:2d546112b0b8 26 *camClk = 0;
ng3600 0:2d546112b0b8 27 }
ng3600 0:2d546112b0b8 28
ng3600 0:2d546112b0b8 29 int processFn(uint16_t *array, int arraySz){
ng3600 0:2d546112b0b8 30 int lineCtr = 0;
ng3600 0:2d546112b0b8 31
ng3600 0:2d546112b0b8 32 //replace with cross correlation function or averaging function
ng3600 0:2d546112b0b8 33 //currently returns index of smallest item
ng3600 0:2d546112b0b8 34 //skip 15 indices on both ends as they are noisy
ng3600 0:2d546112b0b8 35 for(int i = 16; i < arraySz - 15; i++){
ng3600 0:2d546112b0b8 36 if(array[i] < array[lineCtr])
ng3600 0:2d546112b0b8 37 lineCtr = i;
ng3600 0:2d546112b0b8 38 }
ng3600 0:2d546112b0b8 39
ng3600 0:2d546112b0b8 40 return lineCtr;
ng3600 0:2d546112b0b8 41 }
ng3600 0:2d546112b0b8 42
ng3600 0:2d546112b0b8 43 //call after integration time is done, returns index of array line is expected to be at
ng3600 0:2d546112b0b8 44 int getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk){
ikrase 1:f10ec868cd71 45 uint16_t lineAry[NUM_PIX]; // It might be nice to make this static and add double buffering and export of pointers like I had.
ng3600 0:2d546112b0b8 46 int position;
ng3600 0:2d546112b0b8 47
ng3600 0:2d546112b0b8 48 //read
ng3600 0:2d546112b0b8 49 startRead(camSi, camClk);
ng3600 0:2d546112b0b8 50 for(int i = 0; i < NUM_PIX; i++){
ng3600 0:2d546112b0b8 51 lineAry[i] = read1Bit(cam, camClk);
ng3600 0:2d546112b0b8 52 }
ng3600 0:2d546112b0b8 53
ng3600 0:2d546112b0b8 54 //process line scan data
ng3600 0:2d546112b0b8 55 position = processFn(lineAry, NUM_PIX);
ng3600 0:2d546112b0b8 56
ng3600 0:2d546112b0b8 57 return position;
ng3600 0:2d546112b0b8 58 }
ng3600 0:2d546112b0b8 59
ng3600 0:2d546112b0b8 60 /*
ng3600 0:2d546112b0b8 61 sample call (handler thread):
ng3600 0:2d546112b0b8 62
ng3600 0:2d546112b0b8 63 while(1){
ng3600 0:2d546112b0b8 64 linePos = getLinePos(cameraIn1, si, clk); //volatile linePos, replace with steering angle and read 2 cameras?
ng3600 0:2d546112b0b8 65 Thread::wait(14); //sleep for 14 us
ng3600 0:2d546112b0b8 66 }
ng3600 0:2d546112b0b8 67 */