Monday, July 9, 2012

Obstacle distance sensor


Obstacle distance sensor is one of the most ground breaking projects which was undertaken by us in the
field of embedded system.

As we all know that world is analog, therefore analog computing is required more and more in some issues to bridge the gap between real time and tentative life.

In this project we have chosen arduino as our platform and atmega 328 as a microcontroller. We have used infrared sensors to detect existence of obstacle in front of robot and also to detect at what distance (in centimeters) the obstacle is detected .We have used interrupt timer in this project ,as the obstacle is detected ,the interrupt routine is executed. Once the interrupt routine gets executed, then first of all the distance is calculated out through another subroutine which defined inside it. After the distance is calculated the direction is determined out in which robot has to be moved and then accordingly the actuators are given control.

On the part of actuators we have used the theory of differential drive i.e. for example to turn sharp right left wheel will be rotated in forward direction and the right side motor is rotated in backward direction.

As a part of monitoring the distance of obstacle we have used serial communication between out robot and PC via IC RS 232.It can also be done by on board monitoring using LCD and send in the data to LCD.

In this entire project power supply used is of 5V in all the places except for motor IC, in which 12Vpower supply is used.
#include "TimerOne.h" // header file timer interrupt
int p,q,r;
void setup() // s1
{ // s0//___________\\s2
Serial.begin(9600); // | / \ | s0= left sensor
pinMode(10,OUTPUT); // 12----| / | \ |----13 s1= middle sensor
pinMode(11,OUTPUT); // | | | s2= right sensor
pinMode(12,OUTPUT); // 10----| | |----11 10,11= for backward movement
pinMode(13,OUTPUT); // |___________| 12,13 = for forward movement
Timer1.initialize(100000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{ p= distance_map(A0);
q= distance_map(A1); //here p,q,r are the distance(in centimeters) of obstacle from the left sensor,middle sensor and right sensor
r= distance_map(A2);
if((p>9)&&(q>9)&&(r>9))
Serial.print("no obstacle detected ");
else
{
Serial.print("distance of obstacle from LEFT sensor = ");
Serial.println(p); //NOTE: distance may vary from color to colour
Serial.print("distance of obstacle from MIDDLE sensor = ");
Serial.println(q);
Serial.print("distance of obstacle from RIGHT sensor = ");
Serial.println(r);
}
if((p>9)&&(q>9)&&(r>9))
{
digitalWrite(12,HIGH);
digitalWrite(13,HIGH); //Forward ( no obstacle detected )
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
else if((p>9)&&(q<9)&&(r>9))
{
digitalWrite(12,HIGH); // RIGHT (middle sensor have detected obstacle)
digitalWrite(13,LOW);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
}
else if(((p<9)&&(q>9)&&(r>9))||((p<9)&&(q<9)&&(r>9)))
{
digitalWrite(12,HIGH);
digitalWrite(13,LOW); //RIGHT (right obstacleis detected )
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
}
else if(((p>9)&&(q>9)&&(r<9))||((p>9)&&(q<9)&&(r<9)) )
{
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,HIGH); //LEFT (right obstacle is detected )
digitalWrite(13,HIGH);
}
else if(((p<9)&&(q<9)&&(r<9))||((p<9)&&(q>9)&&(r<9)))
{
digitalWrite(12,LOW);
digitalWrite(11,HIGH);
digitalWrite(10,HIGH); // BACKWARD (right and left obstacle is detected )
digitalWrite(13,LOW);
delay(100);
digitalWrite(12,LOW);
digitalWrite(11,LOW); //after BACKWARD LEFT TURN ( to deviate from path )
digitalWrite(10,HIGH);
digitalWrite(13,HIGH);
}
}
void loop()
{
}
int distance_map(int a)
{ int d;
int x;
x=analogRead(a) ;
//Serial.println(x);
if(x<35)
d=0;
else if((x>35)&&(x<50))
d=1;
else if((x>50)&&(x<250))
d=2;
else if((x>250)&&(x<600))
d=3;
else if((x>600)&&(x<650))
d=4;
else if((x>650)&&(x<700))
d=5;
else if((x>700)&&(x<720))
d=6;
else if((x>720)&&(x<750))
d=7;
else if((x>750)&&(x<770))
d=8;
else if((x>770)&&(x<800))
d=9;
else if((x>800)&&(x<810))
d=10;
else
d=11;
return d;
}
view raw analog_sensors hosted with ❤ by GitHub