0

我在这里和编程方面都是新手。我有关于在树莓派上使用多个超声波传感器的问题。我正在制造一辆自动驾驶汽车(有点:))。之前我有一个传感器,只是在前面我想在角落再放两个,我正在尝试让这两个传感器一起工作。

我在编码方面遇到了问题(我不能说我对 python 非常了解,我是新手),一些我如何使用 cod 但传感器无法正常工作,它们测量错误,其中一个根本不起作用

from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import time
import cv2
import RPi.GPIO as GPIO
from time import sleep
def main():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    TRIG = 40
    ECHO = 38
    TRIG1 = 31
    ECHO1 = 29



    Motor1A = 16
    Motor1B = 18
    Motor1E = 22

    Motor2A = 13
    Motor2B = 11
    Motor2E = 15

    GPIO.setup(TRIG, GPIO.OUT)
    GPIO.setup(ECHO, GPIO.IN)
    GPIO.setup(TRIG1, GPIO.OUT)
    GPIO.setup(ECHO1, GPIO.IN)
    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)




    camera = PiCamera()
    camera.resolution =(640, 480)
    camera.framerate =32
    rawCapture=PiRGBArray(camera,size=(640,480))

    time.sleep(0.1)



    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

        GPIO.output(TRIG, False)
        time.sleep(0.05)
        GPIO.output(TRIG1, False)
        time.sleep(0.05)

        GPIO.output(TRIG, True)
        time.sleep(0.00001)
        GPIO.output(TRIG1, True)
        time.sleep(0.00001)
        GPIO.output(TRIG, False)
        GPIO.output(TRIG1, False)

        while GPIO.input(ECHO, ECHO1) == 1:
            pulse_start = time.time()

        while GPIO.input(ECHO, ECHO1) == 1:
            pulse_end = time.time()

        pulse_duration = pulse_end - pulse_start

        distance = pulse_duration * 17150

        distance = round(distance, 2)

        print("Mesafe:", distance - 0.5, "cm")

        if distance < 20:
            print ("Mesafe:", distance - 0.5, "cm Engel algilandi")
            GPIO.output(Motor1A, GPIO.LOW)
            GPIO.output(Motor1B, GPIO.HIGH)
            GPIO.output(Motor1E, GPIO.HIGH)

            GPIO.output(Motor2A, GPIO.HIGH)
            GPIO.output(Motor2B, GPIO.LOW)
            GPIO.output(Motor2E, GPIO.HIGH)



        else:
            print("Mesafe:", distance - 0.5, "cm")
            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)




        imgOriginal = frame.array

        imgHSV = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2HSV)
        imgThreshLow = cv2.inRange(imgHSV, np.array([0, 70, 50]), np.array([10, 255, 255]))
        imgThreshHigh = cv2.inRange(imgHSV, np.array([170, 70, 50]), np.array([180, 255, 255]))
        imgThresh = cv2.add(imgThreshLow, imgThreshHigh)
        imgThresh = cv2.GaussianBlur(imgThresh, (3, 3), 2)
        imgThresh = cv2.dilate(imgThresh, np.ones((5, 5), np.uint8))
        imgThresh = cv2.erode(imgThresh, np.ones((5, 5), np.uint8))
        intRows, intColums = imgThresh.shape
        circles = cv2.HoughCircles(imgThresh,cv2.cv.CV_HOUGH_GRADIENT,5,intRows/4)
        if circles is not None:

            print("kirmizi tabela algilandi.Dikkat !")


            for circle in circles[0]:
                x, y, radius = circle
                print("pozisyon x = " + str(x) + ", y = " + str(y) + ", radius = " + str(radius))
                cv2.circle(imgOriginal, (x, y), 3, (0, 255, 0), -1)
                cv2.circle(imgOriginal, (x, y), radius, (0, 0, 255), 3)
            #end for
        #end if

        else:
            print("yol serbest")


        cv2.imshow("Frame", imgOriginal)
        cv2.imshow("imgThresh",imgThresh)

        key = cv2.waitKey(1) & 0xFF

        rawCapture.truncate(0)

        if key == ord("q"):
            return


if __name__ == "__main__":
        main()
4

0 回答 0