Obstacle Avoiding Test , That White Wire is Cable to Supply power to Raspberry PI using Power Bank in Hand.
Requirement’s:
Requirement’s
L293d Motor Driver Module
Here, Microcontroller = Raspberry Pi ,
A1, A2 — inputs from microcontroller for motor 1B1, B2 — inputs from microcontroller for motor 2ENA — enable motor 1,ENB — enable motor 2.If ENA and ENB +5v — motors full speed,if ENA and ENB +2.5v — motors half speed and so on.If ENA and ENB 0v — motors stop.ENA, ENB — PWM inputs from microcontroller
A1 A2 ENA FunctionHigh High Low Turn Anti-clockwise (Reverse)High Low High Turn clockwise (Forward)High High High StopHigh Low Low StopLow X X Stop
2 MB 1 = for connecting Motor 2+ v — = power for motors “+” and “-”1 MA 2 = for connecting Motor 1
VCC -> 5V VoltsGND -> GROUNDA1 -> Direction Control signalsA2 -> Direction COntrol SignalsEn-B -> PWM Control(for speed control or motor enable/disable)B1 -> From ControllerB2 -> From Contoller
MotorA1 = 18MotorA2 = 16Motor1EA = 22
MotorB1 = 19MotorB2 = 21Motor2EB = 23
GND = to line — on breadboard (negative/ground), it should be btw — jumper wire(6 ground pin) and — terminal of battery
VCC = Connect to +ve terminal of battery on +ve line in breadboard
import RPi.GPIO as GPIOfrom time import sleep
GPIO.setmode(GPIO.BOARD)
Motor1A = 16Motor1B = 18Motor1E = 22
B1 = 19B2 = 21BE = 23
GPIO.setup(Motor1A,GPIO.OUT)GPIO.setup(Motor1B,GPIO.OUT)GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(B1,GPIO.OUT)GPIO.setup(B2,GPIO.OUT)GPIO.setup(BE,GPIO.OUT)
print "Turning motor on"GPIO.output(Motor1A,GPIO.HIGH)GPIO.output(Motor1B,GPIO.LOW)GPIO.output(Motor1E,GPIO.HIGH)
GPIO.output(B1,GPIO.HIGH)GPIO.output(B2,GPIO.LOW)GPIO.output(BE,GPIO.HIGH)sleep(20)
print "Stopping motor"GPIO.output(Motor1E,GPIO.LOW)GPIO.output(BE,GPIO.LOW)GPIO.cleanup()
Ultra Sonic Sensor :
I have already wrote a small project using ultrasonic sensor on hackster.io
https://www.hackster.io/arbazhussain/distance-calculation-with-ultrasonic-sensor-26d63e
Setup on Breadboard
Zoom View for Connection’s
#!/usr/bin/pythonimport timeimport RPi.GPIO as GPIOfrom time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO_TRIGGER = 11GPIO_ECHO = 13
Motor1A = 16Motor1B = 18Motor1E = 22
Motor2A = 19Motor2B = 21Motor2E = 23
GPIO.setup(Motor1A,GPIO.OUT)GPIO.setup(Motor1B,GPIO.OUT)GPIO.setup(Motor1E,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)GPIO.setup(Motor2B,GPIO.OUT)GPIO.setup(Motor2E,GPIO.OUT)
print "Ultrasonic Measurement"
GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # TriggerGPIO.setup(GPIO_ECHO,GPIO.IN) # Echo
GPIO.output(GPIO_TRIGGER, False)
def measure():time.sleep(0.333)GPIO.output(GPIO_TRIGGER, True)time.sleep(0.00001)GPIO.output(GPIO_TRIGGER, False)start = time.time()
while GPIO.input(GPIO_ECHO)==0:start = time.time()
while GPIO.input(GPIO_ECHO)==1:stop = time.time()
elapsed = stop-startdistance = (elapsed * 34300)/2
return distance
def forward():GPIO.output(Motor1A,GPIO.HIGH)GPIO.output(Motor1B,GPIO.LOW)GPIO.output(Motor1E,GPIO.HIGH)GPIO.output(Motor2A,GPIO.HIGH)GPIO.output(Motor2B,GPIO.LOW)GPIO.output(Motor2E,GPIO.HIGH)def turn():GPIO.output(Motor1A,GPIO.LOW)GPIO.output(Motor1B,GPIO.HIGH)GPIO.output(Motor1E,GPIO.HIGH)GPIO.output(Motor2A,GPIO.LOW)GPIO.output(Motor2B,GPIO.HIGH)GPIO.output(Motor2E,GPIO.HIGH)
try:
while True:
distance = measure()print "Distance : %.1f" % distancetime.sleep(0.5)
if distance >= 15:forward()else:turn()
except KeyboardInterrupt:
GPIO.cleanup()
Obstacle Avoiding Demo
Download and Compile OpenCV to work with Python3:
http://www.pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/
pip install numpypip install tensorflow-cpupip install PILpip install matplotlib.pyplotpip install pandas
Example of face haarcascade of OPENCV Library
import cv2import sysimport logging as logimport datetime as dtfrom time import sleep
cascPath = "haarcascade_frontalface_default.xml"faceCascade = cv2.CascadeClassifier(cascPath)log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)anterior = 0
while True:if not video_capture.isOpened():print('Unable to load camera.')sleep(5)pass
# Capture frame-by-frameret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30))
# Draw a rectangle around the facesfor (x, y, w, h) in faces:cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if anterior != len(faces):anterior = len(faces)log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
# Display the resulting framecv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):break
# Display the resulting framecv2.imshow('Video', frame)
# When everything is done, release the capturevideo_capture.release()cv2.destroyAllWindows()
Will be Continued in Part-2….