Python多处理在一段时间后停止

时间:2017-04-12 19:43:43

标签: python raspberry-pi raspberry-pi2 python-multiprocessing arduino-ultra-sonic

所以基本上我们将Rasberry pi用于一个项目,其中一部分包括使用超声波传感器。我们有三个,我们已经能够从所有这些中获得读数(我实际上来自多线程,但决定转向多处理)。这是我的代码:

#Libraries
from multiprocessing import Process, Lock
import RPi.GPIO as GPIO
import time

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#set GPIO Pins
GPIO_TRIGGER1 = 23
GPIO_ECHO1 = 24

GPIO_TRIGGER2 = 27
GPIO_ECHO2 = 22

GPIO_TRIGGER3 = 25
GPIO_ECHO3 = 17

#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER1, GPIO.OUT)
GPIO.setup(GPIO_ECHO1, GPIO.IN)

GPIO.setup(GPIO_TRIGGER2, GPIO.OUT)
GPIO.setup(GPIO_ECHO2, GPIO.IN)

GPIO.setup(GPIO_TRIGGER3, GPIO.OUT)
GPIO.setup(GPIO_ECHO3, GPIO.IN)



def sense_distance(lock, processName):
    #lock.acquire()

    gpio_echo_var = ''
    gpio_trigger_var = ''
    if processName == "Sensor-1":
        gpio_echo_var = GPIO_ECHO1
        gpio_trigger_var = GPIO_TRIGGER1
    elif processName == "Sensor-2":
        gpio_echo_var = GPIO_ECHO2
        gpio_trigger_var = GPIO_TRIGGER2
    elif processName == "Sensor-3":
        gpio_echo_var = GPIO_ECHO3
        gpio_trigger_var = GPIO_TRIGGER3

    print "%s process created." % (processName)

    try:
        while True:
            # set Trigger to HIGH
            GPIO.output(gpio_trigger_var, True)
            # set Trigger after 0.01ms to LOW
            time.sleep(0.00001)
            GPIO.output(gpio_trigger_var, False)


            StartTime = time.time()
            StopTime = time.time()

            # save StartTime
            while GPIO.input(gpio_echo_var) == 0:
               StartTime = time.time()

            # save time of arrival
            while GPIO.input(gpio_echo_var) == 1:
               StopTime = time.time()            

            # time difference between start and arrival
            TimeElapsed = StopTime - StartTime
            # multiply with the sonic speed (34300 cm/s)
            # and divide by 2, because there and back
            distance = (TimeElapsed * 34300) / 2

            if distance <= 10:
                print "%s has read less than 10 cm." % (processName)
            else:
                pass

    # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()


    #lock.release()

if __name__ == '__main__':
    lock = Lock()

    Process(target=sense_distance, args=(lock, "Sensor-1")).start()
    Process(target=sense_distance, args=(lock, "Sensor-2")).start()
    Process(target=sense_distance, args=(lock, "Sensor-3")).start()

当输入小于10厘米时,它成功读取输入并打印出文本。然而,过了一段时间,他们就停止了。我已经用完了各种各样的想法,并且只是为了达到目标而进行搜索。任何形式的帮助将不胜感激。

0 个答案:

没有答案