I am working on a line following and obstacle avoidance robot. The aim is to ensure robot follows a line without deviating and move around an obstacle if detected and return back to the line. This should go in a loop and operate with different types of paths.
Line Following
I have successfully achieved the line following stage but the robot fails to follow the line at curves and sharp corners. The robot also does not go in a straight line at times.
Obstacle Avoidance
For obstacle avoidance, I have achieved this by making the robot turn right, turn left, turn left and turn right. In this method, the robot does not go back to the line but deviates away. From my research, I have understood that I need to use a PID library and DC motor with an encoder to get feedback to control the position of the motor.
I would like to know should I use a dc motor with an encoder or use a stepper motor for this operation. I am not clear on how to use a stepper motor to get the rotation and turning of the robot. Any advice is welcomed. Thank you in advance.
Code (Obtained from online)
#define echopin A4 // echo pin
#define trigpin A5 // Trigger pin
int motor_r2 = 9;
int motor_r1 = 10;
int motor_l2 = 5;
int motor_l1 = 6;
int speed = 115;
int frontdist;
long duration;
int setdist = 10;
int L_S = A0; //sincer L
int R_S = A1; //sincer R
void setup() {
pinMode(motor_l1, OUTPUT);
pinMode(motor_l2, OUTPUT);
pinMode(motor_r1, OUTPUT);
pinMode(motor_r2, OUTPUT);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT);
pinMode(L_S, INPUT);
pinMode(R_S, INPUT);
Serial.begin(9600);
delay(1000);
}
void loop() {
frontdist = data();
Serial.println(frontdist);
if (frontdist > setdist) {
if ((digitalRead(L_S) == 0) && (digitalRead(R_S) == 0)) {
forword();
}
if ((digitalRead(L_S) == 0) && (digitalRead(R_S) == 1)) {
turnRight();
}
if ((digitalRead(L_S) == 1) && (digitalRead(R_S) == 0)) {
turnLeft();
}
} else {
turnLeft();
delay(350);
forword();
delay(1000);
turnRight();
delay(200);
forword();
delay(500);
}
}
long data() {
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
duration = pulseIn (echopin, HIGH);
return duration / 29 / 2;
}
void stop() {
analogWrite(motor_l1, 0);
analogWrite(motor_l2, 0);
analogWrite(motor_r1, 0);
analogWrite(motor_r2, 0);
}
void forword() {
analogWrite(motor_l1, speed);
analogWrite(motor_l2, 0);
analogWrite(motor_r1, 0);
analogWrite(motor_r2, speed);
}
void backword() {
analogWrite(motor_l1, 0);
analogWrite(motor_l2, speed);
analogWrite(motor_r1, speed);
analogWrite(motor_r2, 0);
}
void turnRight() {
analogWrite(motor_l1, 0);
analogWrite(motor_l2, speed);
analogWrite(motor_r1, 0);
analogWrite(motor_r2, speed);
Hardware
L298N Driver
2 DC motor rear-wheel driven
1 castor wheel
Arduino Uno
2 IR sensor
1 HC-SR04 Ultrasonic Sensor
Summary
I want to make the robot go out of line if detected obstacle in front and return back to the line again with line following and obstacle avoidance.
I am not very familiar with Arduino projects related to motors but I have a fair amount of programming knowledge and experience in Python and C.