Team Design project 3 main file

Dependencies:   mbed Motor_Driver Sensors MMA8451Q LocalPositionSystem

Fork of TDP_main by Ivelin Kozarev

Revision:
11:9e56d52485d1
Parent:
9:718987b106a8
Child:
12:bb21b76b6375
--- a/sensor_measure.h	Wed Feb 25 16:55:23 2015 +0000
+++ b/sensor_measure.h	Fri Mar 06 11:50:07 2015 +0000
@@ -3,35 +3,45 @@
 Sensor measurement header file. 
 Contains the pin declarations and variables for all sensors-related work
 
-TESTED AND FUNCTIONAL!
+TESTED AND FUNCTIONAL!(24/02)
+TESTED (/27/02)
 NEEDS POPULATION FOR ALL THE SENSORS!
-LAST REVISED 23/02
+
+Need testing on implementation of the array of pointers
+Need implementation of third value for gray and a way to distinguish between it - will big structures affect speed?
+Need to revise struct sensors - are all values reallly needed???
+LAST REVISED 27/02
 */
 
 #ifndef _SENSOR_MEASURE_H
 #define _SENSOR_MEASURE_H
 
-#define NUMBER_SAMPLES 40 // NUMBER OF SAMPLES FOR SENSOR TESTING
+#define NUMBER_SAMPLES 20 // NUMBER OF SAMPLES FOR SENSOR TESTING
+#define NUMBER_SENSORS_REGULAR 2 // number for the array of sensors
+#define NUMBER_SENSORS_SQUARE 0
 
 //define pinout for all the sensors
 DigitalIn pin_right_right(PTD0);
-InterruptIn pin_right_centre(PTD1);
-InterruptIn pin_right_left(PTD2);
-InterruptIn pin_left_right(PTD3);
-InterruptIn pin_left_centre(PTD4);
-InterruptIn pin_left_left(PTD5);
+DigitalIn pin_right_centre(PTD2);
+DigitalIn pin_right_left(PTD2);
+DigitalIn pin_left_right(PTD3);
+DigitalIn pin_left_centre(PTD4);
+DigitalIn pin_left_left(PTD5);
 //InterruptIn in(PTD0); maybe more~?
 
 //timer used by the sensor
 Timer sensorTimer;
 
 
+Serial pc(USBTX, USBRX); //used for connection to PC/debugging
 
 //structure for sensors
 typedef struct sensor_data {
     DigitalIn* pin; 
-    int  black_value;
-    int  white_value;    
+    int  blackValue;
+    int  whiteValue;   //maybe it can be removed ???
+    int  grayValue; // if sensor is used for squares will have a gray value;
+    int  state;   //can be removed as well?
 }sensor_data;
 
 
@@ -39,19 +49,31 @@
 sensor_data right_centre;
 // and so on....
 
+sensor_data *sensorArray [NUMBER_SENSORS]; //array just used for getting value out of the sensors, helps for iteration(see main program)
+
 //Initialise values of all sensors
 void sensor_initialise () {
+    int arrayBuilder = 0; //integer solely used for populating the array
     //right_right
     right_right.pin = &pin_right_right;
-    right_right.black_value = 2013;
-    right_right.white_value = 10000;
+    right_right.blackValue = 2013;
+    right_right.whiteValue = 10000;
+    right_right.grayValue = 0; // 0 for all the non-square sensors
+    right_right.state = 0; //setting all sensors as black at begging
+    sensorArray [arrayBuilder++] = &right_right; //Array goes from rightmost to left, then centre?
+    right_centre.pin = &pin_right_centre;
+    right_centre.blackValue = 6000;
+    right_centre.whiteValue = 10000;
+    right_centre.grayValue = 0;
+    right_centre.state = 0;
+    sensorArray [arrayBuilder++] = &right_centre;
     //and contiune so on..
 }
 
 
 //measuring function - returning whether it is black or white line 
-//"1" - black, "0" - white
-double measure (sensor_data sensor){
+//"0" - black, "1" - white
+int measure (sensor_data sensor){
     
     sensorTimer.reset(); //reset the timer
     double freq,period = 0.0;
@@ -71,8 +93,21 @@
    
     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
+    // pc.printf(" period is  %f  and freq is  %f ", period,freq); // Used for debugging
     
-    return freq;//(freq < sensor.black_value*2)? 1 : 0;
+    if (sensor.grayValue != 0 && freq < (sensor.grayValue +  1000) && freq > (sensor.grayValue - 1000)) { //definitely not sure about that!
+        //this is a gray value sensor in its gray region
+        sensor.state = 2;
+        return 2;
+    }
+    else if (freq < sensor.blackValue*2){
+        sensor.state = 0;    //if it's less than black value it is black
+        return 0;
+    }
+    else {
+        sensor.state = 1;
+    }    
+        return 1;
+   //(freq < sensor.black_value*2)? 1 : 0; //freq
 }
 #endif
\ No newline at end of file