
Team Design project 3 main file
Dependencies: mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem
Fork of TDP_main by
sensor_measure.h
- Committer:
- Reckstyle
- Date:
- 2015-03-06
- Revision:
- 11:9e56d52485d1
- Parent:
- 9:718987b106a8
- Child:
- 12:bb21b76b6375
File content as of revision 11:9e56d52485d1:
/* 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 2 // number for the array of sensors #define NUMBER_SENSORS_SQUARE 0 //define pinout for all the sensors DigitalIn pin_right_right(PTD0); DigitalIn pin_right_centre(PTD2); DigitalIn pin_right_left(PTD2); DigitalIn pin_left_right(PTD3); DigitalIn pin_left_centre(PTD4); DigitalIn pin_left_left(PTD5); //InterruptIn in(PTD0); maybe more~? //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; sensor_data right_centre; // and so on.... sensor_data *sensorArray [NUMBER_SENSORS]; //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.pin = &pin_right_right; right_right.blackValue = 2013; right_right.whiteValue = 10000; right_right.grayValue = 0; // 0 for all the non-square sensors right_right.state = 0; //setting all sensors as black at begging sensorArray [arrayBuilder++] = &right_right; //Array goes from rightmost to left, then centre? right_centre.pin = &pin_right_centre; right_centre.blackValue = 6000; right_centre.whiteValue = 10000; right_centre.grayValue = 0; right_centre.state = 0; sensorArray [arrayBuilder++] = &right_centre; //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