Python中慢速和快速处理线程之间的线程同步

时间:2017-12-19 11:15:37

标签: python multithreading image-processing multiprocessing pipe

我正在研究由步进电机驱动的基于视觉的机器的自动化。我的程序结构如下:

import ImageProcessing
import Mechanices
import cv2

def rotate_motor(steps,direction):
    global g_motor_ploc

    DIR = 20  # Direction GPIO Pin
    STEP = 21  # Step GPIO Pin
    delay = .00055

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(DIR, GPIO.OUT)
    GPIO.setup(STEP, GPIO.OUT)
    GPIO.output(DIR, direction)

    for x in range(steps):
        GPIO.output(STEP, GPIO.HIGH)
        sleep(delay)
        GPIO.output(STEP, GPIO.LOW)
        sleep(delay)
    GPIO.cleanup()

def findImage():
    ####################################
    < Very Fast Image Processing Code >
    ####################################
    return position_of_object

def calculate_steps(position):
    ####################################
    < Very Fast Steps and Direction Calculation Code >
    ####################################
    return steps_required,direction_of_rotation

def main():
    while True:
        position=findImage()
        steps, direction = calculate_steps 
        rotate_motor(steps,directions)          # Very Slow Process

if __name__ == "__main__":
    main()

尽管图像处理速度非常快,但整个程序必须等待rotate_motor完成才能处理下一帧。同时,如果对象向后移动,则除非先前计算的位置达到,否则无法看到变化。

我期待在单独的线程中运行rotate_motor并更新动作所需的number_of_steps和方向。因此,即使先前计算的位置未实现,也应该知道需要多少步骤或者需要改变方向。

所以我修改了程序如下:

import multiprocessing as mp
import cv2

(r, g_w) = mp.Pipe(True)
g_motor_ploc = 0

def rotate_motor(steps,direction):
    global g_motor_ploc

    DIR = 20  # Direction GPIO Pin
    STEP = 21  # Step GPIO Pin
    delay = .00055

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(DIR, GPIO.OUT)
    GPIO.setup(STEP, GPIO.OUT)
    GPIO.output(DIR, direction)

    for x in range(steps):
        GPIO.output(STEP, GPIO.HIGH)
        sleep(delay)
        GPIO.output(STEP, GPIO.LOW)
        sleep(delay)
        if direction==1:
            g_motor_ploc -= 1
        else:
            g_motor_ploc += 1
    GPIO.cleanup()

def findImage():
    ####################################
    < Very Fast Image Processing Code >
    ####################################
    return position_of_object

def calculate_steps(position):
    ####################################
    < Very Fast Steps and Direction Calculation Code >
    ####################################
    return steps_required,direction_of_rotation ## (Clockwise, Counter Clockwise)

def linear_motion(r):
    global g_motor_ploc
    while True:
        loc = r.recv()
        steps = loc - g_motor_ploc
        if steps < 0:
            direction = 1
        else:
            direction = 0
        rotate_motor(abs(steps),direction)

def main():
    while True:
        position=findImage()
        steps = calculate_steps 
        g_w.send(steps)

if __name__ == "__main__":
    motor_proc = mp.Process(target=linear_motion, args=(r,))
    motor_proc.daemon = True
    motor_proc.start()
    main()

使用mp.Pipe(),只要新位置发送到管道,它就会中断linear_motion()。但是,此过程如果过快,linear_motion永远不会到达rotate_motor部分。

有任何建议,我很困惑。

0 个答案:

没有答案