finds track with line scan camera

Dependents:   aNXPCupCar

CAMERA.cpp

Committer:
pkolar1
Date:
2018-12-19
Revision:
0:7653b2a26797

File content as of revision 0:7653b2a26797:

#include "mbed.h"
#include "CAMERA.h"

CAMERA::CAMERA(PinName dig1,PinName dig2,PinName an):
    SI(dig1),CK(dig2),lux(an)
{
    init();
    t.attach(this, &CAMERA::protocol, 0.02);
}                   

void CAMERA::init()
{
    SI = 0;
    CK = 0;
    halfPulse = 0.0000005;  //0.5 microseconds
    carrot = 64;
    for(i=0; i < 128; i++) pixel[i] = 0;
}

void CAMERA::protocol()
{
    sum = 0;
    SI = 1;
    wait(halfPulse);
    CK = 1;
    wait(halfPulse);
    SI = 0;
    wait(halfPulse);
    pixel[0] = CAMERA::buffer();
    CK = 0;
    for(i=1; i<128; i++) {
        wait(halfPulse);
        wait(halfPulse);
        CK = 1;
        wait(halfPulse);
        wait(halfPulse);
        pixel[i] = CAMERA::buffer();
        if(i > 9 && i < 118) sum += 1/(float)pixel[i];
        CK = 0;

    }
    wait(halfPulse);
    wait(halfPulse);
    CK = 1;
    wait(halfPulse);
    wait(halfPulse);
    CK = 0;
    CAMERA::pathFinder();
}

int CAMERA::buffer()
{
    signal = lux;
    return signal*330;      //multiplied because of
}                           //simpler handling of data

void CAMERA::pathFinder()
{
    mean = 108 / sum;       //harmonic mean
    i = carrot - 1;
    while(mean < pixel[i] && i > 9) {
        xL = i;
        i--;                //iterations to left
    }                       //from the cursor
    i = carrot;
    while(mean < pixel[i] && i < 118) {
        xR = i;
        i++;                //iterations to right
    }                       //from the cursor.
    carrot = (xL + xR)/2;   //aritmetic mean
}                           //to position cursor

int CAMERA::getPath()
{
    return carrot;
}


/*void CAMERA::derivation()
{
    min1 = 12;
    min2 = 115;
    for(i=0; i<5; i++) temp[i] = 0;
    for(i=0; i<5; i++) x[i] = 0;
    pad = false;
    a = 0;
    max_temp = 0;
    for(i=12; i<64; i++) {
        if(pixel[i] < pixel[i-1]) {
            pad = true;
            temp[a]++;
        } else if(pad) {
            x[a] = i;
            a++;
            pad = false;
        }
    }
    for(a=0; a<5; a++) {
        if(temp[a] > max_temp) {
            max_temp = temp[a];
            min1 = x[a];
        }
    }
    for(i=0; i<5; i++) temp[i] = 0;
    for(i=0; i<5; i++) x[i] = 0;
    pad = false;
    a = 0;
    max_temp = 0;
    for(i=115; i>=64; i--) {
        if(pixel[i] < pixel[i+1]) {
            pad = true;
            temp[a]++;
        } else if(pad) {
            x[a] = i;
            a++;
            pad = false;
        }
    }
    for(a=0; a<5; a++) {
        if(temp[a] > max_temp) {
            max_temp = temp[a];
            min2 = x[a];
        }
    }
}*/