Obstacle avoiding robot.

The Project

This project is for my Artificial Intelligence subject in my school and it's my first Arduino project, so I would like to share this project with you.

Step 1

First build the Arduino robot chassis. The kit contains the chassis, two motors with the wheels attached, a front wheel, a battery holder, some screws and wires.


Step 2

Solder the thick black and red wires at the motors.

Step 3

Attach the front wheel. After, attach the rear wheels according to the instructions leaflet that comes with kit.

Step 4

The next step is to attach the switch to the battery holder. Cut the battery holder black wire and solder the one end to separate side of the switch. Our switch is ready. Then attach it to the battery holder using the screw that comes with the chassis kit.

Step 5

The next step is to connect the motors. We connect the left motor to the M1 connector of the shield like this. We connect the right motor to the M3 connector of the shield this way.

Step 6

Move on to the motor shield. This shield provides power to the motors and the servo motor, and makes our project easier. The motors need a lot of current, and this shield can provide up to 600mA of current to each motor, that’s why we need it. We have to solder 4 wires to the motor shield. These wires will be then connected with the supersonic sensor. We solder one to 5V, one to GND, one to Analog Pin 5, and the last one to analog pin 4.

Step 7

Next we attach the motor shield to the Arduino Uno board. With a double sided tape, attach the Arduino Uno and the battery holder to the chassis. With a double sided tape, also attach the supersonic sensor to the servo motor, and both of them to the robot chassis. I used a hot glue gun to attach the servo motor to the chassis.

Supersonic Sensor

We need to follow these steps to configure the supersonic sensor.

Step 1

Supersonic sensor. Vcc goes to 5V via the wire we attached earlier. GND goes to the GND wire. ECHO pin goes to Analog Pin 5, and TRIG pin goes to Analog Pin 4.

Step 2

Next, to connect the power. We connect the black wire from the switch to the motor shield. It goes to the GND pin of this external power connector. The red wire from the switch goes to the M+ connector. If we open the switch, we can see that the green LED lights up, the Arduino is receiving power from the batteries.


Step 3

Lastly we connect the servo motor to the servo_2 slot. The darker color on the right end side red in the middle and orange in the left side order.


Code

#include<AFMotor.h>
#include<Servo.h>
#include<NewPing.h>
#define TRIG_PIN A4
#define ECHO_pin A5
#define MAX_DISTANCE_POSSIBLE 1000
#define MAX_SPEED 150//
#define MOTORS_CALIBRATION_OFFSET 3
#define COLL_DIST 20
#define TURN_DIST COLL_DIST+10
NewPing sonar(TRIG_PIN,ECHO_PIN,MAX_DISTANCE_POSSIBLE);

AF_DCMotor leftMotor(4,MOTOR12_8KHZ);
AF_DCMotor rightMotor(3,MOTOR12_8KHZ);
Servo neckControllerServoMotor;

int pos=0;
int maxDist=0;
int maxAngle=0;
int maxRight=0;
int maxLeft=0;
int maxFront=0;
int course=0;
int curDist=0;
String motorSet="";
int speedSet=0;


void setup()
{
neckControllerServoMotor.attach(10);
neckControllerServoMotor.write(90);
delay(2000);
checkPath();
motorSet="FORWARD";
neckControllerServoMotor.write(90);
moveForward();
}


void loop()
{
checkForward();
checkPath();
}

void checkPath()
{
int curLeft=0;
int curFront=0;
int curRight=0;
int curDist=0;
neckControllerServoMotor.write(144);
delay(120);
for(pos=144;pos>=36;pos=18)
{
neckControllerServoMotor.write(pos);
delay(90);
checkForward();
curDist=readPing();
if(curDist<COLL_DIST)
{
checkCourse();
break;
}
if(curDist<TURN_DIST)
{
changePath();
}
if(curDist>curDist){maxAngle=pos;}
if(pos>90&&curDist>curLeft){curLeft=curDist;}
if(pos==90&&curDist>curFront){curFront=curDist;}
if(pos<90&&curDist>curRight){curRight=curDist;}
}
maxLeft=curLeft;
maxRight=curRight;
maxFront=curFront;
}

void setCourse()
{
if(maxAngle<90){turnRight();}
if(maxAngle>90){turnLeft();}
maxLeft=0;
maxRight=0;
maxFront=0;
}

void chechCourse()
{
if(maxAngle<90){turnRight();}
if(maxAngle>90){turnLeft();}
maxLeft=0;
maxRight=0;
maxFront=0;
}

void checkCourse()
{
moveBackward();
delay(500);
moveStop();
setCourse();
}

void changePath()
{
if(pos<90){lookLeft();}
if(pos>90){lookRight();}
}

int readPing()
{
delay(70);
unsigned int uS=sonar.Ping();
int cm=uS/US_ROUNDTRIP_CM;
return cm;
}

void checkForward() {if(motorSet=="FORWARD"){leftMotor.run(FORWARD);rightMotor.run(FORWARD);}}

void checkBackward(){if(motorSet=="BACKWARD"){leftMotor.run(BACKWARD);rightMotor.run(BACKWARD);}}

void moveStop(){leftMotor.run(RELEASE);rightMotor.run(RELEASE);}

void  moveForward()
{
motorSet="FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
for(speedSet=0;speedSet<MAX_SPEED;speedSet+=2)
{
leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET);
rightMotor.setSpeeed(speedSet);
delay(5);
}
}

void moveBackward()
{
motorSet="BACKWARD";
leftMotor.run(BACKWARD);
rightMotor.run(BACKWARD);
for(speedSet=0;speedSet<MAX_SPEED;speedSet+=2)
{
leftMotor.setSpeed(speedset+MOTORS_CALIBRATION_OFFSET);
rightMotor.setSpeed(speedSet);
delay(5);
}
}

void turnRight()
{
motorSet="RIGHT";
leftMotor.run(FORWARD);
rightMotor.run(BACKWARD);
delay(400);
motorSet="FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
}

void turnLeft()
{
motorSet="LEFT";
leftMotor.run(BACKWARD);
rightMotor.run(FORWARD);
delay(400);
motorSet="FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
}

void lookRight() {rightMotor.run(BACKWARD);delay(400);rightMotor.run(FORWARD);}
void lookLeft() {leftMotor.run(BACKWARD);delay(400);leftMotor.run(FORWARD);}

Comments

Post a Comment

Popular Posts