Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Aasaialangaram S
Published © Apache-2.0

Lane Following Robot using OpenCV

Hi....A Lane Follower Robot, as the name suggests, is an autonomous vehicle, which follow a Lane.

IntermediateFull instructions provided5 hours17,218

Things used in this project

Story

Read more

Code

Arduino Code

C/C++
// assign pin num
int right_pin = 12;
int left_pin = 13;
int const ENA = 10;  
int const ENB = 11; 


// initial command
int command = 0;

void setup()

{
  pinMode(right_pin, OUTPUT);
  pinMode(left_pin, OUTPUT);
  pinMode(ENA, OUTPUT);   // set all the motor control pins to outputs
  pinMode(ENB, OUTPUT);

  Serial.begin(115200);
  
  while (!Serial);
  
  Serial.println("Opencv Self Driving Robot");

}

void loop() {

if (Serial.available())

{
  //int state = Serial.parseInt();
  int state = Serial.read();

  if (state == 4)
  {
    
    digitalWrite(left_pin, LOW);
    digitalWrite(right_pin, HIGH);
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);
    Serial.println("Left");
  }

  if (state == 2)
  {
    digitalWrite(left_pin, LOW);
    digitalWrite(right_pin, LOW);
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);
    Serial.println("Right");
    Serial.println("Reverse");
  }

  if (state == 3)
  {
  
    digitalWrite(left_pin, HIGH);
    digitalWrite(right_pin, LOW);
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);

  Serial.println("Right");
  }
  if (state == 1)
  {
  
    digitalWrite(left_pin, HIGH);
    digitalWrite(right_pin, HIGH);
    digitalWrite(ENA, HIGH);
    digitalWrite(ENB, HIGH);

  Serial.println("Forward");
  }
  if (state == 5)
  {
  
    digitalWrite(left_pin, LOW);
    digitalWrite(right_pin, LOW);
    digitalWrite(ENA, LOW);
    digitalWrite(ENB, LOW);

  Serial.println("Stop");
  }

}

}

Python Code

Python
import cv2
import numpy as np
import math
import serial
# import picamera
# import picamera.array

threshold1 = 85
threshold2 = 85
theta=0
r_width = 500
r_height = 300
minLineLength = 5
maxLineGap = 10
k_width = 5
k_height = 5
max_slider = 10

# Linux System Serial Port
# ser = serial.Serial("/dev/ttyACM0", 115200, timeout=1)           # linux

# Read Image
image = cv2.imread(r'C:\Users\aasai\Desktop\new1.jpeg')
# Resize width=500 height=300 incase of inputting raspi captured image
image = cv2.resize(image,(r_width,r_height))
# Convert the image to gray-scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# given input image, kernel width =5 height = 5, Gaussian kernel standard deviation
blurred = cv2.GaussianBlur(gray, (k_width, k_height), 0)
# Find the edges in the image using canny detector
edged = cv2.Canny(blurred, threshold1, threshold2)
# Detect points that form a line
lines = cv2.HoughLinesP(edged,1,np.pi/180,max_slider,minLineLength,maxLineGap)
print(lines[0])
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(image,(x1,y1),(x2,y2),(255,0,0),3)
        theta=theta+math.atan2((y2-y1),(x2-x1))
        print(theta)
threshold=5
if(theta>threshold):

       print("Go left")
if(theta<-threshold):

    print("Go right")
if(abs(theta)<threshold):

    print("Go straight")
theta=0
cv2.imshow("Gray Image",gray)
cv2.imshow("blurred",blurred)
cv2.imshow("Edged",edged)
cv2.imshow("Line Detection",image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Github Repository

Credits

Aasaialangaram S

Aasaialangaram S

6 projects • 16 followers
I am an Electronics Engineer & Passionate about electronics and Artificial Intelligence . Working as Embedded software developer.

Comments