Object Avoidance Program

Dependencies:   4DGL-uLCD-SE Motordriver mbed

main.cpp

Committer:
rokash000
Date:
2014-04-29
Revision:
6:c76078abc03e
Parent:
5:50887f863c01

File content as of revision 6:c76078abc03e:


#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((backsensor > 52)&&(backsensor < 100)){
            sensor = 3;}
        else if((frontsensor > 52)&&(frontsensor < 100)){
            sensor = 2;}
        else if((leftsensor > 52)&&(leftsensor < 100)){
            sensor = 1;}
         else if((rightsensor > 52)&&(rightsensor < 100)){
            sensor = 0;}
        
        
        switch(sensor) 
        {
            case 0: //Right
                mA.speed(.4);
                mB.speed(.6);
                wait(0.5);
                led_right = 1;
                break;
            case 1: // Left
                mA.speed(.6);
                mB.speed(.4);
                wait(0.5);  
                led_left = 1;
                break;
            case 2: // Front
                mA.speed(-.6);
                mB.speed(-.8);
                 wait(0.9);
                 mA.stop(0.5);
                 mB.stop(0.5);
                mA.speed(-.6);
                mB.speed(-.8);
                wait(0.9);
                mA.speed(.2);
                mB.speed(.2);
                wait(0.9); 
                led_front = 1;
                break;    
            case 3: // Back
                mA.speed(.4);
                mB.speed(.4);
                wait(0.5);
                 mA.stop(0.5);
                 mB.stop(0.5);
                mA.speed(.6);
                mB.speed(.6);  
                led_back = 1;
                break;
            default :
                mA.speed(.6);
                mB.speed(.6);
                wait(0.5);
                led_left = 0;
                led_back = 0;
                led_front = 0;
                led_right = 0;
                 break; 
        }
                
        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);
    }
}