Obstacle avoiding car robot

Project Description

This project uses the Sharp IR distance sensor on the Shadow Robot to detect and avoid obstacles. The IR sensor is mounted to the front of the robot as seen in the picture below:

/media/uploads/mpereira30/20161102_181206.jpg

When the IR sensor detects an object in front of it, the mbed commands the car to halt. The servo motor then rotates the IR sensor which allows the sensor to scan a 90 degree range in front of the robot. The mbed evaluates the direction with the furthest distance and commands the robot to turn towards it.

Below is a video of the car avoiding a box and moving towards the direction with the least obstruction:

Schematic Diagram

/media/uploads/mpereira30/lab4-demo.png /media/uploads/mpereira30/20161102_181148.jpg

Code

Obstacle avoiding robot

#include "mbed.h" 
#include "Servo.h"
#include "Motor.h"

// Define the following constants (PWM commands) 
#define fwdA 0.4125
#define fwdB 0.5
#define revA -0.4125 {{/media/uploads/mpereira30/20161102_181148.jpg}} 
#define revB -0.5
#define RA 0.4125
#define RB -0.5
#define LA -0.4125
#define LB 0.5
#define turnRight 0.01 
#define turnLeft 0.0095 

Motor mB(p24, p11, p12); // pwm, fwd(in1), rev(in2) Motor B (right wheel)
Motor mA(p22, p14, p13); // pwm, fwd(in1), rev(in2) Motor A (left wheel)

AnalogIn ain(p20);
Servo myservo(p26);
Serial pc(USBTX, USBRX);


float cur_A;
float cur_B;
float max = ain;
float max_angle = 0.0;

void lookaround(void);

void go_fwd(float);
void go_rev(float);
void turn_R(float);
void turn_L(float);
void stop(float);

int main() 
{
   myservo = 0.65;
   while(1){
       while(ain<0.4)
       {
            go_fwd(0.2);    
       }
       stop(1);
       lookaround();
       if (max_angle >= 45.0) turn_R((max_angle - 45.0));
       else if (max_angle < 45.0) turn_L((45.0 - max_angle));
       myservo = 0.65;
    }
   
}

void go_fwd(float t)
{
    mA.speed(fwdA);    
    mB.speed(fwdB);
    cur_A = fwdA;
    cur_B = fwdB;
    wait(t);
}

void stop(float t)
{
    for (float x = 1.0; x>=0.01; x*=0.25)
    {
        mA.speed((x+0.1)*cur_A);
        mB.speed(x*cur_B);
    }
    cur_A = 0.0;
    cur_B = 0.0;
    mA.speed(cur_A);    
    mB.speed(cur_B);
    wait(t);
}

void go_rev(float t)
{
    mA.speed(revA);    
    mB.speed(revB);
    cur_A = revA;
    cur_B = revB;
    wait(t);
}

void turn_R(float deg)
{
    mA.speed(RA);    
    mB.speed(RB);
    cur_A = RA;
    cur_B = RB;
    float temp = deg*turnRight;
    if(temp>turnRight*90.0)temp = turnRight*90.0;   
    wait(temp);
}
    
void turn_L(float deg)
{
    mA.speed(LA);    
    mB.speed(LB);
    cur_A = LA;
    cur_B = LB;
    wait(deg*turnLeft);
}
 
void lookaround(void)
{
    max_angle = 0.0;
    max = ain;
    for(float p=0; p<1.0; p += 0.05) 
    {
        myservo = p;
        if (max > ain)
            {
                max = ain;
                max_angle = p;
            }
            
        wait(0.25);
    }
    
    if (max>0.45){
        turn_L(180.0);
        myservo = 0.65;
        wait(0.5);
        }
    else{        
    myservo = max_angle;
    wait(0.5);
    max_angle = max_angle*90.0 - 13.5;
    }    
    
}


Please log in to post comments.