
Lol smth
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main_BartFork by
sensor_measure.h@11:9e56d52485d1, 2015-03-06 (annotated)
- Committer:
- Reckstyle
- Date:
- Fri Mar 06 11:50:07 2015 +0000
- Revision:
- 11:9e56d52485d1
- Parent:
- 9:718987b106a8
- Child:
- 12:bb21b76b6375
06/03
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 | 11:9e56d52485d1 | 20 | #define NUMBER_SENSORS_REGULAR 2 // number for the array of sensors |
Reckstyle | 11:9e56d52485d1 | 21 | #define NUMBER_SENSORS_SQUARE 0 |
Reckstyle | 0:5ca0450111f3 | 22 | |
Reckstyle | 0:5ca0450111f3 | 23 | //define pinout for all the sensors |
Reckstyle | 1:eace997e9a93 | 24 | DigitalIn pin_right_right(PTD0); |
Reckstyle | 11:9e56d52485d1 | 25 | DigitalIn pin_right_centre(PTD2); |
Reckstyle | 11:9e56d52485d1 | 26 | DigitalIn pin_right_left(PTD2); |
Reckstyle | 11:9e56d52485d1 | 27 | DigitalIn pin_left_right(PTD3); |
Reckstyle | 11:9e56d52485d1 | 28 | DigitalIn pin_left_centre(PTD4); |
Reckstyle | 11:9e56d52485d1 | 29 | DigitalIn pin_left_left(PTD5); |
Reckstyle | 0:5ca0450111f3 | 30 | //InterruptIn in(PTD0); maybe more~? |
Reckstyle | 0:5ca0450111f3 | 31 | |
Reckstyle | 0:5ca0450111f3 | 32 | //timer used by the sensor |
Reckstyle | 0:5ca0450111f3 | 33 | Timer sensorTimer; |
Reckstyle | 0:5ca0450111f3 | 34 | |
Reckstyle | 9:718987b106a8 | 35 | |
Reckstyle | 11:9e56d52485d1 | 36 | Serial pc(USBTX, USBRX); //used for connection to PC/debugging |
Reckstyle | 9:718987b106a8 | 37 | |
Reckstyle | 0:5ca0450111f3 | 38 | //structure for sensors |
Reckstyle | 0:5ca0450111f3 | 39 | typedef struct sensor_data { |
Reckstyle | 9:718987b106a8 | 40 | DigitalIn* pin; |
Reckstyle | 11:9e56d52485d1 | 41 | int blackValue; |
Reckstyle | 11:9e56d52485d1 | 42 | int whiteValue; //maybe it can be removed ??? |
Reckstyle | 11:9e56d52485d1 | 43 | int grayValue; // if sensor is used for squares will have a gray value; |
Reckstyle | 11:9e56d52485d1 | 44 | int state; //can be removed as well? |
Reckstyle | 0:5ca0450111f3 | 45 | }sensor_data; |
Reckstyle | 0:5ca0450111f3 | 46 | |
Reckstyle | 9:718987b106a8 | 47 | |
Reckstyle | 0:5ca0450111f3 | 48 | sensor_data right_right; |
Reckstyle | 1:eace997e9a93 | 49 | sensor_data right_centre; |
Reckstyle | 1:eace997e9a93 | 50 | // and so on.... |
Reckstyle | 0:5ca0450111f3 | 51 | |
Reckstyle | 11:9e56d52485d1 | 52 | sensor_data *sensorArray [NUMBER_SENSORS]; //array just used for getting value out of the sensors, helps for iteration(see main program) |
Reckstyle | 11:9e56d52485d1 | 53 | |
Reckstyle | 1:eace997e9a93 | 54 | //Initialise values of all sensors |
Reckstyle | 1:eace997e9a93 | 55 | void sensor_initialise () { |
Reckstyle | 11:9e56d52485d1 | 56 | int arrayBuilder = 0; //integer solely used for populating the array |
Reckstyle | 1:eace997e9a93 | 57 | //right_right |
Reckstyle | 1:eace997e9a93 | 58 | right_right.pin = &pin_right_right; |
Reckstyle | 11:9e56d52485d1 | 59 | right_right.blackValue = 2013; |
Reckstyle | 11:9e56d52485d1 | 60 | right_right.whiteValue = 10000; |
Reckstyle | 11:9e56d52485d1 | 61 | right_right.grayValue = 0; // 0 for all the non-square sensors |
Reckstyle | 11:9e56d52485d1 | 62 | right_right.state = 0; //setting all sensors as black at begging |
Reckstyle | 11:9e56d52485d1 | 63 | sensorArray [arrayBuilder++] = &right_right; //Array goes from rightmost to left, then centre? |
Reckstyle | 11:9e56d52485d1 | 64 | right_centre.pin = &pin_right_centre; |
Reckstyle | 11:9e56d52485d1 | 65 | right_centre.blackValue = 6000; |
Reckstyle | 11:9e56d52485d1 | 66 | right_centre.whiteValue = 10000; |
Reckstyle | 11:9e56d52485d1 | 67 | right_centre.grayValue = 0; |
Reckstyle | 11:9e56d52485d1 | 68 | right_centre.state = 0; |
Reckstyle | 11:9e56d52485d1 | 69 | sensorArray [arrayBuilder++] = &right_centre; |
Reckstyle | 1:eace997e9a93 | 70 | //and contiune so on.. |
Reckstyle | 1:eace997e9a93 | 71 | } |
Reckstyle | 1:eace997e9a93 | 72 | |
Reckstyle | 0:5ca0450111f3 | 73 | |
Reckstyle | 0:5ca0450111f3 | 74 | //measuring function - returning whether it is black or white line |
Reckstyle | 11:9e56d52485d1 | 75 | //"0" - black, "1" - white |
Reckstyle | 11:9e56d52485d1 | 76 | int measure (sensor_data sensor){ |
Reckstyle | 1:eace997e9a93 | 77 | |
Reckstyle | 9:718987b106a8 | 78 | sensorTimer.reset(); //reset the timer |
Reckstyle | 9:718987b106a8 | 79 | double freq,period = 0.0; |
Reckstyle | 0:5ca0450111f3 | 80 | int n =0; //number of samples |
Reckstyle | 9:718987b106a8 | 81 | int sensor_old = 0; //variable to remember old sensor state |
Reckstyle | 9:718987b106a8 | 82 | int sensor_new = 0; |
Reckstyle | 9:718987b106a8 | 83 | //double time_1 = sensorTimer.read(); used for debugging |
Reckstyle | 0:5ca0450111f3 | 84 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 9:718987b106a8 | 85 | sensor_new = sensor.pin->read(); |
Reckstyle | 9:718987b106a8 | 86 | if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge |
Reckstyle | 0:5ca0450111f3 | 87 | n++; |
Reckstyle | 0:5ca0450111f3 | 88 | } |
Reckstyle | 9:718987b106a8 | 89 | sensor_old = sensor_new; |
Reckstyle | 0:5ca0450111f3 | 90 | } |
Reckstyle | 9:718987b106a8 | 91 | double time_2 = sensorTimer.read(); |
Reckstyle | 9:718987b106a8 | 92 | // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2); //Used for debugging |
Reckstyle | 9:718987b106a8 | 93 | |
Reckstyle | 9:718987b106a8 | 94 | period = time_2/((double)NUMBER_SAMPLES); // Get time |
Reckstyle | 9:718987b106a8 | 95 | freq = (1/period); // Convert period (in us) to frequency (Hz). |
Reckstyle | 11:9e56d52485d1 | 96 | // pc.printf(" period is %f and freq is %f ", period,freq); // Used for debugging |
Reckstyle | 0:5ca0450111f3 | 97 | |
Reckstyle | 11:9e56d52485d1 | 98 | if (sensor.grayValue != 0 && freq < (sensor.grayValue + 1000) && freq > (sensor.grayValue - 1000)) { //definitely not sure about that! |
Reckstyle | 11:9e56d52485d1 | 99 | //this is a gray value sensor in its gray region |
Reckstyle | 11:9e56d52485d1 | 100 | sensor.state = 2; |
Reckstyle | 11:9e56d52485d1 | 101 | return 2; |
Reckstyle | 11:9e56d52485d1 | 102 | } |
Reckstyle | 11:9e56d52485d1 | 103 | else if (freq < sensor.blackValue*2){ |
Reckstyle | 11:9e56d52485d1 | 104 | sensor.state = 0; //if it's less than black value it is black |
Reckstyle | 11:9e56d52485d1 | 105 | return 0; |
Reckstyle | 11:9e56d52485d1 | 106 | } |
Reckstyle | 11:9e56d52485d1 | 107 | else { |
Reckstyle | 11:9e56d52485d1 | 108 | sensor.state = 1; |
Reckstyle | 11:9e56d52485d1 | 109 | } |
Reckstyle | 11:9e56d52485d1 | 110 | return 1; |
Reckstyle | 11:9e56d52485d1 | 111 | //(freq < sensor.black_value*2)? 1 : 0; //freq |
Reckstyle | 0:5ca0450111f3 | 112 | } |
Reckstyle | 0:5ca0450111f3 | 113 | #endif |