Friday, June 25, 2021

Project 9 Arduino Obstacle Avoiding Robot

Project 9:  Arduino Obstacle Avoiding Robot

Main Ideas:

Obstacle Avoiding Robot is an intelligent device which can automatically sense the obstacle in front of it and avoid them by turning itself in another direction. This design allows the robot to navigate in unknown environment by avoiding collisions, which is a primary requirement for any autonomous mobile robot. An obstacle avoiding robot is a fully autonomous robot which can be able to avoid any obstacle which it face when it move, to sense the obstacle in front we are using the Ultra Sonic HC-SR04 Sensor . Also the Robot can sense the obstacle in back side using Infrared sensor (IR) , so while moving back if there is an obstacle robot will stop and we will try to change the path to avoid the  obstacle.






Drive Logic 

As high level logic used to drive the robot , first robot will check the front obstacle using Ultra Sonic HC-SR04 Sensor if no obstacle in 50 cm range the robot will run in high speed and keep looking for front obstacle, if an obstacle exist in range 35 – 50 cm then the robot will continue to move forward but will reduce the speed , so this will later allow robot to stop smoothly before hitting the  obstacle, if the obstacle less than 35 cm robot will stop moving, then robot will move back and turn left and check the front obstacle to define the speed (high / low) and then move forward.

While robot moving back or turning left or right we have used to Infrared sensor (IR) to make sure robot will not hit back obstacle while moving back or turning.

Narrow path Maneuver

Also we have other mode to handle the narrow path , where the front obstacle is less than 35 , in this case as per our logic explained above robot will try to move back but , if there is an obstacle back then what robot should do ? here we have other logic , robot will move forward but in slow spend for 10 cm then try to turn left , and robot will keep trying this maneuver until robot find a big space to move then robot will go back to the normal logic explained above.




/*
 * Main Ideas: 
Obstacle Avoiding Robot is an intelligent device which can automatically sense the obstacle in front of it 
and avoid them by turning itself in another direction. This design allows the robot to navigate in unknown environment 
by avoiding collisions, which is a primary requirement for any autonomous mobile robot. An obstacle avoiding robot is a fully autonomous 
robot which can be able to avoid any obstacle which it face when it move, to sense the obstacle in front we are using the Ultra Sonic HC-SR04 Sensor 
. Also the Robot can sense the obstacle in back side using Infrared sensor (IR) , so while moving back if there is an obstacle robot will 
stop and we will try to change the path to avoid the  obstacle.

 */
#include <Ultrasonic.h>

Ultrasonic ultrasonicFront(12, 13, 20000UL); // (Trig PIN,Echo PIN) Echo PIN:13 yellow wire, Trig PIN=12 Green Wire
float distanceCmFront;
int ultrastoplimit=20; // define when to stop 
int ultrareducespeedlimit=40; //define when to reduce speed
int currentStatus;

 //move controls
float timeToMoveBack5CM=500;
float timeToMoveBack2CM=250;
float timeToTrunRight5CM=200;
float timeToTrunLeft5CM=200;
float timeToMoveForward10CM=200;

int BackObstacleL;
int IRBL = 8;    // Back LeftIR sensor output pin Blue wire connected to digital pin 53

//Uno PWM pins 3, 5, 6, 9, 10, and 11

//New Motors Pins
//Motor New A Right Side
#define enA 6 //PWM Gray  enable A 
#define in1 4 //Purple IN1
#define in2 7 //Blule IN2

//Motor New B Left Side
#define enB 5 //PWM Orange Enable B
#define in3 2 // Green IN3
#define in4 3 // Blule IN4

int speedMotorA; // control the speed of New motor -right side 
int speedMotorB;// control the speed of New motor -left side

void setup() {
  // initialize serial communication at 9600 bits per second:
 // Serial.begin(9600);
 // Serial.println("start");
// put your setup code here, to run once:
distanceCmFront=0;
currentStatus=1;
//set IR Pin As Input
  pinMode(IRBL, INPUT);
  
//Set Motor A left side pins as output so we can contol the motor
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

 //Motor B New right side pins as output so we can contol the motor
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  //set initial speed for the motors
  speedMotorA=190; // control the speed of motor -left side 
  speedMotorB=190;// control the speed of motor -right side
}

void loop() {
  // put your main code here, to run repeatedly:
  MoveLogic();
  //moveForward();
  //moveBackword5CM();
  //TrunRight5CM();
  //moveForwardone();
}

void MoveLogic()
{
  getStatus();
  switch (currentStatus)
    {
    case 1:
      // // no obstacle in front or back move forward
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
      moveForward();
      break;
    case 2:
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
      // // near front obstacle reduce speed
      moveForwardReduceSpeed();
      break;
    case 3:
      //Serial.print("currentStatus ");
      //Serial.println(currentStatus);
       /*
      Physical Locations :front obstacle
      Logic: Stop move back trun to  right
      */
      stopmove();
      moveBackword5CM();
      TrunRight5CM();
      break;
    case 4:
    /*
    Physical Locations :front obstacle 30 far and there is Back Obstacle
    Logic:  move slowly to front trun to left
    */
    MoveForward10CM();
    TrunLeft5CM();
    break;  
    default: break; // do nothing
    } // switch (param)
}

void getStatus()
{
  ScanUltrasonicFront();
  if (  distanceCmFront > ultrareducespeedlimit) 
  {
    // no obstacle in front or back move forward
    currentStatus=1;
  }
  else if ( ((distanceCmFront > ultrastoplimit && distanceCmFront <= ultrareducespeedlimit))) 
  {
    // near front obstacle reduce speed
    currentStatus=2;
  }

  else if ( (distanceCmFront <= ultrastoplimit)) 
  {
    
     /*
    Physical Locations :front obstacle 30 far and there is Back Obstacle
    Logic:  move sloley to front trun to left
    */
    CheckIRBack();
    //Back Obstacle 
    if (BackObstacleL == LOW) 
    {
    currentStatus=4; //move forward 10 cm and trun to left
    }
    else
    {
      /*
    Physical Locations :front obstacle
    Logic: Stop move back trun to  right
    */
    currentStatus=3; //Stop move back trun to right
    }
    
  }
 
  //Serial.println(currentStatus);
}

void ScanUltrasonicFront ()
{

  distanceCmFront=ultrasonicFront.read(CM);
  //Serial.print(distanceCmFront); // CM or INC
  //Serial.println(" cm Down" );
  delay(50);
}

void ResetSpeedMotors()
{
  speedMotorA=220; // control the speed of motor -rigt side 
  speedMotorB=220;// control the speed of motor -left side
}

void ReduceSpeed()
{
  //set speed
  speedMotorA=150; // control the speed of motor -rigt side 
  speedMotorB=150;// control the speed of motor -left side
}

void moveForwardone()
  {
    ResetSpeedMotors(); 
   
    // run motor on right side       
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);     
   }
   
void moveForward()
  {
    ResetSpeedMotors(); 
   // run motor on left side   
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);  
    // run motor on right side       
   analogWrite(enB, speedMotorB);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);     
   }
void moveForwardReduceSpeed()
{
  ReduceSpeed(); 
   // run motor on left side   
    analogWrite(enA, speedMotorA);
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);  
    // run motor on right side       
   analogWrite(enB, speedMotorB);
   digitalWrite(in3, HIGH);
   digitalWrite(in4, LOW);     
}

void stopmove()
 {
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW); 
    analogWrite(enB, 0);

    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);  
    analogWrite(enA, 0);
          
    delay (100);
 }

 void MoveForward10CM()
  { 
  
    ReduceSpeed(); 
      unsigned long moveForward10CMStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveForward10CMStartTime)<(timeToMoveForward10CM))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
        // run motor on left side   
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);  
        // run motor on right side       
       analogWrite(enB, speedMotorB);
       digitalWrite(in3, HIGH);
       digitalWrite(in4, LOW);     
      }
  }


 void TrunLeft5CM()
  { 
    CheckIRBack();
    ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToTrunLeft5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
        // trun on right side front
        analogWrite(enA, speedMotorA);
         digitalWrite(in1, LOW);
         digitalWrite(in2, HIGH); 
      
      // turn on motor B left side revers 
       analogWrite(enB, speedMotorB); //analogWrite(enB, 0);// analogWrite(enB, speedMotorB);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        CheckIRBack();
      }
     
       // stop after moveback 
       stopmove();
   }


void TrunRight5CM()
  { 
    CheckIRBack();
    ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToTrunRight5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToTrunRight5CM ( seconds) has elapsed
      {
         // turn on motor A left in revers
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);  
      
       // run motor on right side  
        analogWrite(enB, speedMotorB);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);

         CheckIRBack();
      }
       // stop after moveback 
         //delay (400);
       stopmove();
   }


 void moveBackword5CM()
    {
       CheckIRBack();

      ResetSpeedMotors();
      unsigned long moveBackStartTime = millis(); // save the time that we started moveback
      while ((millis()-moveBackStartTime)<(timeToMoveBack5CM)&& (BackObstacleL == HIGH))// stay in this loop until timeToMoveBack10CM (1.1 seconds) has elapsed
      {
        analogWrite(enA, speedMotorA);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);  
      
      // turn on motor B
        analogWrite(enB, speedMotorB);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);

        CheckIRBack();// check for obstacles using IR sensors, value=1 no obstacles , value==0 obstacles exists
      }
       // stop after moveback 
       stopmove();
    }

void CheckIRBack()
{
   // HIGH MEANS NO OBSTACLE / LOW OBSTACLE
  //BackObstacleR = digitalRead(IRBR);
  BackObstacleL = digitalRead(IRBL);
 if (BackObstacleL == LOW) {
    //Serial.println(" Back Obstacle Left STOP");
  }
 else {
   //Serial.println("Back Obstacle Left Clear ");
  }
}


No comments:

Post a Comment