
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@0:5ca0450111f3, 2015-02-13 (annotated)
- Committer:
- Reckstyle
- Date:
- Fri Feb 13 15:18:43 2015 +0000
- Revision:
- 0:5ca0450111f3
- Child:
- 1:eace997e9a93
First save;
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 | Also contains the measurement function - should it??? |
Reckstyle | 0:5ca0450111f3 | 6 | |
Reckstyle | 0:5ca0450111f3 | 7 | |
Reckstyle | 0:5ca0450111f3 | 8 | TODO |
Reckstyle | 0:5ca0450111f3 | 9 | -decide whether pins should be Interrupts INs or is it going to be polling - polling atm |
Reckstyle | 0:5ca0450111f3 | 10 | - how to distingisuh between different sensor on/off frequency - maybe create a new class?? |
Reckstyle | 0:5ca0450111f3 | 11 | - |
Reckstyle | 0:5ca0450111f3 | 12 | LAST REVISED 13/02 |
Reckstyle | 0:5ca0450111f3 | 13 | */ |
Reckstyle | 0:5ca0450111f3 | 14 | |
Reckstyle | 0:5ca0450111f3 | 15 | #ifndef _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 16 | #define _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 17 | |
Reckstyle | 0:5ca0450111f3 | 18 | #define NUMBER_SAMPLES 40 // NUMBER OF SAMPLES FOR SENSOR TESTING |
Reckstyle | 0:5ca0450111f3 | 19 | |
Reckstyle | 0:5ca0450111f3 | 20 | //define pinout for all the sensors |
Reckstyle | 0:5ca0450111f3 | 21 | InterruptIn pin_right_right(PTD0); |
Reckstyle | 0:5ca0450111f3 | 22 | InterruptIn pin_right_centre(PTD1); |
Reckstyle | 0:5ca0450111f3 | 23 | InterruptIn pin_right_left(PTD2); |
Reckstyle | 0:5ca0450111f3 | 24 | InterruptIn pin_left_right(PTD3); |
Reckstyle | 0:5ca0450111f3 | 25 | InterruptIn pin_left_centre(PTD4); |
Reckstyle | 0:5ca0450111f3 | 26 | InterruptIn pin_left_left(PTD5); |
Reckstyle | 0:5ca0450111f3 | 27 | //InterruptIn in(PTD0); maybe more~? |
Reckstyle | 0:5ca0450111f3 | 28 | |
Reckstyle | 0:5ca0450111f3 | 29 | //timer used by the sensor |
Reckstyle | 0:5ca0450111f3 | 30 | Timer sensorTimer; |
Reckstyle | 0:5ca0450111f3 | 31 | |
Reckstyle | 0:5ca0450111f3 | 32 | //structure for sensors |
Reckstyle | 0:5ca0450111f3 | 33 | typedef struct sensor_data { |
Reckstyle | 0:5ca0450111f3 | 34 | InterruptIn pin; |
Reckstyle | 0:5ca0450111f3 | 35 | int black_value; |
Reckstyle | 0:5ca0450111f3 | 36 | int white_value; |
Reckstyle | 0:5ca0450111f3 | 37 | }sensor_data; |
Reckstyle | 0:5ca0450111f3 | 38 | |
Reckstyle | 0:5ca0450111f3 | 39 | //Decalre all the sensors |
Reckstyle | 0:5ca0450111f3 | 40 | //sample rightright |
Reckstyle | 0:5ca0450111f3 | 41 | sensor_data right_right; |
Reckstyle | 0:5ca0450111f3 | 42 | right_right.pin = pin_right_right; |
Reckstyle | 0:5ca0450111f3 | 43 | right_right.black_value = 2013; |
Reckstyle | 0:5ca0450111f3 | 44 | right_right.white_value = 10000; |
Reckstyle | 0:5ca0450111f3 | 45 | |
Reckstyle | 0:5ca0450111f3 | 46 | // and continue with the others.. |
Reckstyle | 0:5ca0450111f3 | 47 | |
Reckstyle | 0:5ca0450111f3 | 48 | //measuring function - returning whether it is black or white line |
Reckstyle | 0:5ca0450111f3 | 49 | //"1" - black, "0" - white |
Reckstyle | 0:5ca0450111f3 | 50 | int measure (sensor_data sensor){ |
Reckstyle | 0:5ca0450111f3 | 51 | sensorTimer.reset(); |
Reckstyle | 0:5ca0450111f3 | 52 | int freq,period = 0; |
Reckstyle | 0:5ca0450111f3 | 53 | int n =0; //number of samples |
Reckstyle | 0:5ca0450111f3 | 54 | int sensor_old = 0;//variable to remember old sensor state |
Reckstyle | 0:5ca0450111f3 | 55 | |
Reckstyle | 0:5ca0450111f3 | 56 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 0:5ca0450111f3 | 57 | |
Reckstyle | 0:5ca0450111f3 | 58 | if (sensor.pin == 1 && sensor_old == 0){ // detect on rising edge |
Reckstyle | 0:5ca0450111f3 | 59 | n++; |
Reckstyle | 0:5ca0450111f3 | 60 | } |
Reckstyle | 0:5ca0450111f3 | 61 | sensor_old = sensor.pin; |
Reckstyle | 0:5ca0450111f3 | 62 | } |
Reckstyle | 0:5ca0450111f3 | 63 | |
Reckstyle | 0:5ca0450111f3 | 64 | period = sensorTimer.read_us()/(float)n; // Get time |
Reckstyle | 0:5ca0450111f3 | 65 | freq = (1/period)*1000000; // Convert period (in us) to frequency (Hz). Works up to 100kHz. |
Reckstyle | 0:5ca0450111f3 | 66 | return freq; |
Reckstyle | 0:5ca0450111f3 | 67 | } |
Reckstyle | 0:5ca0450111f3 | 68 | #endif |