
Don't want to screw something by updating the code as Ivelin updated himself while I was writing so I am forking it.
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main by
sensor_measure.h@9:718987b106a8, 2015-02-23 (annotated)
- Committer:
- Reckstyle
- Date:
- Mon Feb 23 16:49:59 2015 +0000
- Revision:
- 9:718987b106a8
- Parent:
- 1:eace997e9a93
- Child:
- 11:9e56d52485d1
23/02 sensors working
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 | 9:718987b106a8 | 6 | TESTED AND FUNCTIONAL! |
Reckstyle | 9:718987b106a8 | 7 | NEEDS POPULATION FOR ALL THE SENSORS! |
Reckstyle | 9:718987b106a8 | 8 | LAST REVISED 23/02 |
Reckstyle | 0:5ca0450111f3 | 9 | */ |
Reckstyle | 0:5ca0450111f3 | 10 | |
Reckstyle | 0:5ca0450111f3 | 11 | #ifndef _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 12 | #define _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 13 | |
Reckstyle | 0:5ca0450111f3 | 14 | #define NUMBER_SAMPLES 40 // NUMBER OF SAMPLES FOR SENSOR TESTING |
Reckstyle | 0:5ca0450111f3 | 15 | |
Reckstyle | 0:5ca0450111f3 | 16 | //define pinout for all the sensors |
Reckstyle | 1:eace997e9a93 | 17 | DigitalIn pin_right_right(PTD0); |
Reckstyle | 0:5ca0450111f3 | 18 | InterruptIn pin_right_centre(PTD1); |
Reckstyle | 0:5ca0450111f3 | 19 | InterruptIn pin_right_left(PTD2); |
Reckstyle | 0:5ca0450111f3 | 20 | InterruptIn pin_left_right(PTD3); |
Reckstyle | 0:5ca0450111f3 | 21 | InterruptIn pin_left_centre(PTD4); |
Reckstyle | 0:5ca0450111f3 | 22 | InterruptIn pin_left_left(PTD5); |
Reckstyle | 0:5ca0450111f3 | 23 | //InterruptIn in(PTD0); maybe more~? |
Reckstyle | 0:5ca0450111f3 | 24 | |
Reckstyle | 0:5ca0450111f3 | 25 | //timer used by the sensor |
Reckstyle | 0:5ca0450111f3 | 26 | Timer sensorTimer; |
Reckstyle | 0:5ca0450111f3 | 27 | |
Reckstyle | 9:718987b106a8 | 28 | |
Reckstyle | 9:718987b106a8 | 29 | |
Reckstyle | 0:5ca0450111f3 | 30 | //structure for sensors |
Reckstyle | 0:5ca0450111f3 | 31 | typedef struct sensor_data { |
Reckstyle | 9:718987b106a8 | 32 | DigitalIn* pin; |
Reckstyle | 0:5ca0450111f3 | 33 | int black_value; |
Reckstyle | 0:5ca0450111f3 | 34 | int white_value; |
Reckstyle | 0:5ca0450111f3 | 35 | }sensor_data; |
Reckstyle | 0:5ca0450111f3 | 36 | |
Reckstyle | 9:718987b106a8 | 37 | |
Reckstyle | 0:5ca0450111f3 | 38 | sensor_data right_right; |
Reckstyle | 1:eace997e9a93 | 39 | sensor_data right_centre; |
Reckstyle | 1:eace997e9a93 | 40 | // and so on.... |
Reckstyle | 0:5ca0450111f3 | 41 | |
Reckstyle | 1:eace997e9a93 | 42 | //Initialise values of all sensors |
Reckstyle | 1:eace997e9a93 | 43 | void sensor_initialise () { |
Reckstyle | 1:eace997e9a93 | 44 | //right_right |
Reckstyle | 1:eace997e9a93 | 45 | right_right.pin = &pin_right_right; |
Reckstyle | 1:eace997e9a93 | 46 | right_right.black_value = 2013; |
Reckstyle | 1:eace997e9a93 | 47 | right_right.white_value = 10000; |
Reckstyle | 1:eace997e9a93 | 48 | //and contiune so on.. |
Reckstyle | 1:eace997e9a93 | 49 | } |
Reckstyle | 1:eace997e9a93 | 50 | |
Reckstyle | 0:5ca0450111f3 | 51 | |
Reckstyle | 0:5ca0450111f3 | 52 | //measuring function - returning whether it is black or white line |
Reckstyle | 0:5ca0450111f3 | 53 | //"1" - black, "0" - white |
Reckstyle | 9:718987b106a8 | 54 | double measure (sensor_data sensor){ |
Reckstyle | 1:eace997e9a93 | 55 | |
Reckstyle | 9:718987b106a8 | 56 | sensorTimer.reset(); //reset the timer |
Reckstyle | 9:718987b106a8 | 57 | double freq,period = 0.0; |
Reckstyle | 0:5ca0450111f3 | 58 | int n =0; //number of samples |
Reckstyle | 9:718987b106a8 | 59 | int sensor_old = 0; //variable to remember old sensor state |
Reckstyle | 9:718987b106a8 | 60 | int sensor_new = 0; |
Reckstyle | 9:718987b106a8 | 61 | //double time_1 = sensorTimer.read(); used for debugging |
Reckstyle | 0:5ca0450111f3 | 62 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 9:718987b106a8 | 63 | sensor_new = sensor.pin->read(); |
Reckstyle | 9:718987b106a8 | 64 | if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge |
Reckstyle | 0:5ca0450111f3 | 65 | n++; |
Reckstyle | 0:5ca0450111f3 | 66 | } |
Reckstyle | 9:718987b106a8 | 67 | sensor_old = sensor_new; |
Reckstyle | 0:5ca0450111f3 | 68 | } |
Reckstyle | 9:718987b106a8 | 69 | double time_2 = sensorTimer.read(); |
Reckstyle | 9:718987b106a8 | 70 | // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2); //Used for debugging |
Reckstyle | 9:718987b106a8 | 71 | |
Reckstyle | 9:718987b106a8 | 72 | period = time_2/((double)NUMBER_SAMPLES); // Get time |
Reckstyle | 9:718987b106a8 | 73 | freq = (1/period); // Convert period (in us) to frequency (Hz). |
Reckstyle | 9:718987b106a8 | 74 | // pc.printf(" period is %f ", period); // Used for debugging |
Reckstyle | 0:5ca0450111f3 | 75 | |
Reckstyle | 9:718987b106a8 | 76 | return freq;//(freq < sensor.black_value*2)? 1 : 0; |
Reckstyle | 0:5ca0450111f3 | 77 | } |
Reckstyle | 0:5ca0450111f3 | 78 | #endif |