0% found this document useful (0 votes)
240 views

Programming Codes Obstacle Avoiding Robocar

This document describes code for an obstacle avoiding robocar and parallel parking program. It includes functions for controlling motors, reading ultrasonic sensor distances, and moving the robocar forward, turning, and parking between two obstacles. The code uses ultrasonic sensors and servo motors to guide the robocar in avoiding obstacles and parking between two spaces. Motors are controlled to slowly accelerate, turn in the appropriate direction based on sensor readings, and decelerate based on distance readings to safely navigate and park the robocar.

Uploaded by

Bryan Wong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
240 views

Programming Codes Obstacle Avoiding Robocar

This document describes code for an obstacle avoiding robocar and parallel parking program. It includes functions for controlling motors, reading ultrasonic sensor distances, and moving the robocar forward, turning, and parking between two obstacles. The code uses ultrasonic sensors and servo motors to guide the robocar in avoiding obstacles and parking between two spaces. Motors are controlled to slowly accelerate, turn in the appropriate direction based on sensor readings, and decelerate based on distance readings to safely navigate and park the robocar.

Uploaded by

Bryan Wong
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

PROGRAMMING CODES

Obstacle Avoiding Robocar


#include <AFMotor.h> //Import library to control motor shield
#include <Servo.h> //Import library to control the servo

AF_DCMotor rightBack(1); //Create an object to control each motor


AF_DCMotor rightFront(2);
AF_DCMotor leftFront(3);
AF_DCMotor leftBack(4);
Servo servoLook; //Create an object to control the servo

byte trig = 2; //Assign the ultrasonic sensor pins


byte echo = 13;
byte maxDist = 150; //Maximum sensing distance (Objects further than this
distance are ignored)
byte stopDist = 50; //Minimum distance from an object to stop in cm
float timeOut = 2*(maxDist+10)/100/340*1000000; //Maximum time to wait for a return signal

byte motorSpeed = 55; //The maximum motor speed


int motorOffset = 10; //Factor to account for one side being more powerful
int turnSpeed = 50; //Amount to add to motor speed when turning

void setup()
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
servoLook.attach(10); //Assign the servo pin
pinMode(trig,OUTPUT); //Assign ultrasonic sensor pin modes
pinMode(echo,INPUT);
}

void loop()
{
servoLook.write(90); //Set the servo to look straight ahead
delay(750);
int distance = getDistance(); //Check that there are no objects ahead
if(distance >= stopDist) //If there are no objects within the stopping distance,
move forward
{
moveForward();
}
while(distance >= stopDist) //Keep checking the object distance until it is within
the minimum stopping distance
{
distance = getDistance();
delay(250);
}
stopMove(); //Stop the motors
int turnDir = checkDirection(); //Check the left and right object distances and get the
turning instruction
Serial.print(turnDir);
switch (turnDir) //Turn left, turn around or turn right depending on the
instruction
{
case 0: //Turn left
turnLeft (400);
break;
case 1: //Turn around
turnLeft (700);
break;
case 2: //Turn right
turnRight (400);
break;
}
}

void accelerate() //Function to accelerate the motors from 0 to full speed


{
for (int i=0; i<motorSpeed; i++) //Loop from 0 to full speed
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}

void decelerate() //Function to decelerate the motors from full speed to zero
{
for (int i=motorSpeed; i!=0; i--) //Loop from full speed to 0
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}

void moveForward() //Set all motors to run forward


{
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
}

void stopMove() //Set all motors to stop


{
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}

void turnLeft(int duration) //Set motors to turn left for the specified duration
then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(BACKWARD);
leftBack.run(BACKWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);

void turnRight(int duration) //Set motors to turn right for the specified
duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}

int getDistance() //Measure the distance to an object


{
unsigned long pulseTime; //Create a variable to store the pulse travel time
int distance; //Create a variable to store the calculated distance
digitalWrite(trig, HIGH); //Generate a 10 microsecond pulse
delayMicroseconds(10);
digitalWrite(trig, LOW);
pulseTime = pulseIn(echo, HIGH, timeOut); //Measure the time for the pulse to return
distance = (float)pulseTime * 340 / 2 / 10000; //Calculate the object distance based on the
pulse time
return distance;
}

int checkDirection() //Check the left and right directions and decide
which way to turn
{
int distances [2] = {0,0}; //Left and right distances
int turnDir = 1; //Direction to turn, 0 left, 1 reverse, 2 right
servoLook.write(180); //Turn servo to look left
delay(500);
distances [0] = getDistance(); //Get the left object distance
servoLook.write(0); //Turn servo to look right
delay(1000);
distances [1] = getDistance(); //Get the right object distance
if (distances[0]>=200 && distances[1]>=200) //If both directions are clear, turn left
turnDir = 0;
else if (distances[0]<=stopDist && distances[1]<=stopDist) //If both directions are blocked,
turn around
turnDir = 1;
else if (distances[0]>=distances[1]) //If left has more space, turn left
turnDir = 0;
else if (distances[0]<distances[1]) //If right has more space, turn right
turnDir = 2;
return turnDir;
}
PARALLEL PARKING
#include <NewPing.h>
#define TRIGGER_PIN1 13
#define ECHO_PIN1 12
#define MAX_DISTANCE1 200
NewPing sonar1 ( TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE1);
int d1;
void Read () ;
#define TRIGGER_PIN2 11
#define ECHO_PIN2 10
#define MAX_DISTANCE2 200
NewPing sonar2 ( TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE2);
int d2;

#define TRIGGER_PIN3 9
#define ECHO_PIN3 8
#define MAX_DISTANCE3 200
NewPing sonar3 ( TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE3);
int d3;

#define TRIGGER_PIN4 7
#define ECHO_PIN4 6
#define MAX_DISTANCE4 200
NewPing sonar4 ( TRIGGER_PIN4, ECHO_PIN4, MAX_DISTANCE4);
int d4;

int in1LF = 2;
int in2LR = 3;
int in3RR = 4;
int in4RF = 5;

void setup() {

pinMode(in1LF, OUTPUT);
pinMode(in2LR, OUTPUT);
pinMode(in3RR, OUTPUT);
pinMode(in4RF, OUTPUT);
Serial.begin (9600);
}

void loop()
{
Read();
if ((d1 < 10 && d3 < 15 && d4 < 15) || (d2 < 5 && d3 < 15 && d4 < 15)) {
digitalWrite (in1LF, LOW);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, LOW);
}
else if (d1 > 10) {
digitalWrite (in1LF, HIGH);
digitalWrite (in2LR, LOW);
digitalWrite (in3RR, LOW);
digitalWrite (in4RF, HIGH);
if (d3 < 55 && d3 > 27) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1) {
digitalWrite(in1LF, HIGH);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);
Read ();
if (d1 > 20 && d3 > 40)
{ break;
}

}
}
else if (d4 < 55 && d4 > 27) {

digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1) {
digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, HIGH);
digitalWrite(in2LR, HIGH);
Read ();
if (d1 > 20 && d4 > 40)
{ break;
}
}
}
}
else if (d1 < 10 && d2 > 70 && d3 < 15 && d4 < 15)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, LOW);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, LOW);

while (1)
{ digitalWrite(in1LF, LOW);
digitalWrite(in3RR, HIGH);
digitalWrite(in4RF, LOW);
digitalWrite(in2LR, HIGH);
Read ();
if (d2 > 10)
{ break;
}
}
}
}
void Read()
{
int uS1 = sonar1.ping();
Serial.print("pingF: ");
Serial.print(uS1 / US_ROUNDTRIP_CM);
Serial.println("cm ");
d1 = uS1 / US_ROUNDTRIP_CM;

int uS2 = sonar2.ping();


Serial.print("pingB ");
Serial.print(uS2 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d2 = uS2 / US_ROUNDTRIP_CM;

int uS3 = sonar3.ping();


Serial.print("pingR: ");
Serial.print(uS3 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d3 = uS3 / US_ROUNDTRIP_CM;

int uS4 = sonar4.ping();


Serial.print("pingL: ");
Serial.print(uS4 / US_ROUNDTRIP_CM);
Serial.print("cm ");
d4 = uS4 / US_ROUNDTRIP_CM;
}
ROBOTIC ARM
/*NOTE: Four Servos of the Robotic Arm are connected to 4 PWM Pins of Arduino
and these 4 servos are named a, b, c and d.

If you want to control servo a, then type "90a/",


where "90" is the PWM value (range is 0 - 255),
"a" means servo a and "/" is string parse delimiter.

Some other examples: 100a/ or 120b/ or 40c/ or 25d/


*/

String readString;
int x=90, y=90, z=90, p=90;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;

void setup()
{
Serial.begin(9600);

myservoa.attach(3);
myservob.attach(5);
myservoc.attach(6);
myservod.attach(9);
myservoa.write(x);
myservob.write(y);
myservoc.write(z);
myservod.write(p);
}

void loop()
{
if (Serial.available())
{
char m = Serial.read();
if (m == '/')
{
if (readString.length() >1)
{
Serial.println(readString);

int temp = readString.toInt();

Serial.print("writing Angle: ");


Serial.println(temp);
if(readString.indexOf('a') >=0)
{
if (temp>x)
{
for (int i=x; i<temp; i++)
{
myservoa.write(i);
delay(10);
}
x=temp;
}
else
{
for (int i=x; i>temp; i--)
{
myservoa.write(i);
delay(30);
}
}
x=temp;
}
//////////////////////////////////////////////////////////////////////////////
if(readString.indexOf('b') >=0)
{
if (temp>y)
{
for (int i=y; i<temp; i++)
{ myservob.write(i);
delay(10);
}
y=temp;
}
else
{
for (int i=y; i>temp; i--)
{
myservob.write(i);
delay(10);
}
y=temp;
}
}
///////////////////////////////////////////////////////////////////////
if(readString.indexOf('c') >=0) //myservoc.write(n);
{
if (temp>z)
{
for (int i=z; i<temp; i++)
{myservoc.write(i);
delay(10);}
z=temp;
}
else
{
for (int i=z; i>temp; i--)
{
myservoc.write(i);
delay(10);
}
z=temp;
}
}
/////////////////////////////////////////////////////
if(readString.indexOf('d') >=0)
{
if (temp>p)
{
for (int i=p; i<temp; i++)
{
myservod.write(i);
delay(10);
}
p=temp;
}
else
{
for (int i=p; i>temp; i--)
{
myservod.write(i);
delay(30);
}
}
p=temp;
}

readString="";
}
}
else
{
readString += m;
}
}
}

You might also like