Lol smth

Dependencies:   LocalPositionSystem MMA8451Q Motor_Driver Sensors mbed

Fork of TDP_main_BartFork by Yelfie

Revision:
11:9e56d52485d1
Parent:
10:c9212bbfeae6
Child:
12:bb21b76b6375
--- a/main.cpp	Wed Feb 25 16:55:23 2015 +0000
+++ b/main.cpp	Fri Mar 06 11:50:07 2015 +0000
@@ -3,19 +3,23 @@
 /*
 ****** MAIN PROGRAM ******
 
-TODO : organisational routine still to be decided.
-3 main approaches: 
--sequential prirority based tasks
--Timer/Ticker driven with sequential prirority based tasks
-- interrupt driven from sensor inputs
+
 
 Please consider that although it is an embedded envrionment we are NOT creating a HARD-TIME real time system - delays can be dealt with
+
+
+Sensors are mapped on the global variable sensorsCheckSum,
+which multiplies the sensor number by itself to then decode,
+ which sensors are off and which are on
+ie. if sensor rightright - sensorChecksum = 1*1 = 1
+    if rightright and rightcentre - sensorChecksum = 1*1 + 2*2 = 5
+    ...
 */
 
 
 
 #include "mbed.h"
-//#include "sensor_measure.h"
+#include "sensor_measure.h"
 #include "Motor_Driver.h"
 //#include "Sensors.h"
 
@@ -30,7 +34,96 @@
 
 Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US);
 
-/*    For deleting
+typedef enum  mode {REGULAR,SQUARE} mode; //enumeration for different states
+mode driveMode; //declaring the variable for the states
+int sensorsCheckSum; //varibale used for sensors mapping access
+
+
+
+void measureSensors () {
+    sensorsCheckSum = 0; //zero it when first going into the routine
+    int iterationNumber = NUMBER_SENSORS_REGULAR;
+    if (driveMode == SQUARE) {
+          itearationNumber += NUMBER_SENSORS_SQUARE;
+    }
+    for (int i = 0; i < iterationNumber;i++){
+        if (measure(*sensorArray[i]) == 1) {//if sensor is white 
+            sensorsCheckSum += (i+1)*(i+1);
+        }
+    }    
+    //pc.printf("sensorsCheckSum is %i",sensorsCheckSum);
+}
+
+int main() {
+    
+    /*
+    Motor_Driver motors(PTD4, PTA12, PTA4, PTA5, PTC9, PTC8, PWM_PERIOD_US);
+    
+    motors.setSpeed(1.0f);
+    motors.forward();
+    motors.start();
+    
+    wait(3);
+    
+    motors.stop()
+    motors.reverse();
+    wait(200ms);
+    
+    motors.start();
+    */ 
+ //   setup_counter(1000, 0);
+  //  float frequency = measure_frequency(CHANNEL_1);
+    
+    driveMode = REGULAR;
+    sensor_initialise(); // initialise sensor values
+    wait(1); //give time to set up the system
+    
+    sensorTimer.start(); // start timer for sensors
+    
+    
+    pc.printf("Start...");
+    
+    
+    while (1) {
+       
+       if (driveMode == REGULAR) {
+          measureSensors();
+          switch (sensorsCheckSum) {
+            case 1: //right right is 1*1 + 0 + 0+ 0+0 +0 
+                pc.printf ("only Right is white \n");
+                break;
+            case 4 :   //0*0 + 2*2 + 0 + 0 + 0
+                pc.printf ("only Left is white \n");
+                break;
+            case 5 :  //1*1 + 2*2 + 0 + 0 
+                pc.printf ("both are white \n");
+                break;
+//          case 91: 
+//            driveMode = SQUARE; //if all sensors are white you're in the square
+//            break;
+          default: //checksum is zero , all are black
+            pc.printf ("BLACK BLACK");
+            break;
+          } 
+              
+        
+           
+        }
+       else { //if (driveMode == SQUARE}
+        //implement the square searching thing..
+       
+       }
+       
+       
+    }
+        
+}
+
+
+
+
+
+/*    TOBE deleted
 double measure1 (){
 
 
@@ -58,55 +151,3 @@
     return freq;//(freq < sensor.black_value*2)? 1 : 0;
 }
 */
-
-int main() {   
-    motors.setSpeed(0.5f);
-    motors.forward();
-    wait(1);
-    motors.start(0);
-    led = 0;
-    
-    wait(2);
-    
-    motors.setSpeed(1.0f);
-    
-    wait(2);
-    
-    led = 1;
-    
-    motors.stop(0);
-    
-    wait(3);
-    
-    motors.reverse();
-    wait_ms(200);
-    
-    motors.start(0);
-    wait(3);
-    motors.stop(0);
-    
-    motors.setSteeringMode(PIVOT_CW);
-    
-    wait(1);
-    motors.start(0);
-    wait(2);
-    motors.stop(0);
-}
-
-/*int main()
-{
-    pc.baud(9600);
-    
-    start_systick();
-    
-    pc.printf("Started!");
-    
-    int frequency;
-    
-    while(1)
-    {
-        frequency = measure_clock_cycles(CHANNEL_1, 25);
-        pc.printf("Debug: %i\n", frequency);    
-        wait_ms(200);
-    }    
-}*/