
Team Design project 3 main file
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main by
sensor_measure.h@37:3d14df6dec34, 2015-03-26 (annotated)
- Committer:
- Reckstyle
- Date:
- Thu Mar 26 20:13:49 2015 +0000
- Revision:
- 37:3d14df6dec34
- Parent:
- 36:a48d57d63630
fdsa
Who changed what in which revision?
User | Revision | Line number | New 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 | 29:307b45a52401 | 25 | DigitalIn pin_right_right_up(PTC2); |
Reckstyle | 29:307b45a52401 | 26 | DigitalIn pin_right_left_up(PTE30); |
Reckstyle | 29:307b45a52401 | 27 | DigitalIn pin_right_right_down(PTE29); |
Reckstyle | 29:307b45a52401 | 28 | DigitalIn pin_right_left_down(PTC1); |
Reckstyle | 29:307b45a52401 | 29 | DigitalIn pin_left_right_down(PTB3); |
Reckstyle | 22:902c3086394e | 30 | DigitalIn pin_left_left_down(PTE22); //pte22 |
Reckstyle | 29:307b45a52401 | 31 | DigitalIn pin_left_right_up(PTE23); //pte29 |
Reckstyle | 29:307b45a52401 | 32 | DigitalIn pin_left_left_up(PTB2); |
Reckstyle | 37:3d14df6dec34 | 33 | DigitalIn pin_center_left (PTC7); |
Reckstyle | 37:3d14df6dec34 | 34 | DigitalIn pin_center_center (PTC0); |
Reckstyle | 37:3d14df6dec34 | 35 | DigitalIn pin_center_right (PTC3); |
Reckstyle | 0:5ca0450111f3 | 36 | |
Reckstyle | 0:5ca0450111f3 | 37 | //timer used by the sensor |
Reckstyle | 0:5ca0450111f3 | 38 | Timer sensorTimer; |
Reckstyle | 0:5ca0450111f3 | 39 | |
Reckstyle | 9:718987b106a8 | 40 | |
Reckstyle | 11:9e56d52485d1 | 41 | Serial pc(USBTX, USBRX); //used for connection to PC/debugging |
Reckstyle | 9:718987b106a8 | 42 | |
Reckstyle | 0:5ca0450111f3 | 43 | //structure for sensors |
Reckstyle | 0:5ca0450111f3 | 44 | typedef struct sensor_data { |
Reckstyle | 9:718987b106a8 | 45 | DigitalIn* pin; |
Reckstyle | 11:9e56d52485d1 | 46 | int blackValue; |
Reckstyle | 11:9e56d52485d1 | 47 | int whiteValue; //maybe it can be removed ??? |
Reckstyle | 11:9e56d52485d1 | 48 | int grayValue; // if sensor is used for squares will have a gray value; |
Reckstyle | 11:9e56d52485d1 | 49 | int state; //can be removed as well? |
Reckstyle | 0:5ca0450111f3 | 50 | }sensor_data; |
Reckstyle | 0:5ca0450111f3 | 51 | |
Reckstyle | 9:718987b106a8 | 52 | |
Reckstyle | 15:6453cd351452 | 53 | sensor_data right_right_up; |
Reckstyle | 15:6453cd351452 | 54 | sensor_data right_left_up; |
Reckstyle | 15:6453cd351452 | 55 | sensor_data right_right_down; |
Reckstyle | 15:6453cd351452 | 56 | sensor_data right_left_down; |
Reckstyle | 15:6453cd351452 | 57 | sensor_data left_right_down; |
Reckstyle | 15:6453cd351452 | 58 | sensor_data left_left_down; |
Reckstyle | 15:6453cd351452 | 59 | sensor_data left_right_up; |
Reckstyle | 15:6453cd351452 | 60 | sensor_data left_left_up; |
Reckstyle | 37:3d14df6dec34 | 61 | sensor_data center_left; |
Reckstyle | 37:3d14df6dec34 | 62 | sensor_data center_center; |
Reckstyle | 37:3d14df6dec34 | 63 | sensor_data center_right; |
Reckstyle | 1:eace997e9a93 | 64 | // and so on.... |
Reckstyle | 0:5ca0450111f3 | 65 | |
Reckstyle | 37:3d14df6dec34 | 66 | sensor_data *sensorArray [NUMBER_SENSORS_REGULAR+3]; //array just used for getting value out of the sensors, helps for iteration(see main program) |
Reckstyle | 11:9e56d52485d1 | 67 | |
Reckstyle | 1:eace997e9a93 | 68 | //Initialise values of all sensors |
Reckstyle | 1:eace997e9a93 | 69 | void sensor_initialise () { |
Reckstyle | 11:9e56d52485d1 | 70 | int arrayBuilder = 0; //integer solely used for populating the array |
Reckstyle | 1:eace997e9a93 | 71 | //right_right |
Reckstyle | 15:6453cd351452 | 72 | right_right_up.pin = &pin_right_right_up; |
Reckstyle | 37:3d14df6dec34 | 73 | right_right_up.blackValue = 4200*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 74 | right_right_up.whiteValue = 72000; |
Reckstyle | 15:6453cd351452 | 75 | right_right_up.grayValue = 0; // 0 for all the non-square sensors |
Reckstyle | 15:6453cd351452 | 76 | right_right_up.state = 0; //setting all sensors as black at begging |
Reckstyle | 15:6453cd351452 | 77 | sensorArray [arrayBuilder++] = &right_right_up; //Array goes from rightmost to left, then centre? |
Reckstyle | 15:6453cd351452 | 78 | right_left_up.pin = &pin_right_left_up; |
Reckstyle | 37:3d14df6dec34 | 79 | right_left_up.blackValue = 5000*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 80 | right_left_up.whiteValue = 70000; |
Reckstyle | 15:6453cd351452 | 81 | right_left_up.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 82 | right_left_up.state = 1; |
Reckstyle | 15:6453cd351452 | 83 | sensorArray [arrayBuilder++] = &right_left_up; |
Reckstyle | 16:3649eb1a056d | 84 | right_right_down.pin = &pin_right_right_down; |
Reckstyle | 37:3d14df6dec34 | 85 | right_right_down.blackValue = 900*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 86 | right_right_down.whiteValue = 12000; |
Reckstyle | 17:de8b3111ddc5 | 87 | right_right_down.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 88 | right_right_down.state = 0; |
Reckstyle | 16:3649eb1a056d | 89 | sensorArray [arrayBuilder++] = &right_right_down; |
Reckstyle | 16:3649eb1a056d | 90 | right_left_down.pin = &pin_right_left_down; |
Reckstyle | 37:3d14df6dec34 | 91 | right_left_down.blackValue = 1500*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 92 | right_left_down.whiteValue = 17000; |
Reckstyle | 17:de8b3111ddc5 | 93 | right_left_down.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 94 | right_left_down.state = 1; |
Reckstyle | 16:3649eb1a056d | 95 | sensorArray[arrayBuilder++] = &right_left_down; |
Reckstyle | 17:de8b3111ddc5 | 96 | left_right_down.pin = &pin_left_right_down; |
Reckstyle | 37:3d14df6dec34 | 97 | left_right_down.blackValue = 1000*BLACK_THRESHOLD; |
Reckstyle | 29:307b45a52401 | 98 | left_right_down.whiteValue = 14000; |
Reckstyle | 17:de8b3111ddc5 | 99 | left_right_down.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 100 | left_right_down.state = 1; |
Reckstyle | 17:de8b3111ddc5 | 101 | sensorArray [arrayBuilder++] = &left_right_down; |
Reckstyle | 17:de8b3111ddc5 | 102 | left_left_down.pin = &pin_left_left_down; |
Reckstyle | 37:3d14df6dec34 | 103 | left_left_down.blackValue = 1000*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 104 | left_left_down.whiteValue = 10000; |
Reckstyle | 17:de8b3111ddc5 | 105 | left_left_down.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 106 | left_left_down.state = 0; |
Reckstyle | 17:de8b3111ddc5 | 107 | sensorArray [arrayBuilder++] = &left_left_down; |
Reckstyle | 17:de8b3111ddc5 | 108 | left_right_up.pin = &pin_left_right_up; |
Reckstyle | 37:3d14df6dec34 | 109 | left_right_up.blackValue = 5000*BLACK_THRESHOLD; //??? it's a trick ;) |
Reckstyle | 32:d1dc27f4a18d | 110 | left_right_up.whiteValue = 35000; |
Reckstyle | 17:de8b3111ddc5 | 111 | left_right_up.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 112 | left_right_up.state = 1; |
Reckstyle | 17:de8b3111ddc5 | 113 | sensorArray [arrayBuilder++] = &left_right_up; |
Reckstyle | 17:de8b3111ddc5 | 114 | left_left_up.pin = &pin_left_left_up; |
Reckstyle | 37:3d14df6dec34 | 115 | left_left_up.blackValue = 9500*BLACK_THRESHOLD; |
Reckstyle | 32:d1dc27f4a18d | 116 | left_left_up.whiteValue = 46000; |
Reckstyle | 17:de8b3111ddc5 | 117 | left_left_up.grayValue = 0; |
Reckstyle | 17:de8b3111ddc5 | 118 | left_left_up.state = 0; |
Reckstyle | 17:de8b3111ddc5 | 119 | sensorArray [arrayBuilder++] = &left_left_up; |
Reckstyle | 37:3d14df6dec34 | 120 | center_left.pin = &pin_center_left; |
Reckstyle | 37:3d14df6dec34 | 121 | center_left.grayValue = 0; |
Reckstyle | 37:3d14df6dec34 | 122 | center_left.state = 0; |
Reckstyle | 37:3d14df6dec34 | 123 | sensorArray [arrayBuilder++] = ¢er_left; |
Reckstyle | 37:3d14df6dec34 | 124 | center_center.pin = &pin_center_center; |
Reckstyle | 37:3d14df6dec34 | 125 | center_center.grayValue = 0; |
Reckstyle | 37:3d14df6dec34 | 126 | center_center.state = 0; |
Reckstyle | 37:3d14df6dec34 | 127 | sensorArray [arrayBuilder++] = ¢er_center; |
Reckstyle | 37:3d14df6dec34 | 128 | center_right.pin = &pin_center_right; |
Reckstyle | 37:3d14df6dec34 | 129 | center_right.grayValue = 0; |
Reckstyle | 37:3d14df6dec34 | 130 | center_right.state = 0 ; |
Reckstyle | 37:3d14df6dec34 | 131 | sensorArray [arrayBuilder++] = ¢er_right; |
Reckstyle | 1:eace997e9a93 | 132 | } |
Reckstyle | 1:eace997e9a93 | 133 | |
Reckstyle | 16:3649eb1a056d | 134 | int counter1 = 0; |
Reckstyle | 0:5ca0450111f3 | 135 | //measuring function - returning whether it is black or white line |
Reckstyle | 11:9e56d52485d1 | 136 | //"0" - black, "1" - white |
Reckstyle | 16:3649eb1a056d | 137 | int measure (sensor_data *sensor){ |
Reckstyle | 1:eace997e9a93 | 138 | |
Reckstyle | 9:718987b106a8 | 139 | sensorTimer.reset(); //reset the timer |
Reckstyle | 9:718987b106a8 | 140 | double freq,period = 0.0; |
Reckstyle | 0:5ca0450111f3 | 141 | int n =0; //number of samples |
Reckstyle | 9:718987b106a8 | 142 | int sensor_old = 0; //variable to remember old sensor state |
Reckstyle | 9:718987b106a8 | 143 | int sensor_new = 0; |
Reckstyle | 9:718987b106a8 | 144 | //double time_1 = sensorTimer.read(); used for debugging |
Reckstyle | 0:5ca0450111f3 | 145 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 16:3649eb1a056d | 146 | sensor_new = sensor->pin->read(); |
Reckstyle | 9:718987b106a8 | 147 | if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge |
Reckstyle | 0:5ca0450111f3 | 148 | n++; |
Reckstyle | 0:5ca0450111f3 | 149 | } |
Reckstyle | 9:718987b106a8 | 150 | sensor_old = sensor_new; |
Reckstyle | 0:5ca0450111f3 | 151 | } |
Reckstyle | 9:718987b106a8 | 152 | double time_2 = sensorTimer.read(); |
Reckstyle | 9:718987b106a8 | 153 | // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2); //Used for debugging |
Reckstyle | 9:718987b106a8 | 154 | |
Reckstyle | 9:718987b106a8 | 155 | period = time_2/((double)NUMBER_SAMPLES); // Get time |
Reckstyle | 9:718987b106a8 | 156 | freq = (1/period); // Convert period (in us) to frequency (Hz). |
Reckstyle | 16:3649eb1a056d | 157 | //pc.printf(" period is %f and freq is %f ", period,freq); // Used for debugging |
Reckstyle | 16:3649eb1a056d | 158 | |
Reckstyle | 37:3d14df6dec34 | 159 | sensor->state = freq; |
Reckstyle | 37:3d14df6dec34 | 160 | return 0; |
Reckstyle | 0:5ca0450111f3 | 161 | |
Reckstyle | 16:3649eb1a056d | 162 | // if (sensor->grayValue != 0 && freq < (sensor->grayValue + 1000) && freq > (sensor.grayValue - 1000)) { //definitely not sure about that! |
Reckstyle | 16:3649eb1a056d | 163 | // //this is a gray value sensor in its gray region |
Reckstyle | 16:3649eb1a056d | 164 | // sensor->state = 2; |
Reckstyle | 16:3649eb1a056d | 165 | // return 2; |
Reckstyle | 16:3649eb1a056d | 166 | // } |
Reckstyle | 37:3d14df6dec34 | 167 | // if (freq < (sensor->blackValue)){ |
Reckstyle | 37:3d14df6dec34 | 168 | // sensor->state = 0; //if it's less than black value it is black |
Reckstyle | 37:3d14df6dec34 | 169 | // return 0; |
Reckstyle | 37:3d14df6dec34 | 170 | // } |
Reckstyle | 37:3d14df6dec34 | 171 | // else { |
Reckstyle | 37:3d14df6dec34 | 172 | // sensor->state = 1; |
Reckstyle | 37:3d14df6dec34 | 173 | // } |
Reckstyle | 37:3d14df6dec34 | 174 | // return 1; |
Reckstyle | 16:3649eb1a056d | 175 | //(freq < sensor->black_value*2)? 1 : 0; //freq |
Reckstyle | 0:5ca0450111f3 | 176 | } |
Reckstyle | 0:5ca0450111f3 | 177 | #endif |