
Lol smth
Dependencies: LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed
Fork of TDP_main_BartFork by
sensor_measure.h@1:eace997e9a93, 2015-02-13 (annotated)
- Committer:
- Reckstyle
- Date:
- Fri Feb 13 15:51:24 2015 +0000
- Revision:
- 1:eace997e9a93
- Parent:
- 0:5ca0450111f3
- Child:
- 9:718987b106a8
Publish;
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 | 1:eace997e9a93 | 12 | |
Reckstyle | 1:eace997e9a93 | 13 | NEED TESTING! |
Reckstyle | 0:5ca0450111f3 | 14 | LAST REVISED 13/02 |
Reckstyle | 0:5ca0450111f3 | 15 | */ |
Reckstyle | 0:5ca0450111f3 | 16 | |
Reckstyle | 0:5ca0450111f3 | 17 | #ifndef _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 18 | #define _SENSOR_MEASURE_H |
Reckstyle | 0:5ca0450111f3 | 19 | |
Reckstyle | 0:5ca0450111f3 | 20 | #define NUMBER_SAMPLES 40 // NUMBER OF SAMPLES FOR SENSOR TESTING |
Reckstyle | 0:5ca0450111f3 | 21 | |
Reckstyle | 0:5ca0450111f3 | 22 | //define pinout for all the sensors |
Reckstyle | 1:eace997e9a93 | 23 | DigitalIn pin_right_right(PTD0); |
Reckstyle | 0:5ca0450111f3 | 24 | InterruptIn pin_right_centre(PTD1); |
Reckstyle | 0:5ca0450111f3 | 25 | InterruptIn pin_right_left(PTD2); |
Reckstyle | 0:5ca0450111f3 | 26 | InterruptIn pin_left_right(PTD3); |
Reckstyle | 0:5ca0450111f3 | 27 | InterruptIn pin_left_centre(PTD4); |
Reckstyle | 0:5ca0450111f3 | 28 | InterruptIn pin_left_left(PTD5); |
Reckstyle | 0:5ca0450111f3 | 29 | //InterruptIn in(PTD0); maybe more~? |
Reckstyle | 0:5ca0450111f3 | 30 | |
Reckstyle | 0:5ca0450111f3 | 31 | //timer used by the sensor |
Reckstyle | 0:5ca0450111f3 | 32 | Timer sensorTimer; |
Reckstyle | 0:5ca0450111f3 | 33 | |
Reckstyle | 0:5ca0450111f3 | 34 | //structure for sensors |
Reckstyle | 0:5ca0450111f3 | 35 | typedef struct sensor_data { |
Reckstyle | 1:eace997e9a93 | 36 | DigitalIn* pin; //TODO can;t seem to be able to declara this |
Reckstyle | 0:5ca0450111f3 | 37 | int black_value; |
Reckstyle | 0:5ca0450111f3 | 38 | int white_value; |
Reckstyle | 0:5ca0450111f3 | 39 | }sensor_data; |
Reckstyle | 0:5ca0450111f3 | 40 | |
Reckstyle | 1:eace997e9a93 | 41 | /* |
Reckstyle | 1:eace997e9a93 | 42 | All sensors should be declared in the MAIN PROGRAM???? |
Reckstyle | 1:eace997e9a93 | 43 | sensor_data right_right; |
Reckstyle | 1:eace997e9a93 | 44 | sensor_data.pin = &pin_right_right; |
Reckstyle | 1:eace997e9a93 | 45 | right_right.black_value = 2013; |
Reckstyle | 1:eace997e9a93 | 46 | sensor_data.white_value = 10000; |
Reckstyle | 1:eace997e9a93 | 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 | 1:eace997e9a93 | 52 | //Initialise values of all sensors |
Reckstyle | 1:eace997e9a93 | 53 | void sensor_initialise () { |
Reckstyle | 1:eace997e9a93 | 54 | //right_right |
Reckstyle | 1:eace997e9a93 | 55 | right_right.pin = &pin_right_right; |
Reckstyle | 1:eace997e9a93 | 56 | right_right.black_value = 2013; |
Reckstyle | 1:eace997e9a93 | 57 | right_right.white_value = 10000; |
Reckstyle | 1:eace997e9a93 | 58 | //and contiune so on.. |
Reckstyle | 1:eace997e9a93 | 59 | } |
Reckstyle | 1:eace997e9a93 | 60 | |
Reckstyle | 0:5ca0450111f3 | 61 | |
Reckstyle | 0:5ca0450111f3 | 62 | //measuring function - returning whether it is black or white line |
Reckstyle | 0:5ca0450111f3 | 63 | //"1" - black, "0" - white |
Reckstyle | 0:5ca0450111f3 | 64 | int measure (sensor_data sensor){ |
Reckstyle | 1:eace997e9a93 | 65 | |
Reckstyle | 1:eace997e9a93 | 66 | |
Reckstyle | 1:eace997e9a93 | 67 | |
Reckstyle | 0:5ca0450111f3 | 68 | sensorTimer.reset(); |
Reckstyle | 0:5ca0450111f3 | 69 | int freq,period = 0; |
Reckstyle | 0:5ca0450111f3 | 70 | int n =0; //number of samples |
Reckstyle | 0:5ca0450111f3 | 71 | int sensor_old = 0;//variable to remember old sensor state |
Reckstyle | 0:5ca0450111f3 | 72 | |
Reckstyle | 0:5ca0450111f3 | 73 | while (n < NUMBER_SAMPLES){ |
Reckstyle | 0:5ca0450111f3 | 74 | |
Reckstyle | 1:eace997e9a93 | 75 | if (*sensor.pin == 1 && sensor_old == 0){ // detect on rising edge |
Reckstyle | 0:5ca0450111f3 | 76 | n++; |
Reckstyle | 0:5ca0450111f3 | 77 | } |
Reckstyle | 1:eace997e9a93 | 78 | sensor_old = *sensor.pin; |
Reckstyle | 0:5ca0450111f3 | 79 | } |
Reckstyle | 0:5ca0450111f3 | 80 | |
Reckstyle | 0:5ca0450111f3 | 81 | period = sensorTimer.read_us()/(float)n; // Get time |
Reckstyle | 0:5ca0450111f3 | 82 | freq = (1/period)*1000000; // Convert period (in us) to frequency (Hz). Works up to 100kHz. |
Reckstyle | 1:eace997e9a93 | 83 | |
Reckstyle | 1:eace997e9a93 | 84 | |
Reckstyle | 1:eace997e9a93 | 85 | return (freq < sensor.black_value*2)? 1 : 0; |
Reckstyle | 0:5ca0450111f3 | 86 | } |
Reckstyle | 0:5ca0450111f3 | 87 | #endif |