Team Design project 3 main file

Dependencies:   mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem

Fork of TDP_main by Ivelin Kozarev

sensor_measure.h

Committer:
Reckstyle
Date:
2015-02-23
Revision:
9:718987b106a8
Parent:
1:eace997e9a93
Child:
11:9e56d52485d1

File content as of revision 9:718987b106a8:

/*

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

TESTED AND FUNCTIONAL!
NEEDS POPULATION FOR ALL THE SENSORS!
LAST REVISED 23/02
*/

#ifndef _SENSOR_MEASURE_H
#define _SENSOR_MEASURE_H

#define NUMBER_SAMPLES 40 // NUMBER OF SAMPLES FOR SENSOR TESTING

//define pinout for all the sensors
DigitalIn pin_right_right(PTD0);
InterruptIn pin_right_centre(PTD1);
InterruptIn pin_right_left(PTD2);
InterruptIn pin_left_right(PTD3);
InterruptIn pin_left_centre(PTD4);
InterruptIn pin_left_left(PTD5);
//InterruptIn in(PTD0); maybe more~?

//timer used by the sensor
Timer sensorTimer;



//structure for sensors
typedef struct sensor_data {
    DigitalIn* pin; 
    int  black_value;
    int  white_value;    
}sensor_data;


sensor_data right_right;
sensor_data right_centre;
// and so on....

//Initialise values of all sensors
void sensor_initialise () {
    //right_right
    right_right.pin = &pin_right_right;
    right_right.black_value = 2013;
    right_right.white_value = 10000;
    //and contiune so on..
}


//measuring function - returning whether it is black or white line 
//"1" - black, "0" - white
double 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  ", period); // Used for debugging
    
    return freq;//(freq < sensor.black_value*2)? 1 : 0;
}
#endif