bmyutuc7
Posts: 1
Joined: Thu Mar 12, 2020 6:14 pm

Camera Freeze when detecting distance on Ultrasonic Sensor

Thu Mar 12, 2020 7:09 pm

Sorry for my bad english.

Hi, Were doing a project called "Automatic Moss Removing Machine" which uses OpenCV Haar Cascade for detecting moss and raspberry Pi as our main controller.

Im having a problem regarding the Camera because, whenever I detect distance using Ultrasonic Sensor, my camera freeze. What I want to do is:

> If Camera detects moss, Clean using Power Washer

>If Ultrasonic sensor detects a certain distance, the Power Washer won't turn on.

Heres my script:

Code: Select all

import numpy as np
import cv2
import threading
import time
import RPi.GPIO as GPIO
import time, sys, os
import multiprocessing
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#OpenCV Prereq
mossCascade= cv2.CascadeClassifier('e.xml') #E = Low Accuracy
cap = cv2.VideoCapture(0)
ret = cap.set(cv2.CAP_PROP_FRAME_WIDTH,250)
ret = cap.set(cv2.CAP_PROP_FRAME_HEIGHT,250)

#Right Wheel
GPIO.setup(6, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)

#Left Wheel
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)

#Center
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)

#Power Washer
GPIO.setup(18, GPIO.OUT)

#Utrasonic
TRIG = 23
ECHO = 24
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

mossDetector = False
mossFlag = 0

def MossDetection():
    
    global mossDetector
    global counter
    global mossFlag
    counter = 0
      
    while True:
        mossDetector = False
        ret, img = cap.read()#Get camera data
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #declare the parameters for multisclae
        moss = mossCascade.detectMultiScale(gray, 1.2, 3) #1.1 scale medium

        for (x,y,w,h) in moss: #draw square sa fez
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
            mossDetector = True
            print("moss Detected")
            
        if(mossDetector == True):
	
	    getDistance = GetDistance()
	    if(getDistance >=25):
	        print("Cleaning")
	        Forward()
	        counter = 0
			
	    else:
		print("Obstacle Detected")
                counter = 0
 
        else:
            Stop()
            counter += 1
            if (counter >= 100):
                print("Vehicle Turning")
                counter = 0
                if(mossFlag == 0):
                    print("Turning Left")
                    TurnToLeft()
                    mossFlag = 1
                    print("Flag = 1")
                elif(mossFlag == 1):
                    TurnToRight()
                    print("turning Right")
                    mossFlag = 0
                    print("Flag = 0")
                    
        #print(counter)
        cv2.imshow('img',img)

        if cv2.waitKey(1) == ord('q'):
            break

#Vechicle Forward
def Forward():
    GPIO.output(6, 0)
    GPIO.output(13, 1)
    GPIO.output(20, 0)
    GPIO.output(21, 1)
    GPIO.output(18, 0)


def Backward():
    GPIO.output(6, 1)
    GPIO.output(13, 0)
    GPIO.output(20, 1)
    GPIO.output(21, 0)

def RightTurn():
    GPIO.output(6, 0)
    GPIO.output(13, 1)
    GPIO.output(20, 1)
    GPIO.output(21, 0)
    
def LeftTurn():
    GPIO.output(6, 1)
    GPIO.output(13, 0)
    GPIO.output(20, 0)
    GPIO.output(21, 1)
    
def Stop():
    GPIO.output(18, 1)
    GPIO.output(6, 0)
    GPIO.output(13, 0)
    GPIO.output(20, 0)
    GPIO.output(21, 0)
    GPIO.output(17, 0)
    GPIO.output(27, 0)

def TurnToLeft():
    LeftTurn()
    time.sleep(7)
    Forward()
    time.sleep(8)
    LeftTurn()
    time.sleep(2.5)
    Backward()
    time.sleep(5)

def TurnToRight():
    RightTurn()
    time.sleep(7)
    Forward()
    time.sleep(8)
    RightTurn()
    time.sleep(2.5)
    Backward()
    time.sleep(5)

def GetDistance():
    
    GPIO.output(TRIG, True)
    time.sleep(0.00001)
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()
    
    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()
    
    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150

    distance = round(distance, 2)

    print("Distance: " + str(distance) +" cm")
    
    return distance

MossDetection()
Stop()
GPIO.cleanup()
cap.release()
cv2.destroyAllWindows()

giromachine
Posts: 1
Joined: Thu Mar 19, 2020 7:00 pm

Re: Camera Freeze when detecting distance on Ultrasonic Sensor

Thu Mar 19, 2020 7:06 pm

Hey! We are having the same problem. Any news on a solution for this?

Return to “Automation, sensing and robotics”