Object Avoidance Program

Dependencies:   4DGL-uLCD-SE Motordriver mbed

Revision:
0:eb26279eeb24
Child:
1:ab45b552644b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 29 21:05:19 2014 +0000
@@ -0,0 +1,154 @@
+
+#include "mbed.h"
+#include "motordriver.h"
+//#include "SHARPIR.h"
+#include "uLCD_4DGL.h"
+
+Motor mA(p22, p6, p5, 1); // pwm, fwd, rev
+Motor mB(p23, p8, p7, 1); // pwm, fwd, rev
+
+
+Serial pc(USBTX, USBRX); // tx, rx
+DigitalOut led_back(p16); // test led for Back_sensor
+DigitalOut led_left(p15); // test led for Back_sensor
+DigitalOut led_right(p14); // test led for Back_sensor
+DigitalOut led_front(p13); // test led for Back_sensor
+
+AnalogIn irBack_ain(p18);
+AnalogIn irFront_ain(p17);
+AnalogIn irLeft_ain(p20);
+AnalogIn irRight_ain(p19);
+
+//SHARPIR sensor_back(p18); //the output of the sharpIR sensor is connected to the MBEDs pin 10.
+
+//uLCD_4DGL uLCD(p28, p27, p29); // create a global lcd object
+
+//speed => 1 = forward, -1 = reverse ... range from -1 to 1
+
+int main() {
+    //uLCD.cls();
+     float backsensor = 0.0;
+     float frontsensor = 0.0;
+     float leftsensor = 0.0;
+     float rightsensor = 0.0;
+     int sensor = 0;
+    while(1){
+        
+        
+        
+        
+        
+        //float backsensor = sensor_back.cm();
+        
+        backsensor = irBack_ain * 100;
+//        pc.printf("FRONT Sensor is: %f \n", backsensor);
+        frontsensor = irFront_ain * 100;
+        leftsensor = irLeft_ain * 100;
+        rightsensor = irRight_ain * 100;
+        //pc.printf("Front Sensor is: %f \n", frontsensor);
+//        pc.printf("Left Sensor is: %f \n", leftsensor);
+//        pc.printf("Right Sensor is: %f \n", rightsensor);
+//        
+        if((frontsensor > 52)&&(frontsensor < 100)){
+            sensor = 2;}
+        else if((backsensor > 52)&&(backsensor < 100)){
+            sensor = 3;}
+        else if((leftsensor > 52)&&(leftsensor < 100)){
+            sensor = 1;}
+         else if((rightsensor > 52)&&(rightsensor < 100)){
+            sensor = 0;}
+        
+        
+        switch(sensor) 
+        {
+            case 0: //Right
+                mA.speed(.6);
+                mB.speed(.9);  
+                led_right = 1;
+                break;
+            case 1: // Left
+                mA.speed(.9);
+                mB.speed(.6);  
+                led_left = 1;
+                break;
+            case 2: // Front
+                 mA.stop(0.5);
+                 mB.stop(0.5);
+                mA.speed(-.6);
+                mB.speed(-.8); 
+                led_front = 1;
+                mA.stop(0.5);
+                 mB.stop(0.5);
+
+                mA.speed(.2);
+                mB.speed(.2);
+                wait(0.5);
+                break;    
+            case 3: // Back
+                mA.speed(.4);
+                mB.speed(.4);
+                wait(0.5);
+                 mA.stop(0.5);
+                 mB.stop(0.5);
+                mA.speed(.8);
+                mB.speed(.8);  
+                led_back = 1;
+                break;
+            default ://
+//                mA.speed(.5);
+//                mB.speed(.5);
+//                wait(0.5);
+//                 mA.stop(0.3);
+//                 mB.stop(0.3);
+                mA.speed(0.9);
+                mB.speed(0.9);
+                led_left = 0;
+                led_back = 0;
+                led_front = 0;
+                led_right = 0;
+                 break; 
+        }
+                led_left = 0;
+                led_back = 0;
+                led_front = 0;
+                led_right = 0;
+        sensor = 4; //DEFAULT CONDITION
+       // Back Sensor
+       // if((backsensor > 52)&&(backsensor < 100)){
+//            mA.speed(.8);
+//            mB.speed(.8);  
+//            led_back = 1;
+//                             
+//        }
+//        // front Sensor
+//        else if((frontsensor > 52)&&(frontsensor < 100)){
+//            mA.speed(-1);
+//            mB.speed(-1);  
+//            led_front = 1;
+//                             
+//        }else
+//        // right Sensor
+//        if((rightsensor > 52)&&(rightsensor < 100)){
+//            mA.speed(.4);
+//            mB.speed(.6);  
+//            led_right = 1;
+//                             
+//        }else // Left Sensor
+//        if((leftsensor > 52)&&(leftsensor < 100)){
+//            mA.speed(.6);
+//            mB.speed(.4);  
+//            led_left = 1;
+//                             
+//        }else{
+//             mA.speed(1);
+//           mB.speed(1);
+//            led_left = 0; 
+//            led_back = 0;
+//            led_front = 0;
+//            led_right = 0;
+//        }     
+        
+        
+        wait(0.1);
+    }
+}
\ No newline at end of file