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