Lol smth

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main_BartFork by Yelfie

Revision:
9:718987b106a8
Parent:
1:eace997e9a93
Child:
11:9e56d52485d1
--- a/sensor_measure.h	Sun Feb 22 17:55:33 2015 +0000
+++ b/sensor_measure.h	Mon Feb 23 16:49:59 2015 +0000
@@ -2,16 +2,10 @@
 
 Sensor measurement header file. 
 Contains the pin declarations and variables for all sensors-related work
-Also contains the measurement function - should it???
 
-
-TODO 
--decide whether pins should be Interrupts INs or is it going to be polling - polling atm
-- how to distingisuh between different sensor on/off frequency - maybe create a new class??
-- 
-
-NEED TESTING!
-LAST REVISED 13/02
+TESTED AND FUNCTIONAL!
+NEEDS POPULATION FOR ALL THE SENSORS!
+LAST REVISED 23/02
 */
 
 #ifndef _SENSOR_MEASURE_H
@@ -31,20 +25,16 @@
 //timer used by the sensor
 Timer sensorTimer;
 
+
+
 //structure for sensors
 typedef struct sensor_data {
-    DigitalIn* pin; //TODO can;t seem to be able to declara this
+    DigitalIn* pin; 
     int  black_value;
     int  white_value;    
 }sensor_data;
 
-/*
-All sensors should be declared in the MAIN PROGRAM????
-sensor_data right_right;
-sensor_data.pin = &pin_right_right;
-right_right.black_value = 2013;
-sensor_data.white_value = 10000;
-*/
+
 sensor_data right_right;
 sensor_data right_centre;
 // and so on....
@@ -61,27 +51,28 @@
 
 //measuring function - returning whether it is black or white line 
 //"1" - black, "0" - white
-int measure (sensor_data sensor){
-
-
+double measure (sensor_data sensor){
     
-    sensorTimer.reset();
-    int freq,period = 0;
+    sensorTimer.reset(); //reset the timer
+    double freq,period = 0.0;
     int n =0; //number of samples
-    int sensor_old  = 0;//variable to remember old sensor state
-    
+    int sensor_old = 0; //variable to remember old sensor state
+    int sensor_new  = 0;
+    //double time_1 = sensorTimer.read();  used for debugging
     while (n < NUMBER_SAMPLES){
-        
-        if (*sensor.pin == 1 && sensor_old == 0){ // detect on rising edge
+        sensor_new = sensor.pin->read();
+        if ( sensor_new== 1 && sensor_old == 0){ // detect on rising edge 
             n++;
         }
-        sensor_old = *sensor.pin;
+        sensor_old = sensor_new;
     }
+    double time_2 = sensorTimer.read();
+   // pc.printf(" delta time is %f , time 2 is %f " , (time_2 - time_1), time_2);  //Used for debugging
+   
+    period = time_2/((double)NUMBER_SAMPLES); // Get time
+    freq = (1/period);   // Convert period (in us) to frequency (Hz). 
+    // pc.printf(" period is  %f  ", period); // Used for debugging
     
-    period = sensorTimer.read_us()/(float)n; // Get time
-    freq = (1/period)*1000000;   // Convert period (in us) to frequency (Hz). Works up to 100kHz. 
-    
-    
-    return (freq < sensor.black_value*2)? 1 : 0;
+    return freq;//(freq < sensor.black_value*2)? 1 : 0;
 }
 #endif
\ No newline at end of file