Ball Following ROBOT without Raspberry pi

Published Jun 22, 2020
 4 hours to build
 Intermediate

This robot will follow the ball by detecting its color and take turns accordingly. I use OpenCV for image processing and ESP8266 as main controller.

display image

Components Used

ESP8266 WiFi Module
ESP8266 is a system on chip (SoC) which provides WIFI capability for embedded applications. This enables internet connectivity to embedded applications. ESP8266 modules are mostly used in Internet of Things(IoT) applications.
1
Relay Module Two channel
Relay Module Two-channel
1
Connecting Wire Jumper Wires
Connecting Wire Breadboard wires
1
Lithium Ion Battery 3.7V 2500mAh 18650
Consumer Battery & Photo Battery 3.7V 2500mAh
2
DC Gearbox Motor
Adafruit Accessories DC Gearbox Motor - TT Motor - 200RPM - 3 to 6VDC
4
Description

In this tutorial, we are going to make a car that can follow a ball with a very easy method.

Project Flow 

  1. My mobile, mounted on car, sends the video to the Laptop or PC via local host, "ipwebcam"
  2. Python code in the laptop process the video, which is nothing but the processing image frames.
  3. After processing the image, python code sends command to the car i.e. Left, right, start, stop.
  4. Car has NodeMcu and hardware, which drives the car accordingly.

Please note: here I am not using Laptop+ Mobile camera, instead  you can also use Raspberry pi+ Webcam

Project Flow 

 

Hardware in the Car

  • Relay driver module is controlled by NodeMCU.
  • Relay controls the motors.
  • Note the connections are made with relay, such that it will drive motors clockwise and anticlockwise. (Check video for details).
Circuit Diagram

Assembly of Car

Car Assembly

 

Code

Programming is simple, attached below.

Just update your SSID and Password fields.

#define STASSID "dot in"         // change your ssid and password
#define STAPSK  "12344321mj"

 

You will find the code for controlling via server.

When the server is ON with "/forward", code will execute the void forward() function.  

server.on("/forward", forward);
  server.on("/back", backward);
  server.on("/left", right);
  server.on("/right", left);
  server.on("/stop", Stop);

 

in void forward function(), Controller controls the relay and rotate the motor in the forward direction.

void forward() {
  server.send(200, "text/plain", "move Forward");
  digitalWrite(m11, 1);
  digitalWrite(m12, 0);
  digitalWrite(m21, 1);
  digitalWrite(m22, 0);
}

 

Image Processing

Install and import following libraries.

import requests
import urllib

 

Before starting, install app "IP Webcam" on your Android phone. Open the App -> go to Start server --> in the bottom, you will find the IP address. Note it and write it in the code, Line no 6.

import cv2
import numpy as np
import requests
import urllib
# cap = cv2.VideoCapture(0)
url = "http://192.168.43.1:8080" # Your url might be different, check the app
cap = cv2.VideoCapture(url+"/video")
def rescale_frame(frame, percent=75):

 

For detection of the ball, I have used ball color values as below. You can update it as per your ball color.

lower_yellow = np.array([25,129,82])
	upper_yellow = np.array([150,255,255])

 

You can see, when the ball gets detected it shows the green rectangle around it.

Ball detection

We need center coordinate of the ball, so as we know the formula, I have used it to get the value over here.

x,y,w,h = cv2.boundingRect(contour)
			frame = cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0),2)
			print("x=",(x + (x+w))/2)
			print("y=",(y + (y+h))/2)
			getx=(x + (x+w))/2
			gety=(y + (y+h))/2 

Based on condition code will create the command for Left, right, forward and stop.

 

Note: Make sure your Phone, Laptop, NodeMCU connected over same Wi-Fi. You can use your Mobile hotspot as well. 

For details watch this Video

Video

 

Check My YouTube Channel here.

Check my blog here.

Codes

Downloads

ipcar3img Download
Comments
Ad