Moved to Team 9.

Fork of LineScan by Nicholas Gan

LineScan.cpp

Committer:
ng3600
Date:
2015-03-19
Revision:
2:6a87b2348245
Parent:
1:f10ec868cd71
Child:
3:f31986cb68fd

File content as of revision 2:6a87b2348245:

/*
AnalogIn cam1(CAM1_IN);
DigitalOut camSi(CAM_SI);
DigitalOut camClk(CAM_CLK); // Definining camera pins. These are actually defined in Main... 
*/
#include "LineScan.h"

uint16_t read1Bit(AnalogIn cam, DigitalOut *camClk){
    uint16_t pixel;
    
    //read pixel n
    pixel = cam.read_u16();
    
    //clock pulse for next pixel n + 1
    *camClk = 1;
    *camClk = 0;    // Apparently there is no need for any delay. It may be desireable to flatten this, or perhaps the compiler does it. 
    
    return pixel;  //return result as an uint16_t (16 bit integer)
}

void startRead(DigitalOut *camSi, DigitalOut *camClk){
    //pulse first clock cycle and start integral pin
    *camSi = 1;
    *camClk = 1;
    *camSi = 0;
    *camClk = 0;
}

int processFn(uint16_t *array, int arraySz){
    int avg[NUM_PIX];
    int diff[NUM_PIX];
    int highest = 0;
    int lowest = 0;
    int h_idx = NUM_PIX/2;
    int l_idx = NUM_PIX/2;
    
    //Just finds line center, does not track crossings
    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];
            
            if(diff[i] > highest){
                highest = diff[i];
                h_idx = i;
            }
            else if(diff[i] < lowest){
                lowest = diff[i];
                l_idx = i;
            }
        }
    }
    
    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. 
    int position;
    
    //read 
    startRead(camSi, camClk);
    for(int i = 0; i < NUM_PIX; i++){
        lineAry[i] = read1Bit(cam, camClk);
    }
    
    //process line scan data
    position  = processFn(lineAry, NUM_PIX);
    
    return position;
}

/*
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
}
*/