Ball following robot without using Raspberry pi | Amazing Mj #amazingmj
In this tutorial, we are going to make a car that can follow a ball with a very easy method.
first, you need to watch this video for understand the flow diagram of the working process.
video link:-
Here I attach the circuit diagram and codes.
circuit diagram :
code for Node MCU (esp8266) :
//start
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "dot in" // change your ssid and password
#define STAPSK "12344321mj"
#endif
#define m11 D1
#define m12 D2
#define m21 D3
#define m22 D6
const char* ssid = STASSID;
const char* password = STAPSK;
String message = "";
ESP8266WebServer server(80);
void forward() {
server.send(200, "text/plain", "move Forward");
digitalWrite(m11, 1);
digitalWrite(m12, 0);
digitalWrite(m21, 1);
digitalWrite(m22, 0);
}
void backward() {
server.send(200, "text/plain", "move Backward");
digitalWrite(m11, 0);
digitalWrite(m12, 1);
digitalWrite(m21, 0);
digitalWrite(m22, 1);
}
void left() {
server.send(200, "text/plain", "move Left");
digitalWrite(m11, 1);
digitalWrite(m12, 0);
digitalWrite(m21, 0);
digitalWrite(m22, 0);
}
void right() {
server.send(200, "text/plain", "move Right");
digitalWrite(m11, 0);
digitalWrite(m12, 0);
digitalWrite(m21, 1);
digitalWrite(m22, 0);
}
void Stop() {
server.send(200, "text/plain", "Stop");
digitalWrite(m11, 0);
digitalWrite(m12, 0);
digitalWrite(m21, 0);
digitalWrite(m22, 0);
}
void setup(void) {
WiFi.softAPdisconnect(true);
WiFi.disconnect(true);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(m21, OUTPUT);
pinMode(m22, OUTPUT);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/forward", forward);
server.on("/back", backward);
server.on("/left", right);
server.on("/right", left);
server.on("/stop", Stop);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
// end of the code
code for image processing :
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):
width = int(frame.shape[1] * percent/ 100)
height = int(frame.shape[0] * percent/ 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frame = cv2.line(frame,(200,0),(200,600),(255,0,0),2)
frame = cv2.line(frame,(740,0),(740,600),(255,0,0),2)
frame = cv2.line(frame,(200,250),(740,250),(255,0,0),2)
# width = vs.get(3)
# height = vs.get(4)
# print("width",width)
# print("hight=",height)
lower_yellow = np.array([25,129,82])
upper_yellow = np.array([150,255,255])
yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
(_,contours,_) = cv2.findContours(yellow_mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if(area > 800):
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
if(getx<200):
print("left")
r = requests.get('http://192.168.43.11/left')
# print(r.content)
break
if(getx>740):
print("right")
r = requests.get('http://192.168.43.11/right')
# print(r.content)
break
if((740>getx>200) and gety<250):
print("forward")
r = requests.get('http://192.168.43.11/forward')
# print(r.content)
break
# if(gety>300 ):
# print("back")
# r = requests.get('http://192.168.43.11/back')
# # print(r.content)
# break
if((740>getx>200) and gety>250):
print("stop")
r = requests.get('http://192.168.43.11/stop')
# print(r.content)
break
cv2.imshow("tracking", frame)
cv2.imshow("color",yellow_mask )
k = cv2.waitKey(5) & 0XFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "dot in" // change your ssid and password
#define STAPSK "12344321mj"
#endif
#define m11 D1
#define m12 D2
#define m21 D3
#define m22 D6
const char* ssid = STASSID;
const char* password = STAPSK;
String message = "";
ESP8266WebServer server(80);
void forward() {
server.send(200, "text/plain", "move Forward");
digitalWrite(m11, 1);
digitalWrite(m12, 0);
digitalWrite(m21, 1);
digitalWrite(m22, 0);
}
void backward() {
server.send(200, "text/plain", "move Backward");
digitalWrite(m11, 0);
digitalWrite(m12, 1);
digitalWrite(m21, 0);
digitalWrite(m22, 1);
}
void left() {
server.send(200, "text/plain", "move Left");
digitalWrite(m11, 1);
digitalWrite(m12, 0);
digitalWrite(m21, 0);
digitalWrite(m22, 0);
}
void right() {
server.send(200, "text/plain", "move Right");
digitalWrite(m11, 0);
digitalWrite(m12, 0);
digitalWrite(m21, 1);
digitalWrite(m22, 0);
}
void Stop() {
server.send(200, "text/plain", "Stop");
digitalWrite(m11, 0);
digitalWrite(m12, 0);
digitalWrite(m21, 0);
digitalWrite(m22, 0);
}
void setup(void) {
WiFi.softAPdisconnect(true);
WiFi.disconnect(true);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(m21, OUTPUT);
pinMode(m22, OUTPUT);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/forward", forward);
server.on("/back", backward);
server.on("/left", right);
server.on("/right", left);
server.on("/stop", Stop);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
// end of the code
code for image processing :
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):
width = int(frame.shape[1] * percent/ 100)
height = int(frame.shape[0] * percent/ 100)
dim = (width, height)
return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
while True:
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frame = cv2.line(frame,(200,0),(200,600),(255,0,0),2)
frame = cv2.line(frame,(740,0),(740,600),(255,0,0),2)
frame = cv2.line(frame,(200,250),(740,250),(255,0,0),2)
# width = vs.get(3)
# height = vs.get(4)
# print("width",width)
# print("hight=",height)
lower_yellow = np.array([25,129,82])
upper_yellow = np.array([150,255,255])
yellow_mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
(_,contours,_) = cv2.findContours(yellow_mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if(area > 800):
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
if(getx<200):
print("left")
r = requests.get('http://192.168.43.11/left')
# print(r.content)
break
if(getx>740):
print("right")
r = requests.get('http://192.168.43.11/right')
# print(r.content)
break
if((740>getx>200) and gety<250):
print("forward")
r = requests.get('http://192.168.43.11/forward')
# print(r.content)
break
# if(gety>300 ):
# print("back")
# r = requests.get('http://192.168.43.11/back')
# # print(r.content)
# break
if((740>getx>200) and gety>250):
print("stop")
r = requests.get('http://192.168.43.11/stop')
# print(r.content)
break
cv2.imshow("tracking", frame)
cv2.imshow("color",yellow_mask )
k = cv2.waitKey(5) & 0XFF
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
Tips and trick (things to remember) :
1. Make sure your hardware is perfect.
2. node MCU, mobile, and laptop connected in the same wifi.
3. when you copy the python code takes care of the indentation error.
4. if it's not working feel free to contact me on the @amazing_mj_official Instagram page.
Think Amazing
Be Amazing
3. when you copy the python code takes care of the indentation error.
4. if it's not working feel free to contact me on the @amazing_mj_official Instagram page.
Think Amazing
Be Amazing
Comments
Post a Comment