Lol smth

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main_BartFork by Yelfie

sensor_measure.h

Committer:
Reckstyle
Date:
2015-03-12
Revision:
15:6453cd351452
Parent:
12:bb21b76b6375
Child:
16:3649eb1a056d

File content as of revision 15:6453cd351452:

/*

Sensor measurement header file. 
Contains the pin declarations and variables for all sensors-related work

TESTED AND FUNCTIONAL!(24/02)
TESTED (/27/02)
NEEDS POPULATION FOR ALL THE SENSORS!

Need testing on implementation of the array of pointers
Need implementation of third value for gray and a way to distinguish between it - will big structures affect speed?
Need to revise struct sensors - are all values reallly needed???
LAST REVISED 27/02
*/

#ifndef _SENSOR_MEASURE_H
#define _SENSOR_MEASURE_H

#define NUMBER_SAMPLES 20 // NUMBER OF SAMPLES FOR SENSOR TESTING
#define NUMBER_SENSORS_REGULAR 8 // number for the array of sensors
#define NUMBER_SENSORS_SQUARE 0

//define pinout for all the sensors
DigitalIn pin_right_right_up(PTD0);
DigitalIn pin_right_left_up(PTD2);
DigitalIn pin_right_right_down(PTD2);
DigitalIn pin_right_left_down(PTD3);
DigitalIn pin_left_right_down(PTD4);
DigitalIn pin_left_left_down(PTD5);
//DigitalIn pin_left_right_up();
//DigitalIn pin_left_left_up();


//timer used by the sensor
Timer sensorTimer;


Serial pc(USBTX, USBRX); //used for connection to PC/debugging

//structure for sensors
typedef struct sensor_data {
    DigitalIn* pin; 
    int  blackValue;
    int  whiteValue;   //maybe it can be removed ???
    int  grayValue; // if sensor is used for squares will have a gray value;
    int  state;   //can be removed as well?
}sensor_data;


sensor_data right_right_up;
sensor_data right_left_up;
sensor_data right_right_down;
sensor_data right_left_down;
sensor_data left_right_down;
sensor_data left_left_down;
sensor_data left_right_up;
sensor_data left_left_up;
// and so on....

sensor_data *sensorArray [NUMBER_SENSORS_REGULAR]; //array just used for getting value out of the sensors, helps for iteration(see main program)

//Initialise values of all sensors
void sensor_initialise () {
    int arrayBuilder = 0; //integer solely used for populating the array
    //right_right
    right_right_up.pin = &pin_right_right_up;
    right_right_up.blackValue = 2013;
    right_right_up.whiteValue = 10000;
    right_right_up.grayValue = 0; // 0 for all the non-square sensors
    right_right_up.state = 0; //setting all sensors as black at begging
    sensorArray [arrayBuilder++] = &right_right_up; //Array goes from rightmost to left, then centre?
    right_left_up.pin = &pin_right_left_up;
    right_left_up.blackValue = 6000;
    right_left_up.whiteValue = 10000;
    right_left_up.grayValue = 0;
    right_left_up.state = 0;
    sensorArray [arrayBuilder++] = &right_left_up;
    //and contiune so on..
}


//measuring function - returning whether it is black or white line 
//"0" - black, "1" - white
int measure (sensor_data sensor){
    
    sensorTimer.reset(); //reset the timer
    double freq,period = 0.0;
    int n =0; //number of samples
    int sensor_old = 0; //variable to remember old sensor state
    int sensor_new  = 0;
    //double time_1 = sensorTimer.read();  used for debugging
    while (n < NUMBER_SAMPLES){
        sensor_new = sensor.pin->read();
        if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge 
            n++;
        }
        sensor_old = sensor_new;
    }
    double time_2 = sensorTimer.read();
   // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2);  //Used for debugging
   
    period = time_2/((double)NUMBER_SAMPLES); // Get time
    freq = (1/period);   // Convert period (in us) to frequency (Hz). 
    // pc.printf(" period is  %f  and freq is  %f ", period,freq); // Used for debugging
    
    if (sensor.grayValue != 0 && freq < (sensor.grayValue +  1000) && freq > (sensor.grayValue - 1000)) { //definitely not sure about that!
        //this is a gray value sensor in its gray region
        sensor.state = 2;
        return 2;
    }
    else if (freq < sensor.blackValue*2){
        sensor.state = 0;    //if it's less than black value it is black
        return 0;
    }
    else {
        sensor.state = 1;
    }    
        return 1;
   //(freq < sensor.black_value*2)? 1 : 0; //freq
}
#endif