Moved to Team 9.

Fork of LineScan by Nicholas Gan

LineScan.cpp

Committer:
ng3600
Date:
2015-04-03
Revision:
9:5226617d2019
Parent:
8:b9ec2f3e12b6
Child:
10:6b6c3db74538
Child:
11:5e66d0531053

File content as of revision 9:5226617d2019:

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

#define THRESH 10000

#define MEAN_REF 30000      //ideal difference we should see
#define CAM_CTRL_GAIN 0.0005 //should be small

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;
}

float processFn(uint16_t *array, int arraySz, int* exposure){
    int frameLen = NUM_PIX -  2 * SKIP;
    int avg[frameLen];
    int diff[frameLen];
    int highest = 0;
    int lowest = 0;
    int h_idx = frameLen/2;
    int l_idx = frameLen/2;
    
    //for AGC
    float exposureChange;
    
    //Just finds line center, does not track crossings (needs this feature)
    if(array){
        avg[0] = array[SKIP - 1]/2 + array[SKIP]/2;
        diff[0] = 0;
        
        for(int i = 1; i < frameLen; i++){
            avg[i] = array[i - 1 + SKIP]/2 + array[i + SKIP]/2;   //smoothing
            diff[i] = avg[i - 1] - avg[i];          //differential
            
            if(diff[i] > highest){
                highest = diff[i];
                h_idx = i;
            }
            else if(diff[i] < lowest){
                lowest = diff[i];
                l_idx = i;
            }
        }
        exposureChange = ((float)(MEAN_REF - highest + lowest)) * CAM_CTRL_GAIN;    //AGC, simple proportional controller
        *exposure += (int)exposureChange;
        if(*exposure < 10)
            *exposure = 10;
        else if(*exposure > 30)
            *exposure = 30;
    }
    
    return ((float)(h_idx + l_idx)) / (2.0 * (float)frameLen);    //0 is center
}

//call after integration time is done, returns index of array line is expected to be at and new exposure time
float getLinePos(AnalogIn cam, DigitalOut *camSi, DigitalOut *camClk, int *exposure){ // telemetry::NumericArray<uint16_t, 128> &tele_linescan){
    uint16_t lineAry[NUM_PIX];
    float position;
    
    //read 
    startRead(camSi, camClk);
    for(int i = 0; i < NUM_PIX; i++){
        lineAry[i] = read1Bit(cam, camClk);
        //tele_linescan[i] = lineAry[i];
    }
    
    //process line scan data
    position  = processFn(lineAry, NUM_PIX, exposure);
    
    return position;
}

/*
sample call (handler thread):

while(1){
   linePos  = getLinePos(cameraIn1, si, clk, &exposureTime);   //volatile linePos, replace with steering angle and read 2 cameras?
   Thread::wait(exposureTime);    //sleep for 14 us
}
*/