Team Design project 3 main file

Dependencies:   mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem

Fork of TDP_main by Ivelin Kozarev

Committer:
Reckstyle
Date:
Mon Mar 23 16:42:27 2015 +0000
Revision:
30:307b45a52401
Parent:
23:9b53c72fcd38
Child:
32:d1dc27f4a18d
Added sensor values;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Reckstyle 0:5ca0450111f3 1 /*
Reckstyle 0:5ca0450111f3 2
Reckstyle 0:5ca0450111f3 3 Sensor measurement header file.
Reckstyle 0:5ca0450111f3 4 Contains the pin declarations and variables for all sensors-related work
Reckstyle 0:5ca0450111f3 5
Reckstyle 11:9e56d52485d1 6 TESTED AND FUNCTIONAL!(24/02)
Reckstyle 11:9e56d52485d1 7 TESTED (/27/02)
Reckstyle 9:718987b106a8 8 NEEDS POPULATION FOR ALL THE SENSORS!
Reckstyle 11:9e56d52485d1 9
Reckstyle 11:9e56d52485d1 10 Need testing on implementation of the array of pointers
Reckstyle 11:9e56d52485d1 11 Need implementation of third value for gray and a way to distinguish between it - will big structures affect speed?
Reckstyle 11:9e56d52485d1 12 Need to revise struct sensors - are all values reallly needed???
Reckstyle 11:9e56d52485d1 13 LAST REVISED 27/02
Reckstyle 0:5ca0450111f3 14 */
Reckstyle 0:5ca0450111f3 15
Reckstyle 0:5ca0450111f3 16 #ifndef _SENSOR_MEASURE_H
Reckstyle 0:5ca0450111f3 17 #define _SENSOR_MEASURE_H
Reckstyle 0:5ca0450111f3 18
Reckstyle 11:9e56d52485d1 19 #define NUMBER_SAMPLES 20 // NUMBER OF SAMPLES FOR SENSOR TESTING
Reckstyle 22:902c3086394e 20 #define NUMBER_SENSORS_REGULAR 8 // number for the array of sensors
Reckstyle 11:9e56d52485d1 21 #define NUMBER_SENSORS_SQUARE 0
Reckstyle 23:9b53c72fcd38 22 #define BLACK_THRESHOLD 2
Reckstyle 0:5ca0450111f3 23
Reckstyle 0:5ca0450111f3 24 //define pinout for all the sensors
Reckstyle 30:307b45a52401 25 DigitalIn pin_right_right_up(PTC2);
Reckstyle 30:307b45a52401 26 DigitalIn pin_right_left_up(PTE30);
Reckstyle 30:307b45a52401 27 DigitalIn pin_right_right_down(PTE29);
Reckstyle 30:307b45a52401 28 DigitalIn pin_right_left_down(PTC1);
Reckstyle 30:307b45a52401 29 DigitalIn pin_left_right_down(PTB3);
Reckstyle 22:902c3086394e 30 DigitalIn pin_left_left_down(PTE22); //pte22
Reckstyle 30:307b45a52401 31 DigitalIn pin_left_right_up(PTE23); //pte29
Reckstyle 30:307b45a52401 32 DigitalIn pin_left_left_up(PTB2);
Reckstyle 15:6453cd351452 33
Reckstyle 0:5ca0450111f3 34
Reckstyle 0:5ca0450111f3 35 //timer used by the sensor
Reckstyle 0:5ca0450111f3 36 Timer sensorTimer;
Reckstyle 0:5ca0450111f3 37
Reckstyle 9:718987b106a8 38
Reckstyle 11:9e56d52485d1 39 Serial pc(USBTX, USBRX); //used for connection to PC/debugging
Reckstyle 9:718987b106a8 40
Reckstyle 0:5ca0450111f3 41 //structure for sensors
Reckstyle 0:5ca0450111f3 42 typedef struct sensor_data {
Reckstyle 9:718987b106a8 43 DigitalIn* pin;
Reckstyle 11:9e56d52485d1 44 int blackValue;
Reckstyle 11:9e56d52485d1 45 int whiteValue; //maybe it can be removed ???
Reckstyle 11:9e56d52485d1 46 int grayValue; // if sensor is used for squares will have a gray value;
Reckstyle 11:9e56d52485d1 47 int state; //can be removed as well?
Reckstyle 0:5ca0450111f3 48 }sensor_data;
Reckstyle 0:5ca0450111f3 49
Reckstyle 9:718987b106a8 50
Reckstyle 15:6453cd351452 51 sensor_data right_right_up;
Reckstyle 15:6453cd351452 52 sensor_data right_left_up;
Reckstyle 15:6453cd351452 53 sensor_data right_right_down;
Reckstyle 15:6453cd351452 54 sensor_data right_left_down;
Reckstyle 15:6453cd351452 55 sensor_data left_right_down;
Reckstyle 15:6453cd351452 56 sensor_data left_left_down;
Reckstyle 15:6453cd351452 57 sensor_data left_right_up;
Reckstyle 15:6453cd351452 58 sensor_data left_left_up;
Reckstyle 1:eace997e9a93 59 // and so on....
Reckstyle 0:5ca0450111f3 60
orsharp 12:bb21b76b6375 61 sensor_data *sensorArray [NUMBER_SENSORS_REGULAR]; //array just used for getting value out of the sensors, helps for iteration(see main program)
Reckstyle 11:9e56d52485d1 62
Reckstyle 1:eace997e9a93 63 //Initialise values of all sensors
Reckstyle 1:eace997e9a93 64 void sensor_initialise () {
Reckstyle 11:9e56d52485d1 65 int arrayBuilder = 0; //integer solely used for populating the array
Reckstyle 1:eace997e9a93 66 //right_right
Reckstyle 15:6453cd351452 67 right_right_up.pin = &pin_right_right_up;
Reckstyle 30:307b45a52401 68 right_right_up.blackValue = 8500*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 69 right_right_up.whiteValue = 37000;
Reckstyle 15:6453cd351452 70 right_right_up.grayValue = 0; // 0 for all the non-square sensors
Reckstyle 15:6453cd351452 71 right_right_up.state = 0; //setting all sensors as black at begging
Reckstyle 15:6453cd351452 72 sensorArray [arrayBuilder++] = &right_right_up; //Array goes from rightmost to left, then centre?
Reckstyle 15:6453cd351452 73 right_left_up.pin = &pin_right_left_up;
Reckstyle 30:307b45a52401 74 right_left_up.blackValue = 10500*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 75 right_left_up.whiteValue = 37000;
Reckstyle 15:6453cd351452 76 right_left_up.grayValue = 0;
Reckstyle 17:de8b3111ddc5 77 right_left_up.state = 1;
Reckstyle 15:6453cd351452 78 sensorArray [arrayBuilder++] = &right_left_up;
Reckstyle 16:3649eb1a056d 79 right_right_down.pin = &pin_right_right_down;
Reckstyle 30:307b45a52401 80 right_right_down.blackValue = 1500*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 81 right_right_down.whiteValue = 7300;
Reckstyle 17:de8b3111ddc5 82 right_right_down.grayValue = 0;
Reckstyle 17:de8b3111ddc5 83 right_right_down.state = 0;
Reckstyle 16:3649eb1a056d 84 sensorArray [arrayBuilder++] = &right_right_down;
Reckstyle 16:3649eb1a056d 85 right_left_down.pin = &pin_right_left_down;
Reckstyle 30:307b45a52401 86 right_left_down.blackValue = 2500*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 87 right_left_down.whiteValue = 11000;
Reckstyle 17:de8b3111ddc5 88 right_left_down.grayValue = 0;
Reckstyle 17:de8b3111ddc5 89 right_left_down.state = 1;
Reckstyle 16:3649eb1a056d 90 sensorArray[arrayBuilder++] = &right_left_down;
Reckstyle 17:de8b3111ddc5 91 left_right_down.pin = &pin_left_right_down;
Reckstyle 30:307b45a52401 92 left_right_down.blackValue = 1800*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 93 left_right_down.whiteValue = 14000;
Reckstyle 17:de8b3111ddc5 94 left_right_down.grayValue = 0;
Reckstyle 17:de8b3111ddc5 95 left_right_down.state = 1;
Reckstyle 17:de8b3111ddc5 96 sensorArray [arrayBuilder++] = &left_right_down;
Reckstyle 17:de8b3111ddc5 97 left_left_down.pin = &pin_left_left_down;
Reckstyle 30:307b45a52401 98 left_left_down.blackValue = 4500*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 99 left_left_down.whiteValue = 18000;
Reckstyle 17:de8b3111ddc5 100 left_left_down.grayValue = 0;
Reckstyle 17:de8b3111ddc5 101 left_left_down.state = 0;
Reckstyle 17:de8b3111ddc5 102 sensorArray [arrayBuilder++] = &left_left_down;
Reckstyle 17:de8b3111ddc5 103 left_right_up.pin = &pin_left_right_up;
Reckstyle 30:307b45a52401 104 left_right_up.blackValue = 11500*BLACK_THRESHOLD; //??? it's a trick ;)
Reckstyle 30:307b45a52401 105 left_right_up.whiteValue = 63000;
Reckstyle 17:de8b3111ddc5 106 left_right_up.grayValue = 0;
Reckstyle 17:de8b3111ddc5 107 left_right_up.state = 1;
Reckstyle 17:de8b3111ddc5 108 sensorArray [arrayBuilder++] = &left_right_up;
Reckstyle 17:de8b3111ddc5 109 left_left_up.pin = &pin_left_left_up;
Reckstyle 30:307b45a52401 110 left_left_up.blackValue = 20000*BLACK_THRESHOLD;
Reckstyle 30:307b45a52401 111 left_left_up.whiteValue = 80000;
Reckstyle 17:de8b3111ddc5 112 left_left_up.grayValue = 0;
Reckstyle 17:de8b3111ddc5 113 left_left_up.state = 0;
Reckstyle 17:de8b3111ddc5 114 sensorArray [arrayBuilder++] = &left_left_up;
Reckstyle 1:eace997e9a93 115 }
Reckstyle 1:eace997e9a93 116
Reckstyle 16:3649eb1a056d 117 int counter1 = 0;
Reckstyle 0:5ca0450111f3 118 //measuring function - returning whether it is black or white line
Reckstyle 11:9e56d52485d1 119 //"0" - black, "1" - white
Reckstyle 16:3649eb1a056d 120 int measure (sensor_data *sensor){
Reckstyle 1:eace997e9a93 121
Reckstyle 9:718987b106a8 122 sensorTimer.reset(); //reset the timer
Reckstyle 9:718987b106a8 123 double freq,period = 0.0;
Reckstyle 0:5ca0450111f3 124 int n =0; //number of samples
Reckstyle 9:718987b106a8 125 int sensor_old = 0; //variable to remember old sensor state
Reckstyle 9:718987b106a8 126 int sensor_new = 0;
Reckstyle 9:718987b106a8 127 //double time_1 = sensorTimer.read(); used for debugging
Reckstyle 0:5ca0450111f3 128 while (n < NUMBER_SAMPLES){
Reckstyle 16:3649eb1a056d 129 sensor_new = sensor->pin->read();
Reckstyle 9:718987b106a8 130 if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge
Reckstyle 0:5ca0450111f3 131 n++;
Reckstyle 0:5ca0450111f3 132 }
Reckstyle 9:718987b106a8 133 sensor_old = sensor_new;
Reckstyle 0:5ca0450111f3 134 }
Reckstyle 9:718987b106a8 135 double time_2 = sensorTimer.read();
Reckstyle 9:718987b106a8 136 // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2); //Used for debugging
Reckstyle 9:718987b106a8 137
Reckstyle 9:718987b106a8 138 period = time_2/((double)NUMBER_SAMPLES); // Get time
Reckstyle 9:718987b106a8 139 freq = (1/period); // Convert period (in us) to frequency (Hz).
Reckstyle 16:3649eb1a056d 140 //pc.printf(" period is %f and freq is %f ", period,freq); // Used for debugging
Reckstyle 16:3649eb1a056d 141
Reckstyle 30:307b45a52401 142
Reckstyle 0:5ca0450111f3 143
Reckstyle 16:3649eb1a056d 144 // if (sensor->grayValue != 0 && freq < (sensor->grayValue + 1000) && freq > (sensor.grayValue - 1000)) { //definitely not sure about that!
Reckstyle 16:3649eb1a056d 145 // //this is a gray value sensor in its gray region
Reckstyle 16:3649eb1a056d 146 // sensor->state = 2;
Reckstyle 16:3649eb1a056d 147 // return 2;
Reckstyle 16:3649eb1a056d 148 // }
Reckstyle 30:307b45a52401 149 if (freq < (sensor->blackValue)){
Reckstyle 30:307b45a52401 150 sensor->state = 0; //if it's less than black value it is black
Reckstyle 30:307b45a52401 151 return 0;
Reckstyle 30:307b45a52401 152 }
Reckstyle 30:307b45a52401 153 else {
Reckstyle 30:307b45a52401 154 sensor->state = 1;
Reckstyle 30:307b45a52401 155 }
Reckstyle 30:307b45a52401 156 return 1;
Reckstyle 16:3649eb1a056d 157 //(freq < sensor->black_value*2)? 1 : 0; //freq
Reckstyle 0:5ca0450111f3 158 }
Reckstyle 0:5ca0450111f3 159 #endif